| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/browser/usb/usb_chooser_controller.h" | 12 #include "chrome/browser/usb/usb_chooser_controller.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 17 #include "content/public/test/browser_test_utils.h" | 17 #include "content/public/test/browser_test_utils.h" |
| 18 #include "device/base/mock_device_client.h" | 18 #include "device/base/mock_device_client.h" |
| 19 #include "device/usb/mock_usb_device.h" | 19 #include "device/usb/mock_usb_device.h" |
| 20 #include "device/usb/mock_usb_service.h" | 20 #include "device/usb/mock_usb_service.h" |
| 21 #include "device/usb/public/interfaces/chooser_service.mojom.h" | 21 #include "device/usb/public/interfaces/chooser_service.mojom.h" |
| 22 #include "device/usb/webusb_descriptors.h" | |
| 23 #include "mojo/public/cpp/bindings/strong_binding.h" | 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 24 #include "services/service_manager/public/cpp/bind_source_info.h" | 23 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 25 #include "services/service_manager/public/cpp/binder_registry.h" | 24 #include "services/service_manager/public/cpp/binder_registry.h" |
| 26 | 25 |
| 27 using content::RenderFrameHost; | 26 using content::RenderFrameHost; |
| 28 using device::MockDeviceClient; | 27 using device::MockDeviceClient; |
| 29 using device::MockUsbDevice; | 28 using device::MockUsbDevice; |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 96 } |
| 98 | 97 |
| 99 void SetUpOnMainThread() override { | 98 void SetUpOnMainThread() override { |
| 100 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); | 99 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); |
| 101 ASSERT_TRUE(embedded_test_server()->Start()); | 100 ASSERT_TRUE(embedded_test_server()->Start()); |
| 102 | 101 |
| 103 device_client_.reset(new MockDeviceClient()); | 102 device_client_.reset(new MockDeviceClient()); |
| 104 scoped_refptr<MockUsbDevice> mock_device( | 103 scoped_refptr<MockUsbDevice> mock_device( |
| 105 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Device", "123456")); | 104 new MockUsbDevice(0, 0, "Test Manufacturer", "Test Device", "123456")); |
| 106 device_client_->usb_service()->AddDevice(mock_device); | 105 device_client_->usb_service()->AddDevice(mock_device); |
| 107 | |
| 108 mock_device = | |
| 109 new MockUsbDevice(1, 0, "Test Manufacturer", "Test Device", "ABCDEF"); | |
| 110 auto allowed_origins = base::MakeUnique<device::WebUsbAllowedOrigins>(); | |
| 111 allowed_origins->origins.push_back( | |
| 112 embedded_test_server()->GetURL("localhost", "/").GetOrigin()); | |
| 113 mock_device->set_webusb_allowed_origins(std::move(allowed_origins)); | |
| 114 device_client_->usb_service()->AddDevice(mock_device); | |
| 115 } | 106 } |
| 116 | 107 |
| 117 private: | 108 private: |
| 118 std::unique_ptr<MockDeviceClient> device_client_; | 109 std::unique_ptr<MockDeviceClient> device_client_; |
| 119 }; | 110 }; |
| 120 | 111 |
| 121 IN_PROC_BROWSER_TEST_F(WebUsbTest, RequestAndGetDevices) { | 112 IN_PROC_BROWSER_TEST_F(WebUsbTest, RequestAndGetDevices) { |
| 122 ui_test_utils::NavigateToURL( | 113 ui_test_utils::NavigateToURL( |
| 123 browser(), | 114 browser(), |
| 124 embedded_test_server()->GetURL("localhost", "/simple_page.html")); | 115 embedded_test_server()->GetURL("localhost", "/simple_page.html")); |
| 125 content::WebContents* web_contents = | 116 content::WebContents* web_contents = |
| 126 browser()->tab_strip_model()->GetActiveWebContents(); | 117 browser()->tab_strip_model()->GetActiveWebContents(); |
| 127 RenderFrameHost* render_frame_host = web_contents->GetMainFrame(); | 118 RenderFrameHost* render_frame_host = web_contents->GetMainFrame(); |
| 128 EXPECT_THAT(render_frame_host->GetLastCommittedOrigin().Serialize(), | 119 EXPECT_THAT(render_frame_host->GetLastCommittedOrigin().Serialize(), |
| 129 testing::StartsWith("http://localhost:")); | 120 testing::StartsWith("http://localhost:")); |
| 130 | 121 |
| 131 render_frame_host->GetInterfaceRegistry()->AddInterface( | 122 render_frame_host->GetInterfaceRegistry()->AddInterface( |
| 132 base::Bind(&FakeChooserService::Create, render_frame_host)); | 123 base::Bind(&FakeChooserService::Create, render_frame_host)); |
| 133 | 124 |
| 134 // The mock device with vendorId == 0 has no WebUSB allowed origin descriptor | |
| 135 // but because this is a top level frame it will be allowed. | |
| 136 std::string result; | 125 std::string result; |
| 137 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 126 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 138 web_contents, | 127 web_contents, |
| 128 "navigator.usb.getDevices()" |
| 129 " .then(devices => {" |
| 130 " domAutomationController.send(devices.length.toString());" |
| 131 " });", |
| 132 &result)); |
| 133 EXPECT_EQ("0", result); |
| 134 |
| 135 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 136 web_contents, |
| 139 "navigator.usb.requestDevice({ filters: [ { vendorId: 0 } ] })" | 137 "navigator.usb.requestDevice({ filters: [ { vendorId: 0 } ] })" |
| 140 " .then(device => {" | 138 " .then(device => {" |
| 141 " domAutomationController.send(device.serialNumber);" | 139 " domAutomationController.send(device.serialNumber);" |
| 142 " });", | 140 " });", |
| 143 &result)); | 141 &result)); |
| 144 EXPECT_EQ("123456", result); | 142 EXPECT_EQ("123456", result); |
| 145 | 143 |
| 146 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 144 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 147 web_contents, | 145 web_contents, |
| 148 "navigator.usb.getDevices()" | 146 "navigator.usb.getDevices()" |
| 149 " .then(devices => {" | 147 " .then(devices => {" |
| 150 " domAutomationController.send(devices.length.toString());" | 148 " domAutomationController.send(devices.length.toString());" |
| 151 " });", | |
| 152 &result)); | |
| 153 EXPECT_EQ("1", result); | |
| 154 } | |
| 155 | |
| 156 IN_PROC_BROWSER_TEST_F(WebUsbTest, RequestAndGetDevicesInIframe) { | |
| 157 ui_test_utils::NavigateToURL( | |
| 158 browser(), | |
| 159 embedded_test_server()->GetURL("localhost", "/page_with_iframe.html")); | |
| 160 content::WebContents* web_contents = | |
| 161 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 162 RenderFrameHost* main_frame = web_contents->GetMainFrame(); | |
| 163 EXPECT_THAT(main_frame->GetLastCommittedOrigin().Serialize(), | |
| 164 testing::StartsWith("http://localhost:")); | |
| 165 RenderFrameHost* embedded_frame = ChildFrameAt(main_frame, 0); | |
| 166 EXPECT_THAT(embedded_frame->GetLastCommittedOrigin().Serialize(), | |
| 167 testing::StartsWith("http://localhost:")); | |
| 168 | |
| 169 embedded_frame->GetInterfaceRegistry()->AddInterface( | |
| 170 base::Bind(&FakeChooserService::Create, embedded_frame)); | |
| 171 | |
| 172 // The mock device with vendorId == 0 has no allowed origin descriptor so an | |
| 173 // embedded frame will not be able to select it. | |
| 174 std::string result; | |
| 175 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 176 embedded_frame, | |
| 177 "navigator.usb.requestDevice({ filters: [ { vendorId: 0 } ] })" | |
| 178 " .catch(e => { domAutomationController.send(e.toString()); });", | |
| 179 &result)); | |
| 180 EXPECT_EQ("NotFoundError: No device selected.", result); | |
| 181 | |
| 182 // The mock device with vendorId == 1 does however have the embedded test | |
| 183 // server listed as an allowed origin. | |
| 184 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 185 embedded_frame, | |
| 186 "navigator.usb.requestDevice({ filters: [ { vendorId: 1 } ] })" | |
| 187 " .then(device => {" | |
| 188 " domAutomationController.send(device.serialNumber);" | |
| 189 " });", | |
| 190 &result)); | |
| 191 EXPECT_EQ("ABCDEF", result); | |
| 192 | |
| 193 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
| 194 embedded_frame, | |
| 195 "navigator.usb.getDevices()" | |
| 196 " .then(devices => {" | |
| 197 " domAutomationController.send(devices.length.toString());" | |
| 198 " });", | 149 " });", |
| 199 &result)); | 150 &result)); |
| 200 EXPECT_EQ("1", result); | 151 EXPECT_EQ("1", result); |
| 201 } | 152 } |
| 202 | 153 |
| 203 } // namespace | 154 } // namespace |
| OLD | NEW |