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

Side by Side Diff: extensions/browser/api/system_display/system_display_apitest.cc

Issue 779083002: Move system.display tests to extensions/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed ash dependency Created 6 years 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/debug/leak_annotations.h" 5 #include "base/debug/leak_annotations.h"
6 #include "base/strings/string_number_conversions.h" 6 #include "base/strings/string_number_conversions.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_function_test_utils.h"
9 #include "extensions/browser/api/system_display/display_info_provider.h" 7 #include "extensions/browser/api/system_display/display_info_provider.h"
10 #include "extensions/browser/api/system_display/system_display_api.h" 8 #include "extensions/browser/api/system_display/system_display_api.h"
9 #include "extensions/browser/api_test_utils.h"
11 #include "extensions/common/api/system_display.h" 10 #include "extensions/common/api/system_display.h"
11 #include "extensions/shell/test/shell_apitest.h"
12 #include "ui/gfx/display.h" 12 #include "ui/gfx/display.h"
13 #include "ui/gfx/display_observer.h" 13 #include "ui/gfx/display_observer.h"
14 #include "ui/gfx/screen.h" 14 #include "ui/gfx/screen.h"
15 15
16 #if defined(OS_CHROMEOS)
17 #include "ash/display/screen_ash.h"
18 #include "ash/shell.h"
19 #endif
20
21 namespace utils = extension_function_test_utils;
22
23 namespace extensions { 16 namespace extensions {
24 17
25 using core_api::system_display::Bounds; 18 using core_api::system_display::Bounds;
26 using core_api::system_display::DisplayUnitInfo; 19 using core_api::system_display::DisplayUnitInfo;
27 using gfx::Screen; 20 using gfx::Screen;
28 21
29 #if defined(OS_CHROMEOS)
30 class MockScreen : public ash::ScreenAsh {
31 public:
32 MockScreen() {
33 for (int i = 0; i < 4; i++) {
34 gfx::Rect bounds(0, 0, 1280, 720);
35 gfx::Rect work_area(0, 0, 960, 720);
36 gfx::Display display(i, bounds);
37 display.set_work_area(work_area);
38 displays_.push_back(display);
39 }
40 }
41 virtual ~MockScreen() {}
42
43 protected:
44 // Overridden from gfx::Screen:
45 virtual int GetNumDisplays() const override {
46 return displays_.size();
47 }
48 virtual std::vector<gfx::Display> GetAllDisplays() const override {
49 return displays_;
50 }
51 virtual gfx::Display GetPrimaryDisplay() const override {
52 return displays_[0];
53 }
54
55 private:
56 std::vector<gfx::Display> displays_;
57
58 DISALLOW_COPY_AND_ASSIGN(MockScreen);
59 };
60 #else
61 class MockScreen : public Screen { 22 class MockScreen : public Screen {
62 public: 23 public:
63 MockScreen() { 24 MockScreen() {
64 for (int i = 0; i < 4; i++) { 25 for (int i = 0; i < 4; i++) {
65 gfx::Rect bounds(0, 0, 1280, 720); 26 gfx::Rect bounds(0, 0, 1280, 720);
66 gfx::Rect work_area(0, 0, 960, 720); 27 gfx::Rect work_area(0, 0, 960, 720);
67 gfx::Display display(i, bounds); 28 gfx::Display display(i, bounds);
68 display.set_work_area(work_area); 29 display.set_work_area(work_area);
69 displays_.push_back(display); 30 displays_.push_back(display);
70 } 31 }
71 } 32 }
72 ~MockScreen() override {} 33 ~MockScreen() override {}
73 34
74 protected: 35 protected:
75 // Overridden from gfx::Screen: 36 // Overridden from gfx::Screen:
76 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); } 37 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
77 gfx::NativeWindow GetWindowUnderCursor() override { 38 gfx::NativeWindow GetWindowUnderCursor() override {
78 return gfx::NativeWindow(); 39 return gfx::NativeWindow();
79 } 40 }
80 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override { 41 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
81 return gfx::NativeWindow(); 42 return gfx::NativeWindow();
82 } 43 }
83 int GetNumDisplays() const override { return displays_.size(); } 44 int GetNumDisplays() const override {
45 return static_cast<int>(displays_.size());
46 }
84 std::vector<gfx::Display> GetAllDisplays() const override { 47 std::vector<gfx::Display> GetAllDisplays() const override {
85 return displays_; 48 return displays_;
86 } 49 }
87 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override { 50 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override {
88 return gfx::Display(0); 51 return gfx::Display(0);
89 } 52 }
90 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override { 53 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
91 return gfx::Display(0); 54 return gfx::Display(0);
92 } 55 }
93 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override { 56 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
94 return gfx::Display(0); 57 return gfx::Display(0);
95 } 58 }
96 gfx::Display GetPrimaryDisplay() const override { return displays_[0]; } 59 gfx::Display GetPrimaryDisplay() const override { return displays_[0]; }
97 void AddObserver(gfx::DisplayObserver* observer) override {} 60 void AddObserver(gfx::DisplayObserver* observer) override {}
98 void RemoveObserver(gfx::DisplayObserver* observer) override {} 61 void RemoveObserver(gfx::DisplayObserver* observer) override {}
99 62
100 private: 63 private:
101 std::vector<gfx::Display> displays_; 64 std::vector<gfx::Display> displays_;
102 65
103 DISALLOW_COPY_AND_ASSIGN(MockScreen); 66 DISALLOW_COPY_AND_ASSIGN(MockScreen);
104 }; 67 };
James Cook 2014/12/19 18:07:03 It's great you were able to get rid of this #ifdef
105 #endif
106 68
107 class MockDisplayInfoProvider : public DisplayInfoProvider { 69 class MockDisplayInfoProvider : public DisplayInfoProvider {
108 public: 70 public:
109 MockDisplayInfoProvider() {} 71 MockDisplayInfoProvider() {}
110 72
111 ~MockDisplayInfoProvider() override {} 73 ~MockDisplayInfoProvider() override {}
112 74
113 bool SetInfo(const std::string& display_id, 75 bool SetInfo(const std::string& display_id,
114 const core_api::system_display::DisplayProperties& params, 76 const core_api::system_display::DisplayProperties& params,
115 std::string* error) override { 77 std::string* error) override {
116 // Should get called only once per test case. 78 // Should get called only once per test case.
117 EXPECT_FALSE(set_info_value_); 79 EXPECT_FALSE(set_info_value_);
118 set_info_value_ = params.ToValue(); 80 set_info_value_ = params.ToValue();
119 set_info_display_id_ = display_id; 81 set_info_display_id_ = display_id;
120 return true; 82 return true;
121 } 83 }
122 84
123 gfx::Screen* GetActiveScreen() override { return NULL; } 85 gfx::Screen* GetActiveScreen() override { return NULL; }
124 86
125 scoped_ptr<base::DictionaryValue> GetSetInfoValue() { 87 scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
126 return set_info_value_.Pass(); 88 return set_info_value_.Pass();
127 } 89 }
128 90
129 std::string GetSetInfoDisplayId() const { 91 std::string GetSetInfoDisplayId() const { return set_info_display_id_; }
130 return set_info_display_id_;
131 }
132 92
133 private: 93 private:
134 // Update the content of the |unit| obtained for |display| using 94 // Update the content of the |unit| obtained for |display| using
135 // platform specific method. 95 // platform specific method.
136 void UpdateDisplayUnitInfoForPlatform( 96 void UpdateDisplayUnitInfoForPlatform(
137 const gfx::Display& display, 97 const gfx::Display& display,
138 extensions::core_api::system_display::DisplayUnitInfo* unit) override { 98 extensions::core_api::system_display::DisplayUnitInfo* unit) override {
139 int64 id = display.id(); 99 int64 id = display.id();
140 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id); 100 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
141 if (id == 1) 101 if (id == 1)
(...skipping 11 matching lines...) Expand all
153 unit->overscan.bottom = 80; 113 unit->overscan.bottom = 80;
154 } 114 }
155 } 115 }
156 116
157 scoped_ptr<base::DictionaryValue> set_info_value_; 117 scoped_ptr<base::DictionaryValue> set_info_value_;
158 std::string set_info_display_id_; 118 std::string set_info_display_id_;
159 119
160 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider); 120 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
161 }; 121 };
162 122
163 class SystemDisplayApiTest: public ExtensionApiTest { 123 class SystemDisplayApiTest : public ShellApiTest {
164 public: 124 public:
165 SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider), 125 SystemDisplayApiTest()
166 screen_(new MockScreen) {} 126 : provider_(new MockDisplayInfoProvider), screen_(new MockScreen) {}
167 127
168 ~SystemDisplayApiTest() override {} 128 ~SystemDisplayApiTest() override {}
169 129
170 void SetUpOnMainThread() override { 130 void SetUpOnMainThread() override {
171 ExtensionApiTest::SetUpOnMainThread(); 131 ShellApiTest::SetUpOnMainThread();
172 ANNOTATE_LEAKING_OBJECT_PTR( 132 ANNOTATE_LEAKING_OBJECT_PTR(
173 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE)); 133 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
174 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); 134 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
175 DisplayInfoProvider::InitializeForTesting(provider_.get()); 135 DisplayInfoProvider::InitializeForTesting(provider_.get());
176 } 136 }
177 137
178 void TearDownOnMainThread() override {
179 #if defined(OS_CHROMEOS)
180 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
181 ash::Shell::GetScreen());
182 #endif
183 ExtensionApiTest::TearDownOnMainThread();
184 }
185
186 protected: 138 protected:
187 scoped_ptr<MockDisplayInfoProvider> provider_; 139 scoped_ptr<MockDisplayInfoProvider> provider_;
188 scoped_ptr<gfx::Screen> screen_; 140 scoped_ptr<gfx::Screen> screen_;
189 141
190 private: 142 private:
191 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest); 143 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
192 }; 144 };
193 145
194 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) { 146 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
195 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_; 147 ASSERT_TRUE(RunAppTest("system/display")) << message_;
196 } 148 }
197 149
198 #if !defined(OS_CHROMEOS) 150 #if !defined(OS_CHROMEOS)
199 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) { 151 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
200 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> 152 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
201 set_info_function(new SystemDisplaySetDisplayPropertiesFunction()); 153 new SystemDisplaySetDisplayPropertiesFunction());
202 154
203 set_info_function->set_has_callback(true); 155 set_info_function->set_has_callback(true);
204 156
205 EXPECT_EQ("Function available only on ChromeOS.", 157 EXPECT_EQ(
206 utils::RunFunctionAndReturnError(set_info_function.get(), 158 "Function available only on ChromeOS.",
207 "[\"display_id\", {}]", 159 api_test_utils::RunFunctionAndReturnError(
208 browser())); 160 set_info_function.get(), "[\"display_id\", {}]", browser_context()));
209 161
210 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); 162 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
211 EXPECT_FALSE(set_info); 163 EXPECT_FALSE(set_info);
212 } 164 }
213 #endif // !defined(OS_CHROMEOS) 165 #endif // !defined(OS_CHROMEOS)
214 166
215 #if defined(OS_CHROMEOS) 167 #if defined(OS_CHROMEOS)
216 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) { 168 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
217 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary( 169 scoped_ptr<base::DictionaryValue> test_extension_value(
218 "{\n" 170 api_test_utils::ParseDictionary(
219 " \"name\": \"Test\",\n" 171 "{\n"
220 " \"version\": \"1.0\",\n" 172 " \"name\": \"Test\",\n"
221 " \"app\": {\n" 173 " \"version\": \"1.0\",\n"
222 " \"background\": {\n" 174 " \"app\": {\n"
223 " \"scripts\": [\"background.js\"]\n" 175 " \"background\": {\n"
224 " }\n" 176 " \"scripts\": [\"background.js\"]\n"
225 " }\n" 177 " }\n"
226 "}")); 178 " }\n"
179 "}"));
227 scoped_refptr<Extension> test_extension( 180 scoped_refptr<Extension> test_extension(
228 utils::CreateExtension(test_extension_value.get())); 181 api_test_utils::CreateExtension(test_extension_value.get()));
229 182
230 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> 183 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
231 set_info_function(new SystemDisplaySetDisplayPropertiesFunction()); 184 new SystemDisplaySetDisplayPropertiesFunction());
232 185
233 set_info_function->set_extension(test_extension.get()); 186 set_info_function->set_extension(test_extension.get());
234 set_info_function->set_has_callback(true); 187 set_info_function->set_has_callback(true);
235 188
236 EXPECT_EQ("The extension needs to be kiosk enabled to use the function.", 189 EXPECT_EQ(
237 utils::RunFunctionAndReturnError(set_info_function.get(), 190 "The extension needs to be kiosk enabled to use the function.",
238 "[\"display_id\", {}]", 191 api_test_utils::RunFunctionAndReturnError(
239 browser())); 192 set_info_function.get(), "[\"display_id\", {}]", browser_context()));
240 193
241 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); 194 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
242 EXPECT_FALSE(set_info); 195 EXPECT_FALSE(set_info);
243 } 196 }
244 197
245 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) { 198 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
246 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary( 199 scoped_ptr<base::DictionaryValue> test_extension_value(
247 "{\n" 200 api_test_utils::ParseDictionary(
248 " \"name\": \"Test\",\n" 201 "{\n"
249 " \"version\": \"1.0\",\n" 202 " \"name\": \"Test\",\n"
250 " \"app\": {\n" 203 " \"version\": \"1.0\",\n"
251 " \"background\": {\n" 204 " \"app\": {\n"
252 " \"scripts\": [\"background.js\"]\n" 205 " \"background\": {\n"
253 " }\n" 206 " \"scripts\": [\"background.js\"]\n"
254 " },\n" 207 " }\n"
255 " \"kiosk_enabled\": true\n" 208 " },\n"
256 "}")); 209 " \"kiosk_enabled\": true\n"
210 "}"));
257 scoped_refptr<Extension> test_extension( 211 scoped_refptr<Extension> test_extension(
258 utils::CreateExtension(test_extension_value.get())); 212 api_test_utils::CreateExtension(test_extension_value.get()));
259 213
260 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> 214 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
261 set_info_function(new SystemDisplaySetDisplayPropertiesFunction()); 215 new SystemDisplaySetDisplayPropertiesFunction());
262 216
263 set_info_function->set_has_callback(true); 217 set_info_function->set_has_callback(true);
264 set_info_function->set_extension(test_extension.get()); 218 set_info_function->set_extension(test_extension.get());
265 219
266 ASSERT_TRUE(utils::RunFunction( 220 ASSERT_TRUE(api_test_utils::RunFunction(
267 set_info_function.get(), 221 set_info_function.get(),
268 "[\"display_id\", {\n" 222 "[\"display_id\", {\n"
269 " \"isPrimary\": true,\n" 223 " \"isPrimary\": true,\n"
270 " \"mirroringSourceId\": \"mirroringId\",\n" 224 " \"mirroringSourceId\": \"mirroringId\",\n"
271 " \"boundsOriginX\": 100,\n" 225 " \"boundsOriginX\": 100,\n"
272 " \"boundsOriginY\": 200,\n" 226 " \"boundsOriginY\": 200,\n"
273 " \"rotation\": 90,\n" 227 " \"rotation\": 90,\n"
274 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n" 228 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
275 "}]", 229 "}]",
276 browser(), 230 browser_context()));
277 utils::NONE));
278 231
279 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); 232 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
280 ASSERT_TRUE(set_info); 233 ASSERT_TRUE(set_info);
281 EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary")); 234 EXPECT_TRUE(api_test_utils::GetBoolean(set_info.get(), "isPrimary"));
282 EXPECT_EQ("mirroringId", 235 EXPECT_EQ("mirroringId",
283 utils::GetString(set_info.get(), "mirroringSourceId")); 236 api_test_utils::GetString(set_info.get(), "mirroringSourceId"));
284 EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX")); 237 EXPECT_EQ(100, api_test_utils::GetInteger(set_info.get(), "boundsOriginX"));
285 EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY")); 238 EXPECT_EQ(200, api_test_utils::GetInteger(set_info.get(), "boundsOriginY"));
286 EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation")); 239 EXPECT_EQ(90, api_test_utils::GetInteger(set_info.get(), "rotation"));
287 base::DictionaryValue* overscan; 240 base::DictionaryValue* overscan;
288 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan)); 241 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
289 EXPECT_EQ(1, utils::GetInteger(overscan, "left")); 242 EXPECT_EQ(1, api_test_utils::GetInteger(overscan, "left"));
290 EXPECT_EQ(2, utils::GetInteger(overscan, "top")); 243 EXPECT_EQ(2, api_test_utils::GetInteger(overscan, "top"));
291 EXPECT_EQ(3, utils::GetInteger(overscan, "right")); 244 EXPECT_EQ(3, api_test_utils::GetInteger(overscan, "right"));
292 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom")); 245 EXPECT_EQ(4, api_test_utils::GetInteger(overscan, "bottom"));
293 246
294 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId()); 247 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
295 } 248 }
296 #endif // defined(OS_CHROMEOS) 249 #endif // defined(OS_CHROMEOS)
297 250
298 } // namespace extensions 251 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698