OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "base/run_loop.h" |
10 #include "chrome/browser/extensions/extension_service.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/test/base/ui_test_utils.h" | |
13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
14 #include "device/bluetooth/bluetooth_uuid.h" | 11 #include "device/bluetooth/bluetooth_uuid.h" |
15 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 12 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
16 #include "device/bluetooth/test/mock_bluetooth_device.h" | 13 #include "device/bluetooth/test/mock_bluetooth_device.h" |
17 #include "device/bluetooth/test/mock_bluetooth_socket.h" | 14 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
18 #include "extensions/browser/api/bluetooth_socket/bluetooth_socket_api.h" | 15 #include "extensions/browser/api/bluetooth_socket/bluetooth_socket_api.h" |
19 #include "extensions/common/test_util.h" | 16 #include "extensions/common/test_util.h" |
| 17 #include "extensions/shell/test/shell_apitest.h" |
20 #include "extensions/test/extension_test_message_listener.h" | 18 #include "extensions/test/extension_test_message_listener.h" |
21 #include "extensions/test/result_catcher.h" | 19 #include "extensions/test/result_catcher.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
23 | 21 |
24 using device::BluetoothAdapter; | 22 using device::BluetoothAdapter; |
25 using device::BluetoothAdapterFactory; | 23 using device::BluetoothAdapterFactory; |
26 using device::BluetoothDevice; | 24 using device::BluetoothDevice; |
27 using device::BluetoothSocket; | 25 using device::BluetoothSocket; |
28 using device::BluetoothUUID; | 26 using device::BluetoothUUID; |
29 using device::MockBluetoothAdapter; | 27 using device::MockBluetoothAdapter; |
30 using device::MockBluetoothDevice; | 28 using device::MockBluetoothDevice; |
31 using device::MockBluetoothSocket; | 29 using device::MockBluetoothSocket; |
32 using extensions::Extension; | 30 using extensions::Extension; |
33 using extensions::ResultCatcher; | 31 using extensions::ResultCatcher; |
34 | 32 |
35 namespace api = extensions::core_api; | 33 namespace api = extensions::core_api; |
36 | 34 |
37 namespace { | 35 namespace { |
38 | 36 |
39 class BluetoothSocketApiTest : public ExtensionApiTest { | 37 class BluetoothSocketApiTest : public extensions::ShellApiTest { |
40 public: | 38 public: |
41 BluetoothSocketApiTest() {} | 39 BluetoothSocketApiTest() {} |
42 | 40 |
43 void SetUpOnMainThread() override { | 41 void SetUpOnMainThread() override { |
44 ExtensionApiTest::SetUpOnMainThread(); | 42 ShellApiTest::SetUpOnMainThread(); |
45 empty_extension_ = extensions::test_util::CreateEmptyExtension(); | 43 empty_extension_ = extensions::test_util::CreateEmptyExtension(); |
46 SetUpMockAdapter(); | 44 SetUpMockAdapter(); |
47 } | 45 } |
48 | 46 |
49 void SetUpMockAdapter() { | 47 void SetUpMockAdapter() { |
50 // The browser will clean this up when it is torn down. | 48 // The browser will clean this up when it is torn down. |
51 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); | 49 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>(); |
52 BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter_); | 50 BluetoothAdapterFactory::SetAdapterForTesting(mock_adapter_); |
53 | 51 |
54 mock_device1_.reset( | 52 mock_device1_.reset( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 ACTION_TEMPLATE(InvokeCallbackArgument, | 92 ACTION_TEMPLATE(InvokeCallbackArgument, |
95 HAS_1_TEMPLATE_PARAMS(int, k), | 93 HAS_1_TEMPLATE_PARAMS(int, k), |
96 AND_2_VALUE_PARAMS(p0, p1)) { | 94 AND_2_VALUE_PARAMS(p0, p1)) { |
97 ::std::tr1::get<k>(args).Run(p0, p1); | 95 ::std::tr1::get<k>(args).Run(p0, p1); |
98 } | 96 } |
99 | 97 |
100 } // namespace | 98 } // namespace |
101 | 99 |
102 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { | 100 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { |
103 ResultCatcher catcher; | 101 ResultCatcher catcher; |
104 catcher.RestrictToBrowserContext(browser()->profile()); | 102 catcher.RestrictToBrowserContext(browser_context()); |
105 | 103 |
106 // Return the right mock device object for the address used by the test, | 104 // Return the right mock device object for the address used by the test, |
107 // return NULL for the "Device not found" test. | 105 // return NULL for the "Device not found" test. |
108 EXPECT_CALL(*mock_adapter_.get(), GetDevice(mock_device1_->GetAddress())) | 106 EXPECT_CALL(*mock_adapter_.get(), GetDevice(mock_device1_->GetAddress())) |
109 .WillRepeatedly(testing::Return(mock_device1_.get())); | 107 .WillRepeatedly(testing::Return(mock_device1_.get())); |
110 EXPECT_CALL(*mock_adapter_.get(), GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) | 108 EXPECT_CALL(*mock_adapter_.get(), GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) |
111 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); | 109 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); |
112 | 110 |
113 // Return a mock socket object as a successful result to the connect() call. | 111 // Return a mock socket object as a successful result to the connect() call. |
114 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); | 112 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); |
115 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket | 113 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket |
116 = new testing::StrictMock<MockBluetoothSocket>(); | 114 = new testing::StrictMock<MockBluetoothSocket>(); |
117 EXPECT_CALL(*mock_device1_, | 115 EXPECT_CALL(*mock_device1_, |
118 ConnectToService(service_uuid, testing::_, testing::_)) | 116 ConnectToService(service_uuid, testing::_, testing::_)) |
119 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); | 117 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); |
120 | 118 |
121 // Since the socket is unpaused, expect a call to Receive() from the socket | 119 // Since the socket is unpaused, expect a call to Receive() from the socket |
122 // dispatcher. Since there is no data, this will not call its callback. | 120 // dispatcher. Since there is no data, this will not call its callback. |
123 EXPECT_CALL(*mock_socket.get(), Receive(testing::_, testing::_, testing::_)); | 121 EXPECT_CALL(*mock_socket.get(), Receive(testing::_, testing::_, testing::_)); |
124 | 122 |
125 // The test also cleans up by calling Disconnect and Close. | 123 // The test also cleans up by calling Disconnect and Close. |
126 EXPECT_CALL(*mock_socket.get(), Disconnect(testing::_)) | 124 EXPECT_CALL(*mock_socket.get(), Disconnect(testing::_)) |
127 .WillOnce(InvokeCallbackArgument<0>()); | 125 .WillOnce(InvokeCallbackArgument<0>()); |
128 EXPECT_CALL(*mock_socket.get(), Close()); | 126 EXPECT_CALL(*mock_socket.get(), Close()); |
129 | 127 |
130 // Run the test. | 128 // Run the test. |
131 ExtensionTestMessageListener listener("ready", true); | 129 ExtensionTestMessageListener listener("ready", true); |
132 scoped_refptr<const Extension> extension( | 130 scoped_refptr<const Extension> extension( |
133 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/connect"))); | 131 LoadApp("api_test/bluetooth_socket/connect")); |
134 ASSERT_TRUE(extension.get()); | 132 ASSERT_TRUE(extension.get()); |
135 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 133 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
136 | 134 |
137 listener.Reply("go"); | 135 listener.Reply("go"); |
138 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 136 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
139 } | 137 } |
140 | 138 |
141 #if defined(_LIBCPP_VERSION) | 139 #if defined(_LIBCPP_VERSION) |
142 // This test fails in libc++ builds, see http://crbug.com/392205. | 140 // This test fails in libc++ builds, see http://crbug.com/392205. |
143 #define MAYBE_Listen DISABLED_Listen | 141 #define MAYBE_Listen DISABLED_Listen |
144 #else | 142 #else |
145 #define MAYBE_Listen Listen | 143 #define MAYBE_Listen Listen |
146 #endif | 144 #endif |
147 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, MAYBE_Listen) { | 145 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, MAYBE_Listen) { |
148 ResultCatcher catcher; | 146 ResultCatcher catcher; |
149 catcher.RestrictToBrowserContext(browser()->profile()); | 147 catcher.RestrictToBrowserContext(browser_context()); |
150 | 148 |
151 // Return a mock socket object as a successful result to the create service | 149 // Return a mock socket object as a successful result to the create service |
152 // call. | 150 // call. |
153 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); | 151 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); |
154 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket | 152 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket |
155 = new testing::StrictMock<MockBluetoothSocket>(); | 153 = new testing::StrictMock<MockBluetoothSocket>(); |
156 BluetoothAdapter::ServiceOptions service_options; | 154 BluetoothAdapter::ServiceOptions service_options; |
157 service_options.name.reset(new std::string("MyServiceName")); | 155 service_options.name.reset(new std::string("MyServiceName")); |
158 EXPECT_CALL( | 156 EXPECT_CALL( |
159 *mock_adapter_.get(), | 157 *mock_adapter_.get(), |
(...skipping 13 matching lines...) Expand all Loading... |
173 EXPECT_CALL(*mock_server_socket.get(), Accept(testing::_, testing::_)) | 171 EXPECT_CALL(*mock_server_socket.get(), Accept(testing::_, testing::_)) |
174 .Times(2) | 172 .Times(2) |
175 .WillOnce( | 173 .WillOnce( |
176 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) | 174 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) |
177 .WillOnce(testing::Return()); | 175 .WillOnce(testing::Return()); |
178 | 176 |
179 // Run the test, it sends a ready signal once it's ready for us to dispatch | 177 // Run the test, it sends a ready signal once it's ready for us to dispatch |
180 // a client connection to it. | 178 // a client connection to it. |
181 ExtensionTestMessageListener socket_listening("ready", true); | 179 ExtensionTestMessageListener socket_listening("ready", true); |
182 scoped_refptr<const Extension> extension( | 180 scoped_refptr<const Extension> extension( |
183 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/listen"))); | 181 LoadApp("api_test/bluetooth_socket/listen")); |
184 ASSERT_TRUE(extension.get()); | 182 ASSERT_TRUE(extension.get()); |
185 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); | 183 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); |
186 | 184 |
187 // Connection events are dispatched using a couple of PostTask to the UI | 185 // Connection events are dispatched using a couple of PostTask to the UI |
188 // thread. Waiting until idle ensures the event is dispatched to the | 186 // thread. Waiting until idle ensures the event is dispatched to the |
189 // receiver(s). | 187 // receiver(s). |
190 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
191 ExtensionTestMessageListener listener("ready", true); | 189 ExtensionTestMessageListener listener("ready", true); |
192 socket_listening.Reply("go"); | 190 socket_listening.Reply("go"); |
193 | 191 |
194 // Second stage of tests checks for error conditions, and will clean up | 192 // Second stage of tests checks for error conditions, and will clean up |
195 // the existing server and client sockets. | 193 // the existing server and client sockets. |
196 EXPECT_CALL(*mock_server_socket.get(), Disconnect(testing::_)) | 194 EXPECT_CALL(*mock_server_socket.get(), Disconnect(testing::_)) |
197 .WillOnce(InvokeCallbackArgument<0>()); | 195 .WillOnce(InvokeCallbackArgument<0>()); |
198 EXPECT_CALL(*mock_server_socket.get(), Close()); | 196 EXPECT_CALL(*mock_server_socket.get(), Close()); |
199 | 197 |
200 EXPECT_CALL(*mock_client_socket.get(), Disconnect(testing::_)) | 198 EXPECT_CALL(*mock_client_socket.get(), Disconnect(testing::_)) |
201 .WillOnce(InvokeCallbackArgument<0>()); | 199 .WillOnce(InvokeCallbackArgument<0>()); |
202 EXPECT_CALL(*mock_client_socket.get(), Close()); | 200 EXPECT_CALL(*mock_client_socket.get(), Close()); |
203 | 201 |
204 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 202 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
205 listener.Reply("go"); | 203 listener.Reply("go"); |
206 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 204 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
207 } | 205 } |
208 | 206 |
209 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { | 207 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { |
210 ResultCatcher catcher; | 208 ResultCatcher catcher; |
211 catcher.RestrictToBrowserContext(browser()->profile()); | 209 catcher.RestrictToBrowserContext(browser_context()); |
212 | 210 |
213 // Run the test. | 211 // Run the test. |
214 scoped_refptr<const Extension> extension( | 212 scoped_refptr<const Extension> extension( |
215 LoadExtension(test_data_dir_.AppendASCII( | 213 LoadApp("api_test/bluetooth_socket/permission_denied")); |
216 "bluetooth_socket/permission_denied"))); | |
217 ASSERT_TRUE(extension.get()); | 214 ASSERT_TRUE(extension.get()); |
218 | 215 |
219 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 216 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
220 } | 217 } |
OLD | NEW |