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/debug/leak_annotations.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/extensions/api/system_display/display_info_provider.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "ui/gfx/display.h"
13 #include "ui/gfx/display_observer.h"
14 #include "ui/gfx/screen.h"
16 #if defined(OS_CHROMEOS)
17 #include "ash/display/screen_ash.h"
18 #include "ash/shell.h"
21 namespace utils = extension_function_test_utils;
23 namespace extensions {
25 using api::system_display::Bounds;
26 using api::system_display::DisplayUnitInfo;
29 #if defined(OS_CHROMEOS)
30 class MockScreen : public ash::ScreenAsh {
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);
41 virtual ~MockScreen() {}
44 // Overridden from gfx::Screen:
45 virtual int GetNumDisplays() const OVERRIDE {
46 return displays_.size();
48 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
51 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
55 std::vector<gfx::Display> displays_;
57 DISALLOW_COPY_AND_ASSIGN(MockScreen);
60 class MockScreen : public Screen {
63 for (int i = 0; i < 4; i++) {
64 gfx::Rect bounds(0, 0, 1280, 720);
65 gfx::Rect work_area(0, 0, 960, 720);
66 gfx::Display display(i, bounds);
67 display.set_work_area(work_area);
68 displays_.push_back(display);
71 virtual ~MockScreen() {}
74 // Overridden from gfx::Screen:
75 virtual bool IsDIPEnabled() OVERRIDE { return true; }
76 virtual gfx::Point GetCursorScreenPoint() OVERRIDE { return gfx::Point(); }
77 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
78 return gfx::NativeWindow();
80 virtual gfx::NativeWindow GetWindowAtScreenPoint(
81 const gfx::Point& point) OVERRIDE {
82 return gfx::NativeWindow();
84 virtual int GetNumDisplays() const OVERRIDE {
85 return displays_.size();
87 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
90 virtual gfx::Display GetDisplayNearestWindow(
91 gfx::NativeView window) const OVERRIDE {
92 return gfx::Display(0);
94 virtual gfx::Display GetDisplayNearestPoint(
95 const gfx::Point& point) const OVERRIDE {
96 return gfx::Display(0);
98 virtual gfx::Display GetDisplayMatching(
99 const gfx::Rect& match_rect) const OVERRIDE {
100 return gfx::Display(0);
102 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
105 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {}
106 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {}
109 std::vector<gfx::Display> displays_;
111 DISALLOW_COPY_AND_ASSIGN(MockScreen);
115 class MockDisplayInfoProvider : public DisplayInfoProvider {
117 MockDisplayInfoProvider() {}
119 virtual ~MockDisplayInfoProvider() {}
121 virtual bool SetInfo(
122 const std::string& display_id,
123 const api::system_display::DisplayProperties& params,
124 std::string* error) OVERRIDE {
125 // Should get called only once per test case.
126 EXPECT_FALSE(set_info_value_);
127 set_info_value_ = params.ToValue();
128 set_info_display_id_ = display_id;
132 scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
133 return set_info_value_.Pass();
136 std::string GetSetInfoDisplayId() const {
137 return set_info_display_id_;
141 // Update the content of the |unit| obtained for |display| using
142 // platform specific method.
143 virtual void UpdateDisplayUnitInfoForPlatform(
144 const gfx::Display& display,
145 extensions::api::system_display::DisplayUnitInfo* unit) OVERRIDE {
146 int64 id = display.id();
147 unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
149 unit->mirroring_source_id = "0";
150 unit->is_primary = id == 0 ? true : false;
151 unit->is_internal = id == 0 ? true : false;
152 unit->is_enabled = true;
153 unit->rotation = (90 * id) % 360;
157 unit->overscan.left = 20;
158 unit->overscan.top = 40;
159 unit->overscan.right = 60;
160 unit->overscan.bottom = 80;
164 scoped_ptr<base::DictionaryValue> set_info_value_;
165 std::string set_info_display_id_;
167 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
170 class SystemDisplayApiTest: public ExtensionApiTest {
172 SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider),
173 screen_(new MockScreen) {}
175 virtual ~SystemDisplayApiTest() {}
177 virtual void SetUpOnMainThread() OVERRIDE {
178 ExtensionApiTest::SetUpOnMainThread();
179 ANNOTATE_LEAKING_OBJECT_PTR(
180 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
181 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
182 DisplayInfoProvider::InitializeForTesting(provider_.get());
185 virtual void TearDownOnMainThread() OVERRIDE {
186 #if defined(OS_CHROMEOS)
187 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
188 ash::Shell::GetScreen());
190 ExtensionApiTest::TearDownOnMainThread();
194 scoped_ptr<MockDisplayInfoProvider> provider_;
195 scoped_ptr<gfx::Screen> screen_;
197 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
200 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
201 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_;
204 #if !defined(OS_CHROMEOS)
205 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
206 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
207 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
209 set_info_function->set_has_callback(true);
211 EXPECT_EQ("Function available only on ChromeOS.",
212 utils::RunFunctionAndReturnError(set_info_function.get(),
213 "[\"display_id\", {}]",
216 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
217 EXPECT_FALSE(set_info);
219 #endif // !defined(OS_CHROMEOS)
221 #if defined(OS_CHROMEOS)
222 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
223 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
225 " \"name\": \"Test\",\n"
226 " \"version\": \"1.0\",\n"
228 " \"background\": {\n"
229 " \"scripts\": [\"background.js\"]\n"
233 scoped_refptr<Extension> test_extension(
234 utils::CreateExtension(test_extension_value.get()));
236 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
237 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
239 set_info_function->set_extension(test_extension.get());
240 set_info_function->set_has_callback(true);
242 EXPECT_EQ("The extension needs to be kiosk enabled to use the function.",
243 utils::RunFunctionAndReturnError(set_info_function.get(),
244 "[\"display_id\", {}]",
247 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
248 EXPECT_FALSE(set_info);
251 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
252 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
254 " \"name\": \"Test\",\n"
255 " \"version\": \"1.0\",\n"
257 " \"background\": {\n"
258 " \"scripts\": [\"background.js\"]\n"
261 " \"kiosk_enabled\": true\n"
263 scoped_refptr<Extension> test_extension(
264 utils::CreateExtension(test_extension_value.get()));
266 scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
267 set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
269 set_info_function->set_has_callback(true);
270 set_info_function->set_extension(test_extension.get());
272 ASSERT_TRUE(utils::RunFunction(
273 set_info_function.get(),
274 "[\"display_id\", {\n"
275 " \"isPrimary\": true,\n"
276 " \"mirroringSourceId\": \"mirroringId\",\n"
277 " \"boundsOriginX\": 100,\n"
278 " \"boundsOriginY\": 200,\n"
279 " \"rotation\": 90,\n"
280 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
285 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
286 ASSERT_TRUE(set_info);
287 EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary"));
288 EXPECT_EQ("mirroringId",
289 utils::GetString(set_info.get(), "mirroringSourceId"));
290 EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX"));
291 EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY"));
292 EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation"));
293 base::DictionaryValue* overscan;
294 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
295 EXPECT_EQ(1, utils::GetInteger(overscan, "left"));
296 EXPECT_EQ(2, utils::GetInteger(overscan, "top"));
297 EXPECT_EQ(3, utils::GetInteger(overscan, "right"));
298 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom"));
300 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
302 #endif // defined(OS_CHROMEOS)
304 } // namespace extensions