Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_private_apitest.cc
blob85d88be842d24ac0b1e975ad1cd4c76fbf138617
1 // Copyright 2014 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.
5 #include "base/command_line.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
10 #include "device/bluetooth/test/mock_bluetooth_device.h"
11 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
12 #include "extensions/browser/api/bluetooth/bluetooth_api.h"
13 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/common/api/bluetooth_private.h"
16 #include "extensions/common/switches.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 using device::BluetoothDiscoveryFilter;
20 using device::BluetoothUUID;
21 using device::MockBluetoothAdapter;
22 using device::MockBluetoothDevice;
23 using device::MockBluetoothDiscoverySession;
24 using testing::_;
25 using testing::Eq;
26 using testing::InSequence;
27 using testing::NiceMock;
28 using testing::Return;
29 using testing::ReturnPointee;
30 using testing::WithArgs;
31 using testing::WithoutArgs;
33 namespace bt = extensions::core_api::bluetooth;
34 namespace bt_private = extensions::core_api::bluetooth_private;
36 namespace extensions {
38 namespace {
39 const char kTestExtensionId[] = "jofgjdphhceggjecimellaapdjjadibj";
40 const char kAdapterName[] = "Helix";
41 const char kDeviceName[] = "Red";
43 MATCHER_P(IsFilterEqual, a, "") {
44 return arg->Equals(*a);
48 class BluetoothPrivateApiTest : public ExtensionApiTest {
49 public:
50 BluetoothPrivateApiTest()
51 : adapter_name_(kAdapterName),
52 adapter_powered_(false),
53 adapter_discoverable_(false) {}
55 ~BluetoothPrivateApiTest() override {}
57 void SetUpOnMainThread() override {
58 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
59 switches::kWhitelistedExtensionID, kTestExtensionId);
60 mock_adapter_ = new NiceMock<MockBluetoothAdapter>();
61 event_router()->SetAdapterForTest(mock_adapter_.get());
62 mock_device_.reset(new NiceMock<MockBluetoothDevice>(mock_adapter_.get(),
64 kDeviceName,
65 "11:12:13:14:15:16",
66 false,
67 false));
68 ON_CALL(*mock_adapter_.get(), GetDevice(mock_device_->GetAddress()))
69 .WillByDefault(Return(mock_device_.get()));
70 ON_CALL(*mock_adapter_.get(), IsPresent()).WillByDefault(Return(true));
73 void TearDownOnMainThread() override {}
75 BluetoothEventRouter* event_router() {
76 return BluetoothAPI::Get(browser()->profile())->event_router();
79 void SetName(const std::string& name, const base::Closure& callback) {
80 adapter_name_ = name;
81 callback.Run();
84 void SetPowered(bool powered, const base::Closure& callback) {
85 adapter_powered_ = powered;
86 callback.Run();
89 void SetDiscoverable(bool discoverable, const base::Closure& callback) {
90 adapter_discoverable_ = discoverable;
91 callback.Run();
94 void DispatchPairingEvent(bt_private::PairingEventType pairing_event_type) {
95 bt_private::PairingEvent pairing_event;
96 pairing_event.pairing = pairing_event_type;
97 pairing_event.device.name.reset(new std::string(kDeviceName));
98 pairing_event.device.address = mock_device_->GetAddress();
99 pairing_event.device.vendor_id_source = bt::VENDOR_ID_SOURCE_USB;
100 pairing_event.device.type = bt::DEVICE_TYPE_PHONE;
102 scoped_ptr<base::ListValue> args =
103 bt_private::OnPairing::Create(pairing_event);
104 scoped_ptr<Event> event(
105 new Event(bt_private::OnPairing::kEventName, args.Pass()));
106 EventRouter::Get(browser()->profile())->DispatchEventToExtension(
107 kTestExtensionId, event.Pass());
110 void DispatchAuthorizePairingEvent() {
111 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION);
114 void DispatchPincodePairingEvent() {
115 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE);
118 void DispatchPasskeyPairingEvent() {
119 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY);
122 void CallSetDiscoveryFilterCallback(
123 device::BluetoothAdapter::DiscoverySessionCallback callback) {
124 auto session_ptr = scoped_ptr<NiceMock<MockBluetoothDiscoverySession>>(
125 mock_discovery_session_);
127 callback.Run(session_ptr.Pass());
130 protected:
131 std::string adapter_name_;
132 bool adapter_powered_;
133 bool adapter_discoverable_;
135 scoped_refptr<NiceMock<MockBluetoothAdapter> > mock_adapter_;
136 scoped_ptr<NiceMock<MockBluetoothDevice> > mock_device_;
138 // This discovery session will be owned by EventRouter, we'll only keep
139 // pointer to it.
140 NiceMock<MockBluetoothDiscoverySession>* mock_discovery_session_;
143 ACTION_TEMPLATE(InvokeCallbackArgument,
144 HAS_1_TEMPLATE_PARAMS(int, k),
145 AND_0_VALUE_PARAMS()) {
146 ::std::tr1::get<k>(args).Run();
149 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, SetAdapterState) {
150 ON_CALL(*mock_adapter_.get(), GetName())
151 .WillByDefault(ReturnPointee(&adapter_name_));
152 ON_CALL(*mock_adapter_.get(), IsPowered())
153 .WillByDefault(ReturnPointee(&adapter_powered_));
154 ON_CALL(*mock_adapter_.get(), IsDiscoverable())
155 .WillByDefault(ReturnPointee(&adapter_discoverable_));
157 EXPECT_CALL(*mock_adapter_.get(), SetName("Dome", _, _)).WillOnce(
158 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetName)));
159 EXPECT_CALL(*mock_adapter_.get(), SetPowered(true, _, _)).WillOnce(
160 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetPowered)));
161 EXPECT_CALL(*mock_adapter_.get(), SetDiscoverable(true, _, _)).WillOnce(
162 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetDiscoverable)));
164 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/adapter_state"))
165 << message_;
168 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, NoBluetoothAdapter) {
169 ON_CALL(*mock_adapter_.get(), IsPresent()).WillByDefault(Return(false));
170 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/no_adapter"))
171 << message_;
174 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, CancelPairing) {
175 InSequence s;
176 EXPECT_CALL(*mock_adapter_.get(),
177 AddPairingDelegate(
178 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
179 .WillOnce(WithoutArgs(Invoke(
180 this, &BluetoothPrivateApiTest::DispatchAuthorizePairingEvent)));
181 EXPECT_CALL(*mock_device_, ExpectingConfirmation())
182 .WillRepeatedly(Return(true));
183 EXPECT_CALL(*mock_device_, CancelPairing());
184 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/cancel_pairing"))
185 << message_;
188 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PincodePairing) {
189 EXPECT_CALL(*mock_adapter_.get(),
190 AddPairingDelegate(
191 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
192 .WillOnce(WithoutArgs(
193 Invoke(this, &BluetoothPrivateApiTest::DispatchPincodePairingEvent)));
194 EXPECT_CALL(*mock_device_, ExpectingPinCode()).WillRepeatedly(Return(true));
195 EXPECT_CALL(*mock_device_, SetPinCode("abbbbbbk"));
196 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/pincode_pairing"))
197 << message_;
200 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PasskeyPairing) {
201 EXPECT_CALL(*mock_adapter_.get(),
202 AddPairingDelegate(
203 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
204 .WillOnce(WithoutArgs(
205 Invoke(this, &BluetoothPrivateApiTest::DispatchPasskeyPairingEvent)));
206 EXPECT_CALL(*mock_device_, ExpectingPasskey()).WillRepeatedly(Return(true));
207 EXPECT_CALL(*mock_device_, SetPasskey(900531));
208 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/passkey_pairing"))
209 << message_;
212 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, DisconnectAll) {
213 EXPECT_CALL(*mock_device_, IsConnected())
214 .Times(6)
215 .WillOnce(Return(false))
216 .WillOnce(Return(true))
217 .WillOnce(Return(false))
218 .WillRepeatedly(Return(true));
219 EXPECT_CALL(*mock_device_, Disconnect(_, _))
220 .Times(3)
221 .WillOnce(InvokeCallbackArgument<1>())
222 .WillOnce(InvokeCallbackArgument<1>())
223 .WillOnce(InvokeCallbackArgument<0>());
224 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/disconnect"))
225 << message_;
228 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, DiscoveryFilter) {
229 mock_discovery_session_ = new NiceMock<MockBluetoothDiscoverySession>();
231 BluetoothDiscoveryFilter discovery_filter(
232 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
233 discovery_filter.SetPathloss(50);
234 discovery_filter.AddUUID(BluetoothUUID("cafe"));
235 discovery_filter.AddUUID(
236 BluetoothUUID("0000bebe-0000-1000-8000-00805f9b34fb"));
238 EXPECT_CALL(*mock_adapter_.get(), StartDiscoverySessionWithFilterRaw(
239 IsFilterEqual(&discovery_filter), _, _))
240 .Times(1)
241 .WillOnce(WithArgs<1>(Invoke(
242 this, &BluetoothPrivateApiTest::CallSetDiscoveryFilterCallback)));
243 EXPECT_CALL(*mock_discovery_session_, IsActive())
244 .Times(1)
245 .WillOnce(Return(true));
246 EXPECT_CALL(*mock_discovery_session_,
247 SetDiscoveryFilterRaw(Eq(nullptr), _, _))
248 .Times(1)
249 .WillOnce(InvokeCallbackArgument<1>());
250 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/discovery_filter"))
251 << message_;
254 } // namespace extensions