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
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/chromeos_metrics_provider.h"
9 #include "base/basictypes.h"
10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
11 #include "chrome/browser/chromeos/login/users/user_manager.h"
12 #include "chrome/browser/metrics/chromeos_metrics_provider.h"
13 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
14 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
15 #include "chromeos/dbus/fake_bluetooth_device_client.h"
16 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
17 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
18 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
19 #include "chromeos/dbus/fake_bluetooth_input_client.h"
20 #include "chromeos/dbus/fake_dbus_thread_manager.h"
21 #include "chromeos/login/login_state.h"
22 #include "components/metrics/proto/system_profile.pb.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "content/public/test/test_utils.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using chromeos::DBusThreadManager
;
28 using chromeos::BluetoothAdapterClient
;
29 using chromeos::BluetoothAgentManagerClient
;
30 using chromeos::BluetoothDeviceClient
;
31 using chromeos::BluetoothGattCharacteristicClient
;
32 using chromeos::BluetoothGattDescriptorClient
;
33 using chromeos::BluetoothGattServiceClient
;
34 using chromeos::BluetoothInputClient
;
35 using chromeos::FakeBluetoothAdapterClient
;
36 using chromeos::FakeBluetoothAgentManagerClient
;
37 using chromeos::FakeBluetoothDeviceClient
;
38 using chromeos::FakeBluetoothGattCharacteristicClient
;
39 using chromeos::FakeBluetoothGattDescriptorClient
;
40 using chromeos::FakeBluetoothGattServiceClient
;
41 using chromeos::FakeBluetoothInputClient
;
42 using chromeos::FakeDBusThreadManager
;
44 class ChromeOSMetricsProviderTest
: public testing::Test
{
46 ChromeOSMetricsProviderTest() {}
49 virtual void SetUp() OVERRIDE
{
50 // Set up the fake Bluetooth environment,
51 scoped_ptr
<FakeDBusThreadManager
> fake_dbus_thread_manager(
52 new FakeDBusThreadManager
);
53 fake_dbus_thread_manager
->SetBluetoothAdapterClient(
54 scoped_ptr
<BluetoothAdapterClient
>(new FakeBluetoothAdapterClient
));
55 fake_dbus_thread_manager
->SetBluetoothDeviceClient(
56 scoped_ptr
<BluetoothDeviceClient
>(new FakeBluetoothDeviceClient
));
57 fake_dbus_thread_manager
->SetBluetoothGattCharacteristicClient(
58 scoped_ptr
<BluetoothGattCharacteristicClient
>(
59 new FakeBluetoothGattCharacteristicClient
));
60 fake_dbus_thread_manager
->SetBluetoothGattDescriptorClient(
61 scoped_ptr
<BluetoothGattDescriptorClient
>(
62 new FakeBluetoothGattDescriptorClient
));
63 fake_dbus_thread_manager
->SetBluetoothGattServiceClient(
64 scoped_ptr
<BluetoothGattServiceClient
>(
65 new FakeBluetoothGattServiceClient
));
66 fake_dbus_thread_manager
->SetBluetoothInputClient(
67 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
68 fake_dbus_thread_manager
->SetBluetoothAgentManagerClient(
69 scoped_ptr
<BluetoothAgentManagerClient
>(
70 new FakeBluetoothAgentManagerClient
));
71 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager
.release());
73 // Grab pointers to members of the thread manager for easier testing.
74 fake_bluetooth_adapter_client_
= static_cast<FakeBluetoothAdapterClient
*>(
75 DBusThreadManager::Get()->GetBluetoothAdapterClient());
76 fake_bluetooth_device_client_
= static_cast<FakeBluetoothDeviceClient
*>(
77 DBusThreadManager::Get()->GetBluetoothDeviceClient());
79 // Initialize the login state trackers.
80 if (!chromeos::LoginState::IsInitialized())
81 chromeos::LoginState::Initialize();
84 virtual void TearDown() OVERRIDE
{
85 // Destroy the login state tracker if it was initialized.
86 chromeos::LoginState::Shutdown();
88 DBusThreadManager::Shutdown();
92 FakeBluetoothAdapterClient
* fake_bluetooth_adapter_client_
;
93 FakeBluetoothDeviceClient
* fake_bluetooth_device_client_
;
96 content::TestBrowserThreadBundle thread_bundle_
;
98 DISALLOW_COPY_AND_ASSIGN(ChromeOSMetricsProviderTest
);
101 TEST_F(ChromeOSMetricsProviderTest
, MultiProfileUserCount
) {
102 std::string
user1("user1@example.com");
103 std::string
user2("user2@example.com");
104 std::string
user3("user3@example.com");
106 // |scoped_enabler| takes over the lifetime of |user_manager|.
107 chromeos::FakeUserManager
* user_manager
= new chromeos::FakeUserManager();
108 chromeos::ScopedUserManagerEnabler
scoped_enabler(user_manager
);
109 user_manager
->AddKioskAppUser(user1
);
110 user_manager
->AddKioskAppUser(user2
);
111 user_manager
->AddKioskAppUser(user3
);
113 user_manager
->LoginUser(user1
);
114 user_manager
->LoginUser(user3
);
116 ChromeOSMetricsProvider provider
;
117 provider
.OnDidCreateMetricsLog();
118 metrics::SystemProfileProto system_profile
;
119 provider
.ProvideSystemProfileMetrics(&system_profile
);
120 EXPECT_EQ(2u, system_profile
.multi_profile_user_count());
123 TEST_F(ChromeOSMetricsProviderTest
, MultiProfileCountInvalidated
) {
124 std::string
user1("user1@example.com");
125 std::string
user2("user2@example.com");
126 std::string
user3("user3@example.com");
128 // |scoped_enabler| takes over the lifetime of |user_manager|.
129 chromeos::FakeUserManager
* user_manager
= new chromeos::FakeUserManager();
130 chromeos::ScopedUserManagerEnabler
scoped_enabler(user_manager
);
131 user_manager
->AddKioskAppUser(user1
);
132 user_manager
->AddKioskAppUser(user2
);
133 user_manager
->AddKioskAppUser(user3
);
135 user_manager
->LoginUser(user1
);
137 ChromeOSMetricsProvider provider
;
138 provider
.OnDidCreateMetricsLog();
140 metrics::SystemProfileProto system_profile
;
141 provider
.ProvideSystemProfileMetrics(&system_profile
);
142 EXPECT_EQ(1u, system_profile
.multi_profile_user_count());
144 user_manager
->LoginUser(user2
);
145 provider
.ProvideSystemProfileMetrics(&system_profile
);
146 EXPECT_EQ(0u, system_profile
.multi_profile_user_count());
149 TEST_F(ChromeOSMetricsProviderTest
, BluetoothHardwareDisabled
) {
150 ChromeOSMetricsProvider provider
;
151 provider
.OnDidCreateMetricsLog();
152 metrics::SystemProfileProto system_profile
;
153 provider
.ProvideSystemProfileMetrics(&system_profile
);
155 EXPECT_TRUE(system_profile
.has_hardware());
156 EXPECT_TRUE(system_profile
.hardware().has_bluetooth());
158 EXPECT_TRUE(system_profile
.hardware().bluetooth().is_present());
159 EXPECT_FALSE(system_profile
.hardware().bluetooth().is_enabled());
162 TEST_F(ChromeOSMetricsProviderTest
, BluetoothHardwareEnabled
) {
163 FakeBluetoothAdapterClient::Properties
* properties
=
164 fake_bluetooth_adapter_client_
->GetProperties(
165 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
166 properties
->powered
.ReplaceValue(true);
168 ChromeOSMetricsProvider provider
;
169 metrics::SystemProfileProto system_profile
;
170 provider
.ProvideSystemProfileMetrics(&system_profile
);
172 EXPECT_TRUE(system_profile
.has_hardware());
173 EXPECT_TRUE(system_profile
.hardware().has_bluetooth());
175 EXPECT_TRUE(system_profile
.hardware().bluetooth().is_present());
176 EXPECT_TRUE(system_profile
.hardware().bluetooth().is_enabled());
179 TEST_F(ChromeOSMetricsProviderTest
, BluetoothPairedDevices
) {
180 // The fake bluetooth adapter class already claims to be paired with one
181 // device when initialized. Add a second and third fake device to it so we
182 // can test the cases where a device is not paired (LE device, generally)
183 // and a device that does not have Device ID information.
184 fake_bluetooth_device_client_
->CreateDevice(
185 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
186 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
188 fake_bluetooth_device_client_
->CreateDevice(
189 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
190 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
192 FakeBluetoothDeviceClient::Properties
* properties
=
193 fake_bluetooth_device_client_
->GetProperties(
194 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
195 properties
->paired
.ReplaceValue(true);
197 ChromeOSMetricsProvider provider
;
198 provider
.OnDidCreateMetricsLog();
199 metrics::SystemProfileProto system_profile
;
200 provider
.ProvideSystemProfileMetrics(&system_profile
);
202 ASSERT_TRUE(system_profile
.has_hardware());
203 ASSERT_TRUE(system_profile
.hardware().has_bluetooth());
205 // Only two of the devices should appear.
206 EXPECT_EQ(2, system_profile
.hardware().bluetooth().paired_device_size());
208 typedef metrics::SystemProfileProto::Hardware::Bluetooth::PairedDevice
211 // First device should match the Paired Device object, complete with
212 // parsed Device ID information.
213 PairedDevice device1
= system_profile
.hardware().bluetooth().paired_device(0);
215 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceClass
,
216 device1
.bluetooth_class());
217 EXPECT_EQ(PairedDevice::DEVICE_COMPUTER
, device1
.type());
218 EXPECT_EQ(0x001122U
, device1
.vendor_prefix());
219 EXPECT_EQ(PairedDevice::VENDOR_ID_USB
, device1
.vendor_id_source());
220 EXPECT_EQ(0x05ACU
, device1
.vendor_id());
221 EXPECT_EQ(0x030DU
, device1
.product_id());
222 EXPECT_EQ(0x0306U
, device1
.device_id());
224 // Second device should match the Confirm Passkey object, this has
225 // no Device ID information.
226 PairedDevice device2
= system_profile
.hardware().bluetooth().paired_device(1);
228 EXPECT_EQ(FakeBluetoothDeviceClient::kConfirmPasskeyClass
,
229 device2
.bluetooth_class());
230 EXPECT_EQ(PairedDevice::DEVICE_PHONE
, device2
.type());
231 EXPECT_EQ(0x207D74U
, device2
.vendor_prefix());
232 EXPECT_EQ(PairedDevice::VENDOR_ID_UNKNOWN
, device2
.vendor_id_source());