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 "extensions/browser/app_window/app_window_geometry_cache.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/mock_pref_change_callback.h"
10 #include "base/prefs/pref_service_factory.h"
11 #include "base/prefs/testing_pref_store.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "content/public/test/test_utils.h"
17 #include "extensions/browser/extension_pref_value_map.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extensions_test.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/value_builder.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using content::BrowserThread
;
26 namespace extensions
{
29 const char kWindowId
[] = "windowid";
30 const char kWindowId2
[] = "windowid2";
32 // Create a very simple extension with id.
33 scoped_refptr
<Extension
> CreateExtension(const std::string
& id
) {
34 return ExtensionBuilder()
35 .SetManifest(DictionaryBuilder().Set("name", "test").Set(
43 // Base class for tests.
44 class AppWindowGeometryCacheTest
: public ExtensionsTest
{
46 AppWindowGeometryCacheTest()
47 : ui_thread_(BrowserThread::UI
, &ui_message_loop_
) {}
49 // testing::Test overrides:
50 void SetUp() override
;
51 void TearDown() override
;
53 void AddGeometryAndLoadExtension(const std::string
& extension_id
,
54 const std::string
& window_id
,
55 const gfx::Rect
& bounds
,
56 const gfx::Rect
& screen_bounds
,
57 ui::WindowShowState state
);
59 // Spins the UI threads' message loops to make sure any task
60 // posted to sync the geometry to the value store gets a chance to run.
63 void LoadExtension(const std::string
& extension_id
);
64 void UnloadExtension(const std::string
& extension_id
);
66 // Creates and adds an extension with associated prefs. Returns the extension
68 std::string
AddExtensionWithPrefs(const std::string
& name
);
71 base::MessageLoopForUI ui_message_loop_
;
72 content::TestBrowserThread ui_thread_
;
73 scoped_ptr
<ExtensionPrefValueMap
> extension_pref_value_map_
;
74 scoped_ptr
<PrefService
> pref_service_
;
75 scoped_ptr
<ExtensionPrefs
> extension_prefs_
;
76 scoped_ptr
<AppWindowGeometryCache
> cache_
;
79 void AppWindowGeometryCacheTest::SetUp() {
80 ExtensionsTest::SetUp();
82 // Set up all the dependencies of ExtensionPrefs.
83 extension_pref_value_map_
.reset(new ExtensionPrefValueMap
);
84 base::PrefServiceFactory factory
;
85 factory
.set_user_prefs(new TestingPrefStore
);
86 factory
.set_extension_prefs(new TestingPrefStore
);
87 user_prefs::PrefRegistrySyncable
* pref_registry
=
88 new user_prefs::PrefRegistrySyncable
;
89 // Prefs should be registered before the PrefService is created.
90 ExtensionPrefs::RegisterProfilePrefs(pref_registry
);
91 pref_service_
= factory
.Create(pref_registry
).Pass();
93 extension_prefs_
.reset(ExtensionPrefs::Create(
94 browser_context(), pref_service_
.get(),
95 browser_context()->GetPath().AppendASCII("Extensions"),
96 extension_pref_value_map_
.get(), false /* extensions_disabled */,
97 std::vector
<ExtensionPrefsObserver
*>()));
100 new AppWindowGeometryCache(browser_context(), extension_prefs_
.get()));
101 cache_
->SetSyncDelayForTests(0);
104 void AppWindowGeometryCacheTest::TearDown() {
106 extension_prefs_
.reset();
107 pref_service_
.reset();
108 extension_pref_value_map_
.reset();
110 ExtensionsTest::TearDown();
113 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension(
114 const std::string
& extension_id
,
115 const std::string
& window_id
,
116 const gfx::Rect
& bounds
,
117 const gfx::Rect
& screen_bounds
,
118 ui::WindowShowState state
) {
119 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
120 base::DictionaryValue
* value
= new base::DictionaryValue
;
121 value
->SetInteger("x", bounds
.x());
122 value
->SetInteger("y", bounds
.y());
123 value
->SetInteger("w", bounds
.width());
124 value
->SetInteger("h", bounds
.height());
125 value
->SetInteger("screen_bounds_x", screen_bounds
.x());
126 value
->SetInteger("screen_bounds_y", screen_bounds
.y());
127 value
->SetInteger("screen_bounds_w", screen_bounds
.width());
128 value
->SetInteger("screen_bounds_h", screen_bounds
.height());
129 value
->SetInteger("state", state
);
130 dict
->SetWithoutPathExpansion(window_id
, value
);
131 extension_prefs_
->SetGeometryCache(extension_id
, dict
.Pass());
132 LoadExtension(extension_id
);
135 void AppWindowGeometryCacheTest::WaitForSync() {
136 content::RunAllPendingInMessageLoop();
139 void AppWindowGeometryCacheTest::LoadExtension(
140 const std::string
& extension_id
) {
141 cache_
->LoadGeometryFromStorage(extension_id
);
145 void AppWindowGeometryCacheTest::UnloadExtension(
146 const std::string
& extension_id
) {
147 scoped_refptr
<Extension
> extension
= CreateExtension(extension_id
);
148 cache_
->OnExtensionUnloaded(browser_context(),
150 UnloadedExtensionInfo::REASON_DISABLE
);
154 std::string
AppWindowGeometryCacheTest::AddExtensionWithPrefs(
155 const std::string
& name
) {
156 // Generate the extension with a path based on the name so that extensions
157 // with different names will have different IDs.
158 base::FilePath path
=
159 browser_context()->GetPath().AppendASCII("Extensions").AppendASCII(name
);
160 scoped_refptr
<Extension
> extension
=
163 DictionaryBuilder().Set("name", "test").Set("version", "0.1"))
167 extension_prefs_
->OnExtensionInstalled(
170 syncer::StringOrdinal::CreateInitialOrdinal(),
172 return extension
->id();
175 // Test getting geometry from an empty store.
176 TEST_F(AppWindowGeometryCacheTest
, GetGeometryEmptyStore
) {
177 const std::string extension_id
= AddExtensionWithPrefs("ext1");
178 ASSERT_FALSE(cache_
->GetGeometry(extension_id
, kWindowId
, NULL
, NULL
, NULL
));
181 // Test getting geometry for an unknown extension.
182 TEST_F(AppWindowGeometryCacheTest
, GetGeometryUnkownExtension
) {
183 const std::string extension_id1
= AddExtensionWithPrefs("ext1");
184 const std::string extension_id2
= AddExtensionWithPrefs("ext2");
185 AddGeometryAndLoadExtension(extension_id1
,
187 gfx::Rect(4, 5, 31, 43),
188 gfx::Rect(0, 0, 1600, 900),
189 ui::SHOW_STATE_NORMAL
);
190 ASSERT_FALSE(cache_
->GetGeometry(extension_id2
, kWindowId
, NULL
, NULL
, NULL
));
193 // Test getting geometry for an unknown window in a known extension.
194 TEST_F(AppWindowGeometryCacheTest
, GetGeometryUnkownWindow
) {
195 const std::string extension_id
= AddExtensionWithPrefs("ext1");
196 AddGeometryAndLoadExtension(extension_id
,
198 gfx::Rect(4, 5, 31, 43),
199 gfx::Rect(0, 0, 1600, 900),
200 ui::SHOW_STATE_NORMAL
);
201 ASSERT_FALSE(cache_
->GetGeometry(extension_id
, kWindowId2
, NULL
, NULL
, NULL
));
204 // Test that loading geometry, screen_bounds and state from the store works
206 TEST_F(AppWindowGeometryCacheTest
, GetGeometryAndStateFromStore
) {
207 const std::string extension_id
= AddExtensionWithPrefs("ext1");
208 gfx::Rect
bounds(4, 5, 31, 43);
209 gfx::Rect
screen_bounds(0, 0, 1600, 900);
210 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
211 AddGeometryAndLoadExtension(
212 extension_id
, kWindowId
, bounds
, screen_bounds
, state
);
213 gfx::Rect new_bounds
;
214 gfx::Rect new_screen_bounds
;
215 ui::WindowShowState new_state
= ui::SHOW_STATE_DEFAULT
;
216 ASSERT_TRUE(cache_
->GetGeometry(
217 extension_id
, kWindowId
, &new_bounds
, &new_screen_bounds
, &new_state
));
218 ASSERT_EQ(bounds
, new_bounds
);
219 ASSERT_EQ(screen_bounds
, new_screen_bounds
);
220 ASSERT_EQ(state
, new_state
);
223 // Test corrupt bounds will not be loaded.
224 TEST_F(AppWindowGeometryCacheTest
, CorruptBounds
) {
225 const std::string extension_id
= AddExtensionWithPrefs("ext1");
227 gfx::Rect
screen_bounds(0, 0, 1600, 900);
228 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
229 AddGeometryAndLoadExtension(
230 extension_id
, kWindowId
, bounds
, screen_bounds
, state
);
231 gfx::Rect new_bounds
;
232 gfx::Rect new_screen_bounds
;
233 ui::WindowShowState new_state
= ui::SHOW_STATE_DEFAULT
;
234 ASSERT_FALSE(cache_
->GetGeometry(
235 extension_id
, kWindowId
, &new_bounds
, &new_screen_bounds
, &new_state
));
236 ASSERT_TRUE(new_bounds
.IsEmpty());
237 ASSERT_TRUE(new_screen_bounds
.IsEmpty());
238 ASSERT_EQ(new_state
, ui::SHOW_STATE_DEFAULT
);
241 // Test corrupt screen bounds will not be loaded.
242 TEST_F(AppWindowGeometryCacheTest
, CorruptScreenBounds
) {
243 const std::string extension_id
= AddExtensionWithPrefs("ext1");
244 gfx::Rect
bounds(4, 5, 31, 43);
245 gfx::Rect screen_bounds
;
246 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
247 AddGeometryAndLoadExtension(
248 extension_id
, kWindowId
, bounds
, screen_bounds
, state
);
249 gfx::Rect new_bounds
;
250 gfx::Rect new_screen_bounds
;
251 ui::WindowShowState new_state
= ui::SHOW_STATE_DEFAULT
;
252 ASSERT_FALSE(cache_
->GetGeometry(
253 extension_id
, kWindowId
, &new_bounds
, &new_screen_bounds
, &new_state
));
254 ASSERT_TRUE(new_bounds
.IsEmpty());
255 ASSERT_TRUE(new_screen_bounds
.IsEmpty());
256 ASSERT_EQ(new_state
, ui::SHOW_STATE_DEFAULT
);
259 // Test corrupt state will not be loaded.
260 TEST_F(AppWindowGeometryCacheTest
, CorruptState
) {
261 const std::string extension_id
= AddExtensionWithPrefs("ext1");
262 gfx::Rect
bounds(4, 5, 31, 43);
263 gfx::Rect
screen_bounds(0, 0, 1600, 900);
264 ui::WindowShowState state
= ui::SHOW_STATE_DEFAULT
;
265 AddGeometryAndLoadExtension(
266 extension_id
, kWindowId
, bounds
, screen_bounds
, state
);
267 gfx::Rect new_bounds
;
268 gfx::Rect new_screen_bounds
;
269 ui::WindowShowState new_state
= ui::SHOW_STATE_DEFAULT
;
270 ASSERT_FALSE(cache_
->GetGeometry(
271 extension_id
, kWindowId
, &new_bounds
, &new_screen_bounds
, &new_state
));
272 ASSERT_TRUE(new_bounds
.IsEmpty());
273 ASSERT_TRUE(new_screen_bounds
.IsEmpty());
274 ASSERT_EQ(new_state
, ui::SHOW_STATE_DEFAULT
);
277 // Test saving geometry, screen_bounds and state to the cache and state store,
278 // and reading it back.
279 TEST_F(AppWindowGeometryCacheTest
, SaveGeometryAndStateToStore
) {
280 const std::string extension_id
= AddExtensionWithPrefs("ext1");
281 const std::string
window_id(kWindowId
);
283 // inform cache of extension
284 LoadExtension(extension_id
);
286 // update geometry stored in cache
287 gfx::Rect
bounds(4, 5, 31, 43);
288 gfx::Rect
screen_bounds(0, 0, 1600, 900);
289 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
290 cache_
->SaveGeometry(extension_id
, window_id
, bounds
, screen_bounds
, state
);
292 // make sure that immediately reading back geometry works
293 gfx::Rect new_bounds
;
294 gfx::Rect new_screen_bounds
;
295 ui::WindowShowState new_state
= ui::SHOW_STATE_DEFAULT
;
296 ASSERT_TRUE(cache_
->GetGeometry(
297 extension_id
, window_id
, &new_bounds
, &new_screen_bounds
, &new_state
));
298 ASSERT_EQ(bounds
, new_bounds
);
299 ASSERT_EQ(screen_bounds
, new_screen_bounds
);
300 ASSERT_EQ(state
, new_state
);
302 // unload extension to force cache to save data to the state store
303 UnloadExtension(extension_id
);
305 // check if geometry got stored correctly in the state store
306 const base::DictionaryValue
* dict
=
307 extension_prefs_
->GetGeometryCache(extension_id
);
310 ASSERT_TRUE(dict
->HasKey(window_id
));
312 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".x", &v
));
313 ASSERT_EQ(bounds
.x(), v
);
314 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".y", &v
));
315 ASSERT_EQ(bounds
.y(), v
);
316 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".w", &v
));
317 ASSERT_EQ(bounds
.width(), v
);
318 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".h", &v
));
319 ASSERT_EQ(bounds
.height(), v
);
320 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".screen_bounds_x", &v
));
321 ASSERT_EQ(screen_bounds
.x(), v
);
322 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".screen_bounds_y", &v
));
323 ASSERT_EQ(screen_bounds
.y(), v
);
324 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".screen_bounds_w", &v
));
325 ASSERT_EQ(screen_bounds
.width(), v
);
326 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".screen_bounds_h", &v
));
327 ASSERT_EQ(screen_bounds
.height(), v
);
328 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".state", &v
));
332 LoadExtension(extension_id
);
333 // and make sure the geometry got reloaded properly too
334 ASSERT_TRUE(cache_
->GetGeometry(
335 extension_id
, window_id
, &new_bounds
, &new_screen_bounds
, &new_state
));
336 ASSERT_EQ(bounds
, new_bounds
);
337 ASSERT_EQ(screen_bounds
, new_screen_bounds
);
338 ASSERT_EQ(state
, new_state
);
341 // Tests that we won't do writes to the state store for SaveGeometry calls
342 // which don't change the state we already have.
343 TEST_F(AppWindowGeometryCacheTest
, NoDuplicateWrites
) {
347 const std::string extension_id
= AddExtensionWithPrefs("ext1");
348 gfx::Rect
bounds1(100, 200, 300, 400);
349 gfx::Rect
bounds2(200, 400, 600, 800);
350 gfx::Rect
bounds2_duplicate(200, 400, 600, 800);
352 gfx::Rect
screen_bounds1(0, 0, 1600, 900);
353 gfx::Rect
screen_bounds2(0, 0, 1366, 768);
354 gfx::Rect
screen_bounds2_duplicate(0, 0, 1366, 768);
356 MockPrefChangeCallback
observer(pref_service_
.get());
357 PrefChangeRegistrar registrar
;
358 registrar
.Init(pref_service_
.get());
359 registrar
.Add("extensions.settings", observer
.GetCallback());
361 // Write the first bounds - it should do > 0 writes.
362 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
363 cache_
->SaveGeometry(
364 extension_id
, kWindowId
, bounds1
, screen_bounds1
, ui::SHOW_STATE_NORMAL
);
366 Mock::VerifyAndClearExpectations(&observer
);
368 // Write a different bounds - it should also do > 0 writes.
369 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
370 cache_
->SaveGeometry(
371 extension_id
, kWindowId
, bounds2
, screen_bounds1
, ui::SHOW_STATE_NORMAL
);
373 Mock::VerifyAndClearExpectations(&observer
);
375 // Write a different screen bounds - it should also do > 0 writes.
376 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
377 cache_
->SaveGeometry(
378 extension_id
, kWindowId
, bounds2
, screen_bounds2
, ui::SHOW_STATE_NORMAL
);
380 Mock::VerifyAndClearExpectations(&observer
);
382 // Write a different state - it should also do > 0 writes.
383 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
384 cache_
->SaveGeometry(extension_id
,
388 ui::SHOW_STATE_MAXIMIZED
);
390 Mock::VerifyAndClearExpectations(&observer
);
392 // Write a bounds, screen bounds and state that's a duplicate of what we
393 // already have. This should not do any writes.
394 EXPECT_CALL(observer
, OnPreferenceChanged(_
)).Times(0);
395 cache_
->SaveGeometry(extension_id
,
398 screen_bounds2_duplicate
,
399 ui::SHOW_STATE_MAXIMIZED
);
401 Mock::VerifyAndClearExpectations(&observer
);
404 // Tests that no more than kMaxCachedWindows windows will be cached.
405 TEST_F(AppWindowGeometryCacheTest
, MaxWindows
) {
406 const std::string extension_id
= AddExtensionWithPrefs("ext1");
407 // inform cache of extension
408 LoadExtension(extension_id
);
410 gfx::Rect
bounds(4, 5, 31, 43);
411 gfx::Rect
screen_bounds(0, 0, 1600, 900);
412 for (size_t i
= 0; i
< AppWindowGeometryCache::kMaxCachedWindows
+ 1; ++i
) {
413 std::string window_id
= "window_" + base::IntToString(i
);
414 cache_
->SaveGeometry(
415 extension_id
, window_id
, bounds
, screen_bounds
, ui::SHOW_STATE_NORMAL
);
418 // The first added window should no longer have cached geometry.
419 EXPECT_FALSE(cache_
->GetGeometry(extension_id
, "window_0", NULL
, NULL
, NULL
));
420 // All other windows should still exist.
421 for (size_t i
= 1; i
< AppWindowGeometryCache::kMaxCachedWindows
+ 1; ++i
) {
422 std::string window_id
= "window_" + base::IntToString(i
);
423 EXPECT_TRUE(cache_
->GetGeometry(extension_id
, window_id
, NULL
, NULL
, NULL
));
427 } // namespace extensions