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

Side by Side Diff: chrome/browser/chromeos/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed ASSERT_TRUE checks. Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
6 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_chromeos.h"
7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_factory.h"
6 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h"
7 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" 9 #include "chromeos/dbus/mock_bluetooth_adapter_client.h"
8 #include "chromeos/dbus/mock_bluetooth_device_client.h" 10 #include "chromeos/dbus/mock_bluetooth_device_client.h"
9 #include "chromeos/dbus/mock_bluetooth_manager_client.h" 11 #include "chromeos/dbus/mock_bluetooth_manager_client.h"
10 #include "chromeos/dbus/mock_dbus_thread_manager.h" 12 #include "chromeos/dbus/mock_dbus_thread_manager.h"
11 #include "dbus/object_path.h" 13 #include "dbus/object_path.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 15
14 using ::testing::_; 16 using ::testing::_;
15 using ::testing::Mock; 17 using ::testing::Mock;
16 using ::testing::Return; 18 using ::testing::Return;
17 using ::testing::SaveArg; 19 using ::testing::SaveArg;
18 20
19 namespace chromeos { 21 namespace chromeos {
20 22
21 class BluetoothAdapterDevicesTest : public testing::Test { 23 class BluetoothAdapterDevicesChromeOsTest : public testing::Test {
22 public: 24 public:
23 virtual void SetUp() { 25 virtual void SetUp() {
24 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; 26 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager;
25 27
26 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) 28 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus())
27 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); 29 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL)));
28 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); 30 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
29 31
30 mock_manager_client_ = 32 mock_manager_client_ =
31 mock_dbus_thread_manager->mock_bluetooth_manager_client(); 33 mock_dbus_thread_manager->mock_bluetooth_manager_client();
32 mock_adapter_client_ = 34 mock_adapter_client_ =
33 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); 35 mock_dbus_thread_manager->mock_bluetooth_adapter_client();
34 mock_device_client_ = 36 mock_device_client_ =
35 mock_dbus_thread_manager->mock_bluetooth_device_client(); 37 mock_dbus_thread_manager->mock_bluetooth_device_client();
36 38
37 // Create the default adapter instance; 39 // Create the default adapter instance;
38 // BluetoothManagerClient::DefaultAdapter will be called once, passing 40 // BluetoothManagerClient::DefaultAdapter will be called once, passing
39 // a callback to obtain the adapter path. 41 // a callback to obtain the adapter path.
40 BluetoothManagerClient::AdapterCallback adapter_callback; 42 BluetoothManagerClient::AdapterCallback adapter_callback;
41 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) 43 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
42 .WillOnce(SaveArg<0>(&adapter_callback)); 44 .WillOnce(SaveArg<0>(&adapter_callback));
43 45
44 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) 46 EXPECT_CALL(*mock_manager_client_, AddObserver(_))
45 .Times(1); 47 .Times(1);
46 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) 48 EXPECT_CALL(*mock_adapter_client_, AddObserver(_))
47 .Times(1); 49 .Times(1);
48 50
49 adapter_ = BluetoothAdapter::DefaultAdapter(); 51 adapter_ = BluetoothAdapterFactory::DefaultAdapter();
50 52
51 // Call the adapter callback; 53 // Call the adapter callback;
52 // BluetoothAdapterClient::GetProperties will be called once to obtain 54 // BluetoothAdapterClient::GetProperties will be called once to obtain
53 // the property set. 55 // the property set.
54 MockBluetoothAdapterClient::Properties adapter_properties; 56 MockBluetoothAdapterClient::Properties adapter_properties;
55 adapter_properties.address.ReplaceValue(adapter_address_); 57 adapter_properties.address.ReplaceValue(adapter_address_);
56 adapter_properties.powered.ReplaceValue(true); 58 adapter_properties.powered.ReplaceValue(true);
57 59
58 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) 60 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_))
59 .WillRepeatedly(Return(&adapter_properties)); 61 .WillRepeatedly(Return(&adapter_properties));
60 62
61 // Add an observer to the adapter; expect the usual set of changes to 63 // Add an observer to the adapter; expect the usual set of changes to
62 // an adapter becoming present and then clear to clean up for the test. 64 // an adapter becoming present and then clear to clean up for the test.
63 adapter_->AddObserver(&adapter_observer_); 65 adapter_->AddObserver(&adapter_observer_);
64 66
65 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true)) 67 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true))
66 .Times(1); 68 .Times(1);
67 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true)) 69 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true))
68 .Times(1); 70 .Times(1);
69 71
70 adapter_callback.Run(adapter_path_, true); 72 adapter_callback.Run(adapter_path_, true);
71 73
72 Mock::VerifyAndClearExpectations(mock_manager_client_); 74 Mock::VerifyAndClearExpectations(mock_manager_client_);
73 Mock::VerifyAndClearExpectations(mock_adapter_client_); 75 Mock::VerifyAndClearExpectations(mock_adapter_client_);
74 Mock::VerifyAndClearExpectations(mock_device_client_); 76 Mock::VerifyAndClearExpectations(mock_device_client_);
75 Mock::VerifyAndClearExpectations(&adapter_observer_); 77 Mock::VerifyAndClearExpectations(&adapter_observer_);
76 } 78 }
77 79
78 virtual void TearDown() { 80 virtual void TearDown() {
79 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_.get())) 81 BluetoothAdapterChromeOs* adapter_chromeos =
82 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
83 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_chromeos))
80 .Times(1); 84 .Times(1);
81 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_.get())) 85 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_chromeos))
82 .Times(1); 86 .Times(1);
83 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_.get())) 87 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_chromeos))
84 .Times(1); 88 .Times(1);
85 89
86 adapter_ = NULL; 90 adapter_ = NULL;
87 DBusThreadManager::Shutdown(); 91 DBusThreadManager::Shutdown();
88 } 92 }
89 93
90 protected: 94 protected:
91 MockBluetoothManagerClient* mock_manager_client_; 95 MockBluetoothManagerClient* mock_manager_client_;
92 MockBluetoothAdapterClient* mock_adapter_client_; 96 MockBluetoothAdapterClient* mock_adapter_client_;
93 MockBluetoothDeviceClient* mock_device_client_; 97 MockBluetoothDeviceClient* mock_device_client_;
94 98
95 static const dbus::ObjectPath adapter_path_; 99 static const dbus::ObjectPath adapter_path_;
96 static const std::string adapter_address_; 100 static const std::string adapter_address_;
97 scoped_refptr<BluetoothAdapter> adapter_; 101 scoped_refptr<BluetoothAdapter> adapter_;
98 102
99 MockBluetoothAdapter::Observer adapter_observer_; 103 MockBluetoothAdapter::Observer adapter_observer_;
100 }; 104 };
101 105
102 const dbus::ObjectPath BluetoothAdapterDevicesTest::adapter_path_( 106 const dbus::ObjectPath BluetoothAdapterDevicesChromeOsTest::adapter_path_(
103 "/fake/hci0"); 107 "/fake/hci0");
104 const std::string BluetoothAdapterDevicesTest::adapter_address_ = 108 const std::string BluetoothAdapterDevicesChromeOsTest::adapter_address_ =
105 "CA:FE:4A:C0:FE:FE"; 109 "CA:FE:4A:C0:FE:FE";
106 110
107 TEST_F(BluetoothAdapterDevicesTest, DeviceRemovedAfterFound) { 111 TEST_F(BluetoothAdapterDevicesChromeOsTest, DeviceRemovedAfterFound) {
108 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); 112 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01");
109 const std::string device_address = "BA:C0:11:00:00:01"; 113 const std::string device_address = "BA:C0:11:00:00:01";
110 114
111 MockBluetoothDeviceClient::Properties device_properties; 115 MockBluetoothDeviceClient::Properties device_properties;
112 device_properties.address.ReplaceValue(device_address); 116 device_properties.address.ReplaceValue(device_address);
113 device_properties.name.ReplaceValue("Fake Keyboard"); 117 device_properties.name.ReplaceValue("Fake Keyboard");
114 device_properties.bluetooth_class.ReplaceValue(0x2540); 118 device_properties.bluetooth_class.ReplaceValue(0x2540);
115 119
116 // needs to be a supported class 120 // needs to be a supported class
117 121
118 // should create a bluetooth device, 122 // should create a bluetooth device,
119 // put it in the hash map, 123 // put it in the hash map,
120 // set visible to true, 124 // set visible to true,
121 // update its properties, 125 // update its properties,
122 126
123 // Inform the adapter that the device has been found; 127 // Inform the adapter that the device has been found;
124 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing 128 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing
125 // the device object. 129 // the device object.
126 BluetoothDevice* device; 130 BluetoothDevice* device;
127 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 131 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
128 .Times(1) 132 .Times(1)
129 .WillOnce(SaveArg<1>(&device)); 133 .WillOnce(SaveArg<1>(&device));
130 134
131 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 135 BluetoothAdapterChromeOs* adapter_chromeos =
136 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
137 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
132 ->DeviceFound(adapter_path_, device_address, device_properties); 138 ->DeviceFound(adapter_path_, device_address, device_properties);
133 139
134 // Now inform the adapter that the device has been added and assigned an 140 // Now inform the adapter that the device has been added and assigned an
135 // object path; BluetoothDeviceClient::GetProperties will be called to 141 // object path; BluetoothDeviceClient::GetProperties will be called to
136 // obtain the property set; and 142 // obtain the property set; and
137 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing 143 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing
138 // the same device object as before. 144 // the same device object as before.
139 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) 145 EXPECT_CALL(*mock_device_client_, GetProperties(device_path))
140 .WillRepeatedly(Return(&device_properties)); 146 .WillRepeatedly(Return(&device_properties));
141 147
142 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) 148 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_chromeos, device))
143 .Times(1); 149 .Times(1);
144 150
145 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 151 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
146 ->DeviceCreated(adapter_path_, device_path); 152 ->DeviceCreated(adapter_path_, device_path);
147 153
148 // Finally remove the adapter again; since this is a supported device 154 // Finally remove the adapter again; since this is a supported device
149 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, 155 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called,
150 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. 156 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called.
151 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) 157 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device))
152 .Times(0); 158 .Times(0);
153 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) 159 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device))
154 .Times(1); 160 .Times(1);
155 161
156 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 162 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
157 ->DeviceRemoved(adapter_path_, device_path); 163 ->DeviceRemoved(adapter_path_, device_path);
158 164
159 // Verify that the device is still visible, just no longer paired. 165 // Verify that the device is still visible, just no longer paired.
160 EXPECT_TRUE(device->IsVisible()); 166 EXPECT_TRUE(device->IsVisible());
161 EXPECT_FALSE(device->IsPaired()); 167 EXPECT_FALSE(device->IsPaired());
162 } 168 }
163 169
164 TEST_F(BluetoothAdapterDevicesTest, UnsupportedDeviceRemovedAfterFound) { 170 TEST_F(BluetoothAdapterDevicesChromeOsTest,
171 UnsupportedDeviceRemovedAfterFound) {
165 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_02"); 172 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_02");
166 const std::string device_address = "BA:C0:11:00:00:02"; 173 const std::string device_address = "BA:C0:11:00:00:02";
167 174
168 MockBluetoothDeviceClient::Properties device_properties; 175 MockBluetoothDeviceClient::Properties device_properties;
169 device_properties.address.ReplaceValue(device_address); 176 device_properties.address.ReplaceValue(device_address);
170 device_properties.name.ReplaceValue("Fake Computer"); 177 device_properties.name.ReplaceValue("Fake Computer");
171 device_properties.bluetooth_class.ReplaceValue(0x400100); 178 device_properties.bluetooth_class.ReplaceValue(0x400100);
172 179
173 // Inform the adapter that the unsupported device has been found; 180 // Inform the adapter that the unsupported device has been found;
174 // BluetoothAdapterClient::Observer::DeviceAdded should not be called 181 // BluetoothAdapterClient::Observer::DeviceAdded should not be called
175 // yet because this device is not supported so is hidden from the UI. 182 // yet because this device is not supported so is hidden from the UI.
176 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 183 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
177 .Times(0); 184 .Times(0);
178 185
179 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 186 BluetoothAdapterChromeOs* adapter_chromeos =
187 static_cast<BluetoothAdapterChromeOs*>(adapter_.get());
188 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
180 ->DeviceFound(adapter_path_, device_address, device_properties); 189 ->DeviceFound(adapter_path_, device_address, device_properties);
181 190
182 // Now inform the adapter the device has been added and assigned an 191 // Now inform the adapter the device has been added and assigned an
183 // object path; BluetoothDeviceClient::GetProperties will be called 192 // object path; BluetoothDeviceClient::GetProperties will be called
184 // to obtain the property set; and 193 // to obtain the property set; and
185 // BluetoothAdapterClient::Observer::DeviceAdded will be called, 194 // BluetoothAdapterClient::Observer::DeviceAdded will be called,
186 // passing the device object. 195 // passing the device object.
187 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) 196 EXPECT_CALL(*mock_device_client_, GetProperties(device_path))
188 .WillRepeatedly(Return(&device_properties)); 197 .WillRepeatedly(Return(&device_properties));
189 198
190 BluetoothDevice* device; 199 BluetoothDevice* device;
191 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) 200 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _))
192 .Times(1) 201 .Times(1)
193 .WillOnce(SaveArg<1>(&device)); 202 .WillOnce(SaveArg<1>(&device));
194 203
195 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 204 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
196 ->DeviceCreated(adapter_path_, device_path); 205 ->DeviceCreated(adapter_path_, device_path);
197 206
198 // Finally remove the device again; 207 // Finally remove the device again;
199 // BluetoothAdapterClient::Observer::DeviceRemoved will be called 208 // BluetoothAdapterClient::Observer::DeviceRemoved will be called
200 // before the device object is deleted. 209 // before the device object is deleted.
201 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) 210 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device))
202 .Times(1); 211 .Times(1);
203 212
204 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) 213 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
205 ->DeviceRemoved(adapter_path_, device_path); 214 ->DeviceRemoved(adapter_path_, device_path);
206 } 215 }
207 216
208 } // namespace chromeos 217 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698