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 "chrome/browser/extensions/api/system_display/system_display_api.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/api/system_display/display_info_provider.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_function_test_utils.h"
11 #include "ui/gfx/display.h"
12 #include "ui/gfx/display_observer.h"
13 #include "ui/gfx/screen.h"
15 #if defined(OS_CHROMEOS)
16 #include "ash/screen_ash.h"
17 #include "ash/shell.h"
20 namespace utils = extension_function_test_utils;
22 namespace extensions {
24 using api::system_display::Bounds;
25 using api::system_display::DisplayUnitInfo;
28 #if defined(OS_CHROMEOS)
29 class MockScreen : public ash::ScreenAsh {
32 for (int i = 0; i < 4; i++) {
33 gfx::Rect bounds(0, 0, 1280, 720);
34 gfx::Rect work_area(0, 0, 960, 720);
35 gfx::Display display(i, bounds);
36 display.set_work_area(work_area);
37 displays_.push_back(display);
40 virtual ~MockScreen() {}
43 // Overridden from gfx::Screen:
44 virtual int GetNumDisplays() const OVERRIDE {
45 return displays_.size();
47 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
50 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
54 std::vector<gfx::Display> displays_;
56 DISALLOW_COPY_AND_ASSIGN(MockScreen);
59 class MockScreen : public Screen {
62 for (int i = 0; i < 4; i++) {
63 gfx::Rect bounds(0, 0, 1280, 720);
64 gfx::Rect work_area(0, 0, 960, 720);
65 gfx::Display display(i, bounds);
66 display.set_work_area(work_area);
67 displays_.push_back(display);
70 virtual ~MockScreen() {}
73 // Overridden from gfx::Screen:
74 virtual bool IsDIPEnabled() OVERRIDE { return true; }
75 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { return gfx::Point(); }
76 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
77 return gfx::NativeWindow();
79 virtual gfx::NativeWindow GetWindowAtScreenPoint(
80 const gfx::Point& point) OVERRIDE {
81 return gfx::NativeWindow();
83 virtual int GetNumDisplays() const OVERRIDE {
84 return displays_.size();
86 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
89 virtual gfx::Display GetDisplayNearestWindow(
90 gfx::NativeView window) const OVERRIDE {
91 return gfx::Display(0);
93 virtual gfx::Display GetDisplayNearestPoint(
94 const gfx::Point& point) const OVERRIDE {
95 return gfx::Display(0);
97 virtual gfx::Display GetDisplayMatching(
98 const gfx::Rect& match_rect) const OVERRIDE {
99 return gfx::Display(0);
101 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
104 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {}
105 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {}
108 std::vector<gfx::Display> displays_;
110 DISALLOW_COPY_AND_ASSIGN(MockScreen);
114 class MockDisplayInfoProvider : public DisplayInfoProvider {
116 MockDisplayInfoProvider() {}
118 virtual ~MockDisplayInfoProvider() {}
120 virtual bool SetInfo(
121 const std::string& display_id,
122 const api::system_display::DisplayProperties& params,
123 std::string* error) OVERRIDE {
124 // Should get called only once per test case.
125 EXPECT_FALSE(set_info_value_);
126 set_info_value_ = params.ToValue();
127 set_info_display_id_ = display_id;
131 scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
132 return set_info_value_.Pass();
135 std::string GetSetInfoDisplayId() const {
136 return set_info_display_id_;
140 // Update the content of the |unit| obtained for |display| using
141 // platform specific method.
142 virtual void UpdateDisplayUnitInfoForPlatform(
143 const gfx::Display& display,
144 extensions::api::system_display::DisplayUnitInfo* unit) OVERRIDE {
145 int64 id = display.id();
146 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
148 unit->mirroring_source_id = "0";
149 unit->is_primary = id == 0 ? true : false;
150 unit->is_internal = id == 0 ? true : false;
151 unit->is_enabled = true;
152 unit->rotation = (90 * id) % 360;
156 unit->overscan.left = 20;
157 unit->overscan.top = 40;
158 unit->overscan.right = 60;
159 unit->overscan.bottom = 80;
163 scoped_ptr<base::DictionaryValue> set_info_value_;
164 std::string set_info_display_id_;
166 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
169 class SystemDisplayApiTest: public ExtensionApiTest {
171 SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider),
172 screen_(new MockScreen) {}
174 virtual ~SystemDisplayApiTest() {}
176 virtual void SetUpOnMainThread() OVERRIDE {
177 ExtensionApiTest::SetUpOnMainThread();
178 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
179 DisplayInfoProvider::InitializeForTesting(provider_.get());
182 virtual void CleanUpOnMainThread() OVERRIDE {
183 #if defined(OS_CHROMEOS)
184 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
185 ash::Shell::GetInstance()->screen());
187 ExtensionApiTest::CleanUpOnMainThread();
191 scoped_ptr<MockDisplayInfoProvider> provider_;
192 scoped_ptr<gfx::Screen> screen_;
194 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
197 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
198 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_;
201 #if !defined(OS_CHROMEOS)
202 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
203 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
204 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
206 set_info_function->set_has_callback(true);
208 EXPECT_EQ("Function available only on ChromeOS.",
209 utils::RunFunctionAndReturnError(set_info_function.get(),
210 "[\"display_id\", {}]",
213 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
214 EXPECT_FALSE(set_info);
216 #endif // !defined(OS_CHROMEOS)
218 #if defined(OS_CHROMEOS)
219 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
220 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
222 " \"name\": \"Test\",\n"
223 " \"version\": \"1.0\",\n"
225 " \"background\": {\n"
226 " \"scripts\": [\"background.js\"]\n"
230 scoped_refptr<Extension> test_extension(
231 utils::CreateExtension(test_extension_value.get()));
233 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
234 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
236 set_info_function->set_extension(test_extension.get());
237 set_info_function->set_has_callback(true);
239 EXPECT_EQ("The extension needs to be kiosk enabled to use the function.",
240 utils::RunFunctionAndReturnError(set_info_function.get(),
241 "[\"display_id\", {}]",
244 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
245 EXPECT_FALSE(set_info);
248 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
249 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
251 " \"name\": \"Test\",\n"
252 " \"version\": \"1.0\",\n"
254 " \"background\": {\n"
255 " \"scripts\": [\"background.js\"]\n"
258 " \"kiosk_enabled\": true\n"
260 scoped_refptr<Extension> test_extension(
261 utils::CreateExtension(test_extension_value.get()));
263 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
264 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
266 set_info_function->set_has_callback(true);
267 set_info_function->set_extension(test_extension.get());
269 ASSERT_TRUE(utils::RunFunction(
270 set_info_function.get(),
271 "[\"display_id\", {\n"
272 " \"isPrimary\": true,\n"
273 " \"mirroringSourceId\": \"mirroringId\",\n"
274 " \"boundsOriginX\": 100,\n"
275 " \"boundsOriginY\": 200,\n"
276 " \"rotation\": 90,\n"
277 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
282 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
283 ASSERT_TRUE(set_info);
284 EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary"));
285 EXPECT_EQ("mirroringId",
286 utils::GetString(set_info.get(), "mirroringSourceId"));
287 EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX"));
288 EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY"));
289 EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation"));
290 base::DictionaryValue* overscan;
291 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
292 EXPECT_EQ(1, utils::GetInteger(overscan, "left"));
293 EXPECT_EQ(2, utils::GetInteger(overscan, "top"));
294 EXPECT_EQ(3, utils::GetInteger(overscan, "right"));
295 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom"));
297 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
299 #endif // defined(OS_CHROMEOS)
301 } // namespace extensions