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 "extensions/browser/api/bluetooth/bluetooth_api.h"
12 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/common/api/bluetooth_private.h"
15 #include "extensions/common/switches.h"
16 #include "testing/gmock/include/gmock/gmock.h"
18 using device::MockBluetoothAdapter
;
19 using device::MockBluetoothDevice
;
21 using testing::InSequence
;
22 using testing::NiceMock
;
23 using testing::Return
;
24 using testing::ReturnPointee
;
25 using testing::WithArgs
;
26 using testing::WithoutArgs
;
28 namespace bt
= extensions::core_api::bluetooth
;
29 namespace bt_private
= extensions::core_api::bluetooth_private
;
31 namespace extensions
{
35 const char kTestExtensionId
[] = "jofgjdphhceggjecimellaapdjjadibj";
36 const char kAdapterName
[] = "Helix";
37 const char kDeviceName
[] = "Red";
40 class BluetoothPrivateApiTest
: public ExtensionApiTest
{
42 BluetoothPrivateApiTest()
43 : adapter_name_(kAdapterName
),
44 adapter_powered_(false),
45 adapter_discoverable_(false) {}
47 ~BluetoothPrivateApiTest() override
{}
49 void SetUpOnMainThread() override
{
50 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
51 switches::kWhitelistedExtensionID
, kTestExtensionId
);
52 mock_adapter_
= new NiceMock
<MockBluetoothAdapter
>();
53 event_router()->SetAdapterForTest(mock_adapter_
.get());
54 mock_device_
.reset(new NiceMock
<MockBluetoothDevice
>(mock_adapter_
.get(),
60 ON_CALL(*mock_adapter_
.get(), GetDevice(mock_device_
->GetAddress()))
61 .WillByDefault(Return(mock_device_
.get()));
62 ON_CALL(*mock_adapter_
.get(), IsPresent()).WillByDefault(Return(true));
65 void TearDownOnMainThread() override
{}
67 BluetoothEventRouter
* event_router() {
68 return BluetoothAPI::Get(browser()->profile())->event_router();
71 void SetName(const std::string
& name
, const base::Closure
& callback
) {
76 void SetPowered(bool powered
, const base::Closure
& callback
) {
77 adapter_powered_
= powered
;
81 void SetDiscoverable(bool discoverable
, const base::Closure
& callback
) {
82 adapter_discoverable_
= discoverable
;
86 void DispatchPairingEvent(bt_private::PairingEventType pairing_event_type
) {
87 bt_private::PairingEvent pairing_event
;
88 pairing_event
.pairing
= pairing_event_type
;
89 pairing_event
.device
.name
.reset(new std::string(kDeviceName
));
90 pairing_event
.device
.address
= mock_device_
->GetAddress();
91 pairing_event
.device
.vendor_id_source
= bt::VENDOR_ID_SOURCE_USB
;
92 pairing_event
.device
.type
= bt::DEVICE_TYPE_PHONE
;
94 scoped_ptr
<base::ListValue
> args
=
95 bt_private::OnPairing::Create(pairing_event
);
96 scoped_ptr
<Event
> event(
97 new Event(bt_private::OnPairing::kEventName
, args
.Pass()));
98 EventRouter::Get(browser()->profile())->DispatchEventToExtension(
99 kTestExtensionId
, event
.Pass());
102 void DispatchAuthorizePairingEvent() {
103 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION
);
106 void DispatchPincodePairingEvent() {
107 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE
);
110 void DispatchPasskeyPairingEvent() {
111 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY
);
115 std::string adapter_name_
;
116 bool adapter_powered_
;
117 bool adapter_discoverable_
;
119 scoped_refptr
<NiceMock
<MockBluetoothAdapter
> > mock_adapter_
;
120 scoped_ptr
<NiceMock
<MockBluetoothDevice
> > mock_device_
;
123 ACTION_TEMPLATE(InvokeCallbackArgument
,
124 HAS_1_TEMPLATE_PARAMS(int, k
),
125 AND_0_VALUE_PARAMS()) {
126 ::std::tr1::get
<k
>(args
).Run();
129 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, SetAdapterState
) {
130 ON_CALL(*mock_adapter_
.get(), GetName())
131 .WillByDefault(ReturnPointee(&adapter_name_
));
132 ON_CALL(*mock_adapter_
.get(), IsPowered())
133 .WillByDefault(ReturnPointee(&adapter_powered_
));
134 ON_CALL(*mock_adapter_
.get(), IsDiscoverable())
135 .WillByDefault(ReturnPointee(&adapter_discoverable_
));
137 EXPECT_CALL(*mock_adapter_
.get(), SetName("Dome", _
, _
)).WillOnce(
138 WithArgs
<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetName
)));
139 EXPECT_CALL(*mock_adapter_
.get(), SetPowered(true, _
, _
)).WillOnce(
140 WithArgs
<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetPowered
)));
141 EXPECT_CALL(*mock_adapter_
.get(), SetDiscoverable(true, _
, _
)).WillOnce(
142 WithArgs
<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetDiscoverable
)));
144 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/adapter_state"))
148 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, NoBluetoothAdapter
) {
149 ON_CALL(*mock_adapter_
.get(), IsPresent()).WillByDefault(Return(false));
150 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/no_adapter"))
154 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, CancelPairing
) {
156 EXPECT_CALL(*mock_adapter_
.get(),
158 _
, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
))
159 .WillOnce(WithoutArgs(Invoke(
160 this, &BluetoothPrivateApiTest::DispatchAuthorizePairingEvent
)));
161 EXPECT_CALL(*mock_device_
, ExpectingConfirmation())
162 .WillRepeatedly(Return(true));
163 EXPECT_CALL(*mock_device_
, CancelPairing());
164 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/cancel_pairing"))
168 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, PincodePairing
) {
169 EXPECT_CALL(*mock_adapter_
.get(),
171 _
, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
))
172 .WillOnce(WithoutArgs(
173 Invoke(this, &BluetoothPrivateApiTest::DispatchPincodePairingEvent
)));
174 EXPECT_CALL(*mock_device_
, ExpectingPinCode()).WillRepeatedly(Return(true));
175 EXPECT_CALL(*mock_device_
, SetPinCode("abbbbbbk"));
176 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/pincode_pairing"))
180 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, PasskeyPairing
) {
181 EXPECT_CALL(*mock_adapter_
.get(),
183 _
, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
))
184 .WillOnce(WithoutArgs(
185 Invoke(this, &BluetoothPrivateApiTest::DispatchPasskeyPairingEvent
)));
186 EXPECT_CALL(*mock_device_
, ExpectingPasskey()).WillRepeatedly(Return(true));
187 EXPECT_CALL(*mock_device_
, SetPasskey(900531));
188 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/passkey_pairing"))
192 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest
, DisconnectAll
) {
193 EXPECT_CALL(*mock_device_
, IsConnected())
195 .WillOnce(Return(false))
196 .WillOnce(Return(true))
197 .WillOnce(Return(false))
198 .WillRepeatedly(Return(true));
199 EXPECT_CALL(*mock_device_
, Disconnect(_
, _
))
201 .WillOnce(InvokeCallbackArgument
<1>())
202 .WillOnce(InvokeCallbackArgument
<1>())
203 .WillOnce(InvokeCallbackArgument
<0>());
204 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/disconnect"))
208 } // namespace extensions