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

Unified Diff: chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc

Issue 2142983002: Add desktop engagement metrics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc
diff --git a/chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc b/chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..562934b408711379ac3541eedbc6adeb22a1051f
--- /dev/null
+++ b/chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc
@@ -0,0 +1,152 @@
+// Copyright 2016 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 "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h"
+
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/test/histogram_tester.h"
+#include "base/test/test_mock_time_task_runner.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+// Mock class for |DesktopEngagementService| for testing.
+class MockDesktopEngagementService : public DesktopEngagementService {
+ public:
+ bool IsVisible() { return is_visible_; }
+
Alexei Svitkine (slow) 2016/07/26 19:20:37 Nit: You can omit newlines for these simple access
gayane -on leave until 09-2017 2016/07/26 21:41:19 Done.
+ bool InSession() { return in_session_; }
+
+ bool IsAudioPlaying() { return is_audio_playing_; }
+
+ bool IsTimeOut() { return time_out_; }
+
+ void SetInactivityTimeout(int seconds) {
+ inactivity_timeout_ = base::TimeDelta::FromSeconds(seconds);
+ }
+
+ protected:
+ void OnTimerFired() override {
+ DesktopEngagementService::OnTimerFired();
+ time_out_ = true;
+ }
+
+ private:
+ bool time_out_ = false;
+};
+
+TEST(DesktopEngagementServiceTest, TestVisibility) {
+ base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
+ base::HistogramTester histogram_tester;
+
+ MockDesktopEngagementService instance;
+
+ // The browser becomes visible but it shouldn't start the session.
+ instance.OnVisibilityChanged(true);
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ instance.OnUserEvent();
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ // Even if there is a recent user event visibility change should end the
+ // session.
+ instance.OnUserEvent();
+ instance.OnUserEvent();
+ instance.OnVisibilityChanged(false);
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
+
+ // For the second time only visibility change should start the session.
+ instance.OnVisibilityChanged(true);
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
+ instance.OnVisibilityChanged(false);
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 2);
+}
+
+TEST(DesktopEngagementServiceTest, TestUserEvent) {
+ base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
+ base::HistogramTester histogram_tester;
+
+ MockDesktopEngagementService instance;
+ instance.SetInactivityTimeout(1);
+
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ // User event doesn't go through if nothing is visible.
+ instance.OnUserEvent();
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ instance.OnVisibilityChanged(true);
+ instance.OnUserEvent();
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ // Wait until the session expires.
+ while (!instance.IsTimeOut()) {
+ base::RunLoop().RunUntilIdle();
+ }
+
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
+}
+
+TEST(DesktopEngagementServiceTest, TestAudioEvent) {
+ base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
+ base::HistogramTester histogram_tester;
+
+ MockDesktopEngagementService instance;
+ instance.SetInactivityTimeout(1);
+
+ instance.OnVisibilityChanged(true);
+ instance.OnAudioStart();
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_TRUE(instance.IsVisible());
+ EXPECT_TRUE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ instance.OnVisibilityChanged(false);
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_TRUE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ instance.OnAudioEnd();
+ EXPECT_TRUE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
+
+ // Wait until the session expires.
+ while (!instance.IsTimeOut()) {
+ base::RunLoop().RunUntilIdle();
+ }
+
+ EXPECT_FALSE(instance.InSession());
+ EXPECT_FALSE(instance.IsVisible());
+ EXPECT_FALSE(instance.IsAudioPlaying());
+ histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
+}

Powered by Google App Engine
This is Rietveld 408576698