File: touch_mode_stats_tracker_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (192 lines) | stat: -rw-r--r-- 6,285 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020 The Chromium Authors
// 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_session_duration/touch_mode_stats_tracker.h"

#include <memory>

#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "chrome/browser/metrics/desktop_session_duration/desktop_session_duration_tracker.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/pointer/touch_ui_controller.h"

namespace {

constexpr base::TimeDelta kInactivityTimeout = base::Minutes(5);

class SessionEndWaiter
    : public metrics::DesktopSessionDurationTracker::Observer {
 public:
  explicit SessionEndWaiter(metrics::DesktopSessionDurationTracker* tracker)
      : tracker_(tracker) {
    tracker_->AddObserver(this);
  }

  ~SessionEndWaiter() override { tracker_->RemoveObserver(this); }

  void Wait() {
    ASSERT_FALSE(waiting_);
    if (!tracker_->in_session())
      return;

    waiting_ = true;
    base::RunLoop run_loop;
    end_closure_ = run_loop.QuitClosure();
    run_loop.Run();
  }

  // metrics::DesktopSessionDurationTracker::Observer:
  void OnSessionEnded(base::TimeDelta session_length,
                      base::TimeTicks session_end) override {
    if (!waiting_)
      return;
    end_closure_.Run();
  }

 private:
  raw_ptr<metrics::DesktopSessionDurationTracker> tracker_;
  base::RepeatingClosure end_closure_;
  bool waiting_ = false;
};

}  // namespace

class TouchModeStatsTrackerTest : public ::testing::Test {
 public:
  TouchModeStatsTrackerTest()
      : profile_manager_(TestingBrowserProcess::GetGlobal()) {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kNoFirstRun);
  }
  ~TouchModeStatsTrackerTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("p1");

    metrics::DesktopSessionDurationTracker::Initialize();
    metrics::DesktopSessionDurationTracker::Get()
        ->SetInactivityTimeoutForTesting(kInactivityTimeout);
    touch_mode_stats_tracker_ = std::make_unique<TouchModeStatsTracker>(
        metrics::DesktopSessionDurationTracker::Get(),
        ui::TouchUiController::Get());

    Browser::CreateParams params(profile_, false);
    browser_ = CreateBrowserWithTestWindowForParams(params);
  }

  void TearDown() override {
    browser_.reset();
    touch_mode_stats_tracker_.reset();
    metrics::DesktopSessionDurationTracker::CleanupForTesting();
  }

  void StartSession() {
    BrowserList::SetLastActive(browser_.get());
    task_environment_.RunUntilIdle();
    metrics::DesktopSessionDurationTracker::Get()->OnUserEvent();
  }

  void EndSession() {
    BrowserList::NotifyBrowserNoLongerActive(browser_.get());
    SessionEndWaiter waiter(metrics::DesktopSessionDurationTracker::Get());
    waiter.Wait();
  }

  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  raw_ptr<Profile, DanglingUntriaged> profile_;
  std::unique_ptr<TouchModeStatsTracker> touch_mode_stats_tracker_;

 private:
  TestingProfileManager profile_manager_;
  std::unique_ptr<Browser> browser_;
};

// An entire session spent in touch mode should be logged accordingly.
TEST_F(TouchModeStatsTrackerTest, TouchSession) {
  ui::TouchUiController::TouchUiScoperForTesting enable_touch_mode(true);
  base::HistogramTester histograms;

  StartSession();
  ASSERT_TRUE(metrics::DesktopSessionDurationTracker::Get()->in_session());
  task_environment_.FastForwardBy(base::Minutes(1));
  EndSession();

  histograms.ExpectUniqueTimeSample(
      TouchModeStatsTracker::kSessionTouchDurationHistogramName,
      base::Minutes(1), 1);
}

// The touch duration logged should be 0 for a non-touch session.
TEST_F(TouchModeStatsTrackerTest, NonTouchSession) {
  ui::TouchUiController::TouchUiScoperForTesting disable_touch_mode(false);
  base::HistogramTester histograms;

  StartSession();
  task_environment_.FastForwardBy(base::Minutes(1));
  EndSession();

  histograms.ExpectUniqueTimeSample(
      TouchModeStatsTracker::kSessionTouchDurationHistogramName,
      base::TimeDelta(), 1);
}

// If the touch mode changes during a session, the logged duration
// should comprise the session time spent in touch mode.
TEST_F(TouchModeStatsTrackerTest, TouchChangesDuringSession) {
  ui::TouchUiController::TouchUiScoperForTesting touch_mode_override(false);

  // Check starting in touch mode.
  {
    base::HistogramTester histograms;
    StartSession();

    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(true);
    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(false);
    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(true);
    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(false);
    task_environment_.FastForwardBy(base::Seconds(15));

    EndSession();
    histograms.ExpectUniqueTimeSample(
        TouchModeStatsTracker::kSessionTouchDurationHistogramName,
        base::Seconds(30), 1);
  }

  touch_mode_override.UpdateState(true);
  task_environment_.FastForwardBy(base::Seconds(15));

  // Check starting in non-touch mode.
  {
    base::HistogramTester histograms;
    StartSession();

    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(false);
    task_environment_.FastForwardBy(base::Seconds(15));
    touch_mode_override.UpdateState(true);
    task_environment_.FastForwardBy(base::Seconds(15));

    EndSession();
    histograms.ExpectUniqueTimeSample(
        TouchModeStatsTracker::kSessionTouchDurationHistogramName,
        base::Seconds(30), 1);
  }
}