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
3 // found in the LICENSE file.
5 #include "apps/shell_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/extension_prefs.h"
10 #include "chrome/browser/extensions/test_extension_prefs.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "content/public/test/test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 static const char kWindowId
[] = "windowid";
18 static const char kWindowId2
[] = "windowid2";
20 const char kWindowGeometryKey
[] = "window_geometry";
23 using content::BrowserThread
;
27 // Base class for tests.
28 class ShellWindowGeometryCacheTest
: public testing::Test
{
30 ShellWindowGeometryCacheTest() :
31 ui_thread_(BrowserThread::UI
, &ui_message_loop_
) {
32 prefs_
.reset(new extensions::TestExtensionPrefs(
33 ui_message_loop_
.message_loop_proxy()));
35 new ShellWindowGeometryCache(&profile_
, prefs_
->prefs()));
36 cache_
->SetSyncDelayForTests(0);
39 void AddGeometryAndLoadExtension(
40 const std::string
& extension_id
,
41 const std::string
& window_id
,
42 const gfx::Rect
& bounds
,
43 ui::WindowShowState state
);
45 // Spins the UI threads' message loops to make sure any task
46 // posted to sync the geometry to the value store gets a chance to run.
49 void LoadExtension(const std::string
& extension_id
);
50 void UnloadExtension(const std::string
& extension_id
);
53 TestingProfile profile_
;
54 MessageLoopForUI ui_message_loop_
;
55 content::TestBrowserThread ui_thread_
;
56 scoped_ptr
<extensions::TestExtensionPrefs
> prefs_
;
57 scoped_ptr
<ShellWindowGeometryCache
> cache_
;
60 void ShellWindowGeometryCacheTest::AddGeometryAndLoadExtension(
61 const std::string
& extension_id
,
62 const std::string
& window_id
,
63 const gfx::Rect
& bounds
,
64 ui::WindowShowState state
) {
65 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
66 base::DictionaryValue
* value
= new base::DictionaryValue
;
67 value
->SetInteger("x", bounds
.x());
68 value
->SetInteger("y", bounds
.y());
69 value
->SetInteger("w", bounds
.width());
70 value
->SetInteger("h", 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 ShellWindowGeometryCacheTest::WaitForSync() {
78 content::RunAllPendingInMessageLoop();
81 void ShellWindowGeometryCacheTest::LoadExtension(
82 const std::string
& extension_id
) {
83 cache_
->OnExtensionLoaded(extension_id
);
87 void ShellWindowGeometryCacheTest::UnloadExtension(
88 const std::string
& extension_id
) {
89 cache_
->OnExtensionUnloaded(extension_id
);
93 // Test getting geometry from an empty store.
94 TEST_F(ShellWindowGeometryCacheTest
, GetGeometryEmptyStore
) {
95 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
96 ASSERT_FALSE(cache_
->GetGeometry(extension_id
, kWindowId
, NULL
, NULL
));
99 // Test getting geometry for an unknown extension.
100 TEST_F(ShellWindowGeometryCacheTest
, GetGeometryUnkownExtension
) {
101 const std::string extension_id1
= prefs_
->AddExtensionAndReturnId("ext1");
102 const std::string extension_id2
= prefs_
->AddExtensionAndReturnId("ext2");
103 AddGeometryAndLoadExtension(extension_id1
, kWindowId
,
104 gfx::Rect(4, 5, 31, 43),
105 ui::SHOW_STATE_DEFAULT
);
106 ASSERT_FALSE(cache_
->GetGeometry(extension_id2
, kWindowId
, NULL
, NULL
));
109 // Test getting geometry for an unknown window in a known extension.
110 TEST_F(ShellWindowGeometryCacheTest
, GetGeometryUnkownWindow
) {
111 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
112 AddGeometryAndLoadExtension(extension_id
, kWindowId
,
113 gfx::Rect(4, 5, 31, 43),
114 ui::SHOW_STATE_DEFAULT
);
115 ASSERT_FALSE(cache_
->GetGeometry(extension_id
, kWindowId2
, NULL
, NULL
));
118 // Test that loading geometry and state from the store works correctly.
119 TEST_F(ShellWindowGeometryCacheTest
, GetGeometryAndStateFromStore
) {
120 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
121 gfx::Rect
bounds(4, 5, 31, 43);
122 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
123 AddGeometryAndLoadExtension(extension_id
, kWindowId
, bounds
, state
);
124 gfx::Rect new_bounds
;
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
);
132 // Test saving geometry and state to the cache and state store, and reading
134 TEST_F(ShellWindowGeometryCacheTest
, SaveGeometryAndStateToStore
) {
135 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
136 const std::string
window_id(kWindowId
);
138 // inform cache of extension
139 LoadExtension(extension_id
);
141 // update geometry stored in cache
142 gfx::Rect
bounds(4, 5, 31, 43);
143 ui::WindowShowState state
= ui::SHOW_STATE_NORMAL
;
144 cache_
->SaveGeometry(extension_id
, window_id
, bounds
, state
);
146 // make sure that immediately reading back geometry works
147 gfx::Rect new_bounds
;
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
);
154 // unload extension to force cache to save data to the state store
155 UnloadExtension(extension_id
);
157 // check if geometry got stored correctly in the state store
158 const base::DictionaryValue
* dict
=
159 prefs_
->prefs()->GetGeometryCache(extension_id
);
162 ASSERT_TRUE(dict
->HasKey(window_id
));
164 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".x", &v
));
165 ASSERT_EQ(bounds
.x(), v
);
166 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".y", &v
));
167 ASSERT_EQ(bounds
.y(), v
);
168 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".w", &v
));
169 ASSERT_EQ(bounds
.width(), v
);
170 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".h", &v
));
171 ASSERT_EQ(bounds
.height(), v
);
172 ASSERT_TRUE(dict
->GetInteger(window_id
+ ".state", &v
));
175 // check to make sure cache indeed doesn't know about this extension anymore
176 ASSERT_FALSE(cache_
->GetGeometry(
177 extension_id
, window_id
, &new_bounds
, &new_state
));
180 LoadExtension(extension_id
);
181 // and make sure the geometry got reloaded properly too
182 ASSERT_TRUE(cache_
->GetGeometry(
183 extension_id
, window_id
, &new_bounds
, &new_state
));
184 ASSERT_EQ(bounds
, new_bounds
);
185 ASSERT_EQ(state
, new_state
);
188 // Tests that we won't do writes to the state store for SaveGeometry calls
189 // which don't change the state we already have.
190 TEST_F(ShellWindowGeometryCacheTest
, NoDuplicateWrites
) {
194 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
195 gfx::Rect
bounds1(100, 200, 300, 400);
196 gfx::Rect
bounds2(200, 400, 600, 800);
197 gfx::Rect
bounds2_duplicate(200, 400, 600, 800);
199 MockPrefChangeCallback
observer(prefs_
->pref_service());
200 PrefChangeRegistrar registrar
;
201 registrar
.Init(prefs_
->pref_service());
202 registrar
.Add("extensions.settings", observer
.GetCallback());
204 // Write the first bounds - it should do > 0 writes.
205 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
206 cache_
->SaveGeometry(extension_id
, kWindowId
, bounds1
,
207 ui::SHOW_STATE_DEFAULT
);
209 Mock::VerifyAndClearExpectations(&observer
);
211 // Write a different bounds - it should also do > 0 writes.
212 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
213 cache_
->SaveGeometry(extension_id
, kWindowId
, bounds2
,
214 ui::SHOW_STATE_DEFAULT
);
216 Mock::VerifyAndClearExpectations(&observer
);
218 // Write a different state - it should also do > 0 writes.
219 EXPECT_CALL(observer
, OnPreferenceChanged(_
));
220 cache_
->SaveGeometry(extension_id
, kWindowId
, bounds2
,
221 ui::SHOW_STATE_NORMAL
);
223 Mock::VerifyAndClearExpectations(&observer
);
225 // Write a bounds and state that's a duplicate of what we already have.
226 // This should not do any writes.
227 EXPECT_CALL(observer
, OnPreferenceChanged(_
)).Times(0);
228 cache_
->SaveGeometry(extension_id
, kWindowId
, bounds2_duplicate
,
229 ui::SHOW_STATE_NORMAL
);
231 Mock::VerifyAndClearExpectations(&observer
);
234 // Tests that no more than kMaxCachedWindows windows will be cached.
235 TEST_F(ShellWindowGeometryCacheTest
, MaxWindows
) {
236 const std::string extension_id
= prefs_
->AddExtensionAndReturnId("ext1");
237 // inform cache of extension
238 LoadExtension(extension_id
);
240 gfx::Rect
bounds(4, 5, 31, 43);
241 for (size_t i
= 0; i
< ShellWindowGeometryCache::kMaxCachedWindows
+ 1; ++i
) {
242 std::string window_id
= "window_" + base::IntToString(i
);
243 cache_
->SaveGeometry(extension_id
, window_id
, bounds
,
244 ui::SHOW_STATE_DEFAULT
);
247 // The first added window should no longer have cached geometry.
248 EXPECT_FALSE(cache_
->GetGeometry(extension_id
, "window_0", NULL
, NULL
));
249 // All other windows should still exist.
250 for (size_t i
= 1; i
< ShellWindowGeometryCache::kMaxCachedWindows
+ 1; ++i
) {
251 std::string window_id
= "window_" + base::IntToString(i
);
252 EXPECT_TRUE(cache_
->GetGeometry(extension_id
, window_id
, NULL
, NULL
));
256 } // namespace extensions