| OLD | NEW |
| 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("app@kiosk-apps.device-local.localhost", |
| 85 user_manager::USER_TYPE_KIOSK_APP); |
| 86 client->SetSessionState(session_manager::SessionState::ACTIVE); |
| 87 EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment()); |
| 129 | 88 |
| 130 SetLoginStatus(LoginStatus::PUBLIC); | 89 // Arc kiosk logins are not considered active. |
| 131 EXPECT_TRUE( | 90 client->Reset(); |
| 132 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment()); | 91 client->AddUserSession("app@arc-kiosk-apps.device-local.localhost", |
| 133 | 92 user_manager::USER_TYPE_ARC_KIOSK_APP); |
| 134 SetLoginStatus(LoginStatus::SUPERVISED); | 93 client->SetSessionState(session_manager::SessionState::ACTIVE); |
| 135 EXPECT_TRUE( | 94 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 } | 95 } |
| 142 | 96 |
| 143 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not | 97 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not |
| 144 // recorded when a user is not active in a desktop environment. | 98 // recorded when a user is not active in a desktop environment. |
| 145 TEST_F(UserMetricsRecorderTest, | 99 TEST_F(UserMetricsRecorderTest, |
| 146 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) { | 100 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) { |
| 147 SetUserInActiveDesktopEnvironment(false); | 101 ASSERT_FALSE(test_api().IsUserInActiveDesktopEnvironment()); |
| 148 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); | 102 test_api().RecordPeriodicMetrics(); |
| 149 | 103 |
| 150 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0); | 104 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0); |
| 151 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0); | 105 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0); |
| 152 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0); | 106 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0); |
| 153 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0); | 107 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0); |
| 154 } | 108 } |
| 155 | 109 |
| 156 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are | 110 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are |
| 157 // recorded when a user is active in a desktop environment. | 111 // recorded when a user is active in a desktop environment. |
| 158 TEST_F(UserMetricsRecorderTest, | 112 TEST_F(UserMetricsRecorderTest, |
| 159 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) { | 113 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) { |
| 160 SetUserInActiveDesktopEnvironment(true); | 114 SetSessionStarted(true); |
| 161 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); | 115 ASSERT_TRUE(test_api().IsUserInActiveDesktopEnvironment()); |
| 116 test_api().RecordPeriodicMetrics(); |
| 162 | 117 |
| 163 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1); | 118 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1); |
| 164 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1); | 119 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1); |
| 165 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1); | 120 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1); |
| 166 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1); | 121 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1); |
| 167 } | 122 } |
| 168 | 123 |
| 169 // Verifies recording of stats which are always recorded by | 124 // Verifies recording of stats which are always recorded by |
| 170 // RecordPeriodicMetrics. | 125 // RecordPeriodicMetrics. |
| 171 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) { | 126 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) { |
| 172 SetUserInActiveDesktopEnvironment(true); | 127 SetSessionStarted(true); |
| 173 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); | 128 test_api().RecordPeriodicMetrics(); |
| 174 | 129 |
| 175 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1); | 130 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1); |
| 176 } | 131 } |
| 177 | 132 |
| 178 // Verify the shelf item counts recorded by the | 133 // Verify the shelf item counts recorded by the |
| 179 // UserMetricsRecorder::RecordPeriodicMetrics() method. | 134 // UserMetricsRecorder::RecordPeriodicMetrics() method. |
| 180 TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) { | 135 TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) { |
| 181 // TODO: investigate failure in mash, http://crbug.com/695629. | 136 // TODO: investigate failure in mash, http://crbug.com/695629. |
| 182 if (Shell::GetAshConfig() == Config::MASH) | 137 if (Shell::GetAshConfig() == Config::MASH) |
| 183 return; | 138 return; |
| 184 | 139 |
| 185 SetUserInActiveDesktopEnvironment(true); | 140 SetSessionStarted(true); |
| 186 | 141 |
| 187 // Make sure the shelf contains the app list launcher button. | 142 // Make sure the shelf contains the app list launcher button. |
| 188 ShelfModel* shelf_model = Shell::Get()->shelf_model(); | 143 ShelfModel* shelf_model = Shell::Get()->shelf_model(); |
| 189 ASSERT_EQ(1u, shelf_model->items().size()); | 144 ASSERT_EQ(1u, shelf_model->items().size()); |
| 190 ASSERT_EQ(TYPE_APP_LIST, shelf_model->items()[0].type); | 145 ASSERT_EQ(TYPE_APP_LIST, shelf_model->items()[0].type); |
| 191 | 146 |
| 192 ShelfItem shelf_item; | 147 ShelfItem shelf_item; |
| 193 shelf_item.type = ash::TYPE_PINNED_APP; | 148 shelf_item.type = ash::TYPE_PINNED_APP; |
| 194 shelf_item.app_launch_id = AppLaunchId("app_id_1"); | 149 shelf_item.app_launch_id = AppLaunchId("app_id_1"); |
| 195 shelf_model->Add(shelf_item); | 150 shelf_model->Add(shelf_item); |
| 196 shelf_item.app_launch_id = AppLaunchId("app_id_2"); | 151 shelf_item.app_launch_id = AppLaunchId("app_id_2"); |
| 197 shelf_model->Add(shelf_item); | 152 shelf_model->Add(shelf_item); |
| 198 | 153 |
| 199 shelf_item.type = ash::TYPE_APP; | 154 shelf_item.type = ash::TYPE_APP; |
| 200 shelf_item.app_launch_id = AppLaunchId("app_id_3"); | 155 shelf_item.app_launch_id = AppLaunchId("app_id_3"); |
| 201 shelf_model->Add(shelf_item); | 156 shelf_model->Add(shelf_item); |
| 202 shelf_item.app_launch_id = AppLaunchId("app_id_4"); | 157 shelf_item.app_launch_id = AppLaunchId("app_id_4"); |
| 203 shelf_model->Add(shelf_item); | 158 shelf_model->Add(shelf_item); |
| 204 shelf_item.app_launch_id = AppLaunchId("app_id_5"); | 159 shelf_item.app_launch_id = AppLaunchId("app_id_5"); |
| 205 shelf_model->Add(shelf_item); | 160 shelf_model->Add(shelf_item); |
| 206 | 161 |
| 207 user_metrics_recorder_test_api()->RecordPeriodicMetrics(); | 162 test_api().RecordPeriodicMetrics(); |
| 208 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1); | 163 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1); |
| 209 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1); | 164 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1); |
| 210 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1); | 165 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1); |
| 211 } | 166 } |
| 212 | 167 |
| 213 } // namespace ash | 168 } // namespace ash |
| OLD | NEW |