Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / metrics / chromeos_metrics_provider_unittest.cc
blobf47b5d90a7cad4341b3721c4e201c88584ea57b9
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"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
11 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
12 #include "chrome/browser/metrics/chromeos_metrics_provider.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
15 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
16 #include "chromeos/dbus/fake_bluetooth_device_client.h"
17 #include "chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h"
18 #include "chromeos/dbus/fake_bluetooth_gatt_descriptor_client.h"
19 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
20 #include "chromeos/dbus/fake_bluetooth_input_client.h"
21 #include "chromeos/dbus/power_manager_client.h"
22 #include "chromeos/login/login_state.h"
23 #include "components/metrics/proto/system_profile.pb.h"
24 #include "components/user_manager/user_manager.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 #if defined(USE_X11)
30 #include "ui/events/devices/x11/device_data_manager_x11.h"
31 #endif
33 using chromeos::DBusThreadManager;
34 using chromeos::DBusThreadManagerSetter;
35 using chromeos::BluetoothAdapterClient;
36 using chromeos::BluetoothAgentManagerClient;
37 using chromeos::BluetoothDeviceClient;
38 using chromeos::BluetoothGattCharacteristicClient;
39 using chromeos::BluetoothGattDescriptorClient;
40 using chromeos::BluetoothGattServiceClient;
41 using chromeos::BluetoothInputClient;
42 using chromeos::FakeBluetoothAdapterClient;
43 using chromeos::FakeBluetoothAgentManagerClient;
44 using chromeos::FakeBluetoothDeviceClient;
45 using chromeos::FakeBluetoothGattCharacteristicClient;
46 using chromeos::FakeBluetoothGattDescriptorClient;
47 using chromeos::FakeBluetoothGattServiceClient;
48 using chromeos::FakeBluetoothInputClient;
49 using chromeos::PowerManagerClient;
50 using chromeos::STUB_DBUS_CLIENT_IMPLEMENTATION;
52 class ChromeOSMetricsProviderTest : public testing::Test {
53 public:
54 ChromeOSMetricsProviderTest() {}
56 protected:
57 void SetUp() override {
58 #if defined(USE_X11)
59 ui::DeviceDataManagerX11::CreateInstance();
60 #endif
62 // Set up the fake Bluetooth environment,
63 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
64 DBusThreadManager::GetSetterForTesting();
65 dbus_setter->SetBluetoothAdapterClient(
66 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
67 dbus_setter->SetBluetoothDeviceClient(
68 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
69 dbus_setter->SetBluetoothGattCharacteristicClient(
70 scoped_ptr<BluetoothGattCharacteristicClient>(
71 new FakeBluetoothGattCharacteristicClient));
72 dbus_setter->SetBluetoothGattDescriptorClient(
73 scoped_ptr<BluetoothGattDescriptorClient>(
74 new FakeBluetoothGattDescriptorClient));
75 dbus_setter->SetBluetoothGattServiceClient(
76 scoped_ptr<BluetoothGattServiceClient>(
77 new FakeBluetoothGattServiceClient));
78 dbus_setter->SetBluetoothInputClient(
79 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
80 dbus_setter->SetBluetoothAgentManagerClient(
81 scoped_ptr<BluetoothAgentManagerClient>(
82 new FakeBluetoothAgentManagerClient));
84 // Set up a PowerManagerClient instance for PerfProvider.
85 dbus_setter->SetPowerManagerClient(
86 scoped_ptr<PowerManagerClient>(
87 PowerManagerClient::Create(STUB_DBUS_CLIENT_IMPLEMENTATION)));
89 // Grab pointers to members of the thread manager for easier testing.
90 fake_bluetooth_adapter_client_ = static_cast<FakeBluetoothAdapterClient*>(
91 DBusThreadManager::Get()->GetBluetoothAdapterClient());
92 fake_bluetooth_device_client_ = static_cast<FakeBluetoothDeviceClient*>(
93 DBusThreadManager::Get()->GetBluetoothDeviceClient());
95 // Initialize the login state trackers.
96 if (!chromeos::LoginState::IsInitialized())
97 chromeos::LoginState::Initialize();
100 void TearDown() override {
101 // Destroy the login state tracker if it was initialized.
102 chromeos::LoginState::Shutdown();
104 DBusThreadManager::Shutdown();
107 protected:
108 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
109 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
111 private:
112 content::TestBrowserThreadBundle thread_bundle_;
114 DISALLOW_COPY_AND_ASSIGN(ChromeOSMetricsProviderTest);
117 TEST_F(ChromeOSMetricsProviderTest, MultiProfileUserCount) {
118 std::string user1("user1@example.com");
119 std::string user2("user2@example.com");
120 std::string user3("user3@example.com");
122 // |scoped_enabler| takes over the lifetime of |user_manager|.
123 chromeos::FakeChromeUserManager* user_manager =
124 new chromeos::FakeChromeUserManager();
125 chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
126 user_manager->AddKioskAppUser(user1);
127 user_manager->AddKioskAppUser(user2);
128 user_manager->AddKioskAppUser(user3);
130 user_manager->LoginUser(user1);
131 user_manager->LoginUser(user3);
133 ChromeOSMetricsProvider provider;
134 provider.OnDidCreateMetricsLog();
135 metrics::SystemProfileProto system_profile;
136 provider.ProvideSystemProfileMetrics(&system_profile);
137 EXPECT_EQ(2u, system_profile.multi_profile_user_count());
140 TEST_F(ChromeOSMetricsProviderTest, MultiProfileCountInvalidated) {
141 std::string user1("user1@example.com");
142 std::string user2("user2@example.com");
143 std::string user3("user3@example.com");
145 // |scoped_enabler| takes over the lifetime of |user_manager|.
146 chromeos::FakeChromeUserManager* user_manager =
147 new chromeos::FakeChromeUserManager();
148 chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
149 user_manager->AddKioskAppUser(user1);
150 user_manager->AddKioskAppUser(user2);
151 user_manager->AddKioskAppUser(user3);
153 user_manager->LoginUser(user1);
155 ChromeOSMetricsProvider provider;
156 provider.OnDidCreateMetricsLog();
158 metrics::SystemProfileProto system_profile;
159 provider.ProvideSystemProfileMetrics(&system_profile);
160 EXPECT_EQ(1u, system_profile.multi_profile_user_count());
162 user_manager->LoginUser(user2);
163 provider.ProvideSystemProfileMetrics(&system_profile);
164 EXPECT_EQ(0u, system_profile.multi_profile_user_count());
167 TEST_F(ChromeOSMetricsProviderTest, BluetoothHardwareDisabled) {
168 ChromeOSMetricsProvider provider;
169 provider.OnDidCreateMetricsLog();
170 metrics::SystemProfileProto system_profile;
171 provider.ProvideSystemProfileMetrics(&system_profile);
173 EXPECT_TRUE(system_profile.has_hardware());
174 EXPECT_TRUE(system_profile.hardware().has_bluetooth());
176 EXPECT_TRUE(system_profile.hardware().bluetooth().is_present());
177 EXPECT_FALSE(system_profile.hardware().bluetooth().is_enabled());
180 TEST_F(ChromeOSMetricsProviderTest, BluetoothHardwareEnabled) {
181 FakeBluetoothAdapterClient::Properties* properties =
182 fake_bluetooth_adapter_client_->GetProperties(
183 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
184 properties->powered.ReplaceValue(true);
186 ChromeOSMetricsProvider provider;
187 metrics::SystemProfileProto system_profile;
188 provider.ProvideSystemProfileMetrics(&system_profile);
190 EXPECT_TRUE(system_profile.has_hardware());
191 EXPECT_TRUE(system_profile.hardware().has_bluetooth());
193 EXPECT_TRUE(system_profile.hardware().bluetooth().is_present());
194 EXPECT_TRUE(system_profile.hardware().bluetooth().is_enabled());
197 TEST_F(ChromeOSMetricsProviderTest, BluetoothPairedDevices) {
198 // The fake bluetooth adapter class already claims to be paired with two
199 // device when initialized. Add a third and fourth fake device to it so we
200 // can test the cases where a device is not paired (LE device, generally)
201 // and a device that does not have Device ID information.
202 fake_bluetooth_device_client_->CreateDevice(
203 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
204 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
206 fake_bluetooth_device_client_->CreateDevice(
207 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
208 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
210 FakeBluetoothDeviceClient::Properties* properties =
211 fake_bluetooth_device_client_->GetProperties(
212 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
213 properties->paired.ReplaceValue(true);
215 ChromeOSMetricsProvider provider;
216 provider.OnDidCreateMetricsLog();
217 metrics::SystemProfileProto system_profile;
218 provider.ProvideSystemProfileMetrics(&system_profile);
220 ASSERT_TRUE(system_profile.has_hardware());
221 ASSERT_TRUE(system_profile.hardware().has_bluetooth());
223 // Only three of the devices should appear.
224 EXPECT_EQ(3, system_profile.hardware().bluetooth().paired_device_size());
226 typedef metrics::SystemProfileProto::Hardware::Bluetooth::PairedDevice
227 PairedDevice;
229 // First device should match the Paired Device object, complete with
230 // parsed Device ID information.
231 PairedDevice device1 = system_profile.hardware().bluetooth().paired_device(0);
233 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceClass,
234 device1.bluetooth_class());
235 EXPECT_EQ(PairedDevice::DEVICE_COMPUTER, device1.type());
236 EXPECT_EQ(0x001122U, device1.vendor_prefix());
237 EXPECT_EQ(PairedDevice::VENDOR_ID_USB, device1.vendor_id_source());
238 EXPECT_EQ(0x05ACU, device1.vendor_id());
239 EXPECT_EQ(0x030DU, device1.product_id());
240 EXPECT_EQ(0x0306U, device1.device_id());
242 // Third device should match the Confirm Passkey object, this has
243 // no Device ID information.
244 PairedDevice device2 = system_profile.hardware().bluetooth().paired_device(1);
246 EXPECT_EQ(FakeBluetoothDeviceClient::kConfirmPasskeyClass,
247 device2.bluetooth_class());
248 EXPECT_EQ(PairedDevice::DEVICE_PHONE, device2.type());
249 EXPECT_EQ(0x207D74U, device2.vendor_prefix());
250 EXPECT_EQ(PairedDevice::VENDOR_ID_UNKNOWN, device2.vendor_id_source());