Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: chrome/browser/extensions/shell_window_geometry_cache_unittest.cc

Issue 14031021: Save and restore State for ShellWindows, including panels (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/mock_pref_change_callback.h" 6 #include "base/prefs/mock_pref_change_callback.h"
7 #include "base/strings/string_number_conversions.h" 7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/extension_prefs.h" 8 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/shell_window_geometry_cache.h" 9 #include "chrome/browser/extensions/shell_window_geometry_cache.h"
10 #include "chrome/browser/extensions/test_extension_prefs.h" 10 #include "chrome/browser/extensions/test_extension_prefs.h"
(...skipping 18 matching lines...) Expand all
29 class ShellWindowGeometryCacheTest : public testing::Test { 29 class ShellWindowGeometryCacheTest : public testing::Test {
30 public: 30 public:
31 ShellWindowGeometryCacheTest() : 31 ShellWindowGeometryCacheTest() :
32 ui_thread_(BrowserThread::UI, &ui_message_loop_) { 32 ui_thread_(BrowserThread::UI, &ui_message_loop_) {
33 prefs_.reset(new TestExtensionPrefs(ui_message_loop_.message_loop_proxy())); 33 prefs_.reset(new TestExtensionPrefs(ui_message_loop_.message_loop_proxy()));
34 cache_.reset( 34 cache_.reset(
35 new ShellWindowGeometryCache(&profile_, prefs_->prefs())); 35 new ShellWindowGeometryCache(&profile_, prefs_->prefs()));
36 cache_->SetSyncDelayForTests(0); 36 cache_->SetSyncDelayForTests(0);
37 } 37 }
38 38
39 void AddGeometryAndLoadExtension(const std::string& extension_id, 39 void AddGeometryAndLoadExtension(
40 const std::string& window_id, 40 const std::string& extension_id,
41 const gfx::Rect& bounds); 41 const std::string& window_id,
42 const gfx::Rect& bounds,
43 ui::WindowShowState state);
42 44
43 // Spins the UI threads' message loops to make sure any task 45 // Spins the UI threads' message loops to make sure any task
44 // posted to sync the geometry to the value store gets a chance to run. 46 // posted to sync the geometry to the value store gets a chance to run.
45 void WaitForSync(); 47 void WaitForSync();
46 48
47 void LoadExtension(const std::string& extension_id); 49 void LoadExtension(const std::string& extension_id);
48 void UnloadExtension(const std::string& extension_id); 50 void UnloadExtension(const std::string& extension_id);
49 51
50 protected: 52 protected:
51 TestingProfile profile_; 53 TestingProfile profile_;
52 MessageLoopForUI ui_message_loop_; 54 MessageLoopForUI ui_message_loop_;
53 content::TestBrowserThread ui_thread_; 55 content::TestBrowserThread ui_thread_;
54 scoped_ptr<TestExtensionPrefs> prefs_; 56 scoped_ptr<TestExtensionPrefs> prefs_;
55 scoped_ptr<ShellWindowGeometryCache> cache_; 57 scoped_ptr<ShellWindowGeometryCache> cache_;
56 }; 58 };
57 59
58 void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension( 60 void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension(
59 const std::string& extension_id, const std::string& window_id, 61 const std::string& extension_id,
60 const gfx::Rect& bounds) { 62 const std::string& window_id,
63 const gfx::Rect& bounds,
64 ui::WindowShowState state) {
61 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 65 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
62 base::DictionaryValue* value = new base::DictionaryValue; 66 base::DictionaryValue* value = new base::DictionaryValue;
63 value->SetInteger("x", bounds.x()); 67 value->SetInteger("x", bounds.x());
64 value->SetInteger("y", bounds.y()); 68 value->SetInteger("y", bounds.y());
65 value->SetInteger("w", bounds.width()); 69 value->SetInteger("w", bounds.width());
66 value->SetInteger("h", bounds.height()); 70 value->SetInteger("h", bounds.height());
71 value->SetInteger("state", state);
67 dict->SetWithoutPathExpansion(window_id, value); 72 dict->SetWithoutPathExpansion(window_id, value);
68 prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass()); 73 prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass());
69 LoadExtension(extension_id); 74 LoadExtension(extension_id);
70 } 75 }
71 76
72 void ShellWindowGeometryCacheTest::WaitForSync() { 77 void ShellWindowGeometryCacheTest::WaitForSync() {
73 content::RunAllPendingInMessageLoop(); 78 content::RunAllPendingInMessageLoop();
74 } 79 }
75 80
76 void ShellWindowGeometryCacheTest::LoadExtension( 81 void ShellWindowGeometryCacheTest::LoadExtension(
77 const std::string& extension_id) { 82 const std::string& extension_id) {
78 cache_->OnExtensionLoaded(extension_id); 83 cache_->OnExtensionLoaded(extension_id);
79 WaitForSync(); 84 WaitForSync();
80 } 85 }
81 86
82 void ShellWindowGeometryCacheTest::UnloadExtension( 87 void ShellWindowGeometryCacheTest::UnloadExtension(
83 const std::string& extension_id) { 88 const std::string& extension_id) {
84 cache_->OnExtensionUnloaded(extension_id); 89 cache_->OnExtensionUnloaded(extension_id);
85 WaitForSync(); 90 WaitForSync();
86 } 91 }
87 92
88 // Test getting geometry from an empty store. 93 // Test getting geometry from an empty store.
89 TEST_F(ShellWindowGeometryCacheTest, GetGeometryEmptyStore) { 94 TEST_F(ShellWindowGeometryCacheTest, GetGeometryEmptyStore) {
90 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 95 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
91 gfx::Rect bounds; 96 gfx::Rect bounds;
92 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, &bounds)); 97 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, &bounds, NULL));
93 } 98 }
94 99
95 // Test getting geometry for an unknown extension. 100 // Test getting geometry for an unknown extension.
96 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) { 101 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) {
97 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1"); 102 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1");
98 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2"); 103 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2");
99 AddGeometryAndLoadExtension(extension_id1, kWindowId, 104 AddGeometryAndLoadExtension(extension_id1, kWindowId,
100 gfx::Rect(4, 5, 31, 43)); 105 gfx::Rect(4, 5, 31, 43),
106 ui::SHOW_STATE_DEFAULT);
101 gfx::Rect bounds; 107 gfx::Rect bounds;
102 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, &bounds)); 108 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, &bounds, NULL));
103 } 109 }
104 110
105 // Test getting geometry for an unknown window in a known extension. 111 // Test getting geometry for an unknown window in a known extension.
106 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) { 112 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) {
107 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 113 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
108 AddGeometryAndLoadExtension(extension_id, kWindowId, 114 AddGeometryAndLoadExtension(extension_id, kWindowId,
109 gfx::Rect(4, 5, 31, 43)); 115 gfx::Rect(4, 5, 31, 43),
116 ui::SHOW_STATE_DEFAULT);
110 gfx::Rect bounds; 117 gfx::Rect bounds;
111 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, &bounds)); 118 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, &bounds, NULL));
112 } 119 }
113 120
114 // Test that loading geometry from the store works correctly. 121 // Test that loading geometry from the store works correctly.
flackr 2013/04/26 03:44:38 nit: s/geometry/geometry and state
stevenjb 2013/04/26 16:12:17 Done.
115 TEST_F(ShellWindowGeometryCacheTest, GetGeometryFromStore) { 122 TEST_F(ShellWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
116 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 123 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
117 gfx::Rect bounds(4, 5, 31, 43); 124 gfx::Rect bounds(4, 5, 31, 43);
118 AddGeometryAndLoadExtension(extension_id, kWindowId, bounds); 125 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
119 gfx::Rect newBounds; 126 AddGeometryAndLoadExtension(extension_id, kWindowId, bounds, state);
120 ASSERT_TRUE(cache_->GetGeometry(extension_id, kWindowId, &newBounds)); 127 gfx::Rect new_bounds;
121 ASSERT_EQ(bounds, newBounds); 128 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
129 ASSERT_TRUE(cache_->GetGeometry(
130 extension_id, kWindowId, &new_bounds, &new_state));
131 ASSERT_EQ(bounds, new_bounds);
132 ASSERT_EQ(state, new_state);
122 } 133 }
123 134
124 // Test saving geometry to the cache and state store, and reading it back. 135 // Test saving geometry to the cache and state store, and reading it back.
125 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryToStore) { 136 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
126 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 137 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
127 const std::string window_id(kWindowId); 138 const std::string window_id(kWindowId);
128 139
129 // inform cache of extension 140 // inform cache of extension
130 LoadExtension(extension_id); 141 LoadExtension(extension_id);
131 142
132 // update geometry stored in cache 143 // update geometry stored in cache
133 gfx::Rect bounds(4, 5, 31, 43); 144 gfx::Rect bounds(4, 5, 31, 43);
134 gfx::Rect newBounds; 145 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
135 cache_->SaveGeometry(extension_id, window_id, bounds); 146 cache_->SaveGeometry(extension_id, window_id, bounds, state);
136 147
137 // make sure that immediately reading back geometry works 148 // make sure that immediately reading back geometry works
138 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 149 gfx::Rect new_bounds;
139 ASSERT_EQ(bounds, newBounds); 150 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
151 ASSERT_TRUE(cache_->GetGeometry(
152 extension_id, window_id, &new_bounds, &new_state));
153 ASSERT_EQ(bounds, new_bounds);
154 ASSERT_EQ(state, new_state);
140 155
141 // unload extension to force cache to save data to the state store 156 // unload extension to force cache to save data to the state store
142 UnloadExtension(extension_id); 157 UnloadExtension(extension_id);
143 158
144 // check if geometry got stored correctly in the state store 159 // check if geometry got stored correctly in the state store
145 const base::DictionaryValue* dict = 160 const base::DictionaryValue* dict =
146 prefs_->prefs()->GetGeometryCache(extension_id); 161 prefs_->prefs()->GetGeometryCache(extension_id);
147 ASSERT_TRUE(dict); 162 ASSERT_TRUE(dict);
148 163
149 ASSERT_TRUE(dict->HasKey(window_id)); 164 ASSERT_TRUE(dict->HasKey(window_id));
150 int v; 165 int v;
151 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); 166 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
152 ASSERT_EQ(bounds.x(), v); 167 ASSERT_EQ(bounds.x(), v);
153 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); 168 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
154 ASSERT_EQ(bounds.y(), v); 169 ASSERT_EQ(bounds.y(), v);
155 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); 170 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
156 ASSERT_EQ(bounds.width(), v); 171 ASSERT_EQ(bounds.width(), v);
157 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v)); 172 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
158 ASSERT_EQ(bounds.height(), v); 173 ASSERT_EQ(bounds.height(), v);
174 ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
175 ASSERT_EQ(state, v);
159 176
160 // check to make sure cache indeed doesn't know about this extension anymore 177 // check to make sure cache indeed doesn't know about this extension anymore
161 ASSERT_FALSE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 178 ASSERT_FALSE(cache_->GetGeometry(
179 extension_id, window_id, &new_bounds, &new_state));
162 180
163 // reload extension 181 // reload extension
164 LoadExtension(extension_id); 182 LoadExtension(extension_id);
165 // and make sure the geometry got reloaded properly too 183 // and make sure the geometry got reloaded properly too
166 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 184 ASSERT_TRUE(cache_->GetGeometry(
167 ASSERT_EQ(bounds, newBounds); 185 extension_id, window_id, &new_bounds, &new_state));
186 ASSERT_EQ(bounds, new_bounds);
187 ASSERT_EQ(state, new_state);
168 } 188 }
169 189
170 // Tests that we won't do writes to the state store for SaveGeometry calls 190 // Tests that we won't do writes to the state store for SaveGeometry calls
171 // which don't change the state we already have. 191 // which don't change the state we already have.
172 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) { 192 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) {
173 using testing::_; 193 using testing::_;
174 using testing::Mock; 194 using testing::Mock;
175 195
176 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 196 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
177 gfx::Rect bounds1(100, 200, 300, 400); 197 gfx::Rect bounds1(100, 200, 300, 400);
178 gfx::Rect bounds2(200, 400, 600, 800); 198 gfx::Rect bounds2(200, 400, 600, 800);
179 gfx::Rect bounds2_duplicate(200, 400, 600, 800); 199 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
180 200
181 MockPrefChangeCallback observer(prefs_->pref_service()); 201 MockPrefChangeCallback observer(prefs_->pref_service());
182 PrefChangeRegistrar registrar; 202 PrefChangeRegistrar registrar;
183 registrar.Init(prefs_->pref_service()); 203 registrar.Init(prefs_->pref_service());
184 registrar.Add("extensions.settings", observer.GetCallback()); 204 registrar.Add("extensions.settings", observer.GetCallback());
185 205
186 // Write the first bounds - it should do > 0 writes. 206 // Write the first bounds - it should do > 0 writes.
187 EXPECT_CALL(observer, OnPreferenceChanged(_)); 207 EXPECT_CALL(observer, OnPreferenceChanged(_));
188 cache_->SaveGeometry(extension_id, kWindowId, bounds1); 208 cache_->SaveGeometry(extension_id, kWindowId, bounds1,
209 ui::SHOW_STATE_DEFAULT);
189 WaitForSync(); 210 WaitForSync();
190 Mock::VerifyAndClearExpectations(&observer); 211 Mock::VerifyAndClearExpectations(&observer);
191 212
192 // Write a different bounds - it should also do > 0 writes. 213 // Write a different bounds - it should also do > 0 writes.
193 EXPECT_CALL(observer, OnPreferenceChanged(_)); 214 EXPECT_CALL(observer, OnPreferenceChanged(_));
194 cache_->SaveGeometry(extension_id, kWindowId, bounds2); 215 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
216 ui::SHOW_STATE_DEFAULT);
195 WaitForSync(); 217 WaitForSync();
196 Mock::VerifyAndClearExpectations(&observer); 218 Mock::VerifyAndClearExpectations(&observer);
197 219
198 // Write a bounds that's a duplicate of what we already have. This should 220 // Write a different state - it should also do > 0 writes.
199 // not do any writes. 221 EXPECT_CALL(observer, OnPreferenceChanged(_));
222 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
223 ui::SHOW_STATE_NORMAL);
224 WaitForSync();
225 Mock::VerifyAndClearExpectations(&observer);
226
227 // Write a bounds and state that's a duplicate of what we already have.
228 // This should not do any writes.
200 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0); 229 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
201 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate); 230 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate,
231 ui::SHOW_STATE_NORMAL);
202 WaitForSync(); 232 WaitForSync();
203 Mock::VerifyAndClearExpectations(&observer); 233 Mock::VerifyAndClearExpectations(&observer);
204 } 234 }
205 235
206 // Tests that no more than kMaxCachedWindows windows will be cached. 236 // Tests that no more than kMaxCachedWindows windows will be cached.
207 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) { 237 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) {
208 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 238 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
209 // inform cache of extension 239 // inform cache of extension
210 LoadExtension(extension_id); 240 LoadExtension(extension_id);
211 241
212 gfx::Rect bounds(4, 5, 31, 43); 242 gfx::Rect bounds(4, 5, 31, 43);
213 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 243 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
214 std::string window_id = "window_" + base::IntToString(i); 244 std::string window_id = "window_" + base::IntToString(i);
215 cache_->SaveGeometry(extension_id, window_id, bounds); 245 cache_->SaveGeometry(extension_id, window_id, bounds,
246 ui::SHOW_STATE_DEFAULT);
216 } 247 }
217 248
218 // The first added window should no longer have cached geometry. 249 // The first added window should no longer have cached geometry.
219 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", &bounds)); 250 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", &bounds, NULL));
220 // All other windows should still exist. 251 // All other windows should still exist.
221 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 252 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
222 std::string window_id = "window_" + base::IntToString(i); 253 std::string window_id = "window_" + base::IntToString(i);
223 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, &bounds)); 254 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, &bounds, NULL));
224 } 255 }
225 } 256 }
226 257
227 } // namespace extensions 258 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698