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