OLD | NEW |
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 "apps/shell_window_geometry_cache.h" | 5 #include "apps/shell_window_geometry_cache.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/prefs/mock_pref_change_callback.h" | 7 #include "base/prefs/mock_pref_change_callback.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "chrome/browser/extensions/extension_prefs.h" | 9 #include "chrome/browser/extensions/extension_prefs.h" |
10 #include "chrome/browser/extensions/test_extension_prefs.h" | 10 #include "chrome/browser/extensions/test_extension_prefs.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL)); | 101 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL)); |
102 } | 102 } |
103 | 103 |
104 // Test getting geometry for an unknown extension. | 104 // Test getting geometry for an unknown extension. |
105 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) { | 105 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownExtension) { |
106 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1"); | 106 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1"); |
107 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2"); | 107 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2"); |
108 AddGeometryAndLoadExtension(extension_id1, kWindowId, | 108 AddGeometryAndLoadExtension(extension_id1, kWindowId, |
109 gfx::Rect(4, 5, 31, 43), | 109 gfx::Rect(4, 5, 31, 43), |
110 gfx::Rect(0, 0, 1600, 900), | 110 gfx::Rect(0, 0, 1600, 900), |
111 ui::SHOW_STATE_DEFAULT); | 111 ui::SHOW_STATE_NORMAL); |
112 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL)); | 112 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL)); |
113 } | 113 } |
114 | 114 |
115 // Test getting geometry for an unknown window in a known extension. | 115 // Test getting geometry for an unknown window in a known extension. |
116 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) { | 116 TEST_F(ShellWindowGeometryCacheTest, GetGeometryUnkownWindow) { |
117 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); | 117 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); |
118 AddGeometryAndLoadExtension(extension_id, kWindowId, | 118 AddGeometryAndLoadExtension(extension_id, kWindowId, |
119 gfx::Rect(4, 5, 31, 43), | 119 gfx::Rect(4, 5, 31, 43), |
120 gfx::Rect(0, 0, 1600, 900), | 120 gfx::Rect(0, 0, 1600, 900), |
121 ui::SHOW_STATE_DEFAULT); | 121 ui::SHOW_STATE_NORMAL); |
122 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL)); | 122 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL)); |
123 } | 123 } |
124 | 124 |
125 // Test that loading geometry, screen_bounds and state from the store works | 125 // Test that loading geometry, screen_bounds and state from the store works |
126 // correctly. | 126 // correctly. |
127 TEST_F(ShellWindowGeometryCacheTest, GetGeometryAndStateFromStore) { | 127 TEST_F(ShellWindowGeometryCacheTest, GetGeometryAndStateFromStore) { |
128 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); | 128 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); |
129 gfx::Rect bounds(4, 5, 31, 43); | 129 gfx::Rect bounds(4, 5, 31, 43); |
130 gfx::Rect screen_bounds(0, 0, 1600, 900); | 130 gfx::Rect screen_bounds(0, 0, 1600, 900); |
131 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; | 131 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768); | 275 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768); |
276 | 276 |
277 MockPrefChangeCallback observer(prefs_->pref_service()); | 277 MockPrefChangeCallback observer(prefs_->pref_service()); |
278 PrefChangeRegistrar registrar; | 278 PrefChangeRegistrar registrar; |
279 registrar.Init(prefs_->pref_service()); | 279 registrar.Init(prefs_->pref_service()); |
280 registrar.Add("extensions.settings", observer.GetCallback()); | 280 registrar.Add("extensions.settings", observer.GetCallback()); |
281 | 281 |
282 // Write the first bounds - it should do > 0 writes. | 282 // Write the first bounds - it should do > 0 writes. |
283 EXPECT_CALL(observer, OnPreferenceChanged(_)); | 283 EXPECT_CALL(observer, OnPreferenceChanged(_)); |
284 cache_->SaveGeometry(extension_id, kWindowId, bounds1, | 284 cache_->SaveGeometry(extension_id, kWindowId, bounds1, |
285 screen_bounds1, ui::SHOW_STATE_DEFAULT); | 285 screen_bounds1, ui::SHOW_STATE_NORMAL); |
286 WaitForSync(); | 286 WaitForSync(); |
287 Mock::VerifyAndClearExpectations(&observer); | 287 Mock::VerifyAndClearExpectations(&observer); |
288 | 288 |
289 // Write a different bounds - it should also do > 0 writes. | 289 // Write a different bounds - it should also do > 0 writes. |
290 EXPECT_CALL(observer, OnPreferenceChanged(_)); | 290 EXPECT_CALL(observer, OnPreferenceChanged(_)); |
291 cache_->SaveGeometry(extension_id, kWindowId, bounds2, | 291 cache_->SaveGeometry(extension_id, kWindowId, bounds2, |
292 screen_bounds1, ui::SHOW_STATE_DEFAULT); | 292 screen_bounds1, ui::SHOW_STATE_NORMAL); |
293 WaitForSync(); | 293 WaitForSync(); |
294 Mock::VerifyAndClearExpectations(&observer); | 294 Mock::VerifyAndClearExpectations(&observer); |
295 | 295 |
296 // Write a different screen bounds - it should also do > 0 writes. | 296 // Write a different screen bounds - it should also do > 0 writes. |
297 EXPECT_CALL(observer, OnPreferenceChanged(_)); | 297 EXPECT_CALL(observer, OnPreferenceChanged(_)); |
298 cache_->SaveGeometry(extension_id, kWindowId, bounds2, | 298 cache_->SaveGeometry(extension_id, kWindowId, bounds2, |
299 screen_bounds2, ui::SHOW_STATE_DEFAULT); | 299 screen_bounds2, ui::SHOW_STATE_NORMAL); |
300 WaitForSync(); | 300 WaitForSync(); |
301 Mock::VerifyAndClearExpectations(&observer); | 301 Mock::VerifyAndClearExpectations(&observer); |
302 | 302 |
303 // Write a different state - it should also do > 0 writes. | 303 // Write a different state - it should also do > 0 writes. |
304 EXPECT_CALL(observer, OnPreferenceChanged(_)); | 304 EXPECT_CALL(observer, OnPreferenceChanged(_)); |
305 cache_->SaveGeometry(extension_id, kWindowId, bounds2, | 305 cache_->SaveGeometry(extension_id, kWindowId, bounds2, |
306 screen_bounds2, ui::SHOW_STATE_NORMAL); | 306 screen_bounds2, ui::SHOW_STATE_MAXIMIZED); |
307 WaitForSync(); | 307 WaitForSync(); |
308 Mock::VerifyAndClearExpectations(&observer); | 308 Mock::VerifyAndClearExpectations(&observer); |
309 | 309 |
310 // Write a bounds, screen bounds and state that's a duplicate of what we | 310 // Write a bounds, screen bounds and state that's a duplicate of what we |
311 // already have. This should not do any writes. | 311 // already have. This should not do any writes. |
312 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0); | 312 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0); |
313 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate, | 313 cache_->SaveGeometry(extension_id, kWindowId, bounds2_duplicate, |
314 screen_bounds2_duplicate, ui::SHOW_STATE_NORMAL); | 314 screen_bounds2_duplicate, ui::SHOW_STATE_MAXIMIZED); |
315 WaitForSync(); | 315 WaitForSync(); |
316 Mock::VerifyAndClearExpectations(&observer); | 316 Mock::VerifyAndClearExpectations(&observer); |
317 } | 317 } |
318 | 318 |
319 // Tests that no more than kMaxCachedWindows windows will be cached. | 319 // Tests that no more than kMaxCachedWindows windows will be cached. |
320 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) { | 320 TEST_F(ShellWindowGeometryCacheTest, MaxWindows) { |
321 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); | 321 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); |
322 // inform cache of extension | 322 // inform cache of extension |
323 LoadExtension(extension_id); | 323 LoadExtension(extension_id); |
324 | 324 |
325 gfx::Rect bounds(4, 5, 31, 43); | 325 gfx::Rect bounds(4, 5, 31, 43); |
326 gfx::Rect screen_bounds(0, 0, 1600, 900); | 326 gfx::Rect screen_bounds(0, 0, 1600, 900); |
327 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { | 327 for (size_t i = 0; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { |
328 std::string window_id = "window_" + base::IntToString(i); | 328 std::string window_id = "window_" + base::IntToString(i); |
329 cache_->SaveGeometry(extension_id, window_id, bounds, | 329 cache_->SaveGeometry(extension_id, window_id, bounds, |
330 screen_bounds, ui::SHOW_STATE_DEFAULT); | 330 screen_bounds, ui::SHOW_STATE_NORMAL); |
331 } | 331 } |
332 | 332 |
333 // The first added window should no longer have cached geometry. | 333 // The first added window should no longer have cached geometry. |
334 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); | 334 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); |
335 // All other windows should still exist. | 335 // All other windows should still exist. |
336 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { | 336 for (size_t i = 1; i < ShellWindowGeometryCache::kMaxCachedWindows + 1; ++i) { |
337 std::string window_id = "window_" + base::IntToString(i); | 337 std::string window_id = "window_" + base::IntToString(i); |
338 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); | 338 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); |
339 } | 339 } |
340 } | 340 } |
341 | 341 |
342 } // namespace extensions | 342 } // namespace extensions |
OLD | NEW |