Show all state properties in chrome://network
[chromium-blink-merge.git] / apps / app_window_geometry_cache_unittest.cc
blobdeda109f1c525893ef83285f6e36618ce70440a5
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/app_window_geometry_cache.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/prefs/mock_pref_change_callback.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/extensions/test_extension_prefs.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "content/public/test/test_utils.h"
13 #include "extensions/browser/extension_prefs.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 const char kWindowId[] = "windowid";
17 const char kWindowId2[] = "windowid2";
19 using content::BrowserThread;
21 namespace apps {
23 // Base class for tests.
24 class AppWindowGeometryCacheTest : public testing::Test {
25 public:
26 AppWindowGeometryCacheTest()
27 : ui_thread_(BrowserThread::UI, &ui_message_loop_) {
28 prefs_.reset(new extensions::TestExtensionPrefs(
29 ui_message_loop_.message_loop_proxy().get()));
30 cache_.reset(new AppWindowGeometryCache(&profile_, prefs_->prefs()));
31 cache_->SetSyncDelayForTests(0);
34 void AddGeometryAndLoadExtension(const std::string& extension_id,
35 const std::string& window_id,
36 const gfx::Rect& bounds,
37 const gfx::Rect& screen_bounds,
38 ui::WindowShowState state);
40 // Spins the UI threads' message loops to make sure any task
41 // posted to sync the geometry to the value store gets a chance to run.
42 void WaitForSync();
44 void LoadExtension(const std::string& extension_id);
45 void UnloadExtension(const std::string& extension_id);
47 protected:
48 TestingProfile profile_;
49 base::MessageLoopForUI ui_message_loop_;
50 content::TestBrowserThread ui_thread_;
51 scoped_ptr<extensions::TestExtensionPrefs> prefs_;
52 scoped_ptr<AppWindowGeometryCache> cache_;
55 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension(
56 const std::string& extension_id,
57 const std::string& window_id,
58 const gfx::Rect& bounds,
59 const gfx::Rect& screen_bounds,
60 ui::WindowShowState state) {
61 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
62 base::DictionaryValue* value = new base::DictionaryValue;
63 value->SetInteger("x", bounds.x());
64 value->SetInteger("y", bounds.y());
65 value->SetInteger("w", bounds.width());
66 value->SetInteger("h", bounds.height());
67 value->SetInteger("screen_bounds_x", screen_bounds.x());
68 value->SetInteger("screen_bounds_y", screen_bounds.y());
69 value->SetInteger("screen_bounds_w", screen_bounds.width());
70 value->SetInteger("screen_bounds_h", screen_bounds.height());
71 value->SetInteger("state", state);
72 dict->SetWithoutPathExpansion(window_id, value);
73 prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass());
74 LoadExtension(extension_id);
77 void AppWindowGeometryCacheTest::WaitForSync() {
78 content::RunAllPendingInMessageLoop();
81 void AppWindowGeometryCacheTest::LoadExtension(
82 const std::string& extension_id) {
83 cache_->LoadGeometryFromStorage(extension_id);
84 WaitForSync();
87 void AppWindowGeometryCacheTest::UnloadExtension(
88 const std::string& extension_id) {
89 cache_->OnExtensionUnloaded(extension_id);
90 WaitForSync();
93 // Test getting geometry from an empty store.
94 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) {
95 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
96 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL));
99 // Test getting geometry for an unknown extension.
100 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownExtension) {
101 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1");
102 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2");
103 AddGeometryAndLoadExtension(extension_id1,
104 kWindowId,
105 gfx::Rect(4, 5, 31, 43),
106 gfx::Rect(0, 0, 1600, 900),
107 ui::SHOW_STATE_NORMAL);
108 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL));
111 // Test getting geometry for an unknown window in a known extension.
112 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownWindow) {
113 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
114 AddGeometryAndLoadExtension(extension_id,
115 kWindowId,
116 gfx::Rect(4, 5, 31, 43),
117 gfx::Rect(0, 0, 1600, 900),
118 ui::SHOW_STATE_NORMAL);
119 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL));
122 // Test that loading geometry, screen_bounds and state from the store works
123 // correctly.
124 TEST_F(AppWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
125 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
126 gfx::Rect bounds(4, 5, 31, 43);
127 gfx::Rect screen_bounds(0, 0, 1600, 900);
128 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
129 AddGeometryAndLoadExtension(
130 extension_id, kWindowId, bounds, screen_bounds, state);
131 gfx::Rect new_bounds;
132 gfx::Rect new_screen_bounds;
133 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
134 ASSERT_TRUE(cache_->GetGeometry(
135 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
136 ASSERT_EQ(bounds, new_bounds);
137 ASSERT_EQ(screen_bounds, new_screen_bounds);
138 ASSERT_EQ(state, new_state);
141 // Test corrupt bounds will not be loaded.
142 TEST_F(AppWindowGeometryCacheTest, CorruptBounds) {
143 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
144 gfx::Rect bounds;
145 gfx::Rect screen_bounds(0, 0, 1600, 900);
146 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
147 AddGeometryAndLoadExtension(
148 extension_id, kWindowId, bounds, screen_bounds, state);
149 gfx::Rect new_bounds;
150 gfx::Rect new_screen_bounds;
151 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
152 ASSERT_FALSE(cache_->GetGeometry(
153 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
154 ASSERT_TRUE(new_bounds.IsEmpty());
155 ASSERT_TRUE(new_screen_bounds.IsEmpty());
156 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
159 // Test corrupt screen bounds will not be loaded.
160 TEST_F(AppWindowGeometryCacheTest, CorruptScreenBounds) {
161 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
162 gfx::Rect bounds(4, 5, 31, 43);
163 gfx::Rect screen_bounds;
164 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
165 AddGeometryAndLoadExtension(
166 extension_id, kWindowId, bounds, screen_bounds, state);
167 gfx::Rect new_bounds;
168 gfx::Rect new_screen_bounds;
169 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
170 ASSERT_FALSE(cache_->GetGeometry(
171 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
172 ASSERT_TRUE(new_bounds.IsEmpty());
173 ASSERT_TRUE(new_screen_bounds.IsEmpty());
174 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
177 // Test corrupt state will not be loaded.
178 TEST_F(AppWindowGeometryCacheTest, CorruptState) {
179 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
180 gfx::Rect bounds(4, 5, 31, 43);
181 gfx::Rect screen_bounds(0, 0, 1600, 900);
182 ui::WindowShowState state = ui::SHOW_STATE_DEFAULT;
183 AddGeometryAndLoadExtension(
184 extension_id, kWindowId, bounds, screen_bounds, state);
185 gfx::Rect new_bounds;
186 gfx::Rect new_screen_bounds;
187 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
188 ASSERT_FALSE(cache_->GetGeometry(
189 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
190 ASSERT_TRUE(new_bounds.IsEmpty());
191 ASSERT_TRUE(new_screen_bounds.IsEmpty());
192 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
195 // Test saving geometry, screen_bounds and state to the cache and state store,
196 // and reading it back.
197 TEST_F(AppWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
198 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
199 const std::string window_id(kWindowId);
201 // inform cache of extension
202 LoadExtension(extension_id);
204 // update geometry stored in cache
205 gfx::Rect bounds(4, 5, 31, 43);
206 gfx::Rect screen_bounds(0, 0, 1600, 900);
207 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
208 cache_->SaveGeometry(extension_id, window_id, bounds, screen_bounds, state);
210 // make sure that immediately reading back geometry works
211 gfx::Rect new_bounds;
212 gfx::Rect new_screen_bounds;
213 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
214 ASSERT_TRUE(cache_->GetGeometry(
215 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state));
216 ASSERT_EQ(bounds, new_bounds);
217 ASSERT_EQ(screen_bounds, new_screen_bounds);
218 ASSERT_EQ(state, new_state);
220 // unload extension to force cache to save data to the state store
221 UnloadExtension(extension_id);
223 // check if geometry got stored correctly in the state store
224 const base::DictionaryValue* dict =
225 prefs_->prefs()->GetGeometryCache(extension_id);
226 ASSERT_TRUE(dict);
228 ASSERT_TRUE(dict->HasKey(window_id));
229 int v;
230 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
231 ASSERT_EQ(bounds.x(), v);
232 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
233 ASSERT_EQ(bounds.y(), v);
234 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
235 ASSERT_EQ(bounds.width(), v);
236 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
237 ASSERT_EQ(bounds.height(), v);
238 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_x", &v));
239 ASSERT_EQ(screen_bounds.x(), v);
240 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_y", &v));
241 ASSERT_EQ(screen_bounds.y(), v);
242 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_w", &v));
243 ASSERT_EQ(screen_bounds.width(), v);
244 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_h", &v));
245 ASSERT_EQ(screen_bounds.height(), v);
246 ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
247 ASSERT_EQ(state, v);
249 // reload extension
250 LoadExtension(extension_id);
251 // and make sure the geometry got reloaded properly too
252 ASSERT_TRUE(cache_->GetGeometry(
253 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state));
254 ASSERT_EQ(bounds, new_bounds);
255 ASSERT_EQ(screen_bounds, new_screen_bounds);
256 ASSERT_EQ(state, new_state);
259 // Tests that we won't do writes to the state store for SaveGeometry calls
260 // which don't change the state we already have.
261 TEST_F(AppWindowGeometryCacheTest, NoDuplicateWrites) {
262 using testing::_;
263 using testing::Mock;
265 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
266 gfx::Rect bounds1(100, 200, 300, 400);
267 gfx::Rect bounds2(200, 400, 600, 800);
268 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
270 gfx::Rect screen_bounds1(0, 0, 1600, 900);
271 gfx::Rect screen_bounds2(0, 0, 1366, 768);
272 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768);
274 MockPrefChangeCallback observer(prefs_->pref_service());
275 PrefChangeRegistrar registrar;
276 registrar.Init(prefs_->pref_service());
277 registrar.Add("extensions.settings", observer.GetCallback());
279 // Write the first bounds - it should do > 0 writes.
280 EXPECT_CALL(observer, OnPreferenceChanged(_));
281 cache_->SaveGeometry(
282 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL);
283 WaitForSync();
284 Mock::VerifyAndClearExpectations(&observer);
286 // Write a different bounds - it should also do > 0 writes.
287 EXPECT_CALL(observer, OnPreferenceChanged(_));
288 cache_->SaveGeometry(
289 extension_id, kWindowId, bounds2, screen_bounds1, ui::SHOW_STATE_NORMAL);
290 WaitForSync();
291 Mock::VerifyAndClearExpectations(&observer);
293 // Write a different screen bounds - it should also do > 0 writes.
294 EXPECT_CALL(observer, OnPreferenceChanged(_));
295 cache_->SaveGeometry(
296 extension_id, kWindowId, bounds2, screen_bounds2, ui::SHOW_STATE_NORMAL);
297 WaitForSync();
298 Mock::VerifyAndClearExpectations(&observer);
300 // Write a different state - it should also do > 0 writes.
301 EXPECT_CALL(observer, OnPreferenceChanged(_));
302 cache_->SaveGeometry(extension_id,
303 kWindowId,
304 bounds2,
305 screen_bounds2,
306 ui::SHOW_STATE_MAXIMIZED);
307 WaitForSync();
308 Mock::VerifyAndClearExpectations(&observer);
310 // Write a bounds, screen bounds and state that's a duplicate of what we
311 // already have. This should not do any writes.
312 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
313 cache_->SaveGeometry(extension_id,
314 kWindowId,
315 bounds2_duplicate,
316 screen_bounds2_duplicate,
317 ui::SHOW_STATE_MAXIMIZED);
318 WaitForSync();
319 Mock::VerifyAndClearExpectations(&observer);
322 // Tests that no more than kMaxCachedWindows windows will be cached.
323 TEST_F(AppWindowGeometryCacheTest, MaxWindows) {
324 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
325 // inform cache of extension
326 LoadExtension(extension_id);
328 gfx::Rect bounds(4, 5, 31, 43);
329 gfx::Rect screen_bounds(0, 0, 1600, 900);
330 for (size_t i = 0; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
331 std::string window_id = "window_" + base::IntToString(i);
332 cache_->SaveGeometry(
333 extension_id, window_id, bounds, screen_bounds, ui::SHOW_STATE_NORMAL);
336 // The first added window should no longer have cached geometry.
337 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL));
338 // All other windows should still exist.
339 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
340 std::string window_id = "window_" + base::IntToString(i);
341 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL));
345 } // namespace extensions