1 // Copyright 2015 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 "ash/metrics/user_metrics_recorder.h"
8 #include "ash/system/user/login_status.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/test/test_system_tray_delegate.h"
11 #include "ash/test/user_metrics_recorder_test_api.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/test/histogram_tester.h"
14 #include "ui/aura/window.h"
19 const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay
[] =
20 "Ash.NumberOfVisibleWindowsInPrimaryDisplay";
22 const char kAsh_ActiveWindowShowTypeOverTime
[] =
23 "Ash.ActiveWindowShowTypeOverTime";
27 // Test fixture for the UserMetricsRecorder class.
28 class UserMetricsRecorderTest
: public test::AshTestBase
{
30 UserMetricsRecorderTest();
31 ~UserMetricsRecorderTest() override
;
34 void SetUp() override
;
35 void TearDown() override
;
37 // Sets the user login status.
38 void SetLoginStatus(user::LoginStatus login_status
);
40 // Sets the current user session to be active or inactive in a desktop
42 void SetUserInActiveDesktopEnvironment(bool is_active
);
44 test::UserMetricsRecorderTestAPI
* user_metrics_recorder_test_api() {
45 return user_metrics_recorder_test_api_
.get();
48 base::HistogramTester
& histograms() { return histograms_
; }
51 // Test API to access private members of the test target.
52 scoped_ptr
<test::UserMetricsRecorderTestAPI
> user_metrics_recorder_test_api_
;
54 // Histogram value verifier.
55 base::HistogramTester histograms_
;
57 // The active SystemTrayDelegate. Not owned.
58 test::TestSystemTrayDelegate
* test_system_tray_delegate_
;
60 DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest
);
63 UserMetricsRecorderTest::UserMetricsRecorderTest() {
66 UserMetricsRecorderTest::~UserMetricsRecorderTest() {
69 void UserMetricsRecorderTest::SetUp() {
70 test::AshTestBase::SetUp();
71 user_metrics_recorder_test_api_
.reset(new test::UserMetricsRecorderTestAPI());
72 test_system_tray_delegate_
= GetSystemTrayDelegate();
75 void UserMetricsRecorderTest::TearDown() {
76 test_system_tray_delegate_
= nullptr;
77 test::AshTestBase::TearDown();
80 void UserMetricsRecorderTest::SetLoginStatus(user::LoginStatus login_status
) {
81 test_system_tray_delegate_
->SetLoginStatus(login_status
);
84 void UserMetricsRecorderTest::SetUserInActiveDesktopEnvironment(
87 SetLoginStatus(user::LOGGED_IN_USER
);
89 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
91 SetLoginStatus(user::LOGGED_IN_LOCKED
);
93 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
97 // Verifies the return value of IsUserInActiveDesktopEnvironment() for the
98 // different login status values.
99 TEST_F(UserMetricsRecorderTest
, VerifyIsUserInActiveDesktopEnvironmentValues
) {
100 SetLoginStatus(user::LOGGED_IN_NONE
);
102 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
104 SetLoginStatus(user::LOGGED_IN_LOCKED
);
106 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
108 SetLoginStatus(user::LOGGED_IN_USER
);
110 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
112 SetLoginStatus(user::LOGGED_IN_OWNER
);
114 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
116 SetLoginStatus(user::LOGGED_IN_GUEST
);
118 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
120 SetLoginStatus(user::LOGGED_IN_PUBLIC
);
122 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
124 SetLoginStatus(user::LOGGED_IN_SUPERVISED
);
126 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
128 SetLoginStatus(user::LOGGED_IN_KIOSK_APP
);
130 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
133 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not
134 // recorded when a user is not active in a desktop environment.
135 TEST_F(UserMetricsRecorderTest
,
136 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment
) {
137 SetUserInActiveDesktopEnvironment(false);
138 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
140 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay
, 0);
143 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are
144 // recorded when a user is active in a desktop environment.
145 TEST_F(UserMetricsRecorderTest
,
146 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment
) {
147 SetUserInActiveDesktopEnvironment(true);
148 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
150 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay
, 1);
153 // Verifies recording of stats which are always recorded by
154 // RecordPeriodicMetrics.
155 TEST_F(UserMetricsRecorderTest
, VerifyStatsRecordedByRecordPeriodicMetrics
) {
156 SetUserInActiveDesktopEnvironment(true);
157 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
159 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime
, 1);