Remove use of JSONReader::DeprecatedRead from activity_log
[chromium-blink-merge.git] / ash / metrics / user_metrics_recorder_unittest.cc
blobf2ffd329f11965ed0ca8a7f358ffa6e7bb815dfa
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"
7 #include "ash/shelf/shelf_model.h"
8 #include "ash/shelf/shelf_util.h"
9 #include "ash/shell.h"
10 #include "ash/system/user/login_status.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/test_shelf_delegate.h"
13 #include "ash/test/test_system_tray_delegate.h"
14 #include "ash/test/user_metrics_recorder_test_api.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/test/histogram_tester.h"
17 #include "ui/aura/window.h"
19 namespace ash {
20 namespace {
22 const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay[] =
23 "Ash.NumberOfVisibleWindowsInPrimaryDisplay";
25 const char kAsh_ActiveWindowShowTypeOverTime[] =
26 "Ash.ActiveWindowShowTypeOverTime";
28 const char kAsh_Shelf_NumberOfItems[] = "Ash.Shelf.NumberOfItems";
30 const char kAsh_Shelf_NumberOfPinnedItems[] = "Ash.Shelf.NumberOfPinnedItems";
32 const char kAsh_Shelf_NumberOfUnpinnedItems[] =
33 "Ash.Shelf.NumberOfUnpinnedItems";
35 } // namespace
37 // Test fixture for the UserMetricsRecorder class.
38 class UserMetricsRecorderTest : public test::AshTestBase {
39 public:
40 UserMetricsRecorderTest();
41 ~UserMetricsRecorderTest() override;
43 // test::AshTestBase:
44 void SetUp() override;
45 void TearDown() override;
47 // Sets the user login status.
48 void SetLoginStatus(user::LoginStatus login_status);
50 // Sets the current user session to be active or inactive in a desktop
51 // environment.
52 void SetUserInActiveDesktopEnvironment(bool is_active);
54 // Creates an aura::Window.
55 aura::Window* CreateTestWindow();
57 test::UserMetricsRecorderTestAPI* user_metrics_recorder_test_api() {
58 return user_metrics_recorder_test_api_.get();
61 base::HistogramTester& histograms() { return histograms_; }
63 private:
64 // Test API to access private members of the test target.
65 scoped_ptr<test::UserMetricsRecorderTestAPI> user_metrics_recorder_test_api_;
67 // Histogram value verifier.
68 base::HistogramTester histograms_;
70 // The active SystemTrayDelegate. Not owned.
71 test::TestSystemTrayDelegate* test_system_tray_delegate_;
73 DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest);
76 UserMetricsRecorderTest::UserMetricsRecorderTest() {
79 UserMetricsRecorderTest::~UserMetricsRecorderTest() {
82 void UserMetricsRecorderTest::SetUp() {
83 test::AshTestBase::SetUp();
84 user_metrics_recorder_test_api_.reset(new test::UserMetricsRecorderTestAPI());
85 test_system_tray_delegate_ = GetSystemTrayDelegate();
88 void UserMetricsRecorderTest::TearDown() {
89 test_system_tray_delegate_ = nullptr;
90 test::AshTestBase::TearDown();
93 void UserMetricsRecorderTest::SetLoginStatus(user::LoginStatus login_status) {
94 test_system_tray_delegate_->SetLoginStatus(login_status);
97 void UserMetricsRecorderTest::SetUserInActiveDesktopEnvironment(
98 bool is_active) {
99 if (is_active) {
100 SetLoginStatus(user::LOGGED_IN_USER);
101 ASSERT_TRUE(
102 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
103 } else {
104 SetLoginStatus(user::LOGGED_IN_LOCKED);
105 ASSERT_FALSE(
106 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
110 aura::Window* UserMetricsRecorderTest::CreateTestWindow() {
111 aura::Window* window = CreateTestWindowInShellWithDelegateAndType(
112 nullptr, ui::wm::WINDOW_TYPE_NORMAL, 0, gfx::Rect());
113 return window;
116 // Verifies the return value of IsUserInActiveDesktopEnvironment() for the
117 // different login status values.
118 TEST_F(UserMetricsRecorderTest, VerifyIsUserInActiveDesktopEnvironmentValues) {
119 SetLoginStatus(user::LOGGED_IN_NONE);
120 EXPECT_FALSE(
121 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
123 SetLoginStatus(user::LOGGED_IN_LOCKED);
124 EXPECT_FALSE(
125 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
127 SetLoginStatus(user::LOGGED_IN_USER);
128 EXPECT_TRUE(
129 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
131 SetLoginStatus(user::LOGGED_IN_OWNER);
132 EXPECT_TRUE(
133 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
135 SetLoginStatus(user::LOGGED_IN_GUEST);
136 EXPECT_TRUE(
137 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
139 SetLoginStatus(user::LOGGED_IN_PUBLIC);
140 EXPECT_TRUE(
141 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
143 SetLoginStatus(user::LOGGED_IN_SUPERVISED);
144 EXPECT_TRUE(
145 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
147 SetLoginStatus(user::LOGGED_IN_KIOSK_APP);
148 EXPECT_FALSE(
149 user_metrics_recorder_test_api()->IsUserInActiveDesktopEnvironment());
152 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are not
153 // recorded when a user is not active in a desktop environment.
154 TEST_F(UserMetricsRecorderTest,
155 VerifyStatsRecordedWhenUserNotInActiveDesktopEnvironment) {
156 SetUserInActiveDesktopEnvironment(false);
157 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
159 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0);
160 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 0);
161 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 0);
162 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 0);
165 // Verifies that the IsUserInActiveDesktopEnvironment() dependent stats are
166 // recorded when a user is active in a desktop environment.
167 TEST_F(UserMetricsRecorderTest,
168 VerifyStatsRecordedWhenUserInActiveDesktopEnvironment) {
169 SetUserInActiveDesktopEnvironment(true);
170 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
172 histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1);
173 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfItems, 1);
174 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfPinnedItems, 1);
175 histograms().ExpectTotalCount(kAsh_Shelf_NumberOfUnpinnedItems, 1);
178 // Verifies recording of stats which are always recorded by
179 // RecordPeriodicMetrics.
180 TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) {
181 SetUserInActiveDesktopEnvironment(true);
182 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
184 histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1);
187 // Verify the shelf item counts recorded by the
188 // UserMetricsRecorder::RecordPeriodicMetrics() method.
189 TEST_F(UserMetricsRecorderTest, ValuesRecordedByRecordShelfItemCounts) {
190 test::TestShelfDelegate* test_shelf_delegate =
191 test::TestShelfDelegate::instance();
192 SetUserInActiveDesktopEnvironment(true);
194 // Make sure the shelf contains the app list launcher button.
195 const ShelfItems& shelf_items = Shell::GetInstance()->shelf_model()->items();
196 ASSERT_EQ(1u, shelf_items.size());
197 ASSERT_EQ(TYPE_APP_LIST, shelf_items[0].type);
199 aura::Window* pinned_window_with_app_id_1 = CreateTestWindow();
200 test_shelf_delegate->AddShelfItem(pinned_window_with_app_id_1, "app_id_1");
201 test_shelf_delegate->PinAppWithID("app_id_1");
203 aura::Window* pinned_window_with_app_id_2 = CreateTestWindow();
204 test_shelf_delegate->AddShelfItem(pinned_window_with_app_id_2, "app_id_2");
205 test_shelf_delegate->PinAppWithID("app_id_2");
207 aura::Window* unpinned_window_with_app_id_3 = CreateTestWindow();
208 test_shelf_delegate->AddShelfItem(unpinned_window_with_app_id_3, "app_id_3");
210 aura::Window* unpinned_window_4 = CreateTestWindow();
211 test_shelf_delegate->AddShelfItem(unpinned_window_4);
213 aura::Window* unpinned_window_5 = CreateTestWindow();
214 test_shelf_delegate->AddShelfItem(unpinned_window_5);
216 user_metrics_recorder_test_api()->RecordPeriodicMetrics();
217 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfItems, 5, 1);
218 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfPinnedItems, 2, 1);
219 histograms().ExpectBucketCount(kAsh_Shelf_NumberOfUnpinnedItems, 3, 1);
222 } // namespace ash