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