| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/system_display/system_display_api.h" | 5 #include "chrome/browser/extensions/api/system_display/system_display_api.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/browser/extensions/api/system_display/display_info_provider.h" | 8 #include "chrome/browser/extensions/api/system_display/display_info_provider.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_function_test_utils.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 |
| 11 | 19 |
| 12 namespace utils = extension_function_test_utils; | 20 namespace utils = extension_function_test_utils; |
| 13 | 21 |
| 14 namespace extensions { | 22 namespace extensions { |
| 15 | 23 |
| 16 using api::system_display::Bounds; | 24 using api::system_display::Bounds; |
| 17 using api::system_display::DisplayUnitInfo; | 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 |
| 18 | 113 |
| 19 class MockDisplayInfoProvider : public DisplayInfoProvider { | 114 class MockDisplayInfoProvider : public DisplayInfoProvider { |
| 20 public: | 115 public: |
| 21 MockDisplayInfoProvider() {} | 116 MockDisplayInfoProvider() {} |
| 22 | 117 |
| 23 virtual bool QueryInfo() OVERRIDE { | 118 virtual ~MockDisplayInfoProvider() {} |
| 24 info_.clear(); | |
| 25 for (int i = 0; i < 4; i++) { | |
| 26 linked_ptr<DisplayUnitInfo> unit(new DisplayUnitInfo()); | |
| 27 unit->id = base::IntToString(i); | |
| 28 unit->name = "DISPLAY NAME FOR " + unit->id; | |
| 29 if (i == 1) | |
| 30 unit->mirroring_source_id = "0"; | |
| 31 unit->is_primary = i == 0 ? true : false; | |
| 32 unit->is_internal = i == 0 ? true : false; | |
| 33 unit->is_enabled = true; | |
| 34 unit->rotation = (90 * i) % 360; | |
| 35 unit->dpi_x = 96.0; | |
| 36 unit->dpi_y = 96.0; | |
| 37 unit->bounds.left = 0; | |
| 38 unit->bounds.top = 0; | |
| 39 unit->bounds.width = 1280; | |
| 40 unit->bounds.height = 720; | |
| 41 if (i == 0) { | |
| 42 unit->overscan.left = 20; | |
| 43 unit->overscan.top = 40; | |
| 44 unit->overscan.right = 60; | |
| 45 unit->overscan.bottom = 80; | |
| 46 } | |
| 47 unit->work_area.left = 0; | |
| 48 unit->work_area.top = 0; | |
| 49 unit->work_area.width = 960; | |
| 50 unit->work_area.height = 720; | |
| 51 info_.push_back(unit); | |
| 52 } | |
| 53 return true; | |
| 54 } | |
| 55 | 119 |
| 56 virtual void SetInfo( | 120 virtual bool SetInfo( |
| 57 const std::string& display_id, | 121 const std::string& display_id, |
| 58 const api::system_display::DisplayProperties& params, | 122 const api::system_display::DisplayProperties& params, |
| 59 const SetInfoCallback& callback) OVERRIDE { | 123 std::string* error) OVERRIDE { |
| 60 // Should get called only once per test case. | 124 // Should get called only once per test case. |
| 61 EXPECT_FALSE(set_info_value_); | 125 EXPECT_FALSE(set_info_value_); |
| 62 set_info_value_ = params.ToValue(); | 126 set_info_value_ = params.ToValue(); |
| 63 set_info_display_id_ = display_id; | 127 set_info_display_id_ = display_id; |
| 64 callback.Run(true, std::string()); | 128 return true; |
| 65 } | 129 } |
| 66 | 130 |
| 67 scoped_ptr<base::DictionaryValue> GetSetInfoValue() { | 131 scoped_ptr<base::DictionaryValue> GetSetInfoValue() { |
| 68 return set_info_value_.Pass(); | 132 return set_info_value_.Pass(); |
| 69 } | 133 } |
| 70 | 134 |
| 71 std::string GetSetInfoDisplayId() const { | 135 std::string GetSetInfoDisplayId() const { |
| 72 return set_info_display_id_; | 136 return set_info_display_id_; |
| 73 } | 137 } |
| 74 | 138 |
| 75 private: | 139 private: |
| 76 virtual ~MockDisplayInfoProvider() {} | 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 } |
| 77 | 162 |
| 78 scoped_ptr<base::DictionaryValue> set_info_value_; | 163 scoped_ptr<base::DictionaryValue> set_info_value_; |
| 79 std::string set_info_display_id_; | 164 std::string set_info_display_id_; |
| 80 | 165 |
| 81 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider); | 166 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider); |
| 82 }; | 167 }; |
| 83 | 168 |
| 84 class SystemDisplayApiTest: public ExtensionApiTest { | 169 class SystemDisplayApiTest: public ExtensionApiTest { |
| 85 public: | 170 public: |
| 86 SystemDisplayApiTest() {} | 171 SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider), |
| 172 screen_(new MockScreen) {} |
| 173 |
| 87 virtual ~SystemDisplayApiTest() {} | 174 virtual ~SystemDisplayApiTest() {} |
| 88 | 175 |
| 89 virtual void SetUpOnMainThread() OVERRIDE { | 176 virtual void SetUpOnMainThread() OVERRIDE { |
| 90 ExtensionApiTest::SetUpOnMainThread(); | 177 ExtensionApiTest::SetUpOnMainThread(); |
| 91 provider_ = new MockDisplayInfoProvider(); | 178 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); |
| 92 // The |provider| will be co-owned by the singleton instance. | 179 DisplayInfoProvider::InitializeForTesting(provider_.get()); |
| 93 DisplayInfoProvider::InitializeForTesting(provider_); | |
| 94 } | 180 } |
| 95 | 181 |
| 96 virtual void CleanUpOnMainThread() OVERRIDE { | 182 virtual void CleanUpOnMainThread() OVERRIDE { |
| 97 // Has to be released before the main thread is gone. | 183 #if defined(OS_CHROMEOS) |
| 98 provider_ = NULL; | 184 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, |
| 185 ash::Shell::GetInstance()->screen()); |
| 186 #endif |
| 99 ExtensionApiTest::CleanUpOnMainThread(); | 187 ExtensionApiTest::CleanUpOnMainThread(); |
| 100 } | 188 } |
| 101 | 189 |
| 102 protected: | 190 protected: |
| 103 scoped_refptr<MockDisplayInfoProvider> provider_; | 191 scoped_ptr<MockDisplayInfoProvider> provider_; |
| 192 scoped_ptr<gfx::Screen> screen_; |
| 104 | 193 |
| 105 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest); | 194 DISALLOW_COPY_AND_ASSIGN(SystemDisplayApiTest); |
| 106 }; | 195 }; |
| 107 | 196 |
| 108 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) { | 197 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, GetDisplay) { |
| 109 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_; | 198 ASSERT_TRUE(RunPlatformAppTest("system/display")) << message_; |
| 110 } | 199 } |
| 111 | 200 |
| 112 #if !defined(OS_CHROMEOS) | 201 #if !defined(OS_CHROMEOS) |
| 113 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) { | 202 IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplay) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 EXPECT_EQ(1, utils::GetInteger(overscan, "left")); | 292 EXPECT_EQ(1, utils::GetInteger(overscan, "left")); |
| 204 EXPECT_EQ(2, utils::GetInteger(overscan, "top")); | 293 EXPECT_EQ(2, utils::GetInteger(overscan, "top")); |
| 205 EXPECT_EQ(3, utils::GetInteger(overscan, "right")); | 294 EXPECT_EQ(3, utils::GetInteger(overscan, "right")); |
| 206 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom")); | 295 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom")); |
| 207 | 296 |
| 208 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId()); | 297 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId()); |
| 209 } | 298 } |
| 210 #endif // defined(OS_CHROMEOS) | 299 #endif // defined(OS_CHROMEOS) |
| 211 | 300 |
| 212 } // namespace extensions | 301 } // namespace extensions |
| OLD | NEW |