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/api/bluetooth_socket/bluetooth_socket_api.h" | 9 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" |
10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 89 } |
90 | 90 |
91 } // namespace | 91 } // namespace |
92 | 92 |
93 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { | 93 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, Connect) { |
94 ResultCatcher catcher; | 94 ResultCatcher catcher; |
95 catcher.RestrictToProfile(browser()->profile()); | 95 catcher.RestrictToProfile(browser()->profile()); |
96 | 96 |
97 // Return the right mock device object for the address used by the test, | 97 // Return the right mock device object for the address used by the test, |
98 // return NULL for the "Device not found" test. | 98 // return NULL for the "Device not found" test. |
99 EXPECT_CALL(*mock_adapter_, GetDevice(mock_device1_->GetAddress())) | 99 EXPECT_CALL(*mock_adapter_.get(), GetDevice(mock_device1_->GetAddress())) |
100 .WillRepeatedly(testing::Return(mock_device1_.get())); | 100 .WillRepeatedly(testing::Return(mock_device1_.get())); |
101 EXPECT_CALL(*mock_adapter_, GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) | 101 EXPECT_CALL(*mock_adapter_.get(), GetDevice(std::string("aa:aa:aa:aa:aa:aa"))) |
102 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); | 102 .WillOnce(testing::Return(static_cast<BluetoothDevice*>(NULL))); |
103 | 103 |
104 // Return a mock socket object as a successful result to the connect() call. | 104 // Return a mock socket object as a successful result to the connect() call. |
105 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); | 105 BluetoothUUID service_uuid("8e3ad063-db38-4289-aa8f-b30e4223cf40"); |
106 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket | 106 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_socket |
107 = new testing::StrictMock<MockBluetoothSocket>(); | 107 = new testing::StrictMock<MockBluetoothSocket>(); |
108 EXPECT_CALL(*mock_device1_, | 108 EXPECT_CALL(*mock_device1_, |
109 ConnectToService(service_uuid, testing::_, testing::_)) | 109 ConnectToService(service_uuid, testing::_, testing::_)) |
110 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); | 110 .WillOnce(InvokeCallbackArgument<1>(mock_socket)); |
111 | 111 |
112 // Since the socket is unpaused, expect a call to Receive() from the socket | 112 // Since the socket is unpaused, expect a call to Receive() from the socket |
113 // dispatcher. Since there is no data, this will not call its callback. | 113 // dispatcher. Since there is no data, this will not call its callback. |
114 EXPECT_CALL(*mock_socket, Receive(testing::_, testing::_, testing::_)); | 114 EXPECT_CALL(*mock_socket.get(), Receive(testing::_, testing::_, testing::_)); |
115 | 115 |
116 // The test also cleans up by calling Disconnect and Close. | 116 // The test also cleans up by calling Disconnect and Close. |
117 EXPECT_CALL(*mock_socket, Disconnect(testing::_)) | 117 EXPECT_CALL(*mock_socket.get(), Disconnect(testing::_)) |
118 .WillOnce(InvokeCallbackArgument<0>()); | 118 .WillOnce(InvokeCallbackArgument<0>()); |
119 EXPECT_CALL(*mock_socket, Close()); | 119 EXPECT_CALL(*mock_socket.get(), Close()); |
120 | 120 |
121 // Run the test. | 121 // Run the test. |
122 ExtensionTestMessageListener listener("ready", true); | 122 ExtensionTestMessageListener listener("ready", true); |
123 scoped_refptr<const Extension> extension( | 123 scoped_refptr<const Extension> extension( |
124 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/connect"))); | 124 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/connect"))); |
125 ASSERT_TRUE(extension.get()); | 125 ASSERT_TRUE(extension.get()); |
126 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 126 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
127 | 127 |
128 listener.Reply("go"); | 128 listener.Reply("go"); |
129 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 129 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
(...skipping 10 matching lines...) Expand all Loading... |
140 catcher.RestrictToProfile(browser()->profile()); | 140 catcher.RestrictToProfile(browser()->profile()); |
141 | 141 |
142 // Return a mock socket object as a successful result to the create service | 142 // Return a mock socket object as a successful result to the create service |
143 // call. | 143 // call. |
144 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); | 144 BluetoothUUID service_uuid("2de497f9-ab28-49db-b6d2-066ea69f1737"); |
145 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket | 145 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_server_socket |
146 = new testing::StrictMock<MockBluetoothSocket>(); | 146 = new testing::StrictMock<MockBluetoothSocket>(); |
147 BluetoothAdapter::ServiceOptions service_options; | 147 BluetoothAdapter::ServiceOptions service_options; |
148 service_options.name.reset(new std::string("MyServiceName")); | 148 service_options.name.reset(new std::string("MyServiceName")); |
149 EXPECT_CALL( | 149 EXPECT_CALL( |
150 *mock_adapter_, | 150 *mock_adapter_.get(), |
151 CreateRfcommService( | 151 CreateRfcommService( |
152 service_uuid, | 152 service_uuid, |
153 testing::Field(&BluetoothAdapter::ServiceOptions::name, | 153 testing::Field(&BluetoothAdapter::ServiceOptions::name, |
154 testing::Pointee(testing::Eq("MyServiceName"))), | 154 testing::Pointee(testing::Eq("MyServiceName"))), |
155 testing::_, testing::_)) | 155 testing::_, |
156 .WillOnce(InvokeCallbackArgument<2>(mock_server_socket)); | 156 testing::_)).WillOnce(InvokeCallbackArgument<2>(mock_server_socket)); |
157 | 157 |
158 // Since the socket is unpaused, expect a call to Accept() from the socket | 158 // Since the socket is unpaused, expect a call to Accept() from the socket |
159 // dispatcher. We'll immediately send back another mock socket to represent | 159 // dispatcher. We'll immediately send back another mock socket to represent |
160 // the client API. Further calls will return no data and behave as if | 160 // the client API. Further calls will return no data and behave as if |
161 // pending. | 161 // pending. |
162 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_client_socket | 162 scoped_refptr<testing::StrictMock<MockBluetoothSocket> > mock_client_socket |
163 = new testing::StrictMock<MockBluetoothSocket>(); | 163 = new testing::StrictMock<MockBluetoothSocket>(); |
164 EXPECT_CALL(*mock_server_socket, Accept(testing::_, testing::_)) | 164 EXPECT_CALL(*mock_server_socket.get(), Accept(testing::_, testing::_)) |
165 .Times(2) | 165 .Times(2) |
166 .WillOnce(InvokeCallbackArgument<0>(mock_device1_.get(), | 166 .WillOnce( |
167 mock_client_socket)) | 167 InvokeCallbackArgument<0>(mock_device1_.get(), mock_client_socket)) |
168 .WillOnce(testing::Return()); | 168 .WillOnce(testing::Return()); |
169 | 169 |
170 // Run the test, it sends a ready signal once it's ready for us to dispatch | 170 // Run the test, it sends a ready signal once it's ready for us to dispatch |
171 // a client connection to it. | 171 // a client connection to it. |
172 ExtensionTestMessageListener socket_listening("ready", true); | 172 ExtensionTestMessageListener socket_listening("ready", true); |
173 scoped_refptr<const Extension> extension( | 173 scoped_refptr<const Extension> extension( |
174 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/listen"))); | 174 LoadExtension(test_data_dir_.AppendASCII("bluetooth_socket/listen"))); |
175 ASSERT_TRUE(extension.get()); | 175 ASSERT_TRUE(extension.get()); |
176 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); | 176 EXPECT_TRUE(socket_listening.WaitUntilSatisfied()); |
177 | 177 |
178 // Connection events are dispatched using a couple of PostTask to the UI | 178 // Connection events are dispatched using a couple of PostTask to the UI |
179 // thread. Waiting until idle ensures the event is dispatched to the | 179 // thread. Waiting until idle ensures the event is dispatched to the |
180 // receiver(s). | 180 // receiver(s). |
181 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
182 ExtensionTestMessageListener listener("ready", true); | 182 ExtensionTestMessageListener listener("ready", true); |
183 socket_listening.Reply("go"); | 183 socket_listening.Reply("go"); |
184 | 184 |
185 // Second stage of tests checks for error conditions, and will clean up | 185 // Second stage of tests checks for error conditions, and will clean up |
186 // the existing server and client sockets. | 186 // the existing server and client sockets. |
187 EXPECT_CALL(*mock_server_socket, Disconnect(testing::_)) | 187 EXPECT_CALL(*mock_server_socket.get(), Disconnect(testing::_)) |
188 .WillOnce(InvokeCallbackArgument<0>()); | 188 .WillOnce(InvokeCallbackArgument<0>()); |
189 EXPECT_CALL(*mock_server_socket, Close()); | 189 EXPECT_CALL(*mock_server_socket.get(), Close()); |
190 | 190 |
191 EXPECT_CALL(*mock_client_socket, Disconnect(testing::_)) | 191 EXPECT_CALL(*mock_client_socket.get(), Disconnect(testing::_)) |
192 .WillOnce(InvokeCallbackArgument<0>()); | 192 .WillOnce(InvokeCallbackArgument<0>()); |
193 EXPECT_CALL(*mock_client_socket, Close()); | 193 EXPECT_CALL(*mock_client_socket.get(), Close()); |
194 | 194 |
195 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 195 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
196 listener.Reply("go"); | 196 listener.Reply("go"); |
197 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 197 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
198 } | 198 } |
199 | 199 |
200 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { | 200 IN_PROC_BROWSER_TEST_F(BluetoothSocketApiTest, PermissionDenied) { |
201 ResultCatcher catcher; | 201 ResultCatcher catcher; |
202 catcher.RestrictToProfile(browser()->profile()); | 202 catcher.RestrictToProfile(browser()->profile()); |
203 | 203 |
204 // Run the test. | 204 // Run the test. |
205 scoped_refptr<const Extension> extension( | 205 scoped_refptr<const Extension> extension( |
206 LoadExtension(test_data_dir_.AppendASCII( | 206 LoadExtension(test_data_dir_.AppendASCII( |
207 "bluetooth_socket/permission_denied"))); | 207 "bluetooth_socket/permission_denied"))); |
208 ASSERT_TRUE(extension.get()); | 208 ASSERT_TRUE(extension.get()); |
209 | 209 |
210 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 210 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
211 } | 211 } |
OLD | NEW |