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

Side by Side Diff: chrome/browser/extensions/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: Less duplicated code 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
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function_test_utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/debug/leak_annotations.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"
10 #include "extensions/browser/api/system_display/system_display_api.h"
11 #include "extensions/common/api/system_display.h"
12 #include "ui/gfx/display.h"
13 #include "ui/gfx/display_observer.h"
14 #include "ui/gfx/screen.h"
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 {
24
25 using core_api::system_display::Bounds;
26 using core_api::system_display::DisplayUnitInfo;
27 using gfx::Screen;
28
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 {
62 public:
63 MockScreen() {
64 for (int i = 0; i < 4; i++) {
65 gfx::Rect bounds(0, 0, 1280, 720);
66 gfx::Rect work_area(0, 0, 960, 720);
67 gfx::Display display(i, bounds);
68 display.set_work_area(work_area);
69 displays_.push_back(display);
70 }
71 }
72 ~MockScreen() override {}
73
74 protected:
75 // Overridden from gfx::Screen:
76 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
77 gfx::NativeWindow GetWindowUnderCursor() override {
78 return gfx::NativeWindow();
79 }
80 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
81 return gfx::NativeWindow();
82 }
83 int GetNumDisplays() const override { return displays_.size(); }
84 std::vector<gfx::Display> GetAllDisplays() const override {
85 return displays_;
86 }
87 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override {
88 return gfx::Display(0);
89 }
90 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
91 return gfx::Display(0);
92 }
93 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
94 return gfx::Display(0);
95 }
96 gfx::Display GetPrimaryDisplay() const override { return displays_[0]; }
97 void AddObserver(gfx::DisplayObserver* observer) override {}
98 void RemoveObserver(gfx::DisplayObserver* observer) override {}
99
100 private:
101 std::vector<gfx::Display> displays_;
102
103 DISALLOW_COPY_AND_ASSIGN(MockScreen);
104 };
105 #endif
106
107 class MockDisplayInfoProvider : public DisplayInfoProvider {
108 public:
109 MockDisplayInfoProvider() {}
110
111 ~MockDisplayInfoProvider() override {}
112
113 bool SetInfo(const std::string& display_id,
114 const core_api::system_display::DisplayProperties& params,
115 std::string* error) override {
116 // Should get called only once per test case.
117 EXPECT_FALSE(set_info_value_);
118 set_info_value_ = params.ToValue();
119 set_info_display_id_ = display_id;
120 return true;
121 }
122
123 gfx::Screen* GetActiveScreen() override { return NULL; }
124
125 scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
126 return set_info_value_.Pass();
127 }
128
129 std::string GetSetInfoDisplayId() const {
130 return set_info_display_id_;
131 }
132
133 private:
134 // Update the content of the |unit| obtained for |display| using
135 // platform specific method.
136 void UpdateDisplayUnitInfoForPlatform(
137 const gfx::Display& display,
138 extensions::core_api::system_display::DisplayUnitInfo* unit) override {
139 int64 id = display.id();
140 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
141 if (id == 1)
142 unit->mirroring_source_id = "0";
143 unit->is_primary = id == 0 ? true : false;
144 unit->is_internal = id == 0 ? true : false;
145 unit->is_enabled = true;
146 unit->rotation = (90 * id) % 360;
147 unit->dpi_x = 96.0;
148 unit->dpi_y = 96.0;
149 if (id == 0) {
150 unit->overscan.left = 20;
151 unit->overscan.top = 40;
152 unit->overscan.right = 60;
153 unit->overscan.bottom = 80;
154 }
155 }
156
157 scoped_ptr<base::DictionaryValue> set_info_value_;
158 std::string set_info_display_id_;
159
160 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
161 };
162
163 class SystemDisplayApiTest: public ExtensionApiTest {
164 public:
165 SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider),
166 screen_(new MockScreen) {}
167
168 ~SystemDisplayApiTest() override {}
169
170 void SetUpOnMainThread() override {
171 ExtensionApiTest::SetUpOnMainThread();
172 ANNOTATE_LEAKING_OBJECT_PTR(
173 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
174 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
175 DisplayInfoProvider::InitializeForTesting(provider_.get());
176 }
177
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:
187 scoped_ptr<MockDisplayInfoProvider> provider_;
188 scoped_ptr<gfx::Screen> screen_;
189
190 private:
191 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
192 };
193
194 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
195 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_;
196 }
197
198 #if !defined(OS_CHROMEOS)
199 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
200 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
201 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
202
203 set_info_function->set_has_callback(true);
204
205 EXPECT_EQ("Function available only on ChromeOS.",
206 utils::RunFunctionAndReturnError(set_info_function.get(),
207 "[\"display_id\", {}]",
208 browser()));
209
210 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
211 EXPECT_FALSE(set_info);
212 }
213 #endif // !defined(OS_CHROMEOS)
214
215 #if defined(OS_CHROMEOS)
216 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
217 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
218 "{\n"
219 " \"name\": \"Test\",\n"
220 " \"version\": \"1.0\",\n"
221 " \"app\": {\n"
222 " \"background\": {\n"
223 " \"scripts\": [\"background.js\"]\n"
224 " }\n"
225 " }\n"
226 "}"));
227 scoped_refptr<Extension> test_extension(
228 utils::CreateExtension(test_extension_value.get()));
229
230 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
231 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
232
233 set_info_function->set_extension(test_extension.get());
234 set_info_function->set_has_callback(true);
235
236 EXPECT_EQ("The extension needs to be kiosk enabled to use the function.",
237 utils::RunFunctionAndReturnError(set_info_function.get(),
238 "[\"display_id\", {}]",
239 browser()));
240
241 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
242 EXPECT_FALSE(set_info);
243 }
244
245 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
246 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
247 "{\n"
248 " \"name\": \"Test\",\n"
249 " \"version\": \"1.0\",\n"
250 " \"app\": {\n"
251 " \"background\": {\n"
252 " \"scripts\": [\"background.js\"]\n"
253 " }\n"
254 " },\n"
255 " \"kiosk_enabled\": true\n"
256 "}"));
257 scoped_refptr<Extension> test_extension(
258 utils::CreateExtension(test_extension_value.get()));
259
260 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
261 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
262
263 set_info_function->set_has_callback(true);
264 set_info_function->set_extension(test_extension.get());
265
266 ASSERT_TRUE(utils::RunFunction(
267 set_info_function.get(),
268 "[\"display_id\", {\n"
269 " \"isPrimary\": true,\n"
270 " \"mirroringSourceId\": \"mirroringId\",\n"
271 " \"boundsOriginX\": 100,\n"
272 " \"boundsOriginY\": 200,\n"
273 " \"rotation\": 90,\n"
274 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
275 "}]",
276 browser(),
277 utils::NONE));
278
279 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
280 ASSERT_TRUE(set_info);
281 EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary"));
282 EXPECT_EQ("mirroringId",
283 utils::GetString(set_info.get(), "mirroringSourceId"));
284 EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX"));
285 EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY"));
286 EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation"));
287 base::DictionaryValue* overscan;
288 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
289 EXPECT_EQ(1, utils::GetInteger(overscan, "left"));
290 EXPECT_EQ(2, utils::GetInteger(overscan, "top"));
291 EXPECT_EQ(3, utils::GetInteger(overscan, "right"));
292 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom"));
293
294 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
295 }
296 #endif // defined(OS_CHROMEOS)
297
298 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_function_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698