1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/debug/leak_annotations.h"
#include "base/strings/string_number_conversions.h"
#include "extensions/browser/api/system_display/display_info_provider.h"
#include "extensions/browser/api/system_display/system_display_api.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/common/api/system_display.h"
#include "extensions/shell/test/shell_apitest.h"
#include "ui/gfx/display.h"
#include "ui/gfx/display_observer.h"
#include "ui/gfx/screen.h"
namespace extensions {
using core_api::system_display::Bounds;
using core_api::system_display::DisplayUnitInfo;
using gfx::Screen;
class MockScreen : public Screen {
public:
MockScreen() {
for (int i = 0; i < 4; i++) {
gfx::Rect bounds(0, 0, 1280, 720);
gfx::Rect work_area(0, 0, 960, 720);
gfx::Display display(i, bounds);
display.set_work_area(work_area);
displays_.push_back(display);
}
}
~MockScreen() override {}
protected:
// Overridden from gfx::Screen:
gfx::Point GetCursorScreenPoint() override { return gfx::Point(); }
gfx::NativeWindow GetWindowUnderCursor() override {
return gfx::NativeWindow();
}
gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override {
return gfx::NativeWindow();
}
int GetNumDisplays() const override {
return static_cast<int>(displays_.size());
}
std::vector<gfx::Display> GetAllDisplays() const override {
return displays_;
}
gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override {
return gfx::Display(0);
}
gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override {
return gfx::Display(0);
}
gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override {
return gfx::Display(0);
}
gfx::Display GetPrimaryDisplay() const override { return displays_[0]; }
void AddObserver(gfx::DisplayObserver* observer) override {}
void RemoveObserver(gfx::DisplayObserver* observer) override {}
private:
std::vector<gfx::Display> displays_;
DISALLOW_COPY_AND_ASSIGN(MockScreen);
};
class MockDisplayInfoProvider : public DisplayInfoProvider {
public:
MockDisplayInfoProvider() {}
~MockDisplayInfoProvider() override {}
bool SetInfo(const std::string& display_id,
const core_api::system_display::DisplayProperties& params,
std::string* error) override {
// Should get called only once per test case.
EXPECT_FALSE(set_info_value_);
set_info_value_ = params.ToValue();
set_info_display_id_ = display_id;
return true;
}
gfx::Screen* GetActiveScreen() override { return NULL; }
scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
return set_info_value_.Pass();
}
std::string GetSetInfoDisplayId() const { return set_info_display_id_; }
private:
// Update the content of the |unit| obtained for |display| using
// platform specific method.
void UpdateDisplayUnitInfoForPlatform(
const gfx::Display& display,
extensions::core_api::system_display::DisplayUnitInfo* unit) override {
int64 id = display.id();
unit->name = "DISPLAY NAME FOR " + base::Int64ToString(id);
if (id == 1)
unit->mirroring_source_id = "0";
unit->is_primary = id == 0 ? true : false;
unit->is_internal = id == 0 ? true : false;
unit->is_enabled = true;
unit->rotation = (90 * id) % 360;
unit->dpi_x = 96.0;
unit->dpi_y = 96.0;
if (id == 0) {
unit->overscan.left = 20;
unit->overscan.top = 40;
unit->overscan.right = 60;
unit->overscan.bottom = 80;
}
}
scoped_ptr<base::DictionaryValue> set_info_value_;
std::string set_info_display_id_;
DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
};
class SystemDisplayApiTest : public ShellApiTest {
public:
SystemDisplayApiTest()
: provider_(new MockDisplayInfoProvider), screen_(new MockScreen) {}
~SystemDisplayApiTest() override {}
void SetUpOnMainThread() override {
ShellApiTest::SetUpOnMainThread();
ANNOTATE_LEAKING_OBJECT_PTR(
gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
DisplayInfoProvider::InitializeForTesting(provider_.get());
}
protected:
scoped_ptr<MockDisplayInfoProvider> provider_;
scoped_ptr<gfx::Screen> screen_;
private:
DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
};
IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
ASSERT_TRUE(RunAppTest("system/display")) << message_;
}
#if !defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
new SystemDisplaySetDisplayPropertiesFunction());
set_info_function->set_has_callback(true);
EXPECT_EQ(
"Function available only on ChromeOS.",
api_test_utils::RunFunctionAndReturnError(
set_info_function.get(), "[\"display_id\", {}]", browser_context()));
scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
EXPECT_FALSE(set_info);
}
#endif // !defined(OS_CHROMEOS)
#if defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
scoped_ptr<base::DictionaryValue> test_extension_value(
api_test_utils::ParseDictionary(
"{\n"
" \"name\": \"Test\",\n"
" \"version\": \"1.0\",\n"
" \"app\": {\n"
" \"background\": {\n"
" \"scripts\": [\"background.js\"]\n"
" }\n"
" }\n"
"}"));
scoped_refptr<Extension> test_extension(
api_test_utils::CreateExtension(test_extension_value.get()));
scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
new SystemDisplaySetDisplayPropertiesFunction());
set_info_function->set_extension(test_extension.get());
set_info_function->set_has_callback(true);
EXPECT_EQ(
"The extension needs to be kiosk enabled to use the function.",
api_test_utils::RunFunctionAndReturnError(
set_info_function.get(), "[\"display_id\", {}]", browser_context()));
scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
EXPECT_FALSE(set_info);
}
IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
scoped_ptr<base::DictionaryValue> test_extension_value(
api_test_utils::ParseDictionary(
"{\n"
" \"name\": \"Test\",\n"
" \"version\": \"1.0\",\n"
" \"app\": {\n"
" \"background\": {\n"
" \"scripts\": [\"background.js\"]\n"
" }\n"
" },\n"
" \"kiosk_enabled\": true\n"
"}"));
scoped_refptr<Extension> test_extension(
api_test_utils::CreateExtension(test_extension_value.get()));
scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
new SystemDisplaySetDisplayPropertiesFunction());
set_info_function->set_has_callback(true);
set_info_function->set_extension(test_extension.get());
ASSERT_TRUE(api_test_utils::RunFunction(
set_info_function.get(),
"[\"display_id\", {\n"
" \"isPrimary\": true,\n"
" \"mirroringSourceId\": \"mirroringId\",\n"
" \"boundsOriginX\": 100,\n"
" \"boundsOriginY\": 200,\n"
" \"rotation\": 90,\n"
" \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
"}]",
browser_context()));
scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
ASSERT_TRUE(set_info);
EXPECT_TRUE(api_test_utils::GetBoolean(set_info.get(), "isPrimary"));
EXPECT_EQ("mirroringId",
api_test_utils::GetString(set_info.get(), "mirroringSourceId"));
EXPECT_EQ(100, api_test_utils::GetInteger(set_info.get(), "boundsOriginX"));
EXPECT_EQ(200, api_test_utils::GetInteger(set_info.get(), "boundsOriginY"));
EXPECT_EQ(90, api_test_utils::GetInteger(set_info.get(), "rotation"));
base::DictionaryValue* overscan;
ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan));
EXPECT_EQ(1, api_test_utils::GetInteger(overscan, "left"));
EXPECT_EQ(2, api_test_utils::GetInteger(overscan, "top"));
EXPECT_EQ(3, api_test_utils::GetInteger(overscan, "right"));
EXPECT_EQ(4, api_test_utils::GetInteger(overscan, "bottom"));
EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
}
#endif // defined(OS_CHROMEOS)
} // namespace extensions
|