OLD | NEW |
| (Empty) |
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. | |
4 | |
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/api/bluetooth/bluetooth_api.h" | |
9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | |
10 #include "chrome/browser/extensions/extension_apitest.h" | |
11 #include "chrome/common/extensions/api/bluetooth_private.h" | |
12 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
13 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
14 #include "extensions/browser/event_router.h" | |
15 #include "extensions/common/switches.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 | |
18 using device::MockBluetoothAdapter; | |
19 using device::MockBluetoothDevice; | |
20 using testing::_; | |
21 using testing::InSequence; | |
22 using testing::NiceMock; | |
23 using testing::Return; | |
24 using testing::ReturnPointee; | |
25 using testing::WithArgs; | |
26 using testing::WithoutArgs; | |
27 | |
28 namespace bt = extensions::api::bluetooth; | |
29 namespace bt_private = extensions::api::bluetooth_private; | |
30 | |
31 namespace { | |
32 | |
33 const char kTestExtensionId[] = "jofgjdphhceggjecimellaapdjjadibj"; | |
34 const char kAdapterName[] = "Helix"; | |
35 const char kDeviceName[] = "Red"; | |
36 } | |
37 | |
38 class BluetoothPrivateApiTest : public ExtensionApiTest { | |
39 public: | |
40 BluetoothPrivateApiTest() | |
41 : adapter_name_(kAdapterName), | |
42 adapter_powered_(false), | |
43 adapter_discoverable_(false) {} | |
44 | |
45 virtual ~BluetoothPrivateApiTest() {} | |
46 | |
47 virtual void SetUpOnMainThread() OVERRIDE { | |
48 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
49 extensions::switches::kWhitelistedExtensionID, kTestExtensionId); | |
50 mock_adapter_ = new NiceMock<MockBluetoothAdapter>(); | |
51 event_router()->SetAdapterForTest(mock_adapter_.get()); | |
52 mock_device_.reset(new NiceMock<MockBluetoothDevice>(mock_adapter_.get(), | |
53 0, | |
54 kDeviceName, | |
55 "11:12:13:14:15:16", | |
56 false, | |
57 false)); | |
58 ON_CALL(*mock_adapter_, GetDevice(mock_device_->GetAddress())) | |
59 .WillByDefault(Return(mock_device_.get())); | |
60 ON_CALL(*mock_adapter_, IsPresent()) | |
61 .WillByDefault(Return(true)); | |
62 } | |
63 | |
64 virtual void TearDownOnMainThread() OVERRIDE {} | |
65 | |
66 extensions::BluetoothEventRouter* event_router() { | |
67 return extensions::BluetoothAPI::Get(browser()->profile()) | |
68 ->event_router(); | |
69 } | |
70 | |
71 void SetName(const std::string& name, const base::Closure& callback) { | |
72 adapter_name_ = name; | |
73 callback.Run(); | |
74 } | |
75 | |
76 void SetPowered(bool powered, const base::Closure& callback) { | |
77 adapter_powered_ = powered; | |
78 callback.Run(); | |
79 } | |
80 | |
81 void SetDiscoverable(bool discoverable, const base::Closure& callback) { | |
82 adapter_discoverable_ = discoverable; | |
83 callback.Run(); | |
84 } | |
85 | |
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; | |
93 | |
94 scoped_ptr<base::ListValue> args = | |
95 bt_private::OnPairing::Create(pairing_event); | |
96 scoped_ptr<extensions::Event> event( | |
97 new extensions::Event(bt_private::OnPairing::kEventName, args.Pass())); | |
98 extensions::EventRouter::Get(browser()->profile()) | |
99 ->DispatchEventToExtension(kTestExtensionId, event.Pass()); | |
100 } | |
101 | |
102 void DispatchAuthorizePairingEvent() { | |
103 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTAUTHORIZATION); | |
104 } | |
105 | |
106 void DispatchPincodePairingEvent() { | |
107 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPINCODE); | |
108 } | |
109 | |
110 void DispatchPasskeyPairingEvent() { | |
111 DispatchPairingEvent(bt_private::PAIRING_EVENT_TYPE_REQUESTPASSKEY); | |
112 } | |
113 | |
114 protected: | |
115 std::string adapter_name_; | |
116 bool adapter_powered_; | |
117 bool adapter_discoverable_; | |
118 | |
119 scoped_refptr<NiceMock<MockBluetoothAdapter> > mock_adapter_; | |
120 scoped_ptr<NiceMock<MockBluetoothDevice> > mock_device_; | |
121 }; | |
122 | |
123 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, SetAdapterState) { | |
124 ON_CALL(*mock_adapter_, GetName()) | |
125 .WillByDefault(ReturnPointee(&adapter_name_)); | |
126 ON_CALL(*mock_adapter_, IsPowered()) | |
127 .WillByDefault(ReturnPointee(&adapter_powered_)); | |
128 ON_CALL(*mock_adapter_, IsDiscoverable()) | |
129 .WillByDefault(ReturnPointee(&adapter_discoverable_)); | |
130 | |
131 EXPECT_CALL(*mock_adapter_, SetName("Dome", _, _)).WillOnce( | |
132 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetName))); | |
133 EXPECT_CALL(*mock_adapter_, SetPowered(true, _, _)).WillOnce( | |
134 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetPowered))); | |
135 EXPECT_CALL(*mock_adapter_, SetDiscoverable(true, _, _)).WillOnce( | |
136 WithArgs<0, 1>(Invoke(this, &BluetoothPrivateApiTest::SetDiscoverable))); | |
137 | |
138 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/adapter_state")) | |
139 << message_; | |
140 } | |
141 | |
142 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, NoBluetoothAdapter) { | |
143 ON_CALL(*mock_adapter_, IsPresent()) | |
144 .WillByDefault(Return(false)); | |
145 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/no_adapter")) | |
146 << message_; | |
147 } | |
148 | |
149 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, CancelPairing) { | |
150 InSequence s; | |
151 EXPECT_CALL(*mock_adapter_, | |
152 AddPairingDelegate( | |
153 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH)) | |
154 .WillOnce(WithoutArgs(Invoke( | |
155 this, &BluetoothPrivateApiTest::DispatchAuthorizePairingEvent))); | |
156 EXPECT_CALL(*mock_device_, ExpectingConfirmation()) | |
157 .WillRepeatedly(Return(true)); | |
158 EXPECT_CALL(*mock_device_, CancelPairing()); | |
159 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/cancel_pairing")) | |
160 << message_; | |
161 } | |
162 | |
163 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PincodePairing) { | |
164 EXPECT_CALL(*mock_adapter_, | |
165 AddPairingDelegate( | |
166 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH)) | |
167 .WillOnce(WithoutArgs( | |
168 Invoke(this, &BluetoothPrivateApiTest::DispatchPincodePairingEvent))); | |
169 EXPECT_CALL(*mock_device_, ExpectingPinCode()).WillRepeatedly(Return(true)); | |
170 EXPECT_CALL(*mock_device_, SetPinCode("abbbbbbk")); | |
171 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/pincode_pairing")) | |
172 << message_; | |
173 } | |
174 | |
175 IN_PROC_BROWSER_TEST_F(BluetoothPrivateApiTest, PasskeyPairing) { | |
176 EXPECT_CALL(*mock_adapter_, | |
177 AddPairingDelegate( | |
178 _, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH)) | |
179 .WillOnce(WithoutArgs( | |
180 Invoke(this, &BluetoothPrivateApiTest::DispatchPasskeyPairingEvent))); | |
181 EXPECT_CALL(*mock_device_, ExpectingPasskey()).WillRepeatedly(Return(true)); | |
182 EXPECT_CALL(*mock_device_, SetPasskey(900531)); | |
183 ASSERT_TRUE(RunComponentExtensionTest("bluetooth_private/passkey_pairing")) | |
184 << message_; | |
185 } | |
OLD | NEW |