File: user_action_signal_handler_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (136 lines) | stat: -rw-r--r-- 5,346 bytes parent folder | download | duplicates (9)
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
// Copyright 2021 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/segmentation_platform/internal/signals/user_action_signal_handler.h"

#include "base/metrics/metrics_hashes.h"
#include "base/test/task_environment.h"
#include "components/segmentation_platform/internal/database/mock_signal_database.h"
#include "components/segmentation_platform/internal/database/mock_ukm_database.h"
#include "components/segmentation_platform/public/proto/types.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Eq;

namespace segmentation_platform {

namespace {

const char kProfileId[] = "12";
const char kExpectedUserAction[] = "some_event";
const uint64_t kExpectedHash = base::HashMetricName(kExpectedUserAction);

MATCHER_P(SampleEq, hash, "") {
  EXPECT_EQ(arg.type, proto::SignalType::USER_ACTION);
  return arg.name_hash == hash;
}

}  // namespace

class UserActionSignalHandlerTest : public testing::Test {
 public:
  UserActionSignalHandlerTest() = default;
  ~UserActionSignalHandlerTest() override = default;

  void SetUp() override {
    base::SetRecordActionTaskRunner(
        task_environment_.GetMainThreadTaskRunner());
    signal_database_ = std::make_unique<MockSignalDatabase>();
    ukm_db_ = std::make_unique<MockUkmDatabase>();
    user_action_signal_handler_ = std::make_unique<UserActionSignalHandler>(
        kProfileId, signal_database_.get(), ukm_db_.get());
  }

  void SetupUserActions() {
    std::set<uint64_t> actions;
    actions.insert(kExpectedHash);
    user_action_signal_handler_->SetRelevantUserActions(actions);
  }

  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<MockSignalDatabase> signal_database_;
  std::unique_ptr<MockUkmDatabase> ukm_db_;
  std::unique_ptr<UserActionSignalHandler> user_action_signal_handler_;
};

TEST_F(UserActionSignalHandlerTest, UserActionsAreRecorded) {
  // Initialize and register the list of user actions we are listening to.
  user_action_signal_handler_->EnableMetrics(true);
  SetupUserActions();

  // Fire a registered user action. It should be recorded.
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION, kExpectedHash,
                          Eq(std::nullopt), _));
  EXPECT_CALL(*ukm_db_, AddUmaMetric(kProfileId, SampleEq(kExpectedHash)));
  base::RecordComputedActionAt(kExpectedUserAction, base::TimeTicks::Now());

  // Fire an unrelated user action. It should be ignored.
  std::string kUnrelatedUserAction = "unrelated_event";
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION,
                          base::HashMetricName(kUnrelatedUserAction),
                          Eq(std::nullopt), _))
      .Times(0);
  EXPECT_CALL(*ukm_db_, AddUmaMetric(kProfileId, SampleEq(base::HashMetricName(
                                                     kUnrelatedUserAction))))
      .Times(0);
  base::RecordComputedActionAt(kUnrelatedUserAction, base::TimeTicks::Now());
}

TEST_F(UserActionSignalHandlerTest, DisableMetrics) {
  base::TimeTicks time = base::TimeTicks::Now();
  SetupUserActions();

  // Metrics is disabled on startup.
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION,
                          base::HashMetricName(kExpectedUserAction),
                          Eq(std::nullopt), _))
      .Times(0);
  EXPECT_CALL(*ukm_db_,
              AddUmaMetric(kProfileId,
                           SampleEq(base::HashMetricName(kExpectedUserAction))))
      .Times(0);
  base::RecordComputedActionAt(kExpectedUserAction, time);

  // Enable metrics.
  user_action_signal_handler_->EnableMetrics(true);
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION,
                          base::HashMetricName(kExpectedUserAction),
                          Eq(std::nullopt), _))
      .Times(1);
  EXPECT_CALL(*ukm_db_, AddUmaMetric(kProfileId, SampleEq(base::HashMetricName(
                                                     kExpectedUserAction))));
  base::RecordComputedActionAt(kExpectedUserAction, time);

  // Disable metrics again.
  user_action_signal_handler_->EnableMetrics(false);
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION,
                          base::HashMetricName(kExpectedUserAction),
                          Eq(std::nullopt), _))
      .Times(0);
  EXPECT_CALL(*ukm_db_,
              AddUmaMetric(kProfileId,
                           SampleEq(base::HashMetricName(kExpectedUserAction))))
      .Times(0);
  base::RecordComputedActionAt(kExpectedUserAction, time);

  // Enable metrics again.
  user_action_signal_handler_->EnableMetrics(true);
  EXPECT_CALL(*signal_database_,
              WriteSample(proto::SignalType::USER_ACTION,
                          base::HashMetricName(kExpectedUserAction),
                          Eq(std::nullopt), _))
      .Times(1);
  EXPECT_CALL(*ukm_db_, AddUmaMetric(kProfileId, SampleEq(base::HashMetricName(
                                                     kExpectedUserAction))));
  base::RecordComputedActionAt(kExpectedUserAction, time);
}

}  // namespace segmentation_platform