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

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

Issue 14663010: 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 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL));
92 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, &bounds));
93 } 97 }
94 98
95 // Test getting geometry for an unknown extension. 99 // Test getting geometry for an unknown extension.
96 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) { 100 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) {
97 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1"); 101 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1");
98 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2"); 102 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2");
99 AddGeometryAndLoadExtension(extension_id1, kWindowId, 103 AddGeometryAndLoadExtension(extension_id1, kWindowId,
100 gfx::Rect(4, 5, 31, 43)); 104 gfx::Rect(4, 5, 31, 43),
101 gfx::Rect bounds; 105 ui::SHOW_STATE_DEFAULT);
102 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, &bounds)); 106 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL));
103 } 107 }
104 108
105 // Test getting geometry for an unknown window in a known extension. 109 // Test getting geometry for an unknown window in a known extension.
106 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) { 110 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) {
107 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 111 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
108 AddGeometryAndLoadExtension(extension_id, kWindowId, 112 AddGeometryAndLoadExtension(extension_id, kWindowId,
109 gfx::Rect(4, 5, 31, 43)); 113 gfx::Rect(4, 5, 31, 43),
110 gfx::Rect bounds; 114 ui::SHOW_STATE_DEFAULT);
111 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, &bounds)); 115 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL));
112 } 116 }
113 117
114 // Test that loading geometry from the store works correctly. 118 // Test that loading geometry and state from the store works correctly.
115 TEST_F(ShellWindowGeometryCacheTest, GetGeometryFromStore) { 119 TEST_F(ShellWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
116 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 120 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
117 gfx::Rect bounds(4, 5, 31, 43); 121 gfx::Rect bounds(4, 5, 31, 43);
118 AddGeometryAndLoadExtension(extension_id, kWindowId, bounds); 122 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
119 gfx::Rect newBounds; 123 AddGeometryAndLoadExtension(extension_id, kWindowId, bounds, state);
120 ASSERT_TRUE(cache_->GetGeometry(extension_id, kWindowId, &newBounds)); 124 gfx::Rect new_bounds;
121 ASSERT_EQ(bounds, newBounds); 125 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
126 ASSERT_TRUE(cache_->GetGeometry(
127 extension_id, kWindowId, &new_bounds, &new_state));
128 ASSERT_EQ(bounds, new_bounds);
129 ASSERT_EQ(state, new_state);
122 } 130 }
123 131
124 // Test saving geometry to the cache and state store, and reading it back. 132 // Test saving geometry and state to the cache and state store, and reading
125 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryToStore) { 133 // it back.
134 TEST_F(ShellWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
126 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 135 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
127 const std::string window_id(kWindowId); 136 const std::string window_id(kWindowId);
128 137
129 // inform cache of extension 138 // inform cache of extension
130 LoadExtension(extension_id); 139 LoadExtension(extension_id);
131 140
132 // update geometry stored in cache 141 // update geometry stored in cache
133 gfx::Rect bounds(4, 5, 31, 43); 142 gfx::Rect bounds(4, 5, 31, 43);
134 gfx::Rect newBounds; 143 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
135 cache_->SaveGeometry(extension_id, window_id, bounds); 144 cache_->SaveGeometry(extension_id, window_id, bounds, state);
136 145
137 // make sure that immediately reading back geometry works 146 // make sure that immediately reading back geometry works
138 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 147 gfx::Rect new_bounds;
139 ASSERT_EQ(bounds, newBounds); 148 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
149 ASSERT_TRUE(cache_->GetGeometry(
150 extension_id, window_id, &new_bounds, &new_state));
151 ASSERT_EQ(bounds, new_bounds);
152 ASSERT_EQ(state, new_state);
140 153
141 // unload extension to force cache to save data to the state store 154 // unload extension to force cache to save data to the state store
142 UnloadExtension(extension_id); 155 UnloadExtension(extension_id);
143 156
144 // check if geometry got stored correctly in the state store 157 // check if geometry got stored correctly in the state store
145 const base::DictionaryValue* dict = 158 const base::DictionaryValue* dict =
146 prefs_->prefs()->GetGeometryCache(extension_id); 159 prefs_->prefs()->GetGeometryCache(extension_id);
147 ASSERT_TRUE(dict); 160 ASSERT_TRUE(dict);
148 161
149 ASSERT_TRUE(dict->HasKey(window_id)); 162 ASSERT_TRUE(dict->HasKey(window_id));
150 int v; 163 int v;
151 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); 164 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
152 ASSERT_EQ(bounds.x(), v); 165 ASSERT_EQ(bounds.x(), v);
153 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); 166 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
154 ASSERT_EQ(bounds.y(), v); 167 ASSERT_EQ(bounds.y(), v);
155 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); 168 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
156 ASSERT_EQ(bounds.width(), v); 169 ASSERT_EQ(bounds.width(), v);
157 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v)); 170 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
158 ASSERT_EQ(bounds.height(), v); 171 ASSERT_EQ(bounds.height(), v);
172 ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
173 ASSERT_EQ(state, v);
159 174
160 // check to make sure cache indeed doesn't know about this extension anymore 175 // check to make sure cache indeed doesn't know about this extension anymore
161 ASSERT_FALSE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 176 ASSERT_FALSE(cache_->GetGeometry(
177 extension_id, window_id, &new_bounds, &new_state));
162 178
163 // reload extension 179 // reload extension
164 LoadExtension(extension_id); 180 LoadExtension(extension_id);
165 // and make sure the geometry got reloaded properly too 181 // and make sure the geometry got reloaded properly too
166 ASSERT_TRUE(cache_->GetGeometry(extension_id, window_id, &newBounds)); 182 ASSERT_TRUE(cache_->GetGeometry(
167 ASSERT_EQ(bounds, newBounds); 183 extension_id, window_id, &new_bounds, &new_state));
184 ASSERT_EQ(bounds, new_bounds);
185 ASSERT_EQ(state, new_state);
168 } 186 }
169 187
170 // Tests that we won't do writes to the state store for SaveGeometry calls 188 // Tests that we won't do writes to the state store for SaveGeometry calls
171 // which don't change the state we already have. 189 // which don't change the state we already have.
172 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) { 190 TEST_F(ShellWindowGeometryCacheTest, NoDuplicateWrites) {
173 using testing::_; 191 using testing::_;
174 using testing::Mock; 192 using testing::Mock;
175 193
176 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 194 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
177 gfx::Rect bounds1(100, 200, 300, 400); 195 gfx::Rect bounds1(100, 200, 300, 400);
178 gfx::Rect bounds2(200, 400, 600, 800); 196 gfx::Rect bounds2(200, 400, 600, 800);
179 gfx::Rect bounds2_duplicate(200, 400, 600, 800); 197 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
180 198
181 MockPrefChangeCallback observer(prefs_->pref_service()); 199 MockPrefChangeCallback observer(prefs_->pref_service());
182 PrefChangeRegistrar registrar; 200 PrefChangeRegistrar registrar;
183 registrar.Init(prefs_->pref_service()); 201 registrar.Init(prefs_->pref_service());
184 registrar.Add("extensions.settings", observer.GetCallback()); 202 registrar.Add("extensions.settings", observer.GetCallback());
185 203
186 // Write the first bounds - it should do > 0 writes. 204 // Write the first bounds - it should do > 0 writes.
187 EXPECT_CALL(observer, OnPreferenceChanged(_)); 205 EXPECT_CALL(observer, OnPreferenceChanged(_));
188 cache_->SaveGeometry(extension_id, kWindowId, bounds1); 206 cache_->SaveGeometry(extension_id, kWindowId, bounds1,
207 ui::SHOW_STATE_DEFAULT);
189 WaitForSync(); 208 WaitForSync();
190 Mock::VerifyAndClearExpectations(&observer); 209 Mock::VerifyAndClearExpectations(&observer);
191 210
192 // Write a different bounds - it should also do > 0 writes. 211 // Write a different bounds - it should also do > 0 writes.
193 EXPECT_CALL(observer, OnPreferenceChanged(_)); 212 EXPECT_CALL(observer, OnPreferenceChanged(_));
194 cache_->SaveGeometry(extension_id, kWindowId, bounds2); 213 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
214 ui::SHOW_STATE_DEFAULT);
195 WaitForSync(); 215 WaitForSync();
196 Mock::VerifyAndClearExpectations(&observer); 216 Mock::VerifyAndClearExpectations(&observer);
197 217
198 // Write a bounds that's a duplicate of what we already have. This should 218 // Write a different state - it should also do > 0 writes.
199 // not do any writes. 219 EXPECT_CALL(observer, OnPreferenceChanged(_));
220 cache_->SaveGeometry(extension_id, kWindowId, bounds2,
221 ui::SHOW_STATE_NORMAL);
222 WaitForSync();
223 Mock::VerifyAndClearExpectations(&observer);
224
225 // Write a bounds and state that's a duplicate of what we already have.
226 // This should not do any writes.
200 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0); 227 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
201 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate); 228 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate,
229 ui::SHOW_STATE_NORMAL);
202 WaitForSync(); 230 WaitForSync();
203 Mock::VerifyAndClearExpectations(&observer); 231 Mock::VerifyAndClearExpectations(&observer);
204 } 232 }
205 233
206 // Tests that no more than kMaxCachedWindows windows will be cached. 234 // Tests that no more than kMaxCachedWindows windows will be cached.
207 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) { 235 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) {
208 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 236 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
209 // inform cache of extension 237 // inform cache of extension
210 LoadExtension(extension_id); 238 LoadExtension(extension_id);
211 239
212 gfx::Rect bounds(4, 5, 31, 43); 240 gfx::Rect bounds(4, 5, 31, 43);
213 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 241 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
214 std::string window_id = "window_" + base::IntToString(i); 242 std::string window_id = "window_" + base::IntToString(i);
215 cache_->SaveGeometry(extension_id, window_id, bounds); 243 cache_->SaveGeometry(extension_id, window_id, bounds,
244 ui::SHOW_STATE_DEFAULT);
216 } 245 }
217 246
218 // The first added window should no longer have cached geometry. 247 // The first added window should no longer have cached geometry.
219 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", &bounds)); 248 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL));
220 // All other windows should still exist. 249 // All other windows should still exist.
221 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 250 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
222 std::string window_id = "window_" + base::IntToString(i); 251 std::string window_id = "window_" + base::IntToString(i);
223 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, &bounds)); 252 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL));
224 } 253 }
225 } 254 }
226 255
227 } // namespace extensions 256 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/shell_window_geometry_cache.cc ('k') | chrome/browser/sessions/session_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698