OLD | NEW |
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" | 5 #include "extensions/browser/app_window/app_window_geometry_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "components/pref_registry/pref_registry_syncable.h" | |
16 #include "components/prefs/mock_pref_change_callback.h" | 15 #include "components/prefs/mock_pref_change_callback.h" |
17 #include "components/prefs/pref_service_factory.h" | |
18 #include "components/prefs/testing_pref_store.h" | |
19 #include "content/public/test/test_browser_context.h" | 16 #include "content/public/test/test_browser_context.h" |
20 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
21 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
22 #include "extensions/browser/extension_pref_value_map.h" | |
23 #include "extensions/browser/extension_prefs.h" | 19 #include "extensions/browser/extension_prefs.h" |
24 #include "extensions/browser/extensions_test.h" | 20 #include "extensions/browser/extensions_test.h" |
25 #include "extensions/common/extension_builder.h" | 21 #include "extensions/common/extension_builder.h" |
26 #include "extensions/common/value_builder.h" | 22 #include "extensions/common/value_builder.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | |
28 | 23 |
29 using content::BrowserThread; | 24 using content::BrowserThread; |
30 | 25 |
31 namespace extensions { | 26 namespace extensions { |
32 | 27 |
33 namespace { | 28 namespace { |
34 const char kWindowId[] = "windowid"; | 29 const char kWindowId[] = "windowid"; |
35 const char kWindowId2[] = "windowid2"; | 30 const char kWindowId2[] = "windowid2"; |
36 | 31 |
37 // Create a very simple extension with id. | 32 // Create a very simple extension with id. |
(...skipping 30 matching lines...) Expand all Loading... |
68 void LoadExtension(const std::string& extension_id); | 63 void LoadExtension(const std::string& extension_id); |
69 void UnloadExtension(const std::string& extension_id); | 64 void UnloadExtension(const std::string& extension_id); |
70 | 65 |
71 // Creates and adds an extension with associated prefs. Returns the extension | 66 // Creates and adds an extension with associated prefs. Returns the extension |
72 // ID. | 67 // ID. |
73 std::string AddExtensionWithPrefs(const std::string& name); | 68 std::string AddExtensionWithPrefs(const std::string& name); |
74 | 69 |
75 protected: | 70 protected: |
76 base::MessageLoopForUI ui_message_loop_; | 71 base::MessageLoopForUI ui_message_loop_; |
77 content::TestBrowserThread ui_thread_; | 72 content::TestBrowserThread ui_thread_; |
78 std::unique_ptr<ExtensionPrefValueMap> extension_pref_value_map_; | 73 ExtensionPrefs* extension_prefs_; // Weak. |
79 std::unique_ptr<PrefService> pref_service_; | |
80 std::unique_ptr<ExtensionPrefs> extension_prefs_; | |
81 std::unique_ptr<AppWindowGeometryCache> cache_; | 74 std::unique_ptr<AppWindowGeometryCache> cache_; |
82 }; | 75 }; |
83 | 76 |
84 void AppWindowGeometryCacheTest::SetUp() { | 77 void AppWindowGeometryCacheTest::SetUp() { |
85 ExtensionsTest::SetUp(); | 78 ExtensionsTest::SetUp(); |
86 | 79 extension_prefs_ = ExtensionPrefs::Get(browser_context()); |
87 // Set up all the dependencies of ExtensionPrefs. | 80 cache_.reset(new AppWindowGeometryCache(browser_context(), extension_prefs_)); |
88 extension_pref_value_map_.reset(new ExtensionPrefValueMap); | |
89 PrefServiceFactory factory; | |
90 factory.set_user_prefs(new TestingPrefStore); | |
91 factory.set_extension_prefs(new TestingPrefStore); | |
92 user_prefs::PrefRegistrySyncable* pref_registry = | |
93 new user_prefs::PrefRegistrySyncable; | |
94 // Prefs should be registered before the PrefService is created. | |
95 ExtensionPrefs::RegisterProfilePrefs(pref_registry); | |
96 pref_service_ = factory.Create(pref_registry); | |
97 | |
98 extension_prefs_.reset(ExtensionPrefs::Create( | |
99 browser_context(), pref_service_.get(), | |
100 browser_context()->GetPath().AppendASCII("Extensions"), | |
101 extension_pref_value_map_.get(), false /* extensions_disabled */, | |
102 std::vector<ExtensionPrefsObserver*>())); | |
103 | |
104 cache_.reset( | |
105 new AppWindowGeometryCache(browser_context(), extension_prefs_.get())); | |
106 cache_->SetSyncDelayForTests(0); | 81 cache_->SetSyncDelayForTests(0); |
107 } | 82 } |
108 | 83 |
109 void AppWindowGeometryCacheTest::TearDown() { | 84 void AppWindowGeometryCacheTest::TearDown() { |
110 cache_.reset(); | 85 cache_.reset(); |
111 extension_prefs_.reset(); | |
112 pref_service_.reset(); | |
113 extension_pref_value_map_.reset(); | |
114 | |
115 ExtensionsTest::TearDown(); | 86 ExtensionsTest::TearDown(); |
116 } | 87 } |
117 | 88 |
118 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension( | 89 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension( |
119 const std::string& extension_id, | 90 const std::string& extension_id, |
120 const std::string& window_id, | 91 const std::string& window_id, |
121 const gfx::Rect& bounds, | 92 const gfx::Rect& bounds, |
122 const gfx::Rect& screen_bounds, | 93 const gfx::Rect& screen_bounds, |
123 ui::WindowShowState state) { | 94 ui::WindowShowState state) { |
124 std::unique_ptr<base::DictionaryValue> dict = | 95 std::unique_ptr<base::DictionaryValue> dict = |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 326 |
356 const std::string extension_id = AddExtensionWithPrefs("ext1"); | 327 const std::string extension_id = AddExtensionWithPrefs("ext1"); |
357 gfx::Rect bounds1(100, 200, 300, 400); | 328 gfx::Rect bounds1(100, 200, 300, 400); |
358 gfx::Rect bounds2(200, 400, 600, 800); | 329 gfx::Rect bounds2(200, 400, 600, 800); |
359 gfx::Rect bounds2_duplicate(200, 400, 600, 800); | 330 gfx::Rect bounds2_duplicate(200, 400, 600, 800); |
360 | 331 |
361 gfx::Rect screen_bounds1(0, 0, 1600, 900); | 332 gfx::Rect screen_bounds1(0, 0, 1600, 900); |
362 gfx::Rect screen_bounds2(0, 0, 1366, 768); | 333 gfx::Rect screen_bounds2(0, 0, 1366, 768); |
363 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768); | 334 gfx::Rect screen_bounds2_duplicate(0, 0, 1366, 768); |
364 | 335 |
365 MockPrefChangeCallback observer(pref_service_.get()); | 336 MockPrefChangeCallback observer(pref_service()); |
366 PrefChangeRegistrar registrar; | 337 PrefChangeRegistrar registrar; |
367 registrar.Init(pref_service_.get()); | 338 registrar.Init(pref_service()); |
368 registrar.Add("extensions.settings", observer.GetCallback()); | 339 registrar.Add("extensions.settings", observer.GetCallback()); |
369 | 340 |
370 // Write the first bounds - it should do > 0 writes. | 341 // Write the first bounds - it should do > 0 writes. |
371 EXPECT_CALL(observer, OnPreferenceChanged(_)); | 342 EXPECT_CALL(observer, OnPreferenceChanged(_)); |
372 cache_->SaveGeometry( | 343 cache_->SaveGeometry( |
373 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL); | 344 extension_id, kWindowId, bounds1, screen_bounds1, ui::SHOW_STATE_NORMAL); |
374 WaitForSync(); | 345 WaitForSync(); |
375 Mock::VerifyAndClearExpectations(&observer); | 346 Mock::VerifyAndClearExpectations(&observer); |
376 | 347 |
377 // Write a different bounds - it should also do > 0 writes. | 348 // Write a different bounds - it should also do > 0 writes. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // The first added window should no longer have cached geometry. | 398 // The first added window should no longer have cached geometry. |
428 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); | 399 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); |
429 // All other windows should still exist. | 400 // All other windows should still exist. |
430 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { | 401 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { |
431 std::string window_id = "window_" + base::SizeTToString(i); | 402 std::string window_id = "window_" + base::SizeTToString(i); |
432 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); | 403 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); |
433 } | 404 } |
434 } | 405 } |
435 | 406 |
436 } // namespace extensions | 407 } // namespace extensions |
OLD | NEW |