Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / api / system_display / system_display_apitest.cc
blob6d0c173334d0de6ccbe4162616090aa1a852cc47
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.
5 #include "base/debug/leak_annotations.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "extensions/browser/api/system_display/display_info_provider.h"
8 #include "extensions/browser/api/system_display/system_display_api.h"
9 #include "extensions/browser/api_test_utils.h"
10 #include "extensions/common/api/system_display.h"
11 #include "extensions/shell/test/shell_apitest.h"
12 #include "ui/gfx/display.h"
13 #include "ui/gfx/display_observer.h"
14 #include "ui/gfx/screen.h"
16 namespace extensions {
18 using api::system_display::Bounds;
19 using api::system_display::DisplayUnitInfo;
20 using gfx::Screen;
22 class MockScreen : public Screen {
23 public:
24 MockScreen() {
25 for (int i = 0; i < 4; i++) {
26 gfx::Rect bounds(0, 0, 1280, 720);
27 gfx::Rect work_area(0, 0, 960, 720);
28 gfx::Display display(i, bounds);
29 display.set_work_area(work_area);
30 displays_.push_back(display);
33 ~MockScreen() override {}
35 protected:
36 // Overridden from gfx::Screen:
37 gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
38 gfx::NativeWindow GetWindowUnderCursor() override {
39 return gfx::NativeWindow();
41 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
42 return gfx::NativeWindow();
44 int GetNumDisplays() const override {
45 return static_cast<int>(displays_.size());
47 std::vector<gfx::Display> GetAllDisplays() const override {
48 return displays_;
50 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override {
51 return gfx::Display(0);
53 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
54 return gfx::Display(0);
56 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
57 return gfx::Display(0);
59 gfx::Display GetPrimaryDisplay() const override { return displays_[0]; }
60 void AddObserver(gfx::DisplayObserver* observer) override {}
61 void RemoveObserver(gfx::DisplayObserver* observer) override {}
63 private:
64 std::vector<gfx::Display> displays_;
66 DISALLOW_COPY_AND_ASSIGN(MockScreen);
69 class MockDisplayInfoProvider : public DisplayInfoProvider {
70 public:
71 MockDisplayInfoProvider() {}
73 ~MockDisplayInfoProvider() override {}
75 bool SetInfo(const std::string& display_id,
76 const api::system_display::DisplayProperties& params,
77 std::string* error) override {
78 // Should get called only once per test case.
79 EXPECT_FALSE(set_info_value_);
80 set_info_value_ = params.ToValue();
81 set_info_display_id_ = display_id;
82 return true;
85 gfx::Screen* GetActiveScreen() override { return NULL; }
87 void EnableUnifiedDesktop(bool enable) override {
88 unified_desktop_enabled_ = enable;
91 scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
92 return set_info_value_.Pass();
95 std::string GetSetInfoDisplayId() const { return set_info_display_id_; }
97 bool unified_desktop_enabled() const { return unified_desktop_enabled_; }
99 private:
100 // Update the content of the |unit| obtained for |display| using
101 // platform specific method.
102 void UpdateDisplayUnitInfoForPlatform(
103 const gfx::Display& display,
104 extensions::api::system_display::DisplayUnitInfo* unit) override {
105 int64 id = display.id();
106 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
107 if (id == 1)
108 unit->mirroring_source_id = "0";
109 unit->is_primary = id == 0 ? true : false;
110 unit->is_internal = id == 0 ? true : false;
111 unit->is_enabled = true;
112 unit->rotation = (90 * id) % 360;
113 unit->dpi_x = 96.0;
114 unit->dpi_y = 96.0;
115 if (id == 0) {
116 unit->overscan.left = 20;
117 unit->overscan.top = 40;
118 unit->overscan.right = 60;
119 unit->overscan.bottom = 80;
123 scoped_ptr<base::DictionaryValue> set_info_value_;
124 std::string set_info_display_id_;
125 bool unified_desktop_enabled_ = false;
127 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
130 class SystemDisplayApiTest : public ShellApiTest {
131 public:
132 SystemDisplayApiTest()
133 : provider_(new MockDisplayInfoProvider), screen_(new MockScreen) {}
135 ~SystemDisplayApiTest() override {}
137 void SetUpOnMainThread() override {
138 ShellApiTest::SetUpOnMainThread();
139 ANNOTATE_LEAKING_OBJECT_PTR(
140 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
141 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
142 DisplayInfoProvider::InitializeForTesting(provider_.get());
145 protected:
146 scoped_ptr<MockDisplayInfoProvider> provider_;
147 scoped_ptr<gfx::Screen> screen_;
149 private:
150 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
153 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
154 ASSERT_TRUE(RunAppTest("system/display")) << message_;
157 #if !defined(OS_CHROMEOS)
158 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
159 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
160 new SystemDisplaySetDisplayPropertiesFunction());
162 set_info_function->set_has_callback(true);
164 EXPECT_EQ(
165 "Function available only on ChromeOS.",
166 api_test_utils::RunFunctionAndReturnError(
167 set_info_function.get(), "[\"display_id\", {}]", browser_context()));
169 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
170 EXPECT_FALSE(set_info);
172 #endif // !defined(OS_CHROMEOS)
174 #if defined(OS_CHROMEOS)
175 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
176 scoped_ptr<base::DictionaryValue> test_extension_value(
177 api_test_utils::ParseDictionary(
178 "{\n"
179 " \"name\": \"Test\",\n"
180 " \"version\": \"1.0\",\n"
181 " \"app\": {\n"
182 " \"background\": {\n"
183 " \"scripts\": [\"background.js\"]\n"
184 " }\n"
185 " }\n"
186 "}"));
187 scoped_refptr<Extension> test_extension(
188 api_test_utils::CreateExtension(test_extension_value.get()));
190 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
191 new SystemDisplaySetDisplayPropertiesFunction());
193 set_info_function->set_extension(test_extension.get());
194 set_info_function->set_has_callback(true);
196 EXPECT_EQ(
197 "The extension needs to be kiosk enabled to use the function.",
198 api_test_utils::RunFunctionAndReturnError(
199 set_info_function.get(), "[\"display_id\", {}]", browser_context()));
201 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
202 EXPECT_FALSE(set_info);
205 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
206 scoped_ptr<base::DictionaryValue> test_extension_value(
207 api_test_utils::ParseDictionary(
208 "{\n"
209 " \"name\": \"Test\",\n"
210 " \"version\": \"1.0\",\n"
211 " \"app\": {\n"
212 " \"background\": {\n"
213 " \"scripts\": [\"background.js\"]\n"
214 " }\n"
215 " },\n"
216 " \"kiosk_enabled\": true\n"
217 "}"));
218 scoped_refptr<Extension> test_extension(
219 api_test_utils::CreateExtension(test_extension_value.get()));
221 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
222 new SystemDisplaySetDisplayPropertiesFunction());
224 set_info_function->set_has_callback(true);
225 set_info_function->set_extension(test_extension.get());
227 ASSERT_TRUE(api_test_utils::RunFunction(
228 set_info_function.get(),
229 "[\"display_id\", {\n"
230 " \"isPrimary\": true,\n"
231 " \"mirroringSourceId\": \"mirroringId\",\n"
232 " \"boundsOriginX\": 100,\n"
233 " \"boundsOriginY\": 200,\n"
234 " \"rotation\": 90,\n"
235 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
236 "}]",
237 browser_context()));
239 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
240 ASSERT_TRUE(set_info);
241 EXPECT_TRUE(api_test_utils::GetBoolean(set_info.get(), "isPrimary"));
242 EXPECT_EQ("mirroringId",
243 api_test_utils::GetString(set_info.get(), "mirroringSourceId"));
244 EXPECT_EQ(100, api_test_utils::GetInteger(set_info.get(), "boundsOriginX"));
245 EXPECT_EQ(200, api_test_utils::GetInteger(set_info.get(), "boundsOriginY"));
246 EXPECT_EQ(90, api_test_utils::GetInteger(set_info.get(), "rotation"));
247 base::DictionaryValue* overscan;
248 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
249 EXPECT_EQ(1, api_test_utils::GetInteger(overscan, "left"));
250 EXPECT_EQ(2, api_test_utils::GetInteger(overscan, "top"));
251 EXPECT_EQ(3, api_test_utils::GetInteger(overscan, "right"));
252 EXPECT_EQ(4, api_test_utils::GetInteger(overscan, "bottom"));
254 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
257 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, EnableUnifiedDesktop) {
258 scoped_ptr<base::DictionaryValue> test_extension_value(
259 api_test_utils::ParseDictionary("{\n"
260 " \"name\": \"Test\",\n"
261 " \"version\": \"1.0\",\n"
262 " \"app\": {\n"
263 " \"background\": {\n"
264 " \"scripts\": [\"background.js\"]\n"
265 " }\n"
266 " }\n"
267 "}"));
268 scoped_refptr<Extension> test_extension(
269 api_test_utils::CreateExtension(test_extension_value.get()));
271 scoped_refptr<SystemDisplayEnableUnifiedDesktopFunction>
272 enable_unified_function(
273 new SystemDisplayEnableUnifiedDesktopFunction());
275 enable_unified_function->set_has_callback(true);
276 enable_unified_function->set_extension(test_extension.get());
278 EXPECT_FALSE(provider_->unified_desktop_enabled());
280 ASSERT_TRUE(api_test_utils::RunFunction(enable_unified_function.get(),
281 "[true]", browser_context()));
282 EXPECT_TRUE(provider_->unified_desktop_enabled());
285 scoped_refptr<SystemDisplayEnableUnifiedDesktopFunction>
286 enable_unified_function(
287 new SystemDisplayEnableUnifiedDesktopFunction());
289 enable_unified_function->set_has_callback(true);
290 enable_unified_function->set_extension(test_extension.get());
291 ASSERT_TRUE(api_test_utils::RunFunction(enable_unified_function.get(),
292 "[false]", browser_context()));
293 EXPECT_FALSE(provider_->unified_desktop_enabled());
297 #endif // defined(OS_CHROMEOS)
299 } // namespace extensions