Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / app_window / app_window_geometry_cache_unittest.cc
bloba526e7d378bd3976a104cc5e4a3660d95abaff15
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 "base/memory/scoped_ptr.h"
6 #include "base/prefs/mock_pref_change_callback.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/test_extension_prefs.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "content/public/test/test_utils.h"
12 #include "extensions/browser/app_window/app_window_geometry_cache.h"
13 #include "extensions/browser/extension_prefs.h"
14 #include "extensions/common/extension_builder.h"
15 #include "extensions/common/value_builder.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using content::BrowserThread;
20 namespace extensions {
22 namespace {
23 const char kWindowId[] = "windowid";
24 const char kWindowId2[] = "windowid2";
26 // Create a very simple extension with id.
27 scoped_refptr<Extension> CreateExtension(const std::string& id) {
28 return ExtensionBuilder()
29 .SetManifest(DictionaryBuilder().Set("name", "test").Set(
30 "version", "0.1"))
31 .SetID(id)
32 .Build();
35 } // namespace
37 // Base class for tests.
38 class AppWindowGeometryCacheTest : public testing::Test {
39 public:
40 AppWindowGeometryCacheTest()
41 : profile_(new TestingProfile),
42 ui_thread_(BrowserThread::UI, &ui_message_loop_) {
43 prefs_.reset(new TestExtensionPrefs(
44 ui_message_loop_.message_loop_proxy().get()));
45 cache_.reset(new AppWindowGeometryCache(profile_.get(), prefs_->prefs()));
46 cache_->SetSyncDelayForTests(0);
49 void AddGeometryAndLoadExtension(const std::string& extension_id,
50 const std::string& window_id,
51 const gfx::Rect& bounds,
52 const gfx::Rect& screen_bounds,
53 ui::WindowShowState state);
55 // Spins the UI threads' message loops to make sure any task
56 // posted to sync the geometry to the value store gets a chance to run.
57 void WaitForSync();
59 void LoadExtension(const std::string& extension_id);
60 void UnloadExtension(const std::string& extension_id);
62 protected:
63 scoped_ptr<TestingProfile> profile_;
64 base::MessageLoopForUI ui_message_loop_;
65 content::TestBrowserThread ui_thread_;
66 scoped_ptr<TestExtensionPrefs> prefs_;
67 scoped_ptr<AppWindowGeometryCache> cache_;
70 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension(
71 const std::string& extension_id,
72 const std::string& window_id,
73 const gfx::Rect& bounds,
74 const gfx::Rect& screen_bounds,
75 ui::WindowShowState state) {
76 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
77 base::DictionaryValue* value = new base::DictionaryValue;
78 value->SetInteger("x", bounds.x());
79 value->SetInteger("y", bounds.y());
80 value->SetInteger("w", bounds.width());
81 value->SetInteger("h", bounds.height());
82 value->SetInteger("screen_bounds_x", screen_bounds.x());
83 value->SetInteger("screen_bounds_y", screen_bounds.y());
84 value->SetInteger("screen_bounds_w", screen_bounds.width());
85 value->SetInteger("screen_bounds_h", screen_bounds.height());
86 value->SetInteger("state", state);
87 dict->SetWithoutPathExpansion(window_id, value);
88 prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass());
89 LoadExtension(extension_id);
92 void AppWindowGeometryCacheTest::WaitForSync() {
93 content::RunAllPendingInMessageLoop();
96 void AppWindowGeometryCacheTest::LoadExtension(
97 const std::string& extension_id) {
98 cache_->LoadGeometryFromStorage(extension_id);
99 WaitForSync();
102 void AppWindowGeometryCacheTest::UnloadExtension(
103 const std::string& extension_id) {
104 scoped_refptr<Extension> extension = CreateExtension(extension_id);
105 cache_->OnExtensionUnloaded(
106 profile_.get(),
107 extension.get(),
108 UnloadedExtensionInfo::REASON_DISABLE);
109 WaitForSync();
112 // Test getting geometry from an empty store.
113 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) {
114 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
115 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL));
118 // Test getting geometry for an unknown extension.
119 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownExtension) {
120 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1");
121 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2");
122 AddGeometryAndLoadExtension(extension_id1,
123 kWindowId,
124 gfx::Rect(4, 5, 31, 43),
125 gfx::Rect(0, 0, 1600, 900),
126 ui::SHOW_STATE_NORMAL);
127 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL));
130 // Test getting geometry for an unknown window in a known extension.
131 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownWindow) {
132 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
133 AddGeometryAndLoadExtension(extension_id,
134 kWindowId,
135 gfx::Rect(4, 5, 31, 43),
136 gfx::Rect(0, 0, 1600, 900),
137 ui::SHOW_STATE_NORMAL);
138 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL));
141 // Test that loading geometry, screen_bounds and state from the store works
142 // correctly.
143 TEST_F(AppWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
144 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
145 gfx::Rect bounds(4, 5, 31, 43);
146 gfx::Rect screen_bounds(0, 0, 1600, 900);
147 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
148 AddGeometryAndLoadExtension(
149 extension_id, kWindowId, bounds, screen_bounds, state);
150 gfx::Rect new_bounds;
151 gfx::Rect new_screen_bounds;
152 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
153 ASSERT_TRUE(cache_->GetGeometry(
154 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
155 ASSERT_EQ(bounds, new_bounds);
156 ASSERT_EQ(screen_bounds, new_screen_bounds);
157 ASSERT_EQ(state, new_state);
160 // Test corrupt bounds will not be loaded.
161 TEST_F(AppWindowGeometryCacheTest, CorruptBounds) {
162 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
163 gfx::Rect bounds;
164 gfx::Rect screen_bounds(0, 0, 1600, 900);
165 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
166 AddGeometryAndLoadExtension(
167 extension_id, kWindowId, bounds, screen_bounds, state);
168 gfx::Rect new_bounds;
169 gfx::Rect new_screen_bounds;
170 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
171 ASSERT_FALSE(cache_->GetGeometry(
172 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
173 ASSERT_TRUE(new_bounds.IsEmpty());
174 ASSERT_TRUE(new_screen_bounds.IsEmpty());
175 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
178 // Test corrupt screen bounds will not be loaded.
179 TEST_F(AppWindowGeometryCacheTest, CorruptScreenBounds) {
180 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
181 gfx::Rect bounds(4, 5, 31, 43);
182 gfx::Rect screen_bounds;
183 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
184 AddGeometryAndLoadExtension(
185 extension_id, kWindowId, bounds, screen_bounds, state);
186 gfx::Rect new_bounds;
187 gfx::Rect new_screen_bounds;
188 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
189 ASSERT_FALSE(cache_->GetGeometry(
190 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
191 ASSERT_TRUE(new_bounds.IsEmpty());
192 ASSERT_TRUE(new_screen_bounds.IsEmpty());
193 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
196 // Test corrupt state will not be loaded.
197 TEST_F(AppWindowGeometryCacheTest, CorruptState) {
198 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
199 gfx::Rect bounds(4, 5, 31, 43);
200 gfx::Rect screen_bounds(0, 0, 1600, 900);
201 ui::WindowShowState state = ui::SHOW_STATE_DEFAULT;
202 AddGeometryAndLoadExtension(
203 extension_id, kWindowId, bounds, screen_bounds, state);
204 gfx::Rect new_bounds;
205 gfx::Rect new_screen_bounds;
206 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
207 ASSERT_FALSE(cache_->GetGeometry(
208 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
209 ASSERT_TRUE(new_bounds.IsEmpty());
210 ASSERT_TRUE(new_screen_bounds.IsEmpty());
211 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
214 // Test saving geometry, screen_bounds and state to the cache and state store,
215 // and reading it back.
216 TEST_F(AppWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
217 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
218 const std::string window_id(kWindowId);
220 // inform cache of extension
221 LoadExtension(extension_id);
223 // update geometry stored in cache
224 gfx::Rect bounds(4, 5, 31, 43);
225 gfx::Rect screen_bounds(0, 0, 1600, 900);
226 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
227 cache_->SaveGeometry(extension_id, window_id, bounds, screen_bounds, state);
229 // make sure that immediately reading back geometry works
230 gfx::Rect new_bounds;
231 gfx::Rect new_screen_bounds;
232 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
233 ASSERT_TRUE(cache_->GetGeometry(
234 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state));
235 ASSERT_EQ(bounds, new_bounds);
236 ASSERT_EQ(screen_bounds, new_screen_bounds);
237 ASSERT_EQ(state, new_state);
239 // unload extension to force cache to save data to the state store
240 UnloadExtension(extension_id);
242 // check if geometry got stored correctly in the state store
243 const base::DictionaryValue* dict =
244 prefs_->prefs()->GetGeometryCache(extension_id);
245 ASSERT_TRUE(dict);
247 ASSERT_TRUE(dict->HasKey(window_id));
248 int v;
249 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
250 ASSERT_EQ(bounds.x(), v);
251 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
252 ASSERT_EQ(bounds.y(), v);
253 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
254 ASSERT_EQ(bounds.width(), v);
255 ASSERT_TRUE(dict->GetInteger(window_id + ".h", &v));
256 ASSERT_EQ(bounds.height(), v);
257 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_x", &v));
258 ASSERT_EQ(screen_bounds.x(), v);
259 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_y", &v));
260 ASSERT_EQ(screen_bounds.y(), v);
261 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_w", &v));
262 ASSERT_EQ(screen_bounds.width(), v);
263 ASSERT_TRUE(dict->GetInteger(window_id + ".screen_bounds_h", &v));
264 ASSERT_EQ(screen_bounds.height(), v);
265 ASSERT_TRUE(dict->GetInteger(window_id + ".state", &v));
266 ASSERT_EQ(state, v);
268 // reload extension
269 LoadExtension(extension_id);
270 // and make sure the geometry got reloaded properly too
271 ASSERT_TRUE(cache_->GetGeometry(
272 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state));
273 ASSERT_EQ(bounds, new_bounds);
274 ASSERT_EQ(screen_bounds, new_screen_bounds);
275 ASSERT_EQ(state, new_state);
278 // Tests that we won't do writes to the state store for SaveGeometry calls
279 // which don't change the state we already have.
280 TEST_F(AppWindowGeometryCacheTest, NoDuplicateWrites) {
281 using testing::_;
282 using testing::Mock;
284 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
285 gfx::Rect bounds1(100, 200, 300, 400);
286 gfx::Rect bounds2(200, 400, 600, 800);
287 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
289 gfx::Rect screen_bounds1(0, 0, 1600, 900);
290 gfx::Rect screen_bounds2(0, 0, 1366, 768);
291 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768);
293 MockPrefChangeCallback observer(prefs_->pref_service());
294 PrefChangeRegistrar registrar;
295 registrar.Init(prefs_->pref_service());
296 registrar.Add("extensions.settings", observer.GetCallback());
298 // Write the first bounds - it should do > 0 writes.
299 EXPECT_CALL(observer, OnPreferenceChanged(_));
300 cache_->SaveGeometry(
301 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL);
302 WaitForSync();
303 Mock::VerifyAndClearExpectations(&observer);
305 // Write a different bounds - it should also do > 0 writes.
306 EXPECT_CALL(observer, OnPreferenceChanged(_));
307 cache_->SaveGeometry(
308 extension_id, kWindowId, bounds2, screen_bounds1, ui::SHOW_STATE_NORMAL);
309 WaitForSync();
310 Mock::VerifyAndClearExpectations(&observer);
312 // Write a different screen bounds - it should also do > 0 writes.
313 EXPECT_CALL(observer, OnPreferenceChanged(_));
314 cache_->SaveGeometry(
315 extension_id, kWindowId, bounds2, screen_bounds2, ui::SHOW_STATE_NORMAL);
316 WaitForSync();
317 Mock::VerifyAndClearExpectations(&observer);
319 // Write a different state - it should also do > 0 writes.
320 EXPECT_CALL(observer, OnPreferenceChanged(_));
321 cache_->SaveGeometry(extension_id,
322 kWindowId,
323 bounds2,
324 screen_bounds2,
325 ui::SHOW_STATE_MAXIMIZED);
326 WaitForSync();
327 Mock::VerifyAndClearExpectations(&observer);
329 // Write a bounds, screen bounds and state that's a duplicate of what we
330 // already have. This should not do any writes.
331 EXPECT_CALL(observer, OnPreferenceChanged(_)).Times(0);
332 cache_->SaveGeometry(extension_id,
333 kWindowId,
334 bounds2_duplicate,
335 screen_bounds2_duplicate,
336 ui::SHOW_STATE_MAXIMIZED);
337 WaitForSync();
338 Mock::VerifyAndClearExpectations(&observer);
341 // Tests that no more than kMaxCachedWindows windows will be cached.
342 TEST_F(AppWindowGeometryCacheTest, MaxWindows) {
343 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1");
344 // inform cache of extension
345 LoadExtension(extension_id);
347 gfx::Rect bounds(4, 5, 31, 43);
348 gfx::Rect screen_bounds(0, 0, 1600, 900);
349 for (size_t i = 0; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
350 std::string window_id = "window_" + base::IntToString(i);
351 cache_->SaveGeometry(
352 extension_id, window_id, bounds, screen_bounds, ui::SHOW_STATE_NORMAL);
355 // The first added window should no longer have cached geometry.
356 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL));
357 // All other windows should still exist.
358 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
359 std::string window_id = "window_" + base::IntToString(i);
360 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL));
364 } // namespace extensions