OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/command_line.h" | 4 |
5 #include "base/message_loop.h" | 5 #include "chrome/browser/extensions/api/system_info_display/system_info_display_
api.h" |
| 6 |
6 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
7 #include "chrome/browser/extensions/api/system_info_display/display_info_provide
r.h" | 8 #include "chrome/browser/extensions/api/system_info_display/display_info_provide
r.h" |
8 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
9 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_function_test_utils.h" |
10 #include "chrome/common/chrome_switches.h" | 11 |
| 12 namespace utils = extension_function_test_utils; |
11 | 13 |
12 namespace extensions { | 14 namespace extensions { |
13 | 15 |
14 using api::system_info_display::Bounds; | 16 using api::system_info_display::Bounds; |
15 using api::system_info_display::DisplayUnitInfo; | 17 using api::system_info_display::DisplayUnitInfo; |
16 | 18 |
17 class MockDisplayInfoProvider : public DisplayInfoProvider { | 19 class MockDisplayInfoProvider : public DisplayInfoProvider { |
18 public: | 20 public: |
| 21 MockDisplayInfoProvider() {} |
| 22 |
19 virtual bool QueryInfo(DisplayInfo* info) OVERRIDE { | 23 virtual bool QueryInfo(DisplayInfo* info) OVERRIDE { |
20 info->clear(); | 24 info->clear(); |
21 for (int i = 0; i < 4; i++) { | 25 for (int i = 0; i < 4; i++) { |
22 linked_ptr<DisplayUnitInfo> unit(new DisplayUnitInfo()); | 26 linked_ptr<DisplayUnitInfo> unit(new DisplayUnitInfo()); |
23 unit->id = base::IntToString(i); | 27 unit->id = base::IntToString(i); |
24 unit->name = "DISPLAY NAME FOR " + unit->id; | 28 unit->name = "DISPLAY NAME FOR " + unit->id; |
25 if (i == 1) | 29 if (i == 1) |
26 unit->mirroring_source_id = "0"; | 30 unit->mirroring_source_id = "0"; |
27 unit->is_primary = i == 0 ? true : false; | 31 unit->is_primary = i == 0 ? true : false; |
28 unit->is_internal = i == 0 ? true : false; | 32 unit->is_internal = i == 0 ? true : false; |
(...skipping 12 matching lines...) Expand all Loading... |
41 unit->overscan.bottom = 80; | 45 unit->overscan.bottom = 80; |
42 } | 46 } |
43 unit->work_area.left = 0; | 47 unit->work_area.left = 0; |
44 unit->work_area.top = 0; | 48 unit->work_area.top = 0; |
45 unit->work_area.width = 960; | 49 unit->work_area.width = 960; |
46 unit->work_area.height = 720; | 50 unit->work_area.height = 720; |
47 info->push_back(unit); | 51 info->push_back(unit); |
48 } | 52 } |
49 return true; | 53 return true; |
50 } | 54 } |
| 55 |
| 56 virtual void SetInfo( |
| 57 const std::string& display_id, |
| 58 const api::system_info_display::DisplayProperties& params, |
| 59 const SetInfoCallback& callback) OVERRIDE { |
| 60 // Should get called only once per test case. |
| 61 EXPECT_FALSE(set_info_value_); |
| 62 set_info_value_ = params.ToValue(); |
| 63 set_info_display_id_ = display_id; |
| 64 callback.Run(true, std::string()); |
| 65 } |
| 66 |
| 67 scoped_ptr<base::DictionaryValue> GetSetInfoValue() { |
| 68 return set_info_value_.Pass(); |
| 69 } |
| 70 |
| 71 std::string GetSetInfoDisplayId() const { |
| 72 return set_info_display_id_; |
| 73 } |
| 74 |
51 private: | 75 private: |
52 virtual ~MockDisplayInfoProvider() {} | 76 virtual ~MockDisplayInfoProvider() {} |
53 | 77 |
| 78 scoped_ptr<base::DictionaryValue> set_info_value_; |
| 79 std::string set_info_display_id_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(MockDisplayInfoProvider); |
54 }; | 82 }; |
55 | 83 |
56 class SystemInfoDisplayApiTest: public ExtensionApiTest { | 84 class SystemInfoDisplayApiTest: public ExtensionApiTest { |
57 public: | 85 public: |
58 SystemInfoDisplayApiTest() {} | 86 SystemInfoDisplayApiTest() {} |
59 virtual ~SystemInfoDisplayApiTest() {} | 87 virtual ~SystemInfoDisplayApiTest() {} |
60 | 88 |
61 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 89 virtual void SetUpOnMainThread() OVERRIDE { |
62 ExtensionApiTest::SetUpCommandLine(command_line); | 90 ExtensionApiTest::SetUpOnMainThread(); |
63 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | 91 provider_ = new MockDisplayInfoProvider(); |
| 92 // The |provider| will be co-owned by the singleton instance. |
| 93 DisplayInfoProvider::InitializeForTesting(provider_); |
64 } | 94 } |
65 | 95 |
66 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 96 virtual void CleanUpOnMainThread() OVERRIDE { |
67 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 97 // Has to be released before the main thread is gone. |
68 message_loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI)); | 98 provider_ = NULL; |
| 99 ExtensionApiTest::CleanUpOnMainThread(); |
69 } | 100 } |
70 | 101 |
71 private: | 102 protected: |
72 scoped_ptr<base::MessageLoop> message_loop_; | 103 scoped_refptr<MockDisplayInfoProvider> provider_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(SystemInfoDisplayApiTest); |
73 }; | 106 }; |
74 | 107 |
75 IN_PROC_BROWSER_TEST_F(SystemInfoDisplayApiTest, Display) { | 108 IN_PROC_BROWSER_TEST_F(SystemInfoDisplayApiTest, GetDisplay) { |
76 // The |provider| will be owned by the singleton instance. | |
77 scoped_refptr<MockDisplayInfoProvider> provider = | |
78 new MockDisplayInfoProvider(); | |
79 DisplayInfoProvider::InitializeForTesting(provider); | |
80 ASSERT_TRUE(RunPlatformAppTest("systeminfo/display")) << message_; | 109 ASSERT_TRUE(RunPlatformAppTest("systeminfo/display")) << message_; |
81 } | 110 } |
82 | 111 |
| 112 #if !defined(OS_CHROMEOS) |
| 113 IN_PROC_BROWSER_TEST_F(SystemInfoDisplayApiTest, SetDisplay) { |
| 114 scoped_refptr<SystemInfoDisplaySetDisplayPropertiesFunction> |
| 115 set_info_function(new SystemInfoDisplaySetDisplayPropertiesFunction()); |
| 116 |
| 117 set_info_function->set_has_callback(true); |
| 118 |
| 119 EXPECT_EQ("Function available only on ChromeOS.", |
| 120 utils::RunFunctionAndReturnError(set_info_function.get(), |
| 121 "[\"display_id\", {}]", |
| 122 browser())); |
| 123 |
| 124 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); |
| 125 EXPECT_FALSE(set_info); |
| 126 } |
| 127 #endif // !defined(OS_CHROMEOS) |
| 128 |
| 129 #if defined(OS_CHROMEOS) |
| 130 IN_PROC_BROWSER_TEST_F(SystemInfoDisplayApiTest, SetDisplayNotKioskEnabled) { |
| 131 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary( |
| 132 "{\n" |
| 133 " \"name\": \"Test\",\n" |
| 134 " \"version\": \"1.0\",\n" |
| 135 " \"app\": {\n" |
| 136 " \"background\": {\n" |
| 137 " \"scripts\": [\"background.js\"]\n" |
| 138 " }\n" |
| 139 " }\n" |
| 140 "}")); |
| 141 scoped_refptr<Extension> test_extension( |
| 142 utils::CreateExtension(test_extension_value.get())); |
| 143 |
| 144 scoped_refptr<SystemInfoDisplaySetDisplayPropertiesFunction> |
| 145 set_info_function(new SystemInfoDisplaySetDisplayPropertiesFunction()); |
| 146 |
| 147 set_info_function->set_extension(test_extension.get()); |
| 148 set_info_function->set_has_callback(true); |
| 149 |
| 150 EXPECT_EQ("The extension needs to be kiosk enabled to use the function.", |
| 151 utils::RunFunctionAndReturnError(set_info_function.get(), |
| 152 "[\"display_id\", {}]", |
| 153 browser())); |
| 154 |
| 155 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); |
| 156 EXPECT_FALSE(set_info); |
| 157 } |
| 158 |
| 159 IN_PROC_BROWSER_TEST_F(SystemInfoDisplayApiTest, SetDisplayKioskEnabled) { |
| 160 scoped_ptr<base::DictionaryValue> test_extension_value(utils::ParseDictionary( |
| 161 "{\n" |
| 162 " \"name\": \"Test\",\n" |
| 163 " \"version\": \"1.0\",\n" |
| 164 " \"app\": {\n" |
| 165 " \"background\": {\n" |
| 166 " \"scripts\": [\"background.js\"]\n" |
| 167 " }\n" |
| 168 " },\n" |
| 169 " \"kiosk_enabled\": true\n" |
| 170 "}")); |
| 171 scoped_refptr<Extension> test_extension( |
| 172 utils::CreateExtension(test_extension_value.get())); |
| 173 |
| 174 scoped_refptr<SystemInfoDisplaySetDisplayPropertiesFunction> |
| 175 set_info_function(new SystemInfoDisplaySetDisplayPropertiesFunction()); |
| 176 |
| 177 set_info_function->set_has_callback(true); |
| 178 set_info_function->set_extension(test_extension.get()); |
| 179 |
| 180 ASSERT_TRUE(utils::RunFunction( |
| 181 set_info_function.get(), |
| 182 "[\"display_id\", {\n" |
| 183 " \"isPrimary\": true,\n" |
| 184 " \"mirroringSourceId\": \"mirroringId\",\n" |
| 185 " \"boundsOriginX\": 100,\n" |
| 186 " \"boundsOriginY\": 200,\n" |
| 187 " \"rotation\": 90,\n" |
| 188 " \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n" |
| 189 "}]", |
| 190 browser(), |
| 191 utils::NONE)); |
| 192 |
| 193 scoped_ptr<base::DictionaryValue> set_info = provider_->GetSetInfoValue(); |
| 194 ASSERT_TRUE(set_info); |
| 195 EXPECT_TRUE(utils::GetBoolean(set_info.get(), "isPrimary")); |
| 196 EXPECT_EQ("mirroringId", |
| 197 utils::GetString(set_info.get(), "mirroringSourceId")); |
| 198 EXPECT_EQ(100, utils::GetInteger(set_info.get(), "boundsOriginX")); |
| 199 EXPECT_EQ(200, utils::GetInteger(set_info.get(), "boundsOriginY")); |
| 200 EXPECT_EQ(90, utils::GetInteger(set_info.get(), "rotation")); |
| 201 base::DictionaryValue* overscan; |
| 202 ASSERT_TRUE(set_info->GetDictionary("overscan", &overscan)); |
| 203 EXPECT_EQ(1, utils::GetInteger(overscan, "left")); |
| 204 EXPECT_EQ(2, utils::GetInteger(overscan, "top")); |
| 205 EXPECT_EQ(3, utils::GetInteger(overscan, "right")); |
| 206 EXPECT_EQ(4, utils::GetInteger(overscan, "bottom")); |
| 207 |
| 208 EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId()); |
| 209 } |
| 210 #endif // defined(OS_CHROMEOS) |
| 211 |
83 } // namespace extensions | 212 } // namespace extensions |
OLD | NEW |