| 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/extension_settings_backend.h" | |
| 13 #include "chrome/browser/extensions/extension_settings_frontend.h" | 12 #include "chrome/browser/extensions/extension_settings_frontend.h" |
| 14 #include "chrome/browser/extensions/extension_settings_storage.h" | 13 #include "chrome/browser/extensions/extension_settings_storage.h" |
| 14 #include "chrome/browser/extensions/extension_settings_test_util.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 |
| 17 using namespace extension_settings_test_util; |
| 17 | 18 |
| 18 class ExtensionSettingsFrontendTest : public testing::Test { | 19 class ExtensionSettingsFrontendTest : public testing::Test { |
| 19 public: | 20 public: |
| 20 ExtensionSettingsFrontendTest() | 21 ExtensionSettingsFrontendTest() |
| 21 : ui_thread_(BrowserThread::UI, MessageLoop::current()), | 22 : ui_thread_(BrowserThread::UI, MessageLoop::current()), |
| 22 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} | 23 file_thread_(BrowserThread::FILE, MessageLoop::current()) {} |
| 23 | 24 |
| 24 virtual void SetUp() OVERRIDE { | 25 virtual void SetUp() OVERRIDE { |
| 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 26 profile_.reset(new TestingProfile(temp_dir_.path())); | 27 profile_.reset(new MockProfile(temp_dir_.path())); |
| 27 frontend_.reset(new ExtensionSettingsFrontend(profile_.get())); | 28 frontend_.reset(new ExtensionSettingsFrontend(profile_.get())); |
| 28 } | 29 } |
| 29 | 30 |
| 30 virtual void TearDown() OVERRIDE { | 31 virtual void TearDown() OVERRIDE { |
| 31 frontend_.reset(); | 32 frontend_.reset(); |
| 32 profile_.reset(); | 33 profile_.reset(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 protected: | 36 protected: |
| 36 // Puts the settings backend in |backend|. | |
| 37 void GetBackend(ExtensionSettingsBackend** backend) { | |
| 38 frontend_->RunWithBackend( | |
| 39 base::Bind( | |
| 40 &ExtensionSettingsFrontendTest::AssignBackend, | |
| 41 base::Unretained(this), | |
| 42 backend)); | |
| 43 MessageLoop::current()->RunAllPending(); | |
| 44 ASSERT_TRUE(*backend); | |
| 45 } | |
| 46 | |
| 47 ScopedTempDir temp_dir_; | 37 ScopedTempDir temp_dir_; |
| 48 scoped_ptr<TestingProfile> profile_; | 38 scoped_ptr<MockProfile> profile_; |
| 49 scoped_ptr<ExtensionSettingsFrontend> frontend_; | 39 scoped_ptr<ExtensionSettingsFrontend> frontend_; |
| 50 | 40 |
| 51 private: | 41 private: |
| 52 // Intended as a ExtensionSettingsFrontend::BackendCallback from GetBackend. | |
| 53 void AssignBackend( | |
| 54 ExtensionSettingsBackend** dst, ExtensionSettingsBackend* src) { | |
| 55 *dst = src; | |
| 56 } | |
| 57 | |
| 58 MessageLoop message_loop_; | 42 MessageLoop message_loop_; |
| 59 BrowserThread ui_thread_; | 43 BrowserThread ui_thread_; |
| 60 BrowserThread file_thread_; | 44 BrowserThread file_thread_; |
| 61 }; | 45 }; |
| 62 | 46 |
| 47 // Get a semblance of coverage for both extension and app settings by |
| 48 // alternating in each test. |
| 49 // TODO(kalman): explicitly test the two interact correctly. |
| 50 |
| 63 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { | 51 TEST_F(ExtensionSettingsFrontendTest, SettingsPreservedAcrossReconstruction) { |
| 64 ExtensionSettingsBackend* backend; | 52 const std::string id = "ext"; |
| 65 GetBackend(&backend); | 53 profile_->GetMockExtensionService()->AddExtension(id, false); |
| 66 | 54 |
| 67 const std::string id = "ext"; | 55 ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 68 ExtensionSettingsStorage* storage = backend->GetStorage(id); | |
| 69 | 56 |
| 70 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to | 57 // The correctness of Get/Set/Remove/Clear is tested elsewhere so no need to |
| 71 // be too rigorous. | 58 // be too rigorous. |
| 72 StringValue bar("bar"); | 59 StringValue bar("bar"); |
| 73 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); | 60 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); |
| 74 ASSERT_FALSE(result.HasError()); | 61 ASSERT_FALSE(result.HasError()); |
| 75 | 62 |
| 76 result = storage->Get(); | 63 result = storage->Get(); |
| 77 ASSERT_FALSE(result.HasError()); | 64 ASSERT_FALSE(result.HasError()); |
| 78 EXPECT_FALSE(result.GetSettings()->empty()); | 65 EXPECT_FALSE(result.GetSettings()->empty()); |
| 79 | 66 |
| 80 frontend_.reset(new ExtensionSettingsFrontend(profile_.get())); | 67 frontend_.reset(new ExtensionSettingsFrontend(profile_.get())); |
| 81 GetBackend(&backend); | 68 storage = GetStorage(id, frontend_.get()); |
| 82 storage = backend->GetStorage(id); | |
| 83 | 69 |
| 84 result = storage->Get(); | 70 result = storage->Get(); |
| 85 ASSERT_FALSE(result.HasError()); | 71 ASSERT_FALSE(result.HasError()); |
| 86 EXPECT_FALSE(result.GetSettings()->empty()); | 72 EXPECT_FALSE(result.GetSettings()->empty()); |
| 87 } | 73 } |
| 88 | 74 |
| 89 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { | 75 TEST_F(ExtensionSettingsFrontendTest, SettingsClearedOnUninstall) { |
| 90 ExtensionSettingsBackend* backend; | 76 const std::string id = "ext"; |
| 91 GetBackend(&backend); | 77 profile_->GetMockExtensionService()->AddExtension(id, true); |
| 92 | 78 |
| 93 const std::string id = "ext"; | 79 ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 94 ExtensionSettingsStorage* storage = backend->GetStorage(id); | |
| 95 | 80 |
| 96 StringValue bar("bar"); | 81 StringValue bar("bar"); |
| 97 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); | 82 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); |
| 98 ASSERT_FALSE(result.HasError()); | 83 ASSERT_FALSE(result.HasError()); |
| 99 | 84 |
| 100 // This would be triggered by extension uninstall via an ExtensionDataDeleter. | 85 // This would be triggered by extension uninstall via an ExtensionDataDeleter. |
| 101 backend->DeleteExtensionData(id); | 86 frontend_->DeleteStorageSoon(id); |
| 87 MessageLoop::current()->RunAllPending(); |
| 102 | 88 |
| 103 // The storage area may no longer be valid post-uninstall, so re-request. | 89 // The storage area may no longer be valid post-uninstall, so re-request. |
| 104 storage = backend->GetStorage(id); | 90 storage = GetStorage(id, frontend_.get()); |
| 105 result = storage->Get(); | 91 result = storage->Get(); |
| 106 ASSERT_FALSE(result.HasError()); | 92 ASSERT_FALSE(result.HasError()); |
| 107 EXPECT_TRUE(result.GetSettings()->empty()); | 93 EXPECT_TRUE(result.GetSettings()->empty()); |
| 108 } | 94 } |
| 109 | 95 |
| 110 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { | 96 TEST_F(ExtensionSettingsFrontendTest, LeveldbDatabaseDeletedFromDiskOnClear) { |
| 111 ExtensionSettingsBackend* backend; | 97 const std::string id = "ext"; |
| 112 GetBackend(&backend); | 98 profile_->GetMockExtensionService()->AddExtension(id, false); |
| 113 | 99 |
| 114 const std::string id = "ext"; | 100 ExtensionSettingsStorage* storage = GetStorage(id, frontend_.get()); |
| 115 ExtensionSettingsStorage* storage = backend->GetStorage(id); | |
| 116 | 101 |
| 117 StringValue bar("bar"); | 102 StringValue bar("bar"); |
| 118 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); | 103 ExtensionSettingsStorage::Result result = storage->Set("foo", bar); |
| 119 ASSERT_FALSE(result.HasError()); | 104 ASSERT_FALSE(result.HasError()); |
| 120 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | 105 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); |
| 121 | 106 |
| 122 // Should need to both clear the database and delete the frontend for the | 107 // Should need to both clear the database and delete the frontend for the |
| 123 // leveldb database to be deleted from disk. | 108 // leveldb database to be deleted from disk. |
| 124 result = storage->Clear(); | 109 result = storage->Clear(); |
| 125 ASSERT_FALSE(result.HasError()); | 110 ASSERT_FALSE(result.HasError()); |
| 126 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); | 111 EXPECT_TRUE(file_util::PathExists(temp_dir_.path())); |
| 127 | 112 |
| 128 frontend_.reset(); | 113 frontend_.reset(); |
| 129 MessageLoop::current()->RunAllPending(); | 114 MessageLoop::current()->RunAllPending(); |
| 130 // TODO(kalman): Figure out why this fails, despite appearing to work. | 115 // TODO(kalman): Figure out why this fails, despite appearing to work. |
| 131 // Leaving this commented out rather than disabling the whole test so that the | 116 // Leaving this commented out rather than disabling the whole test so that the |
| 132 // deletion code paths are at least exercised. | 117 // deletion code paths are at least exercised. |
| 133 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); | 118 //EXPECT_FALSE(file_util::PathExists(temp_dir_.path())); |
| 134 } | 119 } |
| OLD | NEW |