| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/file_util.h" | |
| 6 #include "base/files/file_path.h" | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "chrome/browser/extensions/app_notification.h" | |
| 13 #include "chrome/browser/extensions/app_notification_storage.h" | |
| 14 #include "chrome/browser/extensions/app_notification_test_util.h" | |
| 15 #include "chrome/common/extensions/extension_test_util.h" | |
| 16 #include "content/public/test/test_browser_thread.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace util = app_notification_test_util; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 class AppNotificationStorageTest : public testing::Test { | |
| 26 public: | |
| 27 AppNotificationStorageTest() : | |
| 28 file_thread_(BrowserThread::FILE, &message_loop_) {} | |
| 29 virtual ~AppNotificationStorageTest() {} | |
| 30 | |
| 31 virtual void SetUp() OVERRIDE { | |
| 32 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | |
| 33 storage_path_ = dir_.path().AppendASCII("App Notifications"); | |
| 34 storage_.reset(AppNotificationStorage::Create(storage_path_)); | |
| 35 ASSERT_TRUE(storage_.get() != NULL); | |
| 36 } | |
| 37 | |
| 38 protected: | |
| 39 // Returns whether the database file(s) exist on disk yet. | |
| 40 bool DatabaseExistsOnDisk() { | |
| 41 if (!storage_.get()) | |
| 42 return false; | |
| 43 | |
| 44 return file_util::PathExists(storage_path_); | |
| 45 } | |
| 46 | |
| 47 MessageLoop message_loop_; | |
| 48 content::TestBrowserThread file_thread_; | |
| 49 base::ScopedTempDir dir_; | |
| 50 base::FilePath storage_path_; | |
| 51 scoped_ptr<AppNotificationStorage> storage_; | |
| 52 }; | |
| 53 | |
| 54 // Tests simple operations. | |
| 55 TEST_F(AppNotificationStorageTest, Basics) { | |
| 56 std::set<std::string> tmp_ids; | |
| 57 AppNotificationList tmp_list; | |
| 58 std::string id1 = extension_test_util::MakeId("1"); | |
| 59 | |
| 60 // Try operations on non-existent keys. | |
| 61 EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids)); | |
| 62 EXPECT_TRUE(tmp_ids.empty()); | |
| 63 EXPECT_TRUE(storage_->Get(id1, &tmp_list)); | |
| 64 EXPECT_TRUE(tmp_list.empty()); | |
| 65 EXPECT_TRUE(storage_->Delete(id1)); | |
| 66 EXPECT_FALSE(DatabaseExistsOnDisk()); | |
| 67 | |
| 68 // Add some items. | |
| 69 AppNotificationList list; | |
| 70 util::AddNotifications(&list, id1, 2, "whatever"); | |
| 71 EXPECT_TRUE(storage_->Set(id1, list)); | |
| 72 EXPECT_TRUE(DatabaseExistsOnDisk()); | |
| 73 | |
| 74 // Try getting them back. | |
| 75 tmp_list.clear(); | |
| 76 EXPECT_TRUE(storage_->Get(id1, &tmp_list)); | |
| 77 util::ExpectListsEqual(list, tmp_list); | |
| 78 EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids)); | |
| 79 EXPECT_EQ(tmp_ids.size(), 1U); | |
| 80 EXPECT_TRUE(ContainsKey(tmp_ids, id1)); | |
| 81 } | |
| 82 | |
| 83 // Tests with multiple extensions. | |
| 84 TEST_F(AppNotificationStorageTest, MultipleExtensions) { | |
| 85 std::string id1 = extension_test_util::MakeId("1"); | |
| 86 std::string id2 = extension_test_util::MakeId("2"); | |
| 87 | |
| 88 // Add items for id1. | |
| 89 AppNotificationList list1; | |
| 90 util::AddNotifications(&list1, id1, 2, "one"); | |
| 91 EXPECT_TRUE(storage_->Set(id1, list1)); | |
| 92 | |
| 93 // Add items for id2. | |
| 94 AppNotificationList list2; | |
| 95 util::AddNotifications(&list2, id2, 3, "two"); | |
| 96 EXPECT_TRUE(storage_->Set(id2, list2)); | |
| 97 | |
| 98 // Verify the items are present | |
| 99 std::set<std::string> tmp_ids; | |
| 100 EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids)); | |
| 101 EXPECT_EQ(tmp_ids.size(), 2U); | |
| 102 EXPECT_TRUE(ContainsKey(tmp_ids, id1)); | |
| 103 EXPECT_TRUE(ContainsKey(tmp_ids, id2)); | |
| 104 | |
| 105 AppNotificationList tmp_list; | |
| 106 EXPECT_TRUE(storage_->Get(id1, &tmp_list)); | |
| 107 util::ExpectListsEqual(tmp_list, list1); | |
| 108 tmp_list.clear(); | |
| 109 EXPECT_TRUE(storage_->Get(id2, &tmp_list)); | |
| 110 util::ExpectListsEqual(tmp_list, list2); | |
| 111 | |
| 112 // Delete the id1 items and check the results. | |
| 113 EXPECT_TRUE(storage_->Delete(id1)); | |
| 114 tmp_ids.clear(); | |
| 115 EXPECT_TRUE(storage_->GetExtensionIds(&tmp_ids)); | |
| 116 EXPECT_EQ(tmp_ids.size(), 1U); | |
| 117 EXPECT_FALSE(ContainsKey(tmp_ids, id1)); | |
| 118 EXPECT_TRUE(ContainsKey(tmp_ids, id2)); | |
| 119 tmp_list.clear(); | |
| 120 EXPECT_TRUE(storage_->Get(id1, &tmp_list)); | |
| 121 EXPECT_TRUE(tmp_list.empty()); | |
| 122 tmp_list.clear(); | |
| 123 EXPECT_TRUE(storage_->Get(id2, &tmp_list)); | |
| 124 util::ExpectListsEqual(tmp_list, list2); | |
| 125 } | |
| 126 | |
| 127 // Tests using Set to replace existing entries. | |
| 128 TEST_F(AppNotificationStorageTest, ReplaceExisting) { | |
| 129 std::string id = extension_test_util::MakeId("1"); | |
| 130 AppNotificationList list1; | |
| 131 AppNotificationList list2; | |
| 132 util::AddNotifications(&list1, id, 5, "one"); | |
| 133 util::AddNotifications(&list2, id, 7, "two"); | |
| 134 | |
| 135 // Put list1 in, then replace with list2 and verify we get list2 back. | |
| 136 EXPECT_TRUE(storage_->Set(id, list1)); | |
| 137 EXPECT_TRUE(storage_->Set(id, list2)); | |
| 138 AppNotificationList tmp_list; | |
| 139 EXPECT_TRUE(storage_->Get(id, &tmp_list)); | |
| 140 util::ExpectListsEqual(list2, tmp_list); | |
| 141 } | |
| 142 | |
| 143 } // namespace extensions | |
| OLD | NEW |