- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / system_display / system_display_apitest.cc
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 "chrome/browser/extensions/api/system_display/system_display_api.h"
6
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"
14
15 #if defined(OS_CHROMEOS)
16 #include "ash/screen_ash.h"
17 #include "ash/shell.h"
18 #endif
19
20 namespace utils = extension_function_test_utils;
21
22 namespace extensions {
23
24 using api::system_display::Bounds;
25 using api::system_display::DisplayUnitInfo;
26 using gfx::Screen;
27
28 #if defined(OS_CHROMEOS)
29 class MockScreen : public ash::ScreenAsh {
30  public:
31   MockScreen() {
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);
38     }
39   }
40   virtual ~MockScreen() {}
41
42  protected:
43   // Overridden from gfx::Screen:
44   virtual int GetNumDisplays() const OVERRIDE {
45     return displays_.size();
46   }
47   virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
48     return displays_;
49   }
50   virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
51     return displays_[0];
52   }
53  private:
54   std::vector<gfx::Display> displays_;
55
56   DISALLOW_COPY_AND_ASSIGN(MockScreen);
57 };
58 #else
59 class MockScreen : public Screen {
60  public:
61   MockScreen() {
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);
68     }
69   }
70   virtual ~MockScreen() {}
71
72  protected:
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();
78   }
79   virtual gfx::NativeWindow GetWindowAtScreenPoint(
80       const gfx::Point& point) OVERRIDE {
81     return gfx::NativeWindow();
82   }
83   virtual int GetNumDisplays() const OVERRIDE {
84     return displays_.size();
85   }
86   virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
87     return displays_;
88   }
89   virtual gfx::Display GetDisplayNearestWindow(
90       gfx::NativeView window) const OVERRIDE {
91     return gfx::Display(0);
92   }
93   virtual gfx::Display GetDisplayNearestPoint(
94       const gfx::Point& point) const OVERRIDE {
95     return gfx::Display(0);
96   }
97   virtual gfx::Display GetDisplayMatching(
98       const gfx::Rect& match_rect) const OVERRIDE {
99     return gfx::Display(0);
100   }
101   virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
102     return displays_[0];
103   }
104   virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {}
105   virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {}
106
107  private:
108   std::vector<gfx::Display> displays_;
109
110   DISALLOW_COPY_AND_ASSIGN(MockScreen);
111 };
112 #endif
113
114 class MockDisplayInfoProvider : public DisplayInfoProvider {
115  public:
116   MockDisplayInfoProvider() {}
117
118   virtual ~MockDisplayInfoProvider() {}
119
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;
128     return true;
129   }
130
131   scoped_ptr<base::DictionaryValue> GetSetInfoValue() {
132     return set_info_value_.Pass();
133   }
134
135   std::string GetSetInfoDisplayId() const {
136     return set_info_display_id_;
137   }
138
139  private:
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);
147     if (id == 1)
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;
153     unit->dpi_x = 96.0;
154     unit->dpi_y = 96.0;
155     if (id == 0) {
156       unit->overscan.left = 20;
157       unit->overscan.top = 40;
158       unit->overscan.right = 60;
159       unit->overscan.bottom = 80;
160     }
161   }
162
163   scoped_ptr<base::DictionaryValue> set_info_value_;
164   std::string set_info_display_id_;
165
166   DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider);
167 };
168
169 class SystemDisplayApiTest: public ExtensionApiTest {
170  public:
171   SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider),
172                            screen_(new MockScreen) {}
173
174   virtual ~SystemDisplayApiTest() {}
175
176   virtual void SetUpOnMainThread() OVERRIDE {
177     ExtensionApiTest::SetUpOnMainThread();
178     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
179     DisplayInfoProvider::InitializeForTesting(provider_.get());
180   }
181
182   virtual void CleanUpOnMainThread() OVERRIDE {
183 #if defined(OS_CHROMEOS)
184     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
185                                    ash::Shell::GetInstance()->screen());
186 #endif
187     ExtensionApiTest::CleanUpOnMainThread();
188   }
189
190  protected:
191   scoped_ptr<MockDisplayInfoProvider> provider_;
192   scoped_ptr<gfx::Screen> screen_;
193
194   DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest);
195 };
196
197 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) {
198   ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_;
199 }
200
201 #if !defined(OS_CHROMEOS)
202 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) {
203   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
204       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
205
206   set_info_function->set_has_callback(true);
207
208   EXPECT_EQ("Function available only on ChromeOS.",
209             utils::RunFunctionAndReturnError(set_info_function.get(),
210                                              "[\"display_id\", {}]",
211                                              browser()));
212
213   scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
214   EXPECT_FALSE(set_info);
215 }
216 #endif  // !defined(OS_CHROMEOS)
217
218 #if defined(OS_CHROMEOS)
219 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
220   scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
221       "{\n"
222       "  \"name\": \"Test\",\n"
223       "  \"version\": \"1.0\",\n"
224       "  \"app\": {\n"
225       "    \"background\": {\n"
226       "      \"scripts\": [\"background.js\"]\n"
227       "    }\n"
228       "  }\n"
229       "}"));
230   scoped_refptr<Extension> test_extension(
231       utils::CreateExtension(test_extension_value.get()));
232
233   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
234       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
235
236   set_info_function->set_extension(test_extension.get());
237   set_info_function->set_has_callback(true);
238
239   EXPECT_EQ("The extension needs to be kiosk enabled to use the function.",
240             utils::RunFunctionAndReturnError(set_info_function.get(),
241                                              "[\"display_id\", {}]",
242                                              browser()));
243
244   scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue();
245   EXPECT_FALSE(set_info);
246 }
247
248 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
249   scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary(
250       "{\n"
251       "  \"name\": \"Test\",\n"
252       "  \"version\": \"1.0\",\n"
253       "  \"app\": {\n"
254       "    \"background\": {\n"
255       "      \"scripts\": [\"background.js\"]\n"
256       "    }\n"
257       "  },\n"
258       "  \"kiosk_enabled\": true\n"
259       "}"));
260   scoped_refptr<Extension> test_extension(
261       utils::CreateExtension(test_extension_value.get()));
262
263   scoped_refptr<SystemDisplaySetDisplayPropertiesFunction>
264       set_info_function(new SystemDisplaySetDisplayPropertiesFunction());
265
266   set_info_function->set_has_callback(true);
267   set_info_function->set_extension(test_extension.get());
268
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"
278       "}]",
279       browser(),
280       utils::NONE));
281
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"));
296
297   EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
298 }
299 #endif  // defined(OS_CHROMEOS)
300
301 } // namespace extensions