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 "apps/app_window_geometry_cache.h" | |
6 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
7 #include "base/prefs/mock_pref_change_callback.h" | 6 #include "base/prefs/mock_pref_change_callback.h" |
8 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
9 #include "chrome/browser/extensions/test_extension_prefs.h" | 8 #include "chrome/browser/extensions/test_extension_prefs.h" |
10 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
11 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/test/test_browser_thread.h" |
12 #include "content/public/test/test_utils.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" | 13 #include "extensions/browser/extension_prefs.h" |
14 #include "extensions/common/extension_builder.h" | 14 #include "extensions/common/extension_builder.h" |
15 #include "extensions/common/value_builder.h" | 15 #include "extensions/common/value_builder.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using content::BrowserThread; | 18 using content::BrowserThread; |
19 | 19 |
20 namespace apps { | 20 namespace extensions { |
21 | 21 |
22 namespace { | 22 namespace { |
23 const char kWindowId[] = "windowid"; | 23 const char kWindowId[] = "windowid"; |
24 const char kWindowId2[] = "windowid2"; | 24 const char kWindowId2[] = "windowid2"; |
25 | 25 |
26 // Create a very simple extension with id. | 26 // Create a very simple extension with id. |
27 scoped_refptr<extensions::Extension> CreateExtension(const std::string& id) { | 27 scoped_refptr<Extension> CreateExtension(const std::string& id) { |
28 return extensions::ExtensionBuilder() | 28 return ExtensionBuilder() |
29 .SetManifest(extensions::DictionaryBuilder().Set("name", "test").Set( | 29 .SetManifest(DictionaryBuilder().Set("name", "test").Set( |
30 "version", "0.1")) | 30 "version", "0.1")) |
31 .SetID(id) | 31 .SetID(id) |
32 .Build(); | 32 .Build(); |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 // Base class for tests. | 37 // Base class for tests. |
38 class AppWindowGeometryCacheTest : public testing::Test { | 38 class AppWindowGeometryCacheTest : public testing::Test { |
39 public: | 39 public: |
40 AppWindowGeometryCacheTest() | 40 AppWindowGeometryCacheTest() |
41 : profile_(new TestingProfile), | 41 : profile_(new TestingProfile), |
42 ui_thread_(BrowserThread::UI, &ui_message_loop_) { | 42 ui_thread_(BrowserThread::UI, &ui_message_loop_) { |
43 prefs_.reset(new extensions::TestExtensionPrefs( | 43 prefs_.reset(new TestExtensionPrefs( |
44 ui_message_loop_.message_loop_proxy().get())); | 44 ui_message_loop_.message_loop_proxy().get())); |
45 cache_.reset(new AppWindowGeometryCache(profile_.get(), prefs_->prefs())); | 45 cache_.reset(new AppWindowGeometryCache(profile_.get(), prefs_->prefs())); |
46 cache_->SetSyncDelayForTests(0); | 46 cache_->SetSyncDelayForTests(0); |
47 } | 47 } |
48 | 48 |
49 void AddGeometryAndLoadExtension(const std::string& extension_id, | 49 void AddGeometryAndLoadExtension(const std::string& extension_id, |
50 const std::string& window_id, | 50 const std::string& window_id, |
51 const gfx::Rect& bounds, | 51 const gfx::Rect& bounds, |
52 const gfx::Rect& screen_bounds, | 52 const gfx::Rect& screen_bounds, |
53 ui::WindowShowState state); | 53 ui::WindowShowState state); |
54 | 54 |
55 // Spins the UI threads' message loops to make sure any task | 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. | 56 // posted to sync the geometry to the value store gets a chance to run. |
57 void WaitForSync(); | 57 void WaitForSync(); |
58 | 58 |
59 void LoadExtension(const std::string& extension_id); | 59 void LoadExtension(const std::string& extension_id); |
60 void UnloadExtension(const std::string& extension_id); | 60 void UnloadExtension(const std::string& extension_id); |
61 | 61 |
62 protected: | 62 protected: |
63 scoped_ptr<TestingProfile> profile_; | 63 scoped_ptr<TestingProfile> profile_; |
64 base::MessageLoopForUI ui_message_loop_; | 64 base::MessageLoopForUI ui_message_loop_; |
65 content::TestBrowserThread ui_thread_; | 65 content::TestBrowserThread ui_thread_; |
66 scoped_ptr<extensions::TestExtensionPrefs> prefs_; | 66 scoped_ptr<TestExtensionPrefs> prefs_; |
67 scoped_ptr<AppWindowGeometryCache> cache_; | 67 scoped_ptr<AppWindowGeometryCache> cache_; |
68 }; | 68 }; |
69 | 69 |
70 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension( | 70 void AppWindowGeometryCacheTest::AddGeometryAndLoadExtension( |
71 const std::string& extension_id, | 71 const std::string& extension_id, |
72 const std::string& window_id, | 72 const std::string& window_id, |
73 const gfx::Rect& bounds, | 73 const gfx::Rect& bounds, |
74 const gfx::Rect& screen_bounds, | 74 const gfx::Rect& screen_bounds, |
75 ui::WindowShowState state) { | 75 ui::WindowShowState state) { |
76 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 76 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
(...skipping 17 matching lines...) Expand all Loading... |
94 } | 94 } |
95 | 95 |
96 void AppWindowGeometryCacheTest::LoadExtension( | 96 void AppWindowGeometryCacheTest::LoadExtension( |
97 const std::string& extension_id) { | 97 const std::string& extension_id) { |
98 cache_->LoadGeometryFromStorage(extension_id); | 98 cache_->LoadGeometryFromStorage(extension_id); |
99 WaitForSync(); | 99 WaitForSync(); |
100 } | 100 } |
101 | 101 |
102 void AppWindowGeometryCacheTest::UnloadExtension( | 102 void AppWindowGeometryCacheTest::UnloadExtension( |
103 const std::string& extension_id) { | 103 const std::string& extension_id) { |
104 scoped_refptr<extensions::Extension> extension = | 104 scoped_refptr<Extension> extension = CreateExtension(extension_id); |
105 CreateExtension(extension_id); | |
106 cache_->OnExtensionUnloaded( | 105 cache_->OnExtensionUnloaded( |
107 profile_.get(), | 106 profile_.get(), |
108 extension.get(), | 107 extension.get(), |
109 extensions::UnloadedExtensionInfo::REASON_DISABLE); | 108 UnloadedExtensionInfo::REASON_DISABLE); |
110 WaitForSync(); | 109 WaitForSync(); |
111 } | 110 } |
112 | 111 |
113 // Test getting geometry from an empty store. | 112 // Test getting geometry from an empty store. |
114 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) { | 113 TEST_F(AppWindowGeometryCacheTest, GetGeometryEmptyStore) { |
115 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); | 114 const std::string extension_id = prefs_->AddExtensionAndReturnId("ext1"); |
116 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL)); | 115 ASSERT_FALSE(cache_->GetGeometry(extension_id, kWindowId, NULL, NULL, NULL)); |
117 } | 116 } |
118 | 117 |
119 // Test getting geometry for an unknown extension. | 118 // Test getting geometry for an unknown extension. |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 | 354 |
356 // The first added window should no longer have cached geometry. | 355 // The first added window should no longer have cached geometry. |
357 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); | 356 EXPECT_FALSE(cache_->GetGeometry(extension_id, "window_0", NULL, NULL, NULL)); |
358 // All other windows should still exist. | 357 // All other windows should still exist. |
359 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { | 358 for (size_t i = 1; i < AppWindowGeometryCache::kMaxCachedWindows + 1; ++i) { |
360 std::string window_id = "window_" + base::IntToString(i); | 359 std::string window_id = "window_" + base::IntToString(i); |
361 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); | 360 EXPECT_TRUE(cache_->GetGeometry(extension_id, window_id, NULL, NULL, NULL)); |
362 } | 361 } |
363 } | 362 } |
364 | 363 |
365 } // namespace apps | 364 } // namespace extensions |
OLD | NEW |