user_metrics_recorder_unittest.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.
  4. #include "ash/metrics/user_metrics_recorder.h"
  5. #include <memory>
  6. #include "ash/login_status.h"
  7. #include "ash/metrics/user_metrics_recorder_test_api.h"
  8. #include "ash/public/cpp/shelf_model.h"
  9. #include "ash/public/cpp/test/test_shelf_item_delegate.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/session/test_session_controller_client.h"
  12. #include "ash/shell.h"
  13. #include "ash/test/ash_test_base.h"
  14. #include "base/test/metrics/histogram_tester.h"
  15. using session_manager::SessionState;
  16. namespace ash {
  17. namespace {
  18. const char kAsh_ActiveWindowShowTypeOverTime[] =
  19. "Ash.ActiveWindowShowTypeOverTime";
  20. const char kAsh_Shelf_NumberOfItems[] = "Ash.Shelf.NumberOfItems";
  21. const char kAsh_Shelf_NumberOfPinnedItems[] = "Ash.Shelf.NumberOfPinnedItems";
  22. const char kAsh_Shelf_NumberOfUnpinnedItems[] =
  23. "Ash.Shelf.NumberOfUnpinnedItems";
  24. const char kAsh_NotificationBadgeShownPref[] = "Ash.AppNotificationBadgingPref";
  25. } // namespace
  26. // Test fixture for the UserMetricsRecorder class. The tests manage their own
  27. // session state.
  28. class UserMetricsRecorderTest : public NoSessionAshTestBase {
  29. public:
  30. UserMetricsRecorderTest() = default;
  31. UserMetricsRecorderTest(const UserMetricsRecorderTest&) = delete;
  32. UserMetricsRecorderTest& operator=(const UserMetricsRecorderTest&) = delete;
  33. ~UserMetricsRecorderTest() override = default;
  34. UserMetricsRecorderTestAPI& test_api() { return test_api_; }
  35. base::HistogramTester& histograms() { return histograms_; }
  36. private:
  37. // Test API to access private members of the test target.
  38. UserMetricsRecorderTestAPI test_api_;
  39. // Histogram value verifier.
  40. base::HistogramTester histograms_;
  41. };
  42. // Verifies the return value of IsUserInActiveDesktopEnvironment() for the
  43. // different login status values.
  44. TEST_F(UserMetricsRecorderTest, VerifyIsUserInActiveDesktopEnvironmentValues) {
  45. SessionControllerImpl* session = Shell::Get()->session_controller();
  46. // Environment is not active before login.
  47. ASSERT_FALSE(session->IsActiveUserSessionStarted());
  48. EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
  49. // Environment is active after login.
  50. CreateUserSessions(1);
  51. ASSERT_TRUE(session->IsActiveUserSessionStarted());
  52. EXPECT_TRUE(test_api().IsUserInActiveDesktopEnvironment());
  53. // Environment is not active when screen is locked.
  54. TestSessionControllerClient* client = GetSessionControllerClient();
  55. client->SetSessionState(SessionState::LOCKED);
  56. ASSERT_TRUE(session->IsScreenLocked());
  57. EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
  58. // Kiosk logins are not considered active.
  59. client->Reset();
  60. client->AddUserSession("app@kiosk-apps.device-local.localhost",
  61. user_manager::USER_TYPE_KIOSK_APP);
  62. client->SetSessionState(session_manager::SessionState::ACTIVE);
  63. EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
  64. // Arc kiosk logins are not considered active.
  65. client->Reset();
  66. client->AddUserSession("app@arc-kiosk-apps.device-local.localhost",
  67. user_manager::USER_TYPE_ARC_KIOSK_APP);
  68. client->SetSessionState(session_manager::SessionState::ACTIVE);
  69. EXPECT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
  70. }
  71. // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not
  72. // recorded when a user is not active in a desktop environment.
  73. TEST_F(UserMetricsRecorderTest,
  74. VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) {
  75. ASSERT_FALSE(test_api().IsUserInActiveDesktopEnvironment());
  76. test_api().RecordPeriodicMetrics();
  77. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0);
  78. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0);
  79. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0);
  80. histograms().ExpectTotalCount(kAsh_NotificationBadgeShownPref, 0);
  81. }
  82. // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are
  83. // recorded when a user is active in a desktop environment.
  84. TEST_F(UserMetricsRecorderTest,
  85. VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) {
  86. CreateUserSessions(1);
  87. ASSERT_TRUE(test_api().IsUserInActiveDesktopEnvironment());
  88. test_api().RecordPeriodicMetrics();
  89. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1);
  90. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1);
  91. histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1);
  92. histograms().ExpectTotalCount(kAsh_NotificationBadgeShownPref, 1);
  93. }
  94. // Verifies recording of stats which are always recorded by
  95. // RecordPeriodicMetrics.
  96. TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) {
  97. CreateUserSessions(1);
  98. test_api().RecordPeriodicMetrics();
  99. histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1);
  100. }
  101. // Verify the shelf item counts recorded by the
  102. // UserMetricsRecorder::RecordPeriodicMetrics() method.
  103. TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) {
  104. CreateUserSessions(1);
  105. // Make sure the shelf model is empty at first.
  106. ShelfModel* shelf_model = ShelfModel::Get();
  107. ASSERT_EQ(0u, shelf_model->items().size());
  108. ShelfItem shelf_item;
  109. shelf_item.type = TYPE_PINNED_APP;
  110. shelf_item.id = ShelfID("app_id_1");
  111. shelf_model->Add(shelf_item,
  112. std::make_unique<TestShelfItemDelegate>(shelf_item.id));
  113. shelf_item.id = ShelfID("app_id_2");
  114. shelf_model->Add(shelf_item,
  115. std::make_unique<TestShelfItemDelegate>(shelf_item.id));
  116. shelf_item.type = TYPE_APP;
  117. shelf_item.id = ShelfID("app_id_3");
  118. shelf_model->Add(shelf_item,
  119. std::make_unique<TestShelfItemDelegate>(shelf_item.id));
  120. shelf_item.id = ShelfID("app_id_4");
  121. shelf_model->Add(shelf_item,
  122. std::make_unique<TestShelfItemDelegate>(shelf_item.id));
  123. shelf_item.id = ShelfID("app_id_5");
  124. shelf_model->Add(shelf_item,
  125. std::make_unique<TestShelfItemDelegate>(shelf_item.id));
  126. test_api().RecordPeriodicMetrics();
  127. histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1);
  128. histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1);
  129. histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1);
  130. }
  131. } // namespace ash