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 <string> | |
6 | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" | |
10 #include "chrome/browser/extensions/extension_apitest.h" | |
11 #include "chrome/browser/extensions/extension_function_test_utils.h" | |
12 #include "chrome/browser/extensions/extension_service.h" | |
13 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/test/base/ui_test_utils.h" | |
16 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
17 #include "device/bluetooth/bluetooth_uuid.h" | |
18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
19 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
20 #include "device/bluetooth/test/mock_bluetooth_socket.h" | |
21 #include "testing/gmock/include/gmock/gmock.h" | |
22 | |
23 using device::BluetoothAdapter; | |
24 using device::BluetoothAdapterFactory; | |
25 using device::BluetoothDevice; | |
26 using device::BluetoothSocket; | |
27 using device::BluetoothUUID; | |
28 using device::MockBluetoothAdapter; | |
29 using device::MockBluetoothDevice; | |
30 using device::MockBluetoothSocket; | |
31 using extensions::Extension; | |
32 | |
33 namespace utils = extension_function_test_utils; | |
34 namespace api = extensions::api; | |
35 | |
36 namespace { | |
37 | |
38 class BluetoothSocketApiTest : public ExtensionApiTest { | |
39 public: | |
40 BluetoothSocketApiTest() {} | |
41 | |
42 virtual void SetUpOnMainThread() OVERRIDE { | |
43 ExtensionApiTest::SetUpOnMainThread(); | |
44 empty_extension_ = utils::CreateEmptyExtension(); | |
45 SetUpMockAdapter(); | |
46 } | |
47 | |
48 void SetUpMockAdapter() { | |
49 // The browser will clean this up when it is torn down. | |
50 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); | |
51 BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter_); | |
52 | |
53 mock_device1_.reset( | |
54 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_.get(), | |
55 0, | |
56 "d1", | |
57 "11:12:13:14:15:16", | |
58 true /* paired */, | |
59 false /* connected */)); | |
60 mock_device2_.reset( | |
61 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_.get(), | |
62 0, | |
63 "d2", | |
64 "21:22:23:24:25:26", | |
65 true /* paired */, | |
66 false /* connected */)); | |
67 } | |
68 | |
69 protected: | |
70 scoped_refptr<testing::StrictMock<MockBluetoothAdapter> > mock_adapter_; | |
71 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > mock_device1_; | |
72 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > mock_device2_; | |
73 | |
74 private: | |
75 scoped_refptr<Extension> empty_extension_; | |
76 }; | |
77 | |
78 // testing::InvokeArgument<N> does not work with base::Callback, fortunately | |
79 // gmock makes it simple to create action templates that do for the various | |
80 // possible numbers of arguments. | |
81 ACTION_TEMPLATE(InvokeCallbackArgument, | |
82 HAS_1_TEMPLATE_PARAMS(int, k), | |
83 AND_0_VALUE_PARAMS()) { | |
84 ::std::tr1::get<k>(args).Run(); | |
85 } | |
86 | |
87 ACTION_TEMPLATE(InvokeCallbackArgument, | |
88 HAS_1_TEMPLATE_PARAMS(int, k), | |
89 AND_1_VALUE_PARAMS(p0)) { | |
90 ::std::tr1::get<k>(args).Run(p0); | |
91 } | |
92 | |
93 ACTION_TEMPLATE(InvokeCallbackArgument, | |
94 HAS_1_TEMPLATE_PARAMS(int, k), | |
95 AND_2_VALUE_PARAMS(p0, p1)) { | |
96 ::std::tr1::get<k>(args).Run(p0, p1); | |
97 } | |
98 | |
99 } // namespace | |
100 | |
101 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { | |
102 ResultCatcher catcher; | |
103 catcher.RestrictToProfile(browser()->profile()); | |
104 | |
105 // Return the right mock device object for the address used by the test, | |
106 // return NULL for the "Device not found" test. | |
107 EXPECT_CALL(*mock_adapter_.get(), GetDevice(mock_device1_->GetAddress())) | |
108 .WillRepeatedly(testing::Return(mock_device1_.get())); | |
109 EXPECT_CALL(*mock_adapter_.get(), GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) | |
110 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); | |
111 | |
112 // Return a mock socket object as a successful result to the connect() call. | |
113 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); | |
114 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket | |
115 = new testing::StrictMock<MockBluetoothSocket>(); | |
116 EXPECT_CALL(*mock_device1_, | |
117 ConnectToService(service_uuid, testing::_, testing::_)) | |
118 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); | |
119 | |
120 // Since the socket is unpaused, expect a call to Receive() from the socket | |
121 // dispatcher. Since there is no data, this will not call its callback. | |
122 EXPECT_CALL(*mock_socket.get(), Receive(testing::_, testing::_, testing::_)); | |
123 | |
124 // The test also cleans up by calling Disconnect and Close. | |
125 EXPECT_CALL(*mock_socket.get(), Disconnect(testing::_)) | |
126 .WillOnce(InvokeCallbackArgument<0>()); | |
127 EXPECT_CALL(*mock_socket.get(), Close()); | |
128 | |
129 // Run the test. | |
130 ExtensionTestMessageListener listener("ready", true); | |
131 scoped_refptr<const Extension> extension( | |
132 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/connect"))); | |
133 ASSERT_TRUE(extension.get()); | |
134 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
135 | |
136 listener.Reply("go"); | |
137 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
138 } | |
139 | |
140 #if defined(_LIBCPP_VERSION) | |
141 // This test fails in libc++ builds, see http://crbug.com/392205. | |
142 #define MAYBE_Listen DISABLED_Listen | |
143 #else | |
144 #define MAYBE_Listen Listen | |
145 #endif | |
146 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, MAYBE_Listen) { | |
147 ResultCatcher catcher; | |
148 catcher.RestrictToProfile(browser()->profile()); | |
149 | |
150 // Return a mock socket object as a successful result to the create service | |
151 // call. | |
152 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); | |
153 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket | |
154 = new testing::StrictMock<MockBluetoothSocket>(); | |
155 BluetoothAdapter::ServiceOptions service_options; | |
156 service_options.name.reset(new std::string("MyServiceName")); | |
157 EXPECT_CALL( | |
158 *mock_adapter_.get(), | |
159 CreateRfcommService( | |
160 service_uuid, | |
161 testing::Field(&BluetoothAdapter::ServiceOptions::name, | |
162 testing::Pointee(testing::Eq("MyServiceName"))), | |
163 testing::_, | |
164 testing::_)).WillOnce(InvokeCallbackArgument<2>(mock_server_socket)); | |
165 | |
166 // Since the socket is unpaused, expect a call to Accept() from the socket | |
167 // dispatcher. We'll immediately send back another mock socket to represent | |
168 // the client API. Further calls will return no data and behave as if | |
169 // pending. | |
170 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_client_socket | |
171 = new testing::StrictMock<MockBluetoothSocket>(); | |
172 EXPECT_CALL(*mock_server_socket.get(), Accept(testing::_, testing::_)) | |
173 .Times(2) | |
174 .WillOnce( | |
175 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) | |
176 .WillOnce(testing::Return()); | |
177 | |
178 // Run the test, it sends a ready signal once it's ready for us to dispatch | |
179 // a client connection to it. | |
180 ExtensionTestMessageListener socket_listening("ready", true); | |
181 scoped_refptr<const Extension> extension( | |
182 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/listen"))); | |
183 ASSERT_TRUE(extension.get()); | |
184 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); | |
185 | |
186 // Connection events are dispatched using a couple of PostTask to the UI | |
187 // thread. Waiting until idle ensures the event is dispatched to the | |
188 // receiver(s). | |
189 base::RunLoop().RunUntilIdle(); | |
190 ExtensionTestMessageListener listener("ready", true); | |
191 socket_listening.Reply("go"); | |
192 | |
193 // Second stage of tests checks for error conditions, and will clean up | |
194 // the existing server and client sockets. | |
195 EXPECT_CALL(*mock_server_socket.get(), Disconnect(testing::_)) | |
196 .WillOnce(InvokeCallbackArgument<0>()); | |
197 EXPECT_CALL(*mock_server_socket.get(), Close()); | |
198 | |
199 EXPECT_CALL(*mock_client_socket.get(), Disconnect(testing::_)) | |
200 .WillOnce(InvokeCallbackArgument<0>()); | |
201 EXPECT_CALL(*mock_client_socket.get(), Close()); | |
202 | |
203 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
204 listener.Reply("go"); | |
205 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
206 } | |
207 | |
208 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { | |
209 ResultCatcher catcher; | |
210 catcher.RestrictToProfile(browser()->profile()); | |
211 | |
212 // Run the test. | |
213 scoped_refptr<const Extension> extension( | |
214 LoadExtension(test_data_dir_.AppendASCII( | |
215 "bluetooth_socket/permission_denied"))); | |
216 ASSERT_TRUE(extension.get()); | |
217 | |
218 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
219 } | |
OLD | NEW |