File: url_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 (179 lines) | stat: -rw-r--r-- 7,159 bytes parent folder | download | duplicates (10)
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
// Copyright 2022 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/url_signal_handler.h"

#include "base/test/task_environment.h"
#include "components/segmentation_platform/internal/database/mock_ukm_database.h"
#include "components/segmentation_platform/internal/database/ukm_database.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace segmentation_platform {

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
using ::testing::ReturnRef;

void RunFoundCallback(const GURL& url,
                      UrlSignalHandler::FindCallback callback) {
  std::move(callback).Run(true, "found_callback_id");
}
void RunNotFoundCallback(const GURL& url,
                         UrlSignalHandler::FindCallback callback) {
  std::move(callback).Run(false, "");
}

class MockHistoryDelegate : public UrlSignalHandler::HistoryDelegate {
 public:
  MOCK_METHOD(bool, FastCheckUrl, (const GURL& url), (override));

  MOCK_METHOD(void,
              FindUrlInHistory,
              (const GURL& url, UrlSignalHandler::FindCallback callback),
              (override));

  MOCK_METHOD(const std::string&, profile_id, (), (override));
};

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

  void SetUp() override {
    signal_handler_ = std::make_unique<UrlSignalHandler>(&ukm_database_);
  }

  void TearDown() override { signal_handler_.reset(); }

  UrlSignalHandler& signal_handler() { return *signal_handler_; }

  MockUkmDatabase& ukm_database() { return ukm_database_; }

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  MockUkmDatabase ukm_database_;
  std::unique_ptr<UrlSignalHandler> signal_handler_;
};

TEST_F(UrlSignalHandlerTest, TestNoHistoryDelegate) {
  const GURL kUrl1("https://www.url1.com");
  const ukm::SourceId kSourceId1 = 10;

  EXPECT_CALL(ukm_database(),
              UpdateUrlForUkmSource(kSourceId1, kUrl1, false, ""));
  signal_handler().OnUkmSourceUpdated(kSourceId1, {kUrl1});
}

TEST_F(UrlSignalHandlerTest, TestFastCheckSuccess) {
  const GURL kUrl1("https://www.url1.com");
  const ukm::SourceId kSourceId1 = 10;
  MockHistoryDelegate history_delegate;
  const std::string profile_id = "test_id";

  EXPECT_CALL(history_delegate, profile_id())
      .WillRepeatedly(ReturnRef(profile_id));
  EXPECT_CALL(history_delegate, FastCheckUrl(kUrl1)).WillOnce(Return(true));
  EXPECT_CALL(history_delegate, FindUrlInHistory(_, _)).Times(0);
  EXPECT_CALL(ukm_database(),
              UpdateUrlForUkmSource(kSourceId1, kUrl1, true, profile_id));

  signal_handler().AddHistoryDelegate(&history_delegate);
  signal_handler().OnUkmSourceUpdated(kSourceId1, {kUrl1});
  signal_handler().RemoveHistoryDelegate(&history_delegate);
}

TEST_F(UrlSignalHandlerTest, TestMultipleDelegates) {
  const GURL kUrl1("https://www.url1.com");
  const GURL kUrl2("https://www.url2.com");
  const ukm::SourceId kSourceId1 = 10;
  const ukm::SourceId kSourceId2 = 10;
  MockHistoryDelegate history_delegate1;
  MockHistoryDelegate history_delegate2;
  const std::string profile_id = "test_id";

  // URL1 is not found in both the history delegates.
  EXPECT_CALL(history_delegate2, profile_id())
      .WillRepeatedly(ReturnRef(profile_id));
  EXPECT_CALL(history_delegate1, FastCheckUrl(kUrl1)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate2, FastCheckUrl(kUrl1)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate1, FindUrlInHistory(kUrl1, _))
      .WillOnce(&RunNotFoundCallback);
  EXPECT_CALL(history_delegate2, FindUrlInHistory(kUrl1, _))
      .WillOnce(&RunNotFoundCallback);
  // URL2 is found in fast search of second delegate. Delegate1 may or may not
  // be called depending on which delegate is checked first.
  EXPECT_CALL(history_delegate1, FastCheckUrl(kUrl2))
      .Times(AnyNumber())
      .WillOnce(Return(false));
  EXPECT_CALL(history_delegate2, FastCheckUrl(kUrl2)).WillOnce(Return(true));

  EXPECT_CALL(ukm_database(),
              UpdateUrlForUkmSource(kSourceId1, kUrl1, false, ""));
  EXPECT_CALL(ukm_database(),
              UpdateUrlForUkmSource(kSourceId1, kUrl2, true, profile_id));

  signal_handler().AddHistoryDelegate(&history_delegate1);
  signal_handler().AddHistoryDelegate(&history_delegate2);
  signal_handler().OnUkmSourceUpdated(kSourceId1, {kUrl1});
  signal_handler().OnUkmSourceUpdated(kSourceId2, {kUrl2});
  signal_handler().RemoveHistoryDelegate(&history_delegate1);
  signal_handler().RemoveHistoryDelegate(&history_delegate2);
}

TEST_F(UrlSignalHandlerTest, FindInHistory) {
  const GURL kUrl1("https://www.url1.com");
  const GURL kUrl2("https://www.url2.com");
  const ukm::SourceId kSourceId1 = 10;
  const ukm::SourceId kSourceId2 = 10;
  MockHistoryDelegate history_delegate1;
  MockHistoryDelegate history_delegate2;

  // URL1 is found in history search of first delegate. Delegate2 may or may not
  // be called depending on which delegate is checked first.
  EXPECT_CALL(history_delegate1, FastCheckUrl(kUrl1)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate2, FastCheckUrl(kUrl1)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate1, FindUrlInHistory(kUrl1, _))
      .WillOnce(&RunFoundCallback);
  EXPECT_CALL(history_delegate2, FindUrlInHistory(kUrl1, _))
      .Times(AnyNumber())
      .WillOnce(&RunNotFoundCallback);
  // URL1 is not found in both the history delegates.
  EXPECT_CALL(history_delegate1, FastCheckUrl(kUrl2)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate2, FastCheckUrl(kUrl2)).WillOnce(Return(false));
  EXPECT_CALL(history_delegate1, FindUrlInHistory(kUrl2, _))
      .WillOnce(&RunNotFoundCallback);
  EXPECT_CALL(history_delegate2, FindUrlInHistory(kUrl2, _))
      .WillOnce(&RunNotFoundCallback);

  EXPECT_CALL(ukm_database(), UpdateUrlForUkmSource(kSourceId1, kUrl1, true,
                                                    "found_callback_id"));
  EXPECT_CALL(ukm_database(),
              UpdateUrlForUkmSource(kSourceId1, kUrl2, false, ""));

  signal_handler().AddHistoryDelegate(&history_delegate1);
  signal_handler().AddHistoryDelegate(&history_delegate2);
  signal_handler().OnUkmSourceUpdated(kSourceId1, {kUrl1});
  signal_handler().OnUkmSourceUpdated(kSourceId2, {kUrl2});
  signal_handler().RemoveHistoryDelegate(&history_delegate1);
  signal_handler().RemoveHistoryDelegate(&history_delegate2);
}

TEST_F(UrlSignalHandlerTest, Observation) {
  const GURL kUrl1("https://www.url1.com");
  const GURL kUrl2("https://www.url2.com");

  EXPECT_CALL(ukm_database(), OnUrlValidated(kUrl1, ""));
  signal_handler().OnHistoryVisit(kUrl1, "");
  EXPECT_CALL(ukm_database(), OnUrlValidated(kUrl2, ""));
  signal_handler().OnHistoryVisit(kUrl2, "");
  std::vector<GURL> list({kUrl1, kUrl2});
  EXPECT_CALL(ukm_database(), RemoveUrls(list, false));
  signal_handler().OnUrlsRemovedFromHistory(list, false);
}

}  // namespace segmentation_platform