blob: 98feac5fe79a27e28dc24e6aed3117efff70c825 [file] [log] [blame]
jyasskinc993ce8d2016-03-31 00:38:341// Copyright 2016 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// This file contains browsertests for Web Bluetooth that depend on behavior
6// defined in chrome/, not just in content/.
7
8#include "base/command_line.h"
9#include "base/metrics/field_trial.h"
10#include "chrome/browser/permissions/permission_context_base.h"
11#include "chrome/browser/ui/browser.h"
ortunob6374dd82016-05-27 03:04:0712#include "chrome/browser/ui/browser_commands.h"
jyasskinc993ce8d2016-03-31 00:38:3413#include "chrome/browser/ui/tabs/tab_strip_model.h"
14#include "chrome/test/base/in_process_browser_test.h"
15#include "chrome/test/base/ui_test_utils.h"
16#include "components/variations/variations_associated_data.h"
17#include "content/public/browser/render_frame_host.h"
ortunob6374dd82016-05-27 03:04:0718#include "content/public/browser/render_process_host.h"
jyasskinc993ce8d2016-03-31 00:38:3419#include "content/public/common/content_switches.h"
20#include "content/public/test/browser_test_utils.h"
21#include "device/bluetooth/bluetooth_adapter_factory.h"
22#include "device/bluetooth/test/mock_bluetooth_adapter.h"
23
ortunob6374dd82016-05-27 03:04:0724using device::MockBluetoothAdapter;
25using testing::Return;
26
27typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter;
28
jyasskinc993ce8d2016-03-31 00:38:3429namespace {
30
31class WebBluetoothTest : public InProcessBrowserTest {
32 protected:
33 void SetUpCommandLine(base::CommandLine* command_line) override {
34 // This is needed while Web Bluetooth is an Origin Trial, but can go away
35 // once it ships globally.
36 command_line->AppendSwitch(switches::kEnableWebBluetooth);
37 InProcessBrowserTest::SetUpCommandLine(command_line);
38 }
39
40 void SetUpOnMainThread() override {
41 // Navigate to a secure context.
42 embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data");
43 ASSERT_TRUE(embedded_test_server()->Start());
44 ui_test_utils::NavigateToURL(
45 browser(),
46 embedded_test_server()->GetURL("localhost", "/simple_page.html"));
47 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
48 EXPECT_THAT(
49 web_contents_->GetMainFrame()->GetLastCommittedOrigin().Serialize(),
50 testing::StartsWith("http://localhost:"));
51 }
52
53 content::WebContents* web_contents_ = nullptr;
54};
55
ortunob6374dd82016-05-27 03:04:0756IN_PROC_BROWSER_TEST_F(WebBluetoothTest, WebBluetoothAfterCrash) {
57 // Make sure we can use Web Bluetooth after the tab crashes.
58 // Set up adapter with one device.
59 scoped_refptr<NiceMockBluetoothAdapter> adapter(
60 new NiceMockBluetoothAdapter());
61 ON_CALL(*adapter, IsPresent()).WillByDefault(Return(false));
62
63 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
64
65 std::string result;
66 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
67 web_contents_,
68 "navigator.bluetooth.requestDevice({filters: [{services: [0x180d]}]})"
69 " .catch(e => domAutomationController.send(e.toString()));",
70 &result));
71 EXPECT_EQ("NotFoundError: Bluetooth adapter not available.", result);
72
73 // Crash the renderer process.
74 content::RenderProcessHost* process = web_contents_->GetRenderProcessHost();
75 content::RenderProcessHostWatcher crash_observer(
76 process, content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
77 process->Shutdown(0, false);
78 crash_observer.Wait();
79
80 // Reload tab.
81 chrome::Reload(browser(), CURRENT_TAB);
82 content::WaitForLoadStop(
83 browser()->tab_strip_model()->GetActiveWebContents());
84
85 // Use Web Bluetooth again.
86 std::string result_after_crash;
87 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
88 web_contents_,
89 "navigator.bluetooth.requestDevice({filters: [{services: [0x180d]}]})"
90 " .catch(e => domAutomationController.send(e.toString()));",
91 &result_after_crash));
92 EXPECT_EQ("NotFoundError: Bluetooth adapter not available.",
93 result_after_crash);
94}
95
jyasskinc993ce8d2016-03-31 00:38:3496IN_PROC_BROWSER_TEST_F(WebBluetoothTest, KillSwitchShouldBlock) {
97 // Fake the BluetoothAdapter to say it's present.
98 scoped_refptr<device::MockBluetoothAdapter> adapter =
99 new testing::NiceMock<device::MockBluetoothAdapter>;
ortunob6374dd82016-05-27 03:04:07100 EXPECT_CALL(*adapter, IsPresent()).WillRepeatedly(Return(true));
jyasskinc993ce8d2016-03-31 00:38:34101 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
102
103 // Turn on the global kill switch.
104 std::map<std::string, std::string> params;
105 params["Bluetooth"] =
106 PermissionContextBase::kPermissionsKillSwitchBlockedValue;
107 variations::AssociateVariationParams(
108 PermissionContextBase::kPermissionsKillSwitchFieldStudy, "TestGroup",
109 params);
110 base::FieldTrialList::CreateFieldTrial(
111 PermissionContextBase::kPermissionsKillSwitchFieldStudy, "TestGroup");
112
113 std::string rejection;
114 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
115 web_contents_,
116 "navigator.bluetooth.requestDevice({filters: [{name: 'Hello'}]})"
117 " .then(() => { domAutomationController.send('Success'); },"
118 " reason => {"
119 " domAutomationController.send(reason.name + ': ' + reason.message);"
120 " });",
121 &rejection));
122 EXPECT_THAT(rejection,
123 testing::MatchesRegex("NotFoundError: .*globally disabled.*"));
124}
125
scheib74250322016-04-07 03:32:21126// Tests that using Finch field trial parameters for blacklist additions has
127// the effect of rejecting requestDevice calls.
128IN_PROC_BROWSER_TEST_F(WebBluetoothTest, BlacklistShouldBlock) {
129 // Fake the BluetoothAdapter to say it's present.
130 scoped_refptr<device::MockBluetoothAdapter> adapter =
131 new testing::NiceMock<device::MockBluetoothAdapter>;
ortunob6374dd82016-05-27 03:04:07132 EXPECT_CALL(*adapter, IsPresent()).WillRepeatedly(Return(true));
scheib74250322016-04-07 03:32:21133 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
134
135 std::map<std::string, std::string> params;
136 params["blacklist_additions"] = "ee01:e";
137 variations::AssociateVariationParams("WebBluetoothBlacklist", "TestGroup",
138 params);
139 base::FieldTrialList::CreateFieldTrial("WebBluetoothBlacklist", "TestGroup");
140
141 std::string rejection;
142 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
143 web_contents_,
144 "navigator.bluetooth.requestDevice({filters: [{services: [0xee01]}]})"
145 " .then(() => { domAutomationController.send('Success'); },"
146 " reason => {"
147 " domAutomationController.send(reason.name + ': ' + reason.message);"
148 " });",
149 &rejection));
150 EXPECT_THAT(rejection,
151 testing::MatchesRegex("SecurityError: .*blacklisted UUID.*"));
152}
153
jyasskinc993ce8d2016-03-31 00:38:34154} // namespace