| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "chrome/browser/extensions/settings/settings_frontend.h" | 12 #include "chrome/browser/extensions/settings/settings_frontend.h" |
| 13 #include "chrome/browser/extensions/settings/settings_storage.h" | 13 #include "chrome/browser/extensions/settings/settings_storage.h" |
| 14 #include "chrome/browser/extensions/settings/settings_test_util.h" | 14 #include "chrome/browser/extensions/settings/settings_test_util.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "content/test/test_browser_thread.h" | 16 #include "content/test/test_browser_thread.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 using namespace settings_test_util; | 22 using namespace settings_test_util; |
| 23 | 23 |
| 24 class SettingsFrontendTest : public testing::Test { | 24 class ExtensionSettingsFrontendTest : public testing::Test { |
| 25 public: | 25 public: |
| 26 SettingsFrontendTest() | 26 ExtensionSettingsFrontendTest() |
| 27 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | 27 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
| 28 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} | 28 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} |
| 29 | 29 |
| 30 virtual void SetUp() OVERRIDE { | 30 virtual void SetUp() OVERRIDE { |
| 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 32 profile_.reset(new MockProfile(temp_dir_.path())); | 32 profile_.reset(new MockProfile(temp_dir_.path())); |
| 33 frontend_.reset(new SettingsFrontend(profile_.get())); | 33 ResetFrontend(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 virtual void TearDown() OVERRIDE { | 36 virtual void TearDown() OVERRIDE { |
| 37 frontend_.reset(); | 37 frontend_.reset(); |
| 38 profile_.reset(); | 38 profile_.reset(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 protected: | 41 protected: |
| 42 void ResetFrontend() { |
| 43 storage_factory_ = |
| 44 new DelegatingSettingsStorageFactory( |
| 45 new SettingsLeveldbStorage::Factory()); |
| 46 frontend_.reset(SettingsFrontend::Create(storage_factory_, profile_.get())); |
| 47 } |
| 48 |
| 42 ScopedTempDir temp_dir_; | 49 ScopedTempDir temp_dir_; |
| 43 scoped_ptr<MockProfile> profile_; | 50 scoped_ptr<MockProfile> profile_; |
| 44 scoped_ptr<SettingsFrontend> frontend_; | 51 scoped_ptr<SettingsFrontend> frontend_; |
| 45 | 52 |
| 53 // Owned by |frontend_|. |
| 54 DelegatingSettingsStorageFactory* storage_factory_; |
| 55 |
| 46 private: | 56 private: |
| 47 MessageLoop message_loop_; | 57 MessageLoop message_loop_; |
| 48 content::TestBrowserThread ui_thread_; | 58 content::TestBrowserThread ui_thread_; |
| 49 content::TestBrowserThread file_thread_; | 59 content::TestBrowserThread file_thread_; |
| 50 }; | 60 }; |
| 51 | 61 |
| 52 // Get a semblance of coverage for both extension and app settings by | 62 // Get a semblance of coverage for both extension and app settings by |
| 53 // alternating in each test. | 63 // alternating in each test. |
| 54 // TODO(kalman): explicitly test the two interact correctly. | 64 // TODO(kalman): explicitly test the two interact correctly. |
| 55 | 65 |
| 56 TEST_F(SettingsFrontendTest, SettingsPreservedAcrossReconstruction) { | 66 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { |
| 57 const std::string id = "ext"; | 67 const std::string id = "ext"; |
| 58 profile_->GetMockExtensionService()->AddExtension( | 68 profile_->GetMockExtensionService()->AddExtension( |
| 59 id, Extension::TYPE_EXTENSION); | 69 id, Extension::TYPE_EXTENSION); |
| 60 | 70 |
| 61 SettingsStorage* storage = GetStorage(id, frontend_.get()); | 71 SettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 62 | 72 |
| 63 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to | 73 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to |
| 64 // be too rigorous. | 74 // be too rigorous. |
| 65 { | 75 { |
| 66 StringValue bar("bar"); | 76 StringValue bar("bar"); |
| 67 SettingsStorage::WriteResult result = storage->Set("foo", bar); | 77 SettingsStorage::WriteResult result = storage->Set("foo", bar); |
| 68 ASSERT_FALSE(result.HasError()); | 78 ASSERT_FALSE(result.HasError()); |
| 69 } | 79 } |
| 70 | 80 |
| 71 { | 81 { |
| 72 SettingsStorage::ReadResult result = storage->Get(); | 82 SettingsStorage::ReadResult result = storage->Get(); |
| 73 ASSERT_FALSE(result.HasError()); | 83 ASSERT_FALSE(result.HasError()); |
| 74 EXPECT_FALSE(result.settings().empty()); | 84 EXPECT_FALSE(result.settings().empty()); |
| 75 } | 85 } |
| 76 | 86 |
| 77 frontend_.reset(new SettingsFrontend(profile_.get())); | 87 ResetFrontend(); |
| 78 storage = GetStorage(id, frontend_.get()); | 88 storage = GetStorage(id, frontend_.get()); |
| 79 | 89 |
| 80 { | 90 { |
| 81 SettingsStorage::ReadResult result = storage->Get(); | 91 SettingsStorage::ReadResult result = storage->Get(); |
| 82 ASSERT_FALSE(result.HasError()); | 92 ASSERT_FALSE(result.HasError()); |
| 83 EXPECT_FALSE(result.settings().empty()); | 93 EXPECT_FALSE(result.settings().empty()); |
| 84 } | 94 } |
| 85 } | 95 } |
| 86 | 96 |
| 87 TEST_F(SettingsFrontendTest, SettingsClearedOnUninstall) { | 97 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { |
| 88 const std::string id = "ext"; | 98 const std::string id = "ext"; |
| 89 profile_->GetMockExtensionService()->AddExtension( | 99 profile_->GetMockExtensionService()->AddExtension( |
| 90 id, Extension::TYPE_PACKAGED_APP); | 100 id, Extension::TYPE_PACKAGED_APP); |
| 91 | 101 |
| 92 SettingsStorage* storage = GetStorage(id, frontend_.get()); | 102 SettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 93 | 103 |
| 94 { | 104 { |
| 95 StringValue bar("bar"); | 105 StringValue bar("bar"); |
| 96 SettingsStorage::WriteResult result = storage->Set("foo", bar); | 106 SettingsStorage::WriteResult result = storage->Set("foo", bar); |
| 97 ASSERT_FALSE(result.HasError()); | 107 ASSERT_FALSE(result.HasError()); |
| 98 } | 108 } |
| 99 | 109 |
| 100 // This would be triggered by extension uninstall via an ExtensionDataDeleter. | 110 // This would be triggered by extension uninstall via an ExtensionDataDeleter. |
| 101 frontend_->DeleteStorageSoon(id); | 111 frontend_->DeleteStorageSoon(id); |
| 102 MessageLoop::current()->RunAllPending(); | 112 MessageLoop::current()->RunAllPending(); |
| 103 | 113 |
| 104 // The storage area may no longer be valid post-uninstall, so re-request. | 114 // The storage area may no longer be valid post-uninstall, so re-request. |
| 105 storage = GetStorage(id, frontend_.get()); | 115 storage = GetStorage(id, frontend_.get()); |
| 106 { | 116 { |
| 107 SettingsStorage::ReadResult result = storage->Get(); | 117 SettingsStorage::ReadResult result = storage->Get(); |
| 108 ASSERT_FALSE(result.HasError()); | 118 ASSERT_FALSE(result.HasError()); |
| 109 EXPECT_TRUE(result.settings().empty()); | 119 EXPECT_TRUE(result.settings().empty()); |
| 110 } | 120 } |
| 111 } | 121 } |
| 112 | 122 |
| 113 TEST_F(SettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { | 123 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { |
| 114 const std::string id = "ext"; | 124 const std::string id = "ext"; |
| 115 profile_->GetMockExtensionService()->AddExtension( | 125 profile_->GetMockExtensionService()->AddExtension( |
| 116 id, Extension::TYPE_EXTENSION); | 126 id, Extension::TYPE_EXTENSION); |
| 117 | 127 |
| 118 SettingsStorage* storage = GetStorage(id, frontend_.get()); | 128 SettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 119 | 129 |
| 120 { | 130 { |
| 121 StringValue bar("bar"); | 131 StringValue bar("bar"); |
| 122 SettingsStorage::WriteResult result = storage->Set("foo", bar); | 132 SettingsStorage::WriteResult result = storage->Set("foo", bar); |
| 123 ASSERT_FALSE(result.HasError()); | 133 ASSERT_FALSE(result.HasError()); |
| 124 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | 134 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); |
| 125 } | 135 } |
| 126 | 136 |
| 127 // Should need to both clear the database and delete the frontend for the | 137 // Should need to both clear the database and delete the frontend for the |
| 128 // leveldb database to be deleted from disk. | 138 // leveldb database to be deleted from disk. |
| 129 { | 139 { |
| 130 SettingsStorage::WriteResult result = storage->Clear(); | 140 SettingsStorage::WriteResult result = storage->Clear(); |
| 131 ASSERT_FALSE(result.HasError()); | 141 ASSERT_FALSE(result.HasError()); |
| 132 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | 142 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); |
| 133 } | 143 } |
| 134 | 144 |
| 135 frontend_.reset(); | 145 frontend_.reset(); |
| 136 MessageLoop::current()->RunAllPending(); | 146 MessageLoop::current()->RunAllPending(); |
| 137 // TODO(kalman): Figure out why this fails, despite appearing to work. | 147 // TODO(kalman): Figure out why this fails, despite appearing to work. |
| 138 // Leaving this commented out rather than disabling the whole test so that the | 148 // Leaving this commented out rather than disabling the whole test so that the |
| 139 // deletion code paths are at least exercised. | 149 // deletion code paths are at least exercised. |
| 140 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); | 150 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); |
| 141 } | 151 } |
| 142 | 152 |
| 153 TEST_F(ExtensionSettingsFrontendTest, |
| 154 LeveldbCreationFailureFailsAllOperations) { |
| 155 const std::string id = "ext"; |
| 156 profile_->GetMockExtensionService()->AddExtension( |
| 157 id, Extension::TYPE_EXTENSION); |
| 158 |
| 159 storage_factory_->Reset(new NullSettingsStorageFactory()); |
| 160 |
| 161 SettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 162 ASSERT_TRUE(storage != NULL); |
| 163 |
| 164 EXPECT_TRUE(storage->Get().HasError()); |
| 165 EXPECT_TRUE(storage->Clear().HasError()); |
| 166 { |
| 167 StringValue bar("bar"); |
| 168 EXPECT_TRUE(storage->Set("foo", bar).HasError()); |
| 169 } |
| 170 EXPECT_TRUE(storage->Remove("foo").HasError()); |
| 171 |
| 172 // For simplicity: just always fail those requests, even if the leveldb |
| 173 // storage areas start working. |
| 174 storage_factory_->Reset(new SettingsLeveldbStorage::Factory()); |
| 175 |
| 176 storage = GetStorage(id, frontend_.get()); |
| 177 ASSERT_TRUE(storage != NULL); |
| 178 |
| 179 EXPECT_TRUE(storage->Get().HasError()); |
| 180 EXPECT_TRUE(storage->Clear().HasError()); |
| 181 { |
| 182 StringValue bar("bar"); |
| 183 EXPECT_TRUE(storage->Set("foo", bar).HasError()); |
| 184 } |
| 185 EXPECT_TRUE(storage->Remove("foo").HasError()); |
| 186 } |
| 187 |
| 143 } // namespace extensions | 188 } // namespace extensions |
| OLD | NEW |