Home | History | Annotate | Download | only in metrics
      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.
      4 
      5 #include "chrome/browser/metrics/chromeos_metrics_provider.h"
      6 
      7 #include <string>
      8 
      9 #include "base/basictypes.h"
     10 #include "chrome/browser/chromeos/login/users/fake_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"
     28 
     29 #if defined(USE_X11)
     30 #include "ui/events/x/device_data_manager_x11.h"
     31 #endif
     32 
     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;
     51 
     52 class ChromeOSMetricsProviderTest : public testing::Test {
     53  public:
     54   ChromeOSMetricsProviderTest() {}
     55 
     56  protected:
     57   virtual void SetUp() OVERRIDE {
     58 #if defined(USE_X11)
     59     ui::DeviceDataManagerX11::CreateInstance();
     60 #endif
     61 
     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));
     83 
     84     // Set up a PowerManagerClient instance for PerfProvider.
     85     dbus_setter->SetPowerManagerClient(
     86         scoped_ptr<PowerManagerClient>(
     87             PowerManagerClient::Create(STUB_DBUS_CLIENT_IMPLEMENTATION)));
     88 
     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());
     94 
     95     // Initialize the login state trackers.
     96     if (!chromeos::LoginState::IsInitialized())
     97       chromeos::LoginState::Initialize();
     98   }
     99 
    100   virtual void TearDown() OVERRIDE {
    101     // Destroy the login state tracker if it was initialized.
    102     chromeos::LoginState::Shutdown();
    103 
    104     DBusThreadManager::Shutdown();
    105   }
    106 
    107  protected:
    108   FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
    109   FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
    110 
    111  private:
    112   content::TestBrowserThreadBundle thread_bundle_;
    113 
    114   DISALLOW_COPY_AND_ASSIGN(ChromeOSMetricsProviderTest);
    115 };
    116 
    117 TEST_F(ChromeOSMetricsProviderTest, MultiProfileUserCount) {
    118   std::string user1("user1 (at) example.com");
    119   std::string user2("user2 (at) example.com");
    120   std::string user3("user3 (at) example.com");
    121 
    122   // |scoped_enabler| takes over the lifetime of |user_manager|.
    123   chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
    124   chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
    125   user_manager->AddKioskAppUser(user1);
    126   user_manager->AddKioskAppUser(user2);
    127   user_manager->AddKioskAppUser(user3);
    128 
    129   user_manager->LoginUser(user1);
    130   user_manager->LoginUser(user3);
    131 
    132   ChromeOSMetricsProvider provider;
    133   provider.OnDidCreateMetricsLog();
    134   metrics::SystemProfileProto system_profile;
    135   provider.ProvideSystemProfileMetrics(&system_profile);
    136   EXPECT_EQ(2u, system_profile.multi_profile_user_count());
    137 }
    138 
    139 TEST_F(ChromeOSMetricsProviderTest, MultiProfileCountInvalidated) {
    140   std::string user1("user1 (at) example.com");
    141   std::string user2("user2 (at) example.com");
    142   std::string user3("user3 (at) example.com");
    143 
    144   // |scoped_enabler| takes over the lifetime of |user_manager|.
    145   chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
    146   chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
    147   user_manager->AddKioskAppUser(user1);
    148   user_manager->AddKioskAppUser(user2);
    149   user_manager->AddKioskAppUser(user3);
    150 
    151   user_manager->LoginUser(user1);
    152 
    153   ChromeOSMetricsProvider provider;
    154   provider.OnDidCreateMetricsLog();
    155 
    156   metrics::SystemProfileProto system_profile;
    157   provider.ProvideSystemProfileMetrics(&system_profile);
    158   EXPECT_EQ(1u, system_profile.multi_profile_user_count());
    159 
    160   user_manager->LoginUser(user2);
    161   provider.ProvideSystemProfileMetrics(&system_profile);
    162   EXPECT_EQ(0u, system_profile.multi_profile_user_count());
    163 }
    164 
    165 TEST_F(ChromeOSMetricsProviderTest, BluetoothHardwareDisabled) {
    166   ChromeOSMetricsProvider provider;
    167   provider.OnDidCreateMetricsLog();
    168   metrics::SystemProfileProto system_profile;
    169   provider.ProvideSystemProfileMetrics(&system_profile);
    170 
    171   EXPECT_TRUE(system_profile.has_hardware());
    172   EXPECT_TRUE(system_profile.hardware().has_bluetooth());
    173 
    174   EXPECT_TRUE(system_profile.hardware().bluetooth().is_present());
    175   EXPECT_FALSE(system_profile.hardware().bluetooth().is_enabled());
    176 }
    177 
    178 TEST_F(ChromeOSMetricsProviderTest, BluetoothHardwareEnabled) {
    179   FakeBluetoothAdapterClient::Properties* properties =
    180       fake_bluetooth_adapter_client_->GetProperties(
    181           dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
    182   properties->powered.ReplaceValue(true);
    183 
    184   ChromeOSMetricsProvider provider;
    185   metrics::SystemProfileProto system_profile;
    186   provider.ProvideSystemProfileMetrics(&system_profile);
    187 
    188   EXPECT_TRUE(system_profile.has_hardware());
    189   EXPECT_TRUE(system_profile.hardware().has_bluetooth());
    190 
    191   EXPECT_TRUE(system_profile.hardware().bluetooth().is_present());
    192   EXPECT_TRUE(system_profile.hardware().bluetooth().is_enabled());
    193 }
    194 
    195 TEST_F(ChromeOSMetricsProviderTest, BluetoothPairedDevices) {
    196   // The fake bluetooth adapter class already claims to be paired with one
    197   // device when initialized. Add a second and third fake device to it so we
    198   // can test the cases where a device is not paired (LE device, generally)
    199   // and a device that does not have Device ID information.
    200   fake_bluetooth_device_client_->CreateDevice(
    201       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    202       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
    203 
    204   fake_bluetooth_device_client_->CreateDevice(
    205       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
    206       dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
    207 
    208   FakeBluetoothDeviceClient::Properties* properties =
    209       fake_bluetooth_device_client_->GetProperties(
    210           dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
    211   properties->paired.ReplaceValue(true);
    212 
    213   ChromeOSMetricsProvider provider;
    214   provider.OnDidCreateMetricsLog();
    215   metrics::SystemProfileProto system_profile;
    216   provider.ProvideSystemProfileMetrics(&system_profile);
    217 
    218   ASSERT_TRUE(system_profile.has_hardware());
    219   ASSERT_TRUE(system_profile.hardware().has_bluetooth());
    220 
    221   // Only two of the devices should appear.
    222   EXPECT_EQ(2, system_profile.hardware().bluetooth().paired_device_size());
    223 
    224   typedef metrics::SystemProfileProto::Hardware::Bluetooth::PairedDevice
    225       PairedDevice;
    226 
    227   // First device should match the Paired Device object, complete with
    228   // parsed Device ID information.
    229   PairedDevice device1 = system_profile.hardware().bluetooth().paired_device(0);
    230 
    231   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceClass,
    232             device1.bluetooth_class());
    233   EXPECT_EQ(PairedDevice::DEVICE_COMPUTER, device1.type());
    234   EXPECT_EQ(0x001122U, device1.vendor_prefix());
    235   EXPECT_EQ(PairedDevice::VENDOR_ID_USB, device1.vendor_id_source());
    236   EXPECT_EQ(0x05ACU, device1.vendor_id());
    237   EXPECT_EQ(0x030DU, device1.product_id());
    238   EXPECT_EQ(0x0306U, device1.device_id());
    239 
    240   // Second device should match the Confirm Passkey object, this has
    241   // no Device ID information.
    242   PairedDevice device2 = system_profile.hardware().bluetooth().paired_device(1);
    243 
    244   EXPECT_EQ(FakeBluetoothDeviceClient::kConfirmPasskeyClass,
    245             device2.bluetooth_class());
    246   EXPECT_EQ(PairedDevice::DEVICE_PHONE, device2.type());
    247   EXPECT_EQ(0x207D74U, device2.vendor_prefix());
    248   EXPECT_EQ(PairedDevice::VENDOR_ID_UNKNOWN, device2.vendor_id_source());
    249 }
    250