Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: extensions/browser/app_window/app_window_geometry_cache_unittest.cc

Issue 549663002: Move AppWindowGeometryCacheTest to extensions_unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: (app-window) rebase 2 Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | extensions/browser/null_app_sorting.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "extensions/browser/app_window/app_window_geometry_cache.h"
6
7 #include "base/files/file_path.h"
5 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/mock_pref_change_callback.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"
7 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/test_extension_prefs.h" 13 #include "components/pref_registry/pref_registry_syncable.h"
9 #include "chrome/test/base/testing_profile.h" 14 #include "content/public/test/test_browser_context.h"
10 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
11 #include "content/public/test/test_utils.h" 16 #include "content/public/test/test_utils.h"
12 #include "extensions/browser/app_window/app_window_geometry_cache.h" 17 #include "extensions/browser/extension_pref_value_map.h"
13 #include "extensions/browser/extension_prefs.h" 18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extensions_test.h"
20 #include "extensions/browser/null_app_sorting.h"
14 #include "extensions/common/extension_builder.h" 21 #include "extensions/common/extension_builder.h"
15 #include "extensions/common/value_builder.h" 22 #include "extensions/common/value_builder.h"
16 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
17 24
18 using content::BrowserThread; 25 using content::BrowserThread;
19 26
20 namespace extensions { 27 namespace extensions {
21 28
22 namespace { 29 namespace {
23 const char kWindowId[] = "windowid"; 30 const char kWindowId[] = "windowid";
24 const char kWindowId2[] = "windowid2"; 31 const char kWindowId2[] = "windowid2";
25 32
26 // Create a very simple extension with id. 33 // Create a very simple extension with id.
27 scoped_refptr<Extension> CreateExtension(const std::string& id) { 34 scoped_refptr<Extension> CreateExtension(const std::string& id) {
28 return ExtensionBuilder() 35 return ExtensionBuilder()
29 .SetManifest(DictionaryBuilder().Set("name", "test").Set( 36 .SetManifest(DictionaryBuilder().Set("name", "test").Set(
30 "version", "0.1")) 37 "version", "0.1"))
31 .SetID(id) 38 .SetID(id)
32 .Build(); 39 .Build();
33 } 40 }
34 41
35 } // namespace 42 } // namespace
36 43
37 // Base class for tests. 44 // Base class for tests.
38 class AppWindowGeometryCacheTest : public testing::Test { 45 class AppWindowGeometryCacheTest : public ExtensionsTest {
39 public: 46 public:
40 AppWindowGeometryCacheTest() 47 AppWindowGeometryCacheTest()
41 : profile_(new TestingProfile), 48 : ui_thread_(BrowserThread::UI, &ui_message_loop_) {}
42 ui_thread_(BrowserThread::UI, &ui_message_loop_) { 49
43 prefs_.reset(new TestExtensionPrefs( 50 // testing::Test overrides:
44 ui_message_loop_.message_loop_proxy().get())); 51 virtual void SetUp() OVERRIDE;
45 cache_.reset(new AppWindowGeometryCache(profile_.get(), prefs_->prefs())); 52 virtual void TearDown() OVERRIDE;
46 cache_->SetSyncDelayForTests(0);
47 }
48 53
49 void AddGeometryAndLoadExtension(const std::string& extension_id, 54 void AddGeometryAndLoadExtension(const std::string& extension_id,
50 const std::string& window_id, 55 const std::string& window_id,
51 const gfx::Rect& bounds, 56 const gfx::Rect& bounds,
52 const gfx::Rect& screen_bounds, 57 const gfx::Rect& screen_bounds,
53 ui::WindowShowState state); 58 ui::WindowShowState state);
54 59
55 // Spins the UI threads' message loops to make sure any task 60 // 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. 61 // posted to sync the geometry to the value store gets a chance to run.
57 void WaitForSync(); 62 void WaitForSync();
58 63
59 void LoadExtension(const std::string& extension_id); 64 void LoadExtension(const std::string& extension_id);
60 void UnloadExtension(const std::string& extension_id); 65 void UnloadExtension(const std::string& extension_id);
61 66
67 // Creates and adds an extension with associated prefs. Returns the extension
68 // ID.
69 std::string AddExtensionWithPrefs(const std::string& name);
70
62 protected: 71 protected:
63 scoped_ptr<TestingProfile> profile_;
64 base::MessageLoopForUI ui_message_loop_; 72 base::MessageLoopForUI ui_message_loop_;
65 content::TestBrowserThread ui_thread_; 73 content::TestBrowserThread ui_thread_;
66 scoped_ptr<TestExtensionPrefs> prefs_; 74 scoped_ptr<ExtensionPrefValueMap> extension_pref_value_map_;
75 scoped_ptr<PrefService> pref_service_;
76 scoped_ptr<ExtensionPrefs> extension_prefs_;
67 scoped_ptr<AppWindowGeometryCache> cache_; 77 scoped_ptr<AppWindowGeometryCache> cache_;
68 }; 78 };
69 79
80 void AppWindowGeometryCacheTest::SetUp() {
81 ExtensionsTest::SetUp();
82
83 // Set up all the dependencies of ExtensionPrefs.
84 extension_pref_value_map_.reset(new ExtensionPrefValueMap);
85 base::PrefServiceFactory factory;
86 factory.set_user_prefs(new TestingPrefStore);
87 factory.set_extension_prefs(new TestingPrefStore);
88 user_prefs::PrefRegistrySyncable* pref_registry =
89 new user_prefs::PrefRegistrySyncable;
90 // Prefs should be registered before the PrefService is created.
91 ExtensionPrefs::RegisterProfilePrefs(pref_registry);
92 pref_service_ = factory.Create(pref_registry).Pass();
93
94 extension_prefs_.reset(ExtensionPrefs::Create(
95 pref_service_.get(),
96 browser_context()->GetPath().AppendASCII("Extensions"),
97 extension_pref_value_map_.get(),
98 scoped_ptr<AppSorting>(new NullAppSorting),
99 false /* extensions_disabled */,
100 std::vector<ExtensionPrefsObserver*>()));
101
102 cache_.reset(
103 new AppWindowGeometryCache(browser_context(), extension_prefs_.get()));
104 cache_->SetSyncDelayForTests(0);
105 }
106
107 void AppWindowGeometryCacheTest::TearDown() {
108 cache_.reset();
109 extension_prefs_.reset();
110 pref_service_.reset();
111 extension_pref_value_map_.reset();
112
113 ExtensionsTest::TearDown();
114 }
115
70 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension( 116 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension(
71 const std::string& extension_id, 117 const std::string& extension_id,
72 const std::string& window_id, 118 const std::string& window_id,
73 const gfx::Rect& bounds, 119 const gfx::Rect& bounds,
74 const gfx::Rect& screen_bounds, 120 const gfx::Rect& screen_bounds,
75 ui::WindowShowState state) { 121 ui::WindowShowState state) {
76 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 122 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
77 base::DictionaryValue* value = new base::DictionaryValue; 123 base::DictionaryValue* value = new base::DictionaryValue;
78 value->SetInteger("x", bounds.x()); 124 value->SetInteger("x", bounds.x());
79 value->SetInteger("y", bounds.y()); 125 value->SetInteger("y", bounds.y());
80 value->SetInteger("w", bounds.width()); 126 value->SetInteger("w", bounds.width());
81 value->SetInteger("h", bounds.height()); 127 value->SetInteger("h", bounds.height());
82 value->SetInteger("screen_bounds_x", screen_bounds.x()); 128 value->SetInteger("screen_bounds_x", screen_bounds.x());
83 value->SetInteger("screen_bounds_y", screen_bounds.y()); 129 value->SetInteger("screen_bounds_y", screen_bounds.y());
84 value->SetInteger("screen_bounds_w", screen_bounds.width()); 130 value->SetInteger("screen_bounds_w", screen_bounds.width());
85 value->SetInteger("screen_bounds_h", screen_bounds.height()); 131 value->SetInteger("screen_bounds_h", screen_bounds.height());
86 value->SetInteger("state", state); 132 value->SetInteger("state", state);
87 dict->SetWithoutPathExpansion(window_id, value); 133 dict->SetWithoutPathExpansion(window_id, value);
88 prefs_->prefs()->SetGeometryCache(extension_id, dict.Pass()); 134 extension_prefs_->SetGeometryCache(extension_id, dict.Pass());
89 LoadExtension(extension_id); 135 LoadExtension(extension_id);
90 } 136 }
91 137
92 void AppWindowGeometryCacheTest::WaitForSync() { 138 void AppWindowGeometryCacheTest::WaitForSync() {
93 content::RunAllPendingInMessageLoop(); 139 content::RunAllPendingInMessageLoop();
94 } 140 }
95 141
96 void AppWindowGeometryCacheTest::LoadExtension( 142 void AppWindowGeometryCacheTest::LoadExtension(
97 const std::string& extension_id) { 143 const std::string& extension_id) {
98 cache_->LoadGeometryFromStorage(extension_id); 144 cache_->LoadGeometryFromStorage(extension_id);
99 WaitForSync(); 145 WaitForSync();
100 } 146 }
101 147
102 void AppWindowGeometryCacheTest::UnloadExtension( 148 void AppWindowGeometryCacheTest::UnloadExtension(
103 const std::string& extension_id) { 149 const std::string& extension_id) {
104 scoped_refptr<Extension> extension = CreateExtension(extension_id); 150 scoped_refptr<Extension> extension = CreateExtension(extension_id);
105 cache_->OnExtensionUnloaded( 151 cache_->OnExtensionUnloaded(browser_context(),
106 profile_.get(), 152 extension.get(),
153 UnloadedExtensionInfo::REASON_DISABLE);
154 WaitForSync();
155 }
156
157 std::string AppWindowGeometryCacheTest::AddExtensionWithPrefs(
158 const std::string& name) {
159 // Generate the extension with a path based on the name so that extensions
160 // with different names will have different IDs.
161 base::FilePath path =
162 browser_context()->GetPath().AppendASCII("Extensions").AppendASCII(name);
163 scoped_refptr<Extension> extension =
164 ExtensionBuilder()
165 .SetManifest(
166 DictionaryBuilder().Set("name", "test").Set("version", "0.1"))
167 .SetPath(path)
168 .Build();
169
170 extension_prefs_->OnExtensionInstalled(
107 extension.get(), 171 extension.get(),
108 UnloadedExtensionInfo::REASON_DISABLE); 172 Extension::ENABLED,
109 WaitForSync(); 173 syncer::StringOrdinal::CreateInitialOrdinal(),
174 std::string());
175 return extension->id();
110 } 176 }
111 177
112 // Test getting geometry from an empty store. 178 // Test getting geometry from an empty store.
113 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) { 179 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) {
114 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 180 const std::string extension_id = AddExtensionWithPrefs("ext1");
115 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL)); 181 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL));
116 } 182 }
117 183
118 // Test getting geometry for an unknown extension. 184 // Test getting geometry for an unknown extension.
119 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownExtension) { 185 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownExtension) {
120 const std::string extension_id1 = prefs_->AddExtensionAndReturnId("ext1"); 186 const std::string extension_id1 = AddExtensionWithPrefs("ext1");
121 const std::string extension_id2 = prefs_->AddExtensionAndReturnId("ext2"); 187 const std::string extension_id2 = AddExtensionWithPrefs("ext2");
122 AddGeometryAndLoadExtension(extension_id1, 188 AddGeometryAndLoadExtension(extension_id1,
123 kWindowId, 189 kWindowId,
124 gfx::Rect(4, 5, 31, 43), 190 gfx::Rect(4, 5, 31, 43),
125 gfx::Rect(0, 0, 1600, 900), 191 gfx::Rect(0, 0, 1600, 900),
126 ui::SHOW_STATE_NORMAL); 192 ui::SHOW_STATE_NORMAL);
127 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL)); 193 ASSERT_FALSE(cache_->GetGeometry(extension_id2, kWindowId, NULL, NULL, NULL));
128 } 194 }
129 195
130 // Test getting geometry for an unknown window in a known extension. 196 // Test getting geometry for an unknown window in a known extension.
131 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownWindow) { 197 TEST_F(AppWindowGeometryCacheTest, GetGeometryUnkownWindow) {
132 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 198 const std::string extension_id = AddExtensionWithPrefs("ext1");
133 AddGeometryAndLoadExtension(extension_id, 199 AddGeometryAndLoadExtension(extension_id,
134 kWindowId, 200 kWindowId,
135 gfx::Rect(4, 5, 31, 43), 201 gfx::Rect(4, 5, 31, 43),
136 gfx::Rect(0, 0, 1600, 900), 202 gfx::Rect(0, 0, 1600, 900),
137 ui::SHOW_STATE_NORMAL); 203 ui::SHOW_STATE_NORMAL);
138 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL)); 204 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId2, NULL, NULL, NULL));
139 } 205 }
140 206
141 // Test that loading geometry, screen_bounds and state from the store works 207 // Test that loading geometry, screen_bounds and state from the store works
142 // correctly. 208 // correctly.
143 TEST_F(AppWindowGeometryCacheTest, GetGeometryAndStateFromStore) { 209 TEST_F(AppWindowGeometryCacheTest, GetGeometryAndStateFromStore) {
144 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 210 const std::string extension_id = AddExtensionWithPrefs("ext1");
145 gfx::Rect bounds(4, 5, 31, 43); 211 gfx::Rect bounds(4, 5, 31, 43);
146 gfx::Rect screen_bounds(0, 0, 1600, 900); 212 gfx::Rect screen_bounds(0, 0, 1600, 900);
147 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; 213 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
148 AddGeometryAndLoadExtension( 214 AddGeometryAndLoadExtension(
149 extension_id, kWindowId, bounds, screen_bounds, state); 215 extension_id, kWindowId, bounds, screen_bounds, state);
150 gfx::Rect new_bounds; 216 gfx::Rect new_bounds;
151 gfx::Rect new_screen_bounds; 217 gfx::Rect new_screen_bounds;
152 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT; 218 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
153 ASSERT_TRUE(cache_->GetGeometry( 219 ASSERT_TRUE(cache_->GetGeometry(
154 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state)); 220 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
155 ASSERT_EQ(bounds, new_bounds); 221 ASSERT_EQ(bounds, new_bounds);
156 ASSERT_EQ(screen_bounds, new_screen_bounds); 222 ASSERT_EQ(screen_bounds, new_screen_bounds);
157 ASSERT_EQ(state, new_state); 223 ASSERT_EQ(state, new_state);
158 } 224 }
159 225
160 // Test corrupt bounds will not be loaded. 226 // Test corrupt bounds will not be loaded.
161 TEST_F(AppWindowGeometryCacheTest, CorruptBounds) { 227 TEST_F(AppWindowGeometryCacheTest, CorruptBounds) {
162 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 228 const std::string extension_id = AddExtensionWithPrefs("ext1");
163 gfx::Rect bounds; 229 gfx::Rect bounds;
164 gfx::Rect screen_bounds(0, 0, 1600, 900); 230 gfx::Rect screen_bounds(0, 0, 1600, 900);
165 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; 231 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
166 AddGeometryAndLoadExtension( 232 AddGeometryAndLoadExtension(
167 extension_id, kWindowId, bounds, screen_bounds, state); 233 extension_id, kWindowId, bounds, screen_bounds, state);
168 gfx::Rect new_bounds; 234 gfx::Rect new_bounds;
169 gfx::Rect new_screen_bounds; 235 gfx::Rect new_screen_bounds;
170 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT; 236 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
171 ASSERT_FALSE(cache_->GetGeometry( 237 ASSERT_FALSE(cache_->GetGeometry(
172 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state)); 238 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
173 ASSERT_TRUE(new_bounds.IsEmpty()); 239 ASSERT_TRUE(new_bounds.IsEmpty());
174 ASSERT_TRUE(new_screen_bounds.IsEmpty()); 240 ASSERT_TRUE(new_screen_bounds.IsEmpty());
175 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT); 241 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
176 } 242 }
177 243
178 // Test corrupt screen bounds will not be loaded. 244 // Test corrupt screen bounds will not be loaded.
179 TEST_F(AppWindowGeometryCacheTest, CorruptScreenBounds) { 245 TEST_F(AppWindowGeometryCacheTest, CorruptScreenBounds) {
180 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 246 const std::string extension_id = AddExtensionWithPrefs("ext1");
181 gfx::Rect bounds(4, 5, 31, 43); 247 gfx::Rect bounds(4, 5, 31, 43);
182 gfx::Rect screen_bounds; 248 gfx::Rect screen_bounds;
183 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; 249 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
184 AddGeometryAndLoadExtension( 250 AddGeometryAndLoadExtension(
185 extension_id, kWindowId, bounds, screen_bounds, state); 251 extension_id, kWindowId, bounds, screen_bounds, state);
186 gfx::Rect new_bounds; 252 gfx::Rect new_bounds;
187 gfx::Rect new_screen_bounds; 253 gfx::Rect new_screen_bounds;
188 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT; 254 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
189 ASSERT_FALSE(cache_->GetGeometry( 255 ASSERT_FALSE(cache_->GetGeometry(
190 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state)); 256 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
191 ASSERT_TRUE(new_bounds.IsEmpty()); 257 ASSERT_TRUE(new_bounds.IsEmpty());
192 ASSERT_TRUE(new_screen_bounds.IsEmpty()); 258 ASSERT_TRUE(new_screen_bounds.IsEmpty());
193 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT); 259 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
194 } 260 }
195 261
196 // Test corrupt state will not be loaded. 262 // Test corrupt state will not be loaded.
197 TEST_F(AppWindowGeometryCacheTest, CorruptState) { 263 TEST_F(AppWindowGeometryCacheTest, CorruptState) {
198 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 264 const std::string extension_id = AddExtensionWithPrefs("ext1");
199 gfx::Rect bounds(4, 5, 31, 43); 265 gfx::Rect bounds(4, 5, 31, 43);
200 gfx::Rect screen_bounds(0, 0, 1600, 900); 266 gfx::Rect screen_bounds(0, 0, 1600, 900);
201 ui::WindowShowState state = ui::SHOW_STATE_DEFAULT; 267 ui::WindowShowState state = ui::SHOW_STATE_DEFAULT;
202 AddGeometryAndLoadExtension( 268 AddGeometryAndLoadExtension(
203 extension_id, kWindowId, bounds, screen_bounds, state); 269 extension_id, kWindowId, bounds, screen_bounds, state);
204 gfx::Rect new_bounds; 270 gfx::Rect new_bounds;
205 gfx::Rect new_screen_bounds; 271 gfx::Rect new_screen_bounds;
206 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT; 272 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
207 ASSERT_FALSE(cache_->GetGeometry( 273 ASSERT_FALSE(cache_->GetGeometry(
208 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state)); 274 extension_id, kWindowId, &new_bounds, &new_screen_bounds, &new_state));
209 ASSERT_TRUE(new_bounds.IsEmpty()); 275 ASSERT_TRUE(new_bounds.IsEmpty());
210 ASSERT_TRUE(new_screen_bounds.IsEmpty()); 276 ASSERT_TRUE(new_screen_bounds.IsEmpty());
211 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT); 277 ASSERT_EQ(new_state, ui::SHOW_STATE_DEFAULT);
212 } 278 }
213 279
214 // Test saving geometry, screen_bounds and state to the cache and state store, 280 // Test saving geometry, screen_bounds and state to the cache and state store,
215 // and reading it back. 281 // and reading it back.
216 TEST_F(AppWindowGeometryCacheTest, SaveGeometryAndStateToStore) { 282 TEST_F(AppWindowGeometryCacheTest, SaveGeometryAndStateToStore) {
217 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 283 const std::string extension_id = AddExtensionWithPrefs("ext1");
218 const std::string window_id(kWindowId); 284 const std::string window_id(kWindowId);
219 285
220 // inform cache of extension 286 // inform cache of extension
221 LoadExtension(extension_id); 287 LoadExtension(extension_id);
222 288
223 // update geometry stored in cache 289 // update geometry stored in cache
224 gfx::Rect bounds(4, 5, 31, 43); 290 gfx::Rect bounds(4, 5, 31, 43);
225 gfx::Rect screen_bounds(0, 0, 1600, 900); 291 gfx::Rect screen_bounds(0, 0, 1600, 900);
226 ui::WindowShowState state = ui::SHOW_STATE_NORMAL; 292 ui::WindowShowState state = ui::SHOW_STATE_NORMAL;
227 cache_->SaveGeometry(extension_id, window_id, bounds, screen_bounds, state); 293 cache_->SaveGeometry(extension_id, window_id, bounds, screen_bounds, state);
228 294
229 // make sure that immediately reading back geometry works 295 // make sure that immediately reading back geometry works
230 gfx::Rect new_bounds; 296 gfx::Rect new_bounds;
231 gfx::Rect new_screen_bounds; 297 gfx::Rect new_screen_bounds;
232 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT; 298 ui::WindowShowState new_state = ui::SHOW_STATE_DEFAULT;
233 ASSERT_TRUE(cache_->GetGeometry( 299 ASSERT_TRUE(cache_->GetGeometry(
234 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state)); 300 extension_id, window_id, &new_bounds, &new_screen_bounds, &new_state));
235 ASSERT_EQ(bounds, new_bounds); 301 ASSERT_EQ(bounds, new_bounds);
236 ASSERT_EQ(screen_bounds, new_screen_bounds); 302 ASSERT_EQ(screen_bounds, new_screen_bounds);
237 ASSERT_EQ(state, new_state); 303 ASSERT_EQ(state, new_state);
238 304
239 // unload extension to force cache to save data to the state store 305 // unload extension to force cache to save data to the state store
240 UnloadExtension(extension_id); 306 UnloadExtension(extension_id);
241 307
242 // check if geometry got stored correctly in the state store 308 // check if geometry got stored correctly in the state store
243 const base::DictionaryValue* dict = 309 const base::DictionaryValue* dict =
244 prefs_->prefs()->GetGeometryCache(extension_id); 310 extension_prefs_->GetGeometryCache(extension_id);
245 ASSERT_TRUE(dict); 311 ASSERT_TRUE(dict);
246 312
247 ASSERT_TRUE(dict->HasKey(window_id)); 313 ASSERT_TRUE(dict->HasKey(window_id));
248 int v; 314 int v;
249 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v)); 315 ASSERT_TRUE(dict->GetInteger(window_id + ".x", &v));
250 ASSERT_EQ(bounds.x(), v); 316 ASSERT_EQ(bounds.x(), v);
251 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v)); 317 ASSERT_TRUE(dict->GetInteger(window_id + ".y", &v));
252 ASSERT_EQ(bounds.y(), v); 318 ASSERT_EQ(bounds.y(), v);
253 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v)); 319 ASSERT_TRUE(dict->GetInteger(window_id + ".w", &v));
254 ASSERT_EQ(bounds.width(), v); 320 ASSERT_EQ(bounds.width(), v);
(...skipping 19 matching lines...) Expand all
274 ASSERT_EQ(screen_bounds, new_screen_bounds); 340 ASSERT_EQ(screen_bounds, new_screen_bounds);
275 ASSERT_EQ(state, new_state); 341 ASSERT_EQ(state, new_state);
276 } 342 }
277 343
278 // Tests that we won't do writes to the state store for SaveGeometry calls 344 // Tests that we won't do writes to the state store for SaveGeometry calls
279 // which don't change the state we already have. 345 // which don't change the state we already have.
280 TEST_F(AppWindowGeometryCacheTest, NoDuplicateWrites) { 346 TEST_F(AppWindowGeometryCacheTest, NoDuplicateWrites) {
281 using testing::_; 347 using testing::_;
282 using testing::Mock; 348 using testing::Mock;
283 349
284 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 350 const std::string extension_id = AddExtensionWithPrefs("ext1");
285 gfx::Rect bounds1(100, 200, 300, 400); 351 gfx::Rect bounds1(100, 200, 300, 400);
286 gfx::Rect bounds2(200, 400, 600, 800); 352 gfx::Rect bounds2(200, 400, 600, 800);
287 gfx::Rect bounds2_duplicate(200, 400, 600, 800); 353 gfx::Rect bounds2_duplicate(200, 400, 600, 800);
288 354
289 gfx::Rect screen_bounds1(0, 0, 1600, 900); 355 gfx::Rect screen_bounds1(0, 0, 1600, 900);
290 gfx::Rect screen_bounds2(0, 0, 1366, 768); 356 gfx::Rect screen_bounds2(0, 0, 1366, 768);
291 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768); 357 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768);
292 358
293 MockPrefChangeCallback observer(prefs_->pref_service()); 359 MockPrefChangeCallback observer(pref_service_.get());
294 PrefChangeRegistrar registrar; 360 PrefChangeRegistrar registrar;
295 registrar.Init(prefs_->pref_service()); 361 registrar.Init(pref_service_.get());
296 registrar.Add("extensions.settings", observer.GetCallback()); 362 registrar.Add("extensions.settings", observer.GetCallback());
297 363
298 // Write the first bounds - it should do > 0 writes. 364 // Write the first bounds - it should do > 0 writes.
299 EXPECT_CALL(observer, OnPreferenceChanged(_)); 365 EXPECT_CALL(observer, OnPreferenceChanged(_));
300 cache_->SaveGeometry( 366 cache_->SaveGeometry(
301 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL); 367 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL);
302 WaitForSync(); 368 WaitForSync();
303 Mock::VerifyAndClearExpectations(&observer); 369 Mock::VerifyAndClearExpectations(&observer);
304 370
305 // Write a different bounds - it should also do > 0 writes. 371 // Write a different bounds - it should also do > 0 writes.
(...skipping 27 matching lines...) Expand all
333 kWindowId, 399 kWindowId,
334 bounds2_duplicate, 400 bounds2_duplicate,
335 screen_bounds2_duplicate, 401 screen_bounds2_duplicate,
336 ui::SHOW_STATE_MAXIMIZED); 402 ui::SHOW_STATE_MAXIMIZED);
337 WaitForSync(); 403 WaitForSync();
338 Mock::VerifyAndClearExpectations(&observer); 404 Mock::VerifyAndClearExpectations(&observer);
339 } 405 }
340 406
341 // Tests that no more than kMaxCachedWindows windows will be cached. 407 // Tests that no more than kMaxCachedWindows windows will be cached.
342 TEST_F(AppWindowGeometryCacheTest, MaxWindows) { 408 TEST_F(AppWindowGeometryCacheTest, MaxWindows) {
343 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); 409 const std::string extension_id = AddExtensionWithPrefs("ext1");
344 // inform cache of extension 410 // inform cache of extension
345 LoadExtension(extension_id); 411 LoadExtension(extension_id);
346 412
347 gfx::Rect bounds(4, 5, 31, 43); 413 gfx::Rect bounds(4, 5, 31, 43);
348 gfx::Rect screen_bounds(0, 0, 1600, 900); 414 gfx::Rect screen_bounds(0, 0, 1600, 900);
349 for (size_t i = 0; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 415 for (size_t i = 0; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
350 std::string window_id = "window_" + base::IntToString(i); 416 std::string window_id = "window_" + base::IntToString(i);
351 cache_->SaveGeometry( 417 cache_->SaveGeometry(
352 extension_id, window_id, bounds, screen_bounds, ui::SHOW_STATE_NORMAL); 418 extension_id, window_id, bounds, screen_bounds, ui::SHOW_STATE_NORMAL);
353 } 419 }
354 420
355 // The first added window should no longer have cached geometry. 421 // The first added window should no longer have cached geometry.
356 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); 422 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL));
357 // All other windows should still exist. 423 // All other windows should still exist.
358 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { 424 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) {
359 std::string window_id = "window_" + base::IntToString(i); 425 std::string window_id = "window_" + base::IntToString(i);
360 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); 426 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL));
361 } 427 }
362 } 428 }
363 429
364 } // namespace extensions 430 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | extensions/browser/null_app_sorting.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698