Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: components/proximity_auth/bluetooth_connection_unittest.cc

Issue 1359043005: Fix BluetoothConnection to disconnect when the underlying device disconnects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proximity_auth_system2
Patch Set: fix test Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/proximity_auth/bluetooth_connection.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/proximity_auth/bluetooth_connection.h" 5 #include "components/proximity_auth/bluetooth_connection.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/numerics/safe_conversions.h" 8 #include "base/numerics/safe_conversions.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "components/proximity_auth/remote_device.h" 10 #include "components/proximity_auth/remote_device.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 MOCK_METHOD2(OnDidSendMessage, 66 MOCK_METHOD2(OnDidSendMessage,
67 void(const WireMessage& message, bool success)); 67 void(const WireMessage& message, bool success));
68 68
69 void SetStatus(Status status) override { 69 void SetStatus(Status status) override {
70 SetStatusProxy(status); 70 SetStatusProxy(status);
71 BluetoothConnection::SetStatus(status); 71 BluetoothConnection::SetStatus(status);
72 } 72 }
73 73
74 using BluetoothConnection::status; 74 using BluetoothConnection::status;
75 using BluetoothConnection::Connect; 75 using BluetoothConnection::Connect;
76 using BluetoothConnection::DeviceChanged;
76 using BluetoothConnection::DeviceRemoved; 77 using BluetoothConnection::DeviceRemoved;
77 using BluetoothConnection::Disconnect; 78 using BluetoothConnection::Disconnect;
78 79
79 private: 80 private:
80 RemoteDevice CreateRemoteDevice() { 81 RemoteDevice CreateRemoteDevice() {
81 return RemoteDevice(kDeviceUserId, kDeviceName, kPublicKey, 82 return RemoteDevice(kDeviceUserId, kDeviceName, kPublicKey,
82 kBluetoothAddress, kPersistentSymmetricKey); 83 kBluetoothAddress, kPersistentSymmetricKey);
83 } 84 }
84 85
85 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection); 86 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnection);
(...skipping 21 matching lines...) Expand all
107 uuid_(kUuid) { 108 uuid_(kUuid) {
108 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); 109 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
109 110
110 // Suppress uninteresting Gmock call warnings. 111 // Suppress uninteresting Gmock call warnings.
111 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber()); 112 EXPECT_CALL(*adapter_, GetDevice(_)).Times(AnyNumber());
112 } 113 }
113 114
114 // Transition the connection into an in-progress state. 115 // Transition the connection into an in-progress state.
115 void BeginConnecting(MockBluetoothConnection* connection) { 116 void BeginConnecting(MockBluetoothConnection* connection) {
116 EXPECT_EQ(Connection::DISCONNECTED, connection->status()); 117 EXPECT_EQ(Connection::DISCONNECTED, connection->status());
118 ON_CALL(device_, IsConnected()).WillByDefault(Return(false));
117 119
118 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_)); 120 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(&device_));
119 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS)); 121 EXPECT_CALL(*connection, SetStatusProxy(Connection::IN_PROGRESS));
120 EXPECT_CALL(*adapter_, AddObserver(connection)); 122 EXPECT_CALL(*adapter_, AddObserver(connection));
121 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _)); 123 EXPECT_CALL(device_, ConnectToServiceInsecurely(uuid_, _, _));
122 connection->Connect(); 124 connection->Connect();
123 125
124 EXPECT_EQ(Connection::IN_PROGRESS, connection->status()); 126 EXPECT_EQ(Connection::IN_PROGRESS, connection->status());
125 } 127 }
126 128
(...skipping 12 matching lines...) Expand all
139 connection->Connect(); 141 connection->Connect();
140 ASSERT_FALSE(callback.is_null()); 142 ASSERT_FALSE(callback.is_null());
141 143
142 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED)); 144 EXPECT_CALL(*connection, SetStatusProxy(Connection::CONNECTED));
143 EXPECT_CALL(*socket_, Receive(_, _, _)) 145 EXPECT_CALL(*socket_, Receive(_, _, _))
144 .WillOnce(DoAll(SaveArg<1>(&receive_callback_), 146 .WillOnce(DoAll(SaveArg<1>(&receive_callback_),
145 SaveArg<2>(&receive_error_callback_))); 147 SaveArg<2>(&receive_error_callback_)));
146 callback.Run(socket_); 148 callback.Run(socket_);
147 149
148 EXPECT_EQ(Connection::CONNECTED, connection->status()); 150 EXPECT_EQ(Connection::CONNECTED, connection->status());
151 ON_CALL(device_, IsConnected()).WillByDefault(Return(true));
149 } 152 }
150 153
151 device::BluetoothSocket::ReceiveCompletionCallback* receive_callback() { 154 device::BluetoothSocket::ReceiveCompletionCallback* receive_callback() {
152 return &receive_callback_; 155 return &receive_callback_;
153 } 156 }
154 device::BluetoothSocket::ReceiveErrorCompletionCallback* 157 device::BluetoothSocket::ReceiveErrorCompletionCallback*
155 receive_error_callback() { 158 receive_error_callback() {
156 return &receive_error_callback_; 159 return &receive_error_callback_;
157 } 160 }
158 161
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 connection.SendMessage(wire_message.Pass()); 452 connection.SendMessage(wire_message.Pass());
450 453
451 ASSERT_FALSE(error_callback.is_null()); 454 ASSERT_FALSE(error_callback.is_null());
452 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false)); 455 EXPECT_CALL(connection, OnDidSendMessage(Ref(*expected_wire_message), false));
453 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED)); 456 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED));
454 EXPECT_CALL(*socket_, Disconnect(_)); 457 EXPECT_CALL(*socket_, Disconnect(_));
455 EXPECT_CALL(*adapter_, RemoveObserver(&connection)); 458 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
456 error_callback.Run("The most helpful of error messages"); 459 error_callback.Run("The most helpful of error messages");
457 } 460 }
458 461
462 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_Disconnected) {
463 // Create a connected connection.
464 StrictMock<MockBluetoothConnection> connection;
465 Connect(&connection);
466 EXPECT_TRUE(connection.IsConnected());
467
468 // If the remote device disconnects, |connection| should also disconnect.
469 ON_CALL(device_, IsConnected()).WillByDefault(Return(false));
470 EXPECT_CALL(connection, SetStatusProxy(Connection::DISCONNECTED));
471 EXPECT_CALL(*socket_, Disconnect(_));
472 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
473 connection.DeviceChanged(adapter_.get(), &device_);
474 EXPECT_FALSE(connection.IsConnected());
475 }
476
477 TEST_F(ProximityAuthBluetoothConnectionTest, DeviceChanged_NotDisconnected) {
478 // Nothing should happen if DeviceChanged is called, but the remote device is
479 // not disconnected.
480 StrictMock<MockBluetoothConnection> connection;
481 Connect(&connection);
482 EXPECT_TRUE(connection.IsConnected());
483 connection.DeviceChanged(adapter_.get(), &device_);
484 EXPECT_TRUE(connection.IsConnected());
485
486 // The connection disconnects and unregisters as an observer upon destruction.
487 EXPECT_CALL(*socket_, Disconnect(_));
488 EXPECT_CALL(*adapter_, RemoveObserver(&connection));
489 }
490
459 } // namespace proximity_auth 491 } // namespace proximity_auth
OLDNEW
« no previous file with comments | « components/proximity_auth/bluetooth_connection.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698