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

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: Rebase 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));
Mr4D (OOO till 08-26) 2013/05/06 21:40:16 Not that this really changes anything, but why do
stevenjb 2013/05/07 16:06:32 Changed bounds to be NULL also.
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 and state from the store works correctly.
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 and state to the cache and state store, and reading
125 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryToStore) { 136 // it back.
137 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
126 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 138 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
127 const std::string window_id(kWindowId); 139 const std::string window_id(kWindowId);
128 140
129 // inform cache of extension 141 // inform cache of extension
130 LoadExtension(extension_id); 142 LoadExtension(extension_id);
131 143
132 // update geometry stored in cache 144 // update geometry stored in cache
133 gfx::Rect bounds(4, 5, 31, 43); 145 gfx::Rect bounds(4, 5, 31, 43);
134 gfx::Rect newBounds; 146 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
135 cache_->SaveGeometry(extension_id, window_id, bounds); 147 cache_->SaveGeometry(extension_id, window_id, bounds, state);
136 148
137 // make sure that immediately reading back geometry works 149 // make sure that immediately reading back geometry works
138 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 150 gfx::Rect new_bounds;
139 ASSERT_EQ(bounds, newBounds); 151 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
152 ASSERT_TRUE(cache_->GetGeometry(
153 extension_id, window_id, &new_bounds, &new_state));
154 ASSERT_EQ(bounds, new_bounds);
155 ASSERT_EQ(state, new_state);
140 156
141 // unload extension to force cache to save data to the state store 157 // unload extension to force cache to save data to the state store
142 UnloadExtension(extension_id); 158 UnloadExtension(extension_id);
143 159
144 // check if geometry got stored correctly in the state store 160 // check if geometry got stored correctly in the state store
145 const base::DictionaryValue* dict = 161 const base::DictionaryValue* dict =
146 prefs_->prefs()->GetGeometryCache(extension_id); 162 prefs_->prefs()->GetGeometryCache(extension_id);
147 ASSERT_TRUE(dict); 163 ASSERT_TRUE(dict);
148 164
149 ASSERT_TRUE(dict->HasKey(window_id)); 165 ASSERT_TRUE(dict->HasKey(window_id));
150 int v; 166 int v;
151 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); 167 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
152 ASSERT_EQ(bounds.x(), v); 168 ASSERT_EQ(bounds.x(), v);
153 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); 169 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
154 ASSERT_EQ(bounds.y(), v); 170 ASSERT_EQ(bounds.y(), v);
155 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); 171 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
156 ASSERT_EQ(bounds.width(), v); 172 ASSERT_EQ(bounds.width(), v);
157 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v)); 173 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
158 ASSERT_EQ(bounds.height(), v); 174 ASSERT_EQ(bounds.height(), v);
175 ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
176 ASSERT_EQ(state, v);
159 177
160 // check to make sure cache indeed doesn't know about this extension anymore 178 // check to make sure cache indeed doesn't know about this extension anymore
161 ASSERT_FALSE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 179 ASSERT_FALSE(cache_->GetGeometry(
180 extension_id, window_id, &new_bounds, &new_state));
162 181
163 // reload extension 182 // reload extension
164 LoadExtension(extension_id); 183 LoadExtension(extension_id);
165 // and make sure the geometry got reloaded properly too 184 // and make sure the geometry got reloaded properly too
166 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 185 ASSERT_TRUE(cache_->GetGeometry(
167 ASSERT_EQ(bounds, newBounds); 186 extension_id, window_id, &new_bounds, &new_state));
187 ASSERT_EQ(bounds, new_bounds);
188 ASSERT_EQ(state, new_state);
168 } 189 }
169 190
170 // Tests that we won't do writes to the state store for SaveGeometry calls 191 // Tests that we won't do writes to the state store for SaveGeometry calls
171 // which don't change the state we already have. 192 // which don't change the state we already have.
172 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) { 193 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) {
173 using testing::_; 194 using testing::_;
174 using testing::Mock; 195 using testing::Mock;
175 196
176 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 197 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
177 gfx::Rect bounds1(100, 200, 300, 400); 198 gfx::Rect bounds1(100, 200, 300, 400);
178 gfx::Rect bounds2(200, 400, 600, 800); 199 gfx::Rect bounds2(200, 400, 600, 800);
179 gfx::Rect bounds2_duplicate(200, 400, 600, 800); 200 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
180 201
181 MockPrefChangeCallback observer(prefs_->pref_service()); 202 MockPrefChangeCallback observer(prefs_->pref_service());
182 PrefChangeRegistrar registrar; 203 PrefChangeRegistrar registrar;
183 registrar.Init(prefs_->pref_service()); 204 registrar.Init(prefs_->pref_service());
184 registrar.Add("extensions.settings", observer.GetCallback()); 205 registrar.Add("extensions.settings", observer.GetCallback());
185 206
186 // Write the first bounds - it should do > 0 writes. 207 // Write the first bounds - it should do > 0 writes.
187 EXPECT_CALL(observer, OnPreferenceChanged(_)); 208 EXPECT_CALL(observer, OnPreferenceChanged(_));
188 cache_->SaveGeometry(extension_id, kWindowId, bounds1); 209 cache_->SaveGeometry(extension_id, kWindowId, bounds1,
210 ui::SHOW_STATE_DEFAULT);
189 WaitForSync(); 211 WaitForSync();
190 Mock::VerifyAndClearExpectations(&observer); 212 Mock::VerifyAndClearExpectations(&observer);
191 213
192 // Write a different bounds - it should also do > 0 writes. 214 // Write a different bounds - it should also do > 0 writes.
193 EXPECT_CALL(observer, OnPreferenceChanged(_)); 215 EXPECT_CALL(observer, OnPreferenceChanged(_));
194 cache_->SaveGeometry(extension_id, kWindowId, bounds2); 216 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
217 ui::SHOW_STATE_DEFAULT);
195 WaitForSync(); 218 WaitForSync();
196 Mock::VerifyAndClearExpectations(&observer); 219 Mock::VerifyAndClearExpectations(&observer);
197 220
198 // Write a bounds that's a duplicate of what we already have. This should 221 // Write a different state - it should also do > 0 writes.
199 // not do any writes. 222 EXPECT_CALL(observer, OnPreferenceChanged(_));
223 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
224 ui::SHOW_STATE_NORMAL);
225 WaitForSync();
226 Mock::VerifyAndClearExpectations(&observer);
227
228 // Write a bounds and state that's a duplicate of what we already have.
229 // This should not do any writes.
200 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0); 230 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
201 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate); 231 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate,
232 ui::SHOW_STATE_NORMAL);
202 WaitForSync(); 233 WaitForSync();
203 Mock::VerifyAndClearExpectations(&observer); 234 Mock::VerifyAndClearExpectations(&observer);
204 } 235 }
205 236
206 // Tests that no more than kMaxCachedWindows windows will be cached. 237 // Tests that no more than kMaxCachedWindows windows will be cached.
207 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) { 238 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) {
208 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 239 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
209 // inform cache of extension 240 // inform cache of extension
210 LoadExtension(extension_id); 241 LoadExtension(extension_id);
211 242
212 gfx::Rect bounds(4, 5, 31, 43); 243 gfx::Rect bounds(4, 5, 31, 43);
213 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 244 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
214 std::string window_id = "window_" + base::IntToString(i); 245 std::string window_id = "window_" + base::IntToString(i);
215 cache_->SaveGeometry(extension_id, window_id, bounds); 246 cache_->SaveGeometry(extension_id, window_id, bounds,
247 ui::SHOW_STATE_DEFAULT);
216 } 248 }
217 249
218 // The first added window should no longer have cached geometry. 250 // The first added window should no longer have cached geometry.
219 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", &bounds)); 251 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", &bounds, NULL));
220 // All other windows should still exist. 252 // All other windows should still exist.
221 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 253 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
222 std::string window_id = "window_" + base::IntToString(i); 254 std::string window_id = "window_" + base::IntToString(i);
223 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, &bounds)); 255 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, &bounds, NULL));
224 } 256 }
225 } 257 }
226 258
227 } // namespace extensions 259 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698