File: application_breadcrumbs_logger_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (163 lines) | stat: -rw-r--r-- 6,005 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
// 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 "components/breadcrumbs/core/application_breadcrumbs_logger.h"

#include "base/containers/circular_deque.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/metrics/user_metrics_action.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/breadcrumbs/core/breadcrumb_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

#if defined(TOOLKIT_VIEWS)
#include "ui/views/test/native_widget_factory.h"
#include "ui/views/test/views_test_base.h"
#endif  // TOOLKIT_VIEWS

namespace breadcrumbs {

namespace {

// The particular UserActions used here are not important, but real UserAction
// names are used to prevent a presubmit warning.
const char kUserAction1Name[] = "MobileMenuNewTab";
const char kUserAction2Name[] = "OverscrollActionCloseTab";
// An "InProductHelp.*" user action.
const char kInProductHelpUserActionName[] = "InProductHelp.Dismissed";

// Returns a list of breadcrumb events logged so far.
const base::circular_deque<std::string>& GetEvents() {
  return breadcrumbs::BreadcrumbManager::GetInstance().GetEvents();
}

// Returns true if no breadcrumb events except "Startup" have been logged so
// far.
bool OnlyStartupEventLogged() {
  const auto& events = GetEvents();
  return events.size() == 1u &&
         events.back().find("Startup") != std::string::npos;
}

}  // namespace

// Test fixture for testing ApplicationBreadcrumbsLogger class.
class ApplicationBreadcrumbsLoggerTest : public PlatformTest {
 protected:
  ApplicationBreadcrumbsLoggerTest() {
    base::SetRecordActionTaskRunner(
        task_environment_.GetMainThreadTaskRunner());
    CHECK(temp_dir_.CreateUniqueTempDir());
    logger_ = std::make_unique<ApplicationBreadcrumbsLogger>(
        temp_dir_.GetPath(),
        /*is_metrics_enabled_callback=*/base::BindRepeating(
            [] { return true; }));
  }

  // This must be created before `task_environment_`, to ensure that any tasks
  // that depend on the directory existing (e.g., those posted by `logger_`)
  // have finished.
  base::ScopedTempDir temp_dir_;

  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<ApplicationBreadcrumbsLogger> logger_;
};

// Tests that a recorded UserAction is logged by the
// ApplicationBreadcrumbsLogger.
TEST_F(ApplicationBreadcrumbsLoggerTest, UserAction) {
  ASSERT_TRUE(OnlyStartupEventLogged());

  base::RecordAction(base::UserMetricsAction(kUserAction1Name));
  base::RecordAction(base::UserMetricsAction(kUserAction2Name));

  auto events = GetEvents();
  ASSERT_EQ(3u, events.size());
  events.pop_front();
  EXPECT_NE(std::string::npos, events.front().find(kUserAction1Name));
  events.pop_front();
  EXPECT_NE(std::string::npos, events.front().find(kUserAction2Name));
}

// Tests that not_user_triggered User Action does not show up in breadcrumbs.
TEST_F(ApplicationBreadcrumbsLoggerTest, LogNotUserTriggeredAction) {
  ASSERT_TRUE(OnlyStartupEventLogged());
  base::RecordAction(base::UserMetricsAction("ActiveTabChanged"));
  EXPECT_TRUE(OnlyStartupEventLogged());
}

// Tests that "InProductHelp" UserActions are not logged by
// ApplicationBreadcrumbsLogger as they are very noisy.
TEST_F(ApplicationBreadcrumbsLoggerTest, SkipInProductHelpUserActions) {
  ASSERT_TRUE(OnlyStartupEventLogged());
  base::RecordAction(base::UserMetricsAction(kInProductHelpUserActionName));
  EXPECT_TRUE(OnlyStartupEventLogged());
}

// Tests that memory pressure events are logged by ApplicationBreadcrumbsLogger.
// Test is flaky (https://crbug.com/1305253)
TEST_F(ApplicationBreadcrumbsLoggerTest, MemoryPressure) {
  ASSERT_TRUE(OnlyStartupEventLogged());

  base::MemoryPressureListener::SimulatePressureNotification(
      base::MemoryPressureListener::MemoryPressureLevel::
          MEMORY_PRESSURE_LEVEL_MODERATE);
  base::MemoryPressureListener::SimulatePressureNotification(
      base::MemoryPressureListener::MemoryPressureLevel::
          MEMORY_PRESSURE_LEVEL_CRITICAL);
  base::RunLoop().RunUntilIdle();

  EXPECT_FALSE(OnlyStartupEventLogged());
  auto events = GetEvents();
  ASSERT_EQ(3u, events.size());
  // Pop startup.
  events.pop_front();
  EXPECT_NE(std::string::npos, events.front().find("Moderate"));
  // Ensure UserAction events are labeled as such.
  EXPECT_NE(std::string::npos, events.front().find("Memory Pressure: "));
  events.pop_front();
  EXPECT_NE(std::string::npos, events.front().find("Critical"));
}

#if defined(TOOLKIT_VIEWS)
class ApplicationBreadcrumbsLoggerWidgetTest : public views::ViewsTestBase {
 protected:
  ApplicationBreadcrumbsLoggerWidgetTest() {
    base::SetRecordActionTaskRunner(
        base::SingleThreadTaskRunner::GetCurrentDefault());
    CHECK(temp_dir_.CreateUniqueTempDir());
    logger_ = std::make_unique<ApplicationBreadcrumbsLogger>(
        temp_dir_.GetPath(),
        /*is_metrics_enabled_callback=*/base::BindRepeating(
            [] { return true; }));
  }

  base::ScopedTempDir temp_dir_;
  std::unique_ptr<ApplicationBreadcrumbsLogger> logger_;
};

TEST_F(ApplicationBreadcrumbsLoggerWidgetTest, WidgetClosed) {
  ASSERT_TRUE(OnlyStartupEventLogged());
  auto widget = std::make_unique<views::Widget>();
  views::Widget::InitParams params =
      CreateParams(views::Widget::InitParams::CLIENT_OWNS_WIDGET,
                   views::Widget::InitParams::TYPE_WINDOW);
  params.native_widget = views::test::CreatePlatformNativeWidgetImpl(
      widget.get(), views::test::kStubCapture, nullptr);
  params.name = "TestWidget";
  widget->Init(std::move(params));
  widget->Show();
  widget->Close();
  auto events = GetEvents();
  ASSERT_EQ(2u, events.size());
  events.pop_front();
  EXPECT_NE(std::string::npos,
            events.front().find("Widget Closed: TestWidget"));
}
#endif  // TOOLKIT_VIEWS

}  // namespace breadcrumbs