Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_private_apitest.cc
blobbfe7f6f29a54a5bfb5545fe4ee94ef29dac1637e
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::api::bluetooth;
34 namespace bt_private = extensions::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(new Event(events::BLUETOOTH_PRIVATE_ON_PAIRING,
105 bt_private::OnPairing::kEventName,
106 args.Pass()));
107 EventRouter::Get(browser()->profile())->DispatchEventToExtension(
108 kTestExtensionId, event.Pass());
111 void DispatchAuthorizePairingEvent() {
112 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION);
115 void DispatchPincodePairingEvent() {
116 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE);
119 void DispatchPasskeyPairingEvent() {
120 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY);
123 void CallSetDiscoveryFilterCallback(
124 device::BluetoothAdapter::DiscoverySessionCallback callback) {
125 auto session_ptr = scoped_ptr<NiceMock<MockBluetoothDiscoverySession>>(
126 mock_discovery_session_);
128 callback.Run(session_ptr.Pass());
131 protected:
132 std::string adapter_name_;
133 bool adapter_powered_;
134 bool adapter_discoverable_;
136 scoped_refptr<NiceMock<MockBluetoothAdapter> > mock_adapter_;
137 scoped_ptr<NiceMock<MockBluetoothDevice> > mock_device_;
139 // This discovery session will be owned by EventRouter, we'll only keep
140 // pointer to it.
141 NiceMock<MockBluetoothDiscoverySession>* mock_discovery_session_;
144 ACTION_TEMPLATE(InvokeCallbackArgument,
145 HAS_1_TEMPLATE_PARAMS(int, k),
146 AND_0_VALUE_PARAMS()) {
147 ::std::tr1::get<k>(args).Run();
150 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, SetAdapterState) {
151 ON_CALL(*mock_adapter_.get(), GetName())
152 .WillByDefault(ReturnPointee(&adapter_name_));
153 ON_CALL(*mock_adapter_.get(), IsPowered())
154 .WillByDefault(ReturnPointee(&adapter_powered_));
155 ON_CALL(*mock_adapter_.get(), IsDiscoverable())
156 .WillByDefault(ReturnPointee(&adapter_discoverable_));
158 EXPECT_CALL(*mock_adapter_.get(), SetName("Dome", _, _)).WillOnce(
159 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetName)));
160 EXPECT_CALL(*mock_adapter_.get(), SetPowered(true, _, _)).WillOnce(
161 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetPowered)));
162 EXPECT_CALL(*mock_adapter_.get(), SetDiscoverable(true, _, _)).WillOnce(
163 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetDiscoverable)));
165 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/adapter_state"))
166 << message_;
169 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, NoBluetoothAdapter) {
170 ON_CALL(*mock_adapter_.get(), IsPresent()).WillByDefault(Return(false));
171 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/no_adapter"))
172 << message_;
175 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, CancelPairing) {
176 InSequence s;
177 EXPECT_CALL(*mock_adapter_.get(),
178 AddPairingDelegate(
179 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
180 .WillOnce(WithoutArgs(Invoke(
181 this, &BluetoothPrivateApiTest::DispatchAuthorizePairingEvent)));
182 EXPECT_CALL(*mock_device_, ExpectingConfirmation())
183 .WillRepeatedly(Return(true));
184 EXPECT_CALL(*mock_device_, CancelPairing());
185 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/cancel_pairing"))
186 << message_;
189 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PincodePairing) {
190 EXPECT_CALL(*mock_adapter_.get(),
191 AddPairingDelegate(
192 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
193 .WillOnce(WithoutArgs(
194 Invoke(this, &BluetoothPrivateApiTest::DispatchPincodePairingEvent)));
195 EXPECT_CALL(*mock_device_, ExpectingPinCode()).WillRepeatedly(Return(true));
196 EXPECT_CALL(*mock_device_, SetPinCode("abbbbbbk"));
197 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/pincode_pairing"))
198 << message_;
201 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PasskeyPairing) {
202 EXPECT_CALL(*mock_adapter_.get(),
203 AddPairingDelegate(
204 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH))
205 .WillOnce(WithoutArgs(
206 Invoke(this, &BluetoothPrivateApiTest::DispatchPasskeyPairingEvent)));
207 EXPECT_CALL(*mock_device_, ExpectingPasskey()).WillRepeatedly(Return(true));
208 EXPECT_CALL(*mock_device_, SetPasskey(900531));
209 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/passkey_pairing"))
210 << message_;
213 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, DisconnectAll) {
214 EXPECT_CALL(*mock_device_, IsConnected())
215 .Times(6)
216 .WillOnce(Return(false))
217 .WillOnce(Return(true))
218 .WillOnce(Return(false))
219 .WillRepeatedly(Return(true));
220 EXPECT_CALL(*mock_device_, Disconnect(_, _))
221 .Times(3)
222 .WillOnce(InvokeCallbackArgument<1>())
223 .WillOnce(InvokeCallbackArgument<1>())
224 .WillOnce(InvokeCallbackArgument<0>());
225 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/disconnect"))
226 << message_;
229 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, DiscoveryFilter) {
230 mock_discovery_session_ = new NiceMock<MockBluetoothDiscoverySession>();
232 BluetoothDiscoveryFilter discovery_filter(
233 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
234 discovery_filter.SetPathloss(50);
235 discovery_filter.AddUUID(BluetoothUUID("cafe"));
236 discovery_filter.AddUUID(
237 BluetoothUUID("0000bebe-0000-1000-8000-00805f9b34fb"));
239 EXPECT_CALL(*mock_adapter_.get(), StartDiscoverySessionWithFilterRaw(
240 IsFilterEqual(&discovery_filter), _, _))
241 .Times(1)
242 .WillOnce(WithArgs<1>(Invoke(
243 this, &BluetoothPrivateApiTest::CallSetDiscoveryFilterCallback)));
244 EXPECT_CALL(*mock_discovery_session_, IsActive())
245 .Times(1)
246 .WillOnce(Return(true));
247 EXPECT_CALL(*mock_discovery_session_,
248 SetDiscoveryFilterRaw(Eq(nullptr), _, _))
249 .Times(1)
250 .WillOnce(InvokeCallbackArgument<1>());
251 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/discovery_filter"))
252 << message_;
255 } // namespace extensions