| 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "device/bluetooth/bluetooth_adapter_factory.h" | 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } | 96 } |
| 97 | 97 |
| 98 } // namespace | 98 } // namespace |
| 99 | 99 |
| 100 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { | 100 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { |
| 101 ResultCatcher catcher; | 101 ResultCatcher catcher; |
| 102 catcher.RestrictToBrowserContext(browser_context()); | 102 catcher.RestrictToBrowserContext(browser_context()); |
| 103 | 103 |
| 104 // 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, |
| 105 // return NULL for the "Device not found" test. | 105 // return NULL for the "Device not found" test. |
| 106 EXPECT_CALL(*mock_adapter_.get(), GetDevice(mock_device1_->GetAddress())) | 106 EXPECT_CALL(*mock_adapter_, GetDevice(mock_device1_->GetAddress())) |
| 107 .WillRepeatedly(testing::Return(mock_device1_.get())); | 107 .WillRepeatedly(testing::Return(mock_device1_.get())); |
| 108 EXPECT_CALL(*mock_adapter_.get(), GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) | 108 EXPECT_CALL(*mock_adapter_, GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) |
| 109 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); | 109 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); |
| 110 | 110 |
| 111 // 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. |
| 112 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); | 112 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); |
| 113 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket | 113 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket |
| 114 = new testing::StrictMock<MockBluetoothSocket>(); | 114 = new testing::StrictMock<MockBluetoothSocket>(); |
| 115 EXPECT_CALL(*mock_device1_, | 115 EXPECT_CALL(*mock_device1_, |
| 116 ConnectToService(service_uuid, testing::_, testing::_)) | 116 ConnectToService(service_uuid, testing::_, testing::_)) |
| 117 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); | 117 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); |
| 118 | 118 |
| 119 // 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 |
| 120 // 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. |
| 121 EXPECT_CALL(*mock_socket.get(), Receive(testing::_, testing::_, testing::_)); | 121 EXPECT_CALL(*mock_socket, Receive(testing::_, testing::_, testing::_)); |
| 122 | 122 |
| 123 // The test also cleans up by calling Disconnect and Close. | 123 // The test also cleans up by calling Disconnect and Close. |
| 124 EXPECT_CALL(*mock_socket.get(), Disconnect(testing::_)) | 124 EXPECT_CALL(*mock_socket, Disconnect(testing::_)) |
| 125 .WillOnce(InvokeCallbackArgument<0>()); | 125 .WillOnce(InvokeCallbackArgument<0>()); |
| 126 EXPECT_CALL(*mock_socket.get(), Close()); | 126 EXPECT_CALL(*mock_socket, Close()); |
| 127 | 127 |
| 128 // Run the test. | 128 // Run the test. |
| 129 ExtensionTestMessageListener listener("ready", true); | 129 ExtensionTestMessageListener listener("ready", true); |
| 130 scoped_refptr<const Extension> extension( | 130 scoped_refptr<const Extension> extension( |
| 131 LoadApp("api_test/bluetooth_socket/connect")); | 131 LoadApp("api_test/bluetooth_socket/connect")); |
| 132 ASSERT_TRUE(extension.get()); | 132 ASSERT_TRUE(extension.get()); |
| 133 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 133 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 134 | 134 |
| 135 listener.Reply("go"); | 135 listener.Reply("go"); |
| 136 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 136 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 147 catcher.RestrictToBrowserContext(browser_context()); | 147 catcher.RestrictToBrowserContext(browser_context()); |
| 148 | 148 |
| 149 // 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 |
| 150 // call. | 150 // call. |
| 151 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); | 151 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); |
| 152 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket | 152 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket |
| 153 = new testing::StrictMock<MockBluetoothSocket>(); | 153 = new testing::StrictMock<MockBluetoothSocket>(); |
| 154 BluetoothAdapter::ServiceOptions service_options; | 154 BluetoothAdapter::ServiceOptions service_options; |
| 155 service_options.name.reset(new std::string("MyServiceName")); | 155 service_options.name.reset(new std::string("MyServiceName")); |
| 156 EXPECT_CALL( | 156 EXPECT_CALL( |
| 157 *mock_adapter_.get(), | 157 *mock_adapter_, |
| 158 CreateRfcommService( | 158 CreateRfcommService( |
| 159 service_uuid, | 159 service_uuid, |
| 160 testing::Field(&BluetoothAdapter::ServiceOptions::name, | 160 testing::Field(&BluetoothAdapter::ServiceOptions::name, |
| 161 testing::Pointee(testing::Eq("MyServiceName"))), | 161 testing::Pointee(testing::Eq("MyServiceName"))), |
| 162 testing::_, | 162 testing::_, testing::_)) |
| 163 testing::_)).WillOnce(InvokeCallbackArgument<2>(mock_server_socket)); | 163 .WillOnce(InvokeCallbackArgument<2>(mock_server_socket)); |
| 164 | 164 |
| 165 // Since the socket is unpaused, expect a call to Accept() from the socket | 165 // Since the socket is unpaused, expect a call to Accept() from the socket |
| 166 // dispatcher. We'll immediately send back another mock socket to represent | 166 // dispatcher. We'll immediately send back another mock socket to represent |
| 167 // the client API. Further calls will return no data and behave as if | 167 // the client API. Further calls will return no data and behave as if |
| 168 // pending. | 168 // pending. |
| 169 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_client_socket | 169 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_client_socket |
| 170 = new testing::StrictMock<MockBluetoothSocket>(); | 170 = new testing::StrictMock<MockBluetoothSocket>(); |
| 171 EXPECT_CALL(*mock_server_socket.get(), Accept(testing::_, testing::_)) | 171 EXPECT_CALL(*mock_server_socket, Accept(testing::_, testing::_)) |
| 172 .Times(2) | 172 .Times(2) |
| 173 .WillOnce( | 173 .WillOnce( |
| 174 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) | 174 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) |
| 175 .WillOnce(testing::Return()); | 175 .WillOnce(testing::Return()); |
| 176 | 176 |
| 177 // 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 |
| 178 // a client connection to it. | 178 // a client connection to it. |
| 179 ExtensionTestMessageListener socket_listening("ready", true); | 179 ExtensionTestMessageListener socket_listening("ready", true); |
| 180 scoped_refptr<const Extension> extension( | 180 scoped_refptr<const Extension> extension( |
| 181 LoadApp("api_test/bluetooth_socket/listen")); | 181 LoadApp("api_test/bluetooth_socket/listen")); |
| 182 ASSERT_TRUE(extension.get()); | 182 ASSERT_TRUE(extension.get()); |
| 183 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); | 183 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); |
| 184 | 184 |
| 185 // 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 |
| 186 // thread. Waiting until idle ensures the event is dispatched to the | 186 // thread. Waiting until idle ensures the event is dispatched to the |
| 187 // receiver(s). | 187 // receiver(s). |
| 188 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
| 189 ExtensionTestMessageListener listener("ready", true); | 189 ExtensionTestMessageListener listener("ready", true); |
| 190 socket_listening.Reply("go"); | 190 socket_listening.Reply("go"); |
| 191 | 191 |
| 192 // 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 |
| 193 // the existing server and client sockets. | 193 // the existing server and client sockets. |
| 194 EXPECT_CALL(*mock_server_socket.get(), Disconnect(testing::_)) | 194 EXPECT_CALL(*mock_server_socket, Disconnect(testing::_)) |
| 195 .WillOnce(InvokeCallbackArgument<0>()); | 195 .WillOnce(InvokeCallbackArgument<0>()); |
| 196 EXPECT_CALL(*mock_server_socket.get(), Close()); | 196 EXPECT_CALL(*mock_server_socket, Close()); |
| 197 | 197 |
| 198 EXPECT_CALL(*mock_client_socket.get(), Disconnect(testing::_)) | 198 EXPECT_CALL(*mock_client_socket, Disconnect(testing::_)) |
| 199 .WillOnce(InvokeCallbackArgument<0>()); | 199 .WillOnce(InvokeCallbackArgument<0>()); |
| 200 EXPECT_CALL(*mock_client_socket.get(), Close()); | 200 EXPECT_CALL(*mock_client_socket, Close()); |
| 201 | 201 |
| 202 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 202 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 203 listener.Reply("go"); | 203 listener.Reply("go"); |
| 204 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 204 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 205 } | 205 } |
| 206 | 206 |
| 207 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { | 207 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { |
| 208 ResultCatcher catcher; | 208 ResultCatcher catcher; |
| 209 catcher.RestrictToBrowserContext(browser_context()); | 209 catcher.RestrictToBrowserContext(browser_context()); |
| 210 | 210 |
| 211 // Run the test. | 211 // Run the test. |
| 212 scoped_refptr<const Extension> extension( | 212 scoped_refptr<const Extension> extension( |
| 213 LoadApp("api_test/bluetooth_socket/permission_denied")); | 213 LoadApp("api_test/bluetooth_socket/permission_denied")); |
| 214 ASSERT_TRUE(extension.get()); | 214 ASSERT_TRUE(extension.get()); |
| 215 | 215 |
| 216 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 216 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 217 } | 217 } |
| OLD | NEW |