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

Side by Side Diff: ash/metrics/user_metrics_recorder_unittest.cc

Issue 2826313002: cros: Use SessionController in UserMetricsRecorder to get login status (Closed)
Patch Set: Created 3 years, 8 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
« no previous file with comments | « ash/metrics/user_metrics_recorder.cc ('k') | ash/test/test_system_tray_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "ash/metrics/user_metrics_recorder.h" 5 #include "ash/metrics/user_metrics_recorder.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "ash/login_status.h" 9 #include "ash/login_status.h"
10 #include "ash/public/cpp/config.h" 10 #include "ash/public/cpp/config.h"
11 #include "ash/session/session_controller.h"
11 #include "ash/shelf/shelf_model.h" 12 #include "ash/shelf/shelf_model.h"
12 #include "ash/shell.h" 13 #include "ash/shell.h"
13 #include "ash/shell_port.h" 14 #include "ash/shell_port.h"
14 #include "ash/test/ash_test_base.h" 15 #include "ash/test/ash_test_base.h"
15 #include "ash/test/test_system_tray_delegate.h" 16 #include "ash/test/test_session_controller_client.h"
16 #include "ash/test/user_metrics_recorder_test_api.h" 17 #include "ash/test/user_metrics_recorder_test_api.h"
17 #include "ash/wm_window.h" 18 #include "ash/wm_window.h"
18 #include "base/test/histogram_tester.h" 19 #include "base/test/histogram_tester.h"
19 20
21 using session_manager::SessionState;
22
20 namespace ash { 23 namespace ash {
21 namespace { 24 namespace {
22 25
23 const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay[] = 26 const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay[] =
24 "Ash.NumberOfVisibleWindowsInPrimaryDisplay"; 27 "Ash.NumberOfVisibleWindowsInPrimaryDisplay";
25 28
26 const char kAsh_ActiveWindowShowTypeOverTime[] = 29 const char kAsh_ActiveWindowShowTypeOverTime[] =
27 "Ash.ActiveWindowShowTypeOverTime"; 30 "Ash.ActiveWindowShowTypeOverTime";
28 31
29 const char kAsh_Shelf_NumberOfItems[] = "Ash.Shelf.NumberOfItems"; 32 const char kAsh_Shelf_NumberOfItems[] = "Ash.Shelf.NumberOfItems";
30 33
31 const char kAsh_Shelf_NumberOfPinnedItems[] = "Ash.Shelf.NumberOfPinnedItems"; 34 const char kAsh_Shelf_NumberOfPinnedItems[] = "Ash.Shelf.NumberOfPinnedItems";
32 35
33 const char kAsh_Shelf_NumberOfUnpinnedItems[] = 36 const char kAsh_Shelf_NumberOfUnpinnedItems[] =
34 "Ash.Shelf.NumberOfUnpinnedItems"; 37 "Ash.Shelf.NumberOfUnpinnedItems";
35 38
36 } // namespace 39 } // namespace
37 40
38 // Test fixture for the UserMetricsRecorder class. 41 // Test fixture for the UserMetricsRecorder class. The tests manage their own
39 class UserMetricsRecorderTest : public test::AshTestBase { 42 // session state.
43 class UserMetricsRecorderTest : public test::NoSessionAshTestBase {
40 public: 44 public:
41 UserMetricsRecorderTest(); 45 UserMetricsRecorderTest() = default;
42 ~UserMetricsRecorderTest() override; 46 ~UserMetricsRecorderTest() override = default;
43 47
44 // test::AshTestBase: 48 test::UserMetricsRecorderTestAPI& test_api() { return test_api_; }
45 void SetUp() override;
46 void TearDown() override;
47
48 // Sets the user login status.
49 void SetLoginStatus(LoginStatus login_status);
50
51 // Sets the current user session to be active or inactive in a desktop
52 // environment.
53 void SetUserInActiveDesktopEnvironment(bool is_active);
54
55 test::UserMetricsRecorderTestAPI* user_metrics_recorder_test_api() {
56 return user_metrics_recorder_test_api_.get();
57 }
58 49
59 base::HistogramTester& histograms() { return histograms_; } 50 base::HistogramTester& histograms() { return histograms_; }
60 51
61 private: 52 private:
62 // Test API to access private members of the test target. 53 // Test API to access private members of the test target.
63 std::unique_ptr<test::UserMetricsRecorderTestAPI> 54 test::UserMetricsRecorderTestAPI test_api_;
64 user_metrics_recorder_test_api_;
65 55
66 // Histogram value verifier. 56 // Histogram value verifier.
67 base::HistogramTester histograms_; 57 base::HistogramTester histograms_;
68 58
69 // The active SystemTrayDelegate. Not owned.
70 test::TestSystemTrayDelegate* test_system_tray_delegate_;
71
72 DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest); 59 DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest);
73 }; 60 };
74 61
75 UserMetricsRecorderTest::UserMetricsRecorderTest() {}
76
77 UserMetricsRecorderTest::~UserMetricsRecorderTest() {}
78
79 void UserMetricsRecorderTest::SetUp() {
80 test::AshTestBase::SetUp();
81 user_metrics_recorder_test_api_.reset(new test::UserMetricsRecorderTestAPI());
82 test_system_tray_delegate_ = GetSystemTrayDelegate();
83 }
84
85 void UserMetricsRecorderTest::TearDown() {
86 test_system_tray_delegate_ = nullptr;
87 test::AshTestBase::TearDown();
88 }
89
90 void UserMetricsRecorderTest::SetLoginStatus(LoginStatus login_status) {
91 test_system_tray_delegate_->SetLoginStatus(login_status);
92 }
93
94 void UserMetricsRecorderTest::SetUserInActiveDesktopEnvironment(
95 bool is_active) {
96 if (is_active) {
97 SetLoginStatus(LoginStatus::USER);
98 ASSERT_TRUE(
99 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
100 } else {
101 SetLoginStatus(LoginStatus::LOCKED);
102 ASSERT_FALSE(
103 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
104 }
105 }
106
107 // Verifies the return value of IsUserInActiveDesktopEnvironment() for the 62 // Verifies the return value of IsUserInActiveDesktopEnvironment() for the
108 // different login status values. 63 // different login status values.
109 TEST_F(UserMetricsRecorderTest, VerifyIsUserInActiveDesktopEnvironmentValues) { 64 TEST_F(UserMetricsRecorderTest, VerifyIsUserInActiveDesktopEnvironmentValues) {
110 SetLoginStatus(LoginStatus::NOT_LOGGED_IN); 65 SessionController* session = Shell::Get()->session_controller();
111 EXPECT_FALSE(
112 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
113 66
114 SetLoginStatus(LoginStatus::LOCKED); 67 // Environment is not active before login.
115 EXPECT_FALSE( 68 ASSERT_FALSE(session->IsActiveUserSessionStarted());
116 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); 69 EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
117 70
118 SetLoginStatus(LoginStatus::USER); 71 // Environment is active after login.
119 EXPECT_TRUE( 72 SetSessionStarted(true);
120 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); 73 ASSERT_TRUE(session->IsActiveUserSessionStarted());
74 EXPECT_TRUE(test_api().IsUserInActiveDesktopEnvironment());
121 75
122 SetLoginStatus(LoginStatus::OWNER); 76 // Environment is not active when screen is locked.
123 EXPECT_TRUE( 77 test::TestSessionControllerClient* client = GetSessionControllerClient();
124 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); 78 client->SetSessionState(SessionState::LOCKED);
79 ASSERT_TRUE(session->IsScreenLocked());
80 EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
125 81
126 SetLoginStatus(LoginStatus::GUEST); 82 // Kiosk logins are not considered active.
127 EXPECT_TRUE( 83 client->Reset();
128 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); 84 client->AddUserSession("user1@test.com", user_manager::USER_TYPE_KIOSK_APP);
xiyuan 2017/04/19 23:08:54 nit: "user1@test.com" -> "app@kiosk-apps.device-lo
James Cook 2017/04/19 23:26:59 Done.
85 client->SetSessionState(session_manager::SessionState::ACTIVE);
86 EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
129 87
130 SetLoginStatus(LoginStatus::PUBLIC); 88 // Arc kiosk logins are not considered active.
131 EXPECT_TRUE( 89 client->Reset();
132 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); 90 client->AddUserSession("user1@test.com",
133 91 user_manager::USER_TYPE_ARC_KIOSK_APP);
xiyuan 2017/04/19 23:08:54 nit: "user1@test.com" -> "app@arc-kiosk-apps.devic
James Cook 2017/04/19 23:26:59 Done.
134 SetLoginStatus(LoginStatus::SUPERVISED); 92 client->SetSessionState(session_manager::SessionState::ACTIVE);
135 EXPECT_TRUE( 93 EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
136 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
137
138 SetLoginStatus(LoginStatus::KIOSK_APP);
139 EXPECT_FALSE(
140 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
141 } 94 }
142 95
143 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not 96 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not
144 // recorded when a user is not active in a desktop environment. 97 // recorded when a user is not active in a desktop environment.
145 TEST_F(UserMetricsRecorderTest, 98 TEST_F(UserMetricsRecorderTest,
146 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) { 99 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) {
147 SetUserInActiveDesktopEnvironment(false); 100 ASSERT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
148 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); 101 test_api().RecordPeriodicMetrics();
149 102
150 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0); 103 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0);
151 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0); 104 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0);
152 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0); 105 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0);
153 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0); 106 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0);
154 } 107 }
155 108
156 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are 109 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are
157 // recorded when a user is active in a desktop environment. 110 // recorded when a user is active in a desktop environment.
158 TEST_F(UserMetricsRecorderTest, 111 TEST_F(UserMetricsRecorderTest,
159 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) { 112 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) {
160 SetUserInActiveDesktopEnvironment(true); 113 SetSessionStarted(true);
161 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); 114 ASSERT_TRUE(test_api().IsUserInActiveDesktopEnvironment());
115 test_api().RecordPeriodicMetrics();
162 116
163 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1); 117 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1);
164 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1); 118 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1);
165 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1); 119 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1);
166 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1); 120 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1);
167 } 121 }
168 122
169 // Verifies recording of stats which are always recorded by 123 // Verifies recording of stats which are always recorded by
170 // RecordPeriodicMetrics. 124 // RecordPeriodicMetrics.
171 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) { 125 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) {
172 SetUserInActiveDesktopEnvironment(true); 126 SetSessionStarted(true);
173 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); 127 test_api().RecordPeriodicMetrics();
174 128
175 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1); 129 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1);
176 } 130 }
177 131
178 // Verify the shelf item counts recorded by the 132 // Verify the shelf item counts recorded by the
179 // UserMetricsRecorder::RecordPeriodicMetrics() method. 133 // UserMetricsRecorder::RecordPeriodicMetrics() method.
180 TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) { 134 TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) {
181 // TODO: investigate failure in mash, http://crbug.com/695629. 135 // TODO: investigate failure in mash, http://crbug.com/695629.
182 if (Shell::GetAshConfig() == Config::MASH) 136 if (Shell::GetAshConfig() == Config::MASH)
183 return; 137 return;
184 138
185 SetUserInActiveDesktopEnvironment(true); 139 SetSessionStarted(true);
186 140
187 // Make sure the shelf contains the app list launcher button. 141 // Make sure the shelf contains the app list launcher button.
188 ShelfModel* shelf_model = Shell::Get()->shelf_model(); 142 ShelfModel* shelf_model = Shell::Get()->shelf_model();
189 ASSERT_EQ(1u, shelf_model->items().size()); 143 ASSERT_EQ(1u, shelf_model->items().size());
190 ASSERT_EQ(TYPE_APP_LIST, shelf_model->items()[0].type); 144 ASSERT_EQ(TYPE_APP_LIST, shelf_model->items()[0].type);
191 145
192 ShelfItem shelf_item; 146 ShelfItem shelf_item;
193 shelf_item.type = ash::TYPE_PINNED_APP; 147 shelf_item.type = ash::TYPE_PINNED_APP;
194 shelf_item.app_launch_id = AppLaunchId("app_id_1"); 148 shelf_item.app_launch_id = AppLaunchId("app_id_1");
195 shelf_model->Add(shelf_item); 149 shelf_model->Add(shelf_item);
196 shelf_item.app_launch_id = AppLaunchId("app_id_2"); 150 shelf_item.app_launch_id = AppLaunchId("app_id_2");
197 shelf_model->Add(shelf_item); 151 shelf_model->Add(shelf_item);
198 152
199 shelf_item.type = ash::TYPE_APP; 153 shelf_item.type = ash::TYPE_APP;
200 shelf_item.app_launch_id = AppLaunchId("app_id_3"); 154 shelf_item.app_launch_id = AppLaunchId("app_id_3");
201 shelf_model->Add(shelf_item); 155 shelf_model->Add(shelf_item);
202 shelf_item.app_launch_id = AppLaunchId("app_id_4"); 156 shelf_item.app_launch_id = AppLaunchId("app_id_4");
203 shelf_model->Add(shelf_item); 157 shelf_model->Add(shelf_item);
204 shelf_item.app_launch_id = AppLaunchId("app_id_5"); 158 shelf_item.app_launch_id = AppLaunchId("app_id_5");
205 shelf_model->Add(shelf_item); 159 shelf_model->Add(shelf_item);
206 160
207 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); 161 test_api().RecordPeriodicMetrics();
208 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1); 162 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1);
209 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1); 163 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1);
210 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1); 164 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1);
211 } 165 }
212 166
213 } // namespace ash 167 } // namespace ash
OLDNEW
« no previous file with comments | « ash/metrics/user_metrics_recorder.cc ('k') | ash/test/test_system_tray_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698