Index: ash/metrics/user_metrics_recorder_unittest.cc |
diff --git a/ash/metrics/user_metrics_recorder_unittest.cc b/ash/metrics/user_metrics_recorder_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..34a4263a581b2a3eb09dc4e7b7fb55aa6fe9cb7e |
--- /dev/null |
+++ b/ash/metrics/user_metrics_recorder_unittest.cc |
@@ -0,0 +1,188 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/metrics/user_metrics_recorder.h" |
+ |
+#include "ash/shelf/shelf_layout_manager.h" |
+#include "ash/shell.h" |
+#include "ash/test/ash_test_base.h" |
+#include "ash/test/test_session_state_delegate.h" |
+#include "base/test/histogram_tester.h" |
+#include "ui/aura/window.h" |
+ |
+namespace ash { |
+namespace { |
+ |
+const char kAsh_ShelfAlignmentOverTime[] = "Ash.ShelfAlignmentOverTime"; |
+ |
+const char kAsh_NumberOfVisibleWindowsInPrimaryDisplay[] = |
+ "Ash.NumberOfVisibleWindowsInPrimaryDisplay"; |
+ |
+const char kAsh_ActiveWindowShowTypeOverTime[] = |
+ "Ash.ActiveWindowShowTypeOverTime"; |
+ |
+// Test specific subclass of a UserMetricsRecorder that promotes the visibility |
+// of some protected level functions to enable testing. The repeating timer of |
+// the UserMetricsRecorder will NOT be started. Use the |RecordPeriodicMetrics| |
+// method to trigger a recording. |
+class TestUserMetricsRecorder : public UserMetricsRecorder { |
pkotwicz
2015/04/17 13:45:38
Extending the real class is a common pattern in Ch
bruthig
2015/04/17 14:41:23
Out of curiosity when would it be necessary?
pkotwicz
2015/04/17 17:45:16
Extending the real class is necessary when the tes
sadrul
2015/04/17 22:55:39
In a number of places, we friend a TestApi class t
bruthig
2015/04/21 14:34:05
I've reworked it to use the TestAPI approach and I
|
+ public: |
+ TestUserMetricsRecorder(); |
+ ~TestUserMetricsRecorder() override; |
+ |
+ using UserMetricsRecorder::RecordPeriodicMetrics; |
+ using UserMetricsRecorder::UserIsActive; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestUserMetricsRecorder); |
+}; |
+ |
+TestUserMetricsRecorder::TestUserMetricsRecorder() |
+ : UserMetricsRecorder(false) { |
+} |
+ |
+TestUserMetricsRecorder::~TestUserMetricsRecorder() { |
+} |
+ |
+} // namespace |
+ |
+// Test fixture for the UserMetricsRecorder class. |
+class UserMetricsRecorderTest : public test::AshTestBase { |
+ public: |
+ UserMetricsRecorderTest(); |
+ ~UserMetricsRecorderTest() override; |
+ |
+ void SetUp() override; |
+ void TearDown() override; |
+ |
+ // Convenience function to trigger the periodic metrics recording of the test |
+ // target. |
+ void RecordPeriodicMetrics(); |
+ |
+ // Sets the current user session to be active. |
+ void SetUserActive(); |
+ |
+ // Sets the current user session to be inactive. |
+ void SetUserNotActive(); |
+ |
+ TestUserMetricsRecorder& user_metrics_recorder() { |
+ return user_metrics_recorder_; |
+ } |
+ |
+ base::HistogramTester& histograms() { return histograms_; } |
+ |
+ test::TestSessionStateDelegate* session_state_delegate() { |
+ return session_state_delegate_; |
+ } |
+ |
+ private: |
+ // The test target. |
+ TestUserMetricsRecorder user_metrics_recorder_; |
+ |
+ // Histogram value verifier. |
+ base::HistogramTester histograms_; |
+ |
+ // The current SessionStateDelegate used by the test fixture. Not owned. |
+ test::TestSessionStateDelegate* session_state_delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(UserMetricsRecorderTest); |
+}; |
+ |
+UserMetricsRecorderTest::UserMetricsRecorderTest() { |
+} |
+ |
+UserMetricsRecorderTest::~UserMetricsRecorderTest() { |
+} |
+ |
+void UserMetricsRecorderTest::SetUp() { |
+ test::AshTestBase::SetUp(); |
+ |
+ session_state_delegate_ = static_cast<ash::test::TestSessionStateDelegate*>( |
+ ash::Shell::GetInstance()->session_state_delegate()); |
+} |
+ |
+void UserMetricsRecorderTest::TearDown() { |
+ session_state_delegate_ = nullptr; |
+ |
+ test::AshTestBase::TearDown(); |
+} |
+ |
+void UserMetricsRecorderTest::RecordPeriodicMetrics() { |
+ user_metrics_recorder_.RecordPeriodicMetrics(); |
+} |
+ |
+void UserMetricsRecorderTest::SetUserActive() { |
+ SetSessionStarted(true); |
+ session_state_delegate()->UnlockScreen(); |
+ ASSERT_TRUE(user_metrics_recorder().UserIsActive()); |
+} |
+ |
+void UserMetricsRecorderTest::SetUserNotActive() { |
+ SetSessionStarted(false); |
+ session_state_delegate()->UnlockScreen(); |
+ ASSERT_FALSE(user_metrics_recorder().UserIsActive()); |
+} |
+ |
+// Verify that the user is active when logged in and the screen is unlocked. |
+TEST_F(UserMetricsRecorderTest, VerifyUserIsActiveWhenLoggedIn) { |
+ SetSessionStarted(true); |
+ session_state_delegate()->UnlockScreen(); |
+ EXPECT_TRUE(user_metrics_recorder().UserIsActive()); |
+} |
+ |
+// Verify that the user is not active when not logged in. |
+TEST_F(UserMetricsRecorderTest, VerifyUserIsNotActiveWhenNotLoggedIn) { |
+ SetSessionStarted(false); |
+ session_state_delegate()->UnlockScreen(); |
+ EXPECT_FALSE(user_metrics_recorder().UserIsActive()); |
+} |
+ |
+// Verify that the user is not active when logged in but the screen is locked. |
+TEST_F(UserMetricsRecorderTest, VerifyUserIsNotActiveWhenAccountIsLocked) { |
+ SetSessionStarted(true); |
+ session_state_delegate()->LockScreen(); |
+ EXPECT_FALSE(user_metrics_recorder().UserIsActive()); |
+} |
+ |
+// Verifies that the stats dependent on the presence of a ShelfLayoutManager |
+// are recorded when a ShelfLayoutManager exists. |
+// TODO(bruthig): Add a test to verify the stats are not recorded when a |
+// ShelfLayoutManager does not exist. |
pkotwicz
2015/04/17 13:45:38
When does the ShelfLayoutManager not exist?
bruthig
2015/04/17 14:41:23
I'm not sure, I'm not familiar with this and only
pkotwicz
2015/04/17 17:45:16
I think that the test would 10x more useful if it
bruthig
2015/04/21 14:34:05
To the best of my knowledge a ShelfLayoutManager m
|
+TEST_F(UserMetricsRecorderTest, |
+ VerifyStatsRecordedWhenAShelfLayoutManagerExists) { |
+ SetUserActive(); |
+ ASSERT_TRUE(ShelfLayoutManager::ForShelf(Shell::GetPrimaryRootWindow())); |
+ RecordPeriodicMetrics(); |
+ |
+ histograms().ExpectTotalCount(kAsh_ShelfAlignmentOverTime, 1); |
+} |
+ |
+// Verifies that the UserIsActive() dependent stats are not recorded when a |
+// user is not active. |
+TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedWhenUserIsNotActive) { |
+ SetUserNotActive(); |
+ RecordPeriodicMetrics(); |
+ |
+ histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 0); |
+} |
+ |
+// Verifies that the UserIsActive() dependent stats are recorded when a user |
+// is active. |
+TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedWhenUserIsActive) { |
+ SetUserActive(); |
+ RecordPeriodicMetrics(); |
+ |
+ histograms().ExpectTotalCount(kAsh_NumberOfVisibleWindowsInPrimaryDisplay, 1); |
+} |
+ |
+// Verifies which stats are always recorded and are not dependent on conditional |
+// logic for when they are recorded. |
+TEST_F(UserMetricsRecorderTest, VerifyStatsRecordedByRecordPeriodicMetrics) { |
+ SetUserActive(); |
+ RecordPeriodicMetrics(); |
+ |
+ histograms().ExpectTotalCount(kAsh_ActiveWindowShowTypeOverTime, 1); |
+} |
+ |
+} // namespace ash |