File: session_url_visit_data_fetcher_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 (349 lines) | stat: -rw-r--r-- 12,910 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2024 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/visited_url_ranking/internal/session_url_visit_data_fetcher.h"

#include <memory>
#include <vector>

#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/sync_sessions/open_tabs_ui_delegate.h"
#include "components/sync_sessions/session_sync_service.h"
#include "components/visited_url_ranking/public/fetch_options.h"
#include "components/visited_url_ranking/public/fetcher_config.h"
#include "components/visited_url_ranking/public/url_visit.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

constexpr char kSampleSearchUrl[] = "https://www.google.com/search?q=sample";
constexpr char kSampleSearchUrl2[] = "https://www.google.com/search?q=sample2";

std::unique_ptr<sync_sessions::SyncedSession> BuildSampleSession(
    const char* session_name,
    std::vector<std::unique_ptr<sync_sessions::SyncedSessionWindow>>
        session_windows,
    base::Time modified_time = base::Time::Now()) {
  auto sample_session = std::make_unique<sync_sessions::SyncedSession>();
  sample_session->SetSessionName(session_name);
  sample_session->SetModifiedTime(modified_time);
  for (size_t i = 0; i < session_windows.size(); i++) {
    sample_session->windows[SessionID::FromSerializedValue(i)] =
        std::move(session_windows[i]);
  }

  return sample_session;
}

std::unique_ptr<sync_sessions::SyncedSessionWindow> SampleSessionWindow(
    std::vector<std::unique_ptr<sessions::SessionTab>> tabs,
    base::Time time = base::Time::Now()) {
  auto synced_session_window =
      std::make_unique<sync_sessions::SyncedSessionWindow>();
  synced_session_window->wrapped_window.timestamp = time;
  synced_session_window->wrapped_window.tabs = std::move(tabs);
  return synced_session_window;
}

std::unique_ptr<sessions::SessionTab> SampleSessionTab(
    int tab_id,
    std::u16string title,
    GURL url,
    bool pinned,
    base::Time timestamp = base::Time::Now()) {
  sessions::SerializedNavigationEntry navigation;
  navigation.set_title(title);
  navigation.set_virtual_url(url);
  navigation.set_timestamp(timestamp);
  navigation.set_favicon_url(url);

  auto session_tab = std::make_unique<sessions::SessionTab>();
  session_tab->tab_id = SessionID::FromSerializedValue(tab_id);
  session_tab->current_navigation_index = 0;
  session_tab->navigations.push_back(navigation);
  session_tab->timestamp = timestamp;
  session_tab->pinned = pinned;
  return session_tab;
}

std::unique_ptr<sync_sessions::SyncedSession> GetSampleSession() {
  auto now = base::Time::Now();
  std::vector<std::unique_ptr<sessions::SessionTab>> session_window_tabs = {};
  session_window_tabs.emplace_back(SampleSessionTab(
      1, u"Tab 1", GURL(kSampleSearchUrl), true, now - base::Hours(1)));
  session_window_tabs.emplace_back(SampleSessionTab(
      2, u"Tab 2", GURL(kSampleSearchUrl2), false, now - base::Hours(2)));
  std::vector<std::unique_ptr<sync_sessions::SyncedSessionWindow>>
      session_windows = {};
  session_windows.emplace_back(
      SampleSessionWindow(std::move(session_window_tabs)));
  return BuildSampleSession("Sample Session", std::move(session_windows));
}

}  // namespace

namespace sync_sessions {

class MockOpenTabsUIDelegate : public OpenTabsUIDelegate {
 public:
  MockOpenTabsUIDelegate() = default;
  MockOpenTabsUIDelegate(const MockOpenTabsUIDelegate&) = delete;
  MockOpenTabsUIDelegate& operator=(const MockOpenTabsUIDelegate&) = delete;
  ~MockOpenTabsUIDelegate() override = default;

  MOCK_METHOD1(
      GetAllForeignSessions,
      bool(std::vector<raw_ptr<const SyncedSession, VectorExperimental>>*
               sessions));

  MOCK_METHOD3(GetForeignTab,
               bool(const std::string& tag,
                    const SessionID tab_id,
                    const sessions::SessionTab** tab));

  MOCK_METHOD1(DeleteForeignSession, void(const std::string& tag));

  MOCK_METHOD1(
      GetForeignSession,
      std::vector<const sessions::SessionWindow*>(const std::string& tag));

  MOCK_METHOD2(GetForeignSessionTabs,
               bool(const std::string& tag,
                    std::vector<const sessions::SessionTab*>* tabs));

  MOCK_METHOD1(GetLocalSession, bool(const SyncedSession** local_session));
};

class MockSessionSyncService : public SessionSyncService {
 public:
  MockSessionSyncService() = default;
  MockSessionSyncService(const MockSessionSyncService&) = delete;
  MockSessionSyncService& operator=(const MockSessionSyncService&) = delete;
  ~MockSessionSyncService() override = default;

  MOCK_CONST_METHOD0(GetGlobalIdMapper, syncer::GlobalIdMapper*());

  MOCK_METHOD0(GetOpenTabsUIDelegate, OpenTabsUIDelegate*());

  base::CallbackListSubscription SubscribeToForeignSessionsChanged(
      const base::RepeatingClosure& cb) override {
    return subscriber_list_.Add(cb);
  }

  MOCK_METHOD0(GetControllerDelegate,
               base::WeakPtr<syncer::DataTypeControllerDelegate>());

 private:
  base::RepeatingClosureList subscriber_list_;
};

}  // namespace sync_sessions

namespace visited_url_ranking {

using Source = URLVisit::Source;
using URLType = visited_url_ranking::URLVisitAggregate::URLType;
using ResultOption = visited_url_ranking::FetchOptions::ResultOption;

class SessionURLVisitDataFetcherTest
    : public testing::Test,
      public ::testing::WithParamInterface<Source> {
 public:
  SessionURLVisitDataFetcherTest() = default;

  void SetSessionSyncServiceExpectations() {
    EXPECT_CALL(mock_session_sync_service_, GetOpenTabsUIDelegate())
        .WillOnce(
            testing::Invoke([this]() { return &open_tabs_ui_delegate_; }));
  }

  FetchResult FetchAndGetResult(const FetchOptions& options) {
    FetchResult result = FetchResult(FetchResult::Status::kError, {});
    base::RunLoop wait_loop;
    auto session_url_visit_data_fetcher =
        SessionURLVisitDataFetcher(&mock_session_sync_service_);
    session_url_visit_data_fetcher.FetchURLVisitData(
        options, FetcherConfig(),
        base::BindOnce(
            [](base::OnceClosure stop_waiting, FetchResult* result,
               FetchResult result_arg) {
              result->status = result_arg.status;
              result->data = std::move(result_arg.data);
              std::move(stop_waiting).Run();
            },
            wait_loop.QuitClosure(), &result));
    wait_loop.Run();
    return result;
  }

 protected:
  sync_sessions::MockOpenTabsUIDelegate open_tabs_ui_delegate_;
  sync_sessions::MockSessionSyncService mock_session_sync_service_;

 private:
  base::test::TaskEnvironment task_env_;
};

TEST_F(SessionURLVisitDataFetcherTest, FetchURLVisitDataNoOpenTabsUIDelegate) {
  EXPECT_CALL(mock_session_sync_service_, GetOpenTabsUIDelegate())
      .WillOnce(testing::Invoke([]() { return nullptr; }));

  std::vector<std::unique_ptr<sync_sessions::SyncedSession>> sample_sessions;
  sample_sessions.push_back(GetSampleSession());

  base::Time yesterday = base::Time::Now() - base::Days(1);
  auto result = FetchAndGetResult(FetchOptions(
      {{URLType::kActiveRemoteTab, {.age_limit = base::Days(1)}}},
      {
          {Fetcher::kSession,
           FetchOptions::FetchSources({URLVisit::Source::kForeign})},
      },
      yesterday));
  EXPECT_EQ(result.status, FetchResult::Status::kError);
}

TEST_F(SessionURLVisitDataFetcherTest, FetchURLVisitDataDefaultSources) {
  std::vector<std::unique_ptr<sync_sessions::SyncedSession>>
      local_sample_sessions;
  std::vector<std::unique_ptr<sync_sessions::SyncedSession>>
      foreign_sample_sessions;
  local_sample_sessions.push_back(GetSampleSession());
  foreign_sample_sessions.push_back(GetSampleSession());

  SetSessionSyncServiceExpectations();
  EXPECT_CALL(open_tabs_ui_delegate_, GetLocalSession(testing::_))
      .WillOnce(testing::Invoke(
          [&local_sample_sessions](const sync_sessions::SyncedSession** local) {
            local_sample_sessions[0]->SetSessionName("Local Session");
            *local = local_sample_sessions[0].get();
            return true;
          }));
  EXPECT_CALL(open_tabs_ui_delegate_, GetAllForeignSessions(testing::_))
      .WillOnce(testing::Invoke(
          [&foreign_sample_sessions](
              std::vector<raw_ptr<const sync_sessions::SyncedSession,
                                  VectorExperimental>>* sessions) {
            for (auto& sample_session : foreign_sample_sessions) {
              sessions->push_back(sample_session.get());
            }
            return true;
          }));

  base::Time yesterday = base::Time::Now() - base::Days(1);
  auto result = FetchAndGetResult(FetchOptions(
      {
          {URLType::kActiveRemoteTab, {.age_limit = base::Days(1)}},
      },
      {
          {Fetcher::kSession, FetchOptions::kOriginSources},
      },
      yesterday));
  EXPECT_EQ(result.status, FetchResult::Status::kSuccess);
  EXPECT_EQ(result.data.size(), 2u);
  for (const auto& url_key : {kSampleSearchUrl, kSampleSearchUrl2}) {
    const auto& tab_data =
        std::get<URLVisitAggregate::TabData>(result.data.at(url_key));
    EXPECT_EQ(tab_data.tab_count, 2u);
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         SessionURLVisitDataFetcherTest,
                         ::testing::Values(Source::kLocal, Source::kForeign));

TEST_P(SessionURLVisitDataFetcherTest, FetchURLVisitData) {
  std::vector<std::unique_ptr<sync_sessions::SyncedSession>> sample_sessions;
  sample_sessions.push_back(GetSampleSession());

  SetSessionSyncServiceExpectations();
  const auto source = GetParam();
  if (source == Source::kLocal) {
    EXPECT_CALL(open_tabs_ui_delegate_, GetLocalSession(testing::_))
        .WillOnce(testing::Invoke(
            [&sample_sessions](const sync_sessions::SyncedSession** local) {
              *local = sample_sessions[0].get();
              return true;
            }));
  } else if (source == Source::kForeign) {
    EXPECT_CALL(open_tabs_ui_delegate_, GetAllForeignSessions(testing::_))
        .WillOnce(testing::Invoke(
            [&sample_sessions](
                std::vector<raw_ptr<const sync_sessions::SyncedSession,
                                    VectorExperimental>>* sessions) {
              for (auto& sample_session : sample_sessions) {
                sessions->push_back(sample_session.get());
              }
              return true;
            }));
  }

  auto options = FetchOptions(
      {
          {URLType::kActiveRemoteTab, {.age_limit = base::Days(1)}},
      },
      {
          {Fetcher::kSession, FetchOptions::FetchSources({source})},
      },
      base::Time::Now() - base::Days(1));
  auto result = FetchAndGetResult(options);
  EXPECT_EQ(result.status, FetchResult::Status::kSuccess);
  EXPECT_EQ(result.data.size(), 2u);
  for (const auto& url_key : {kSampleSearchUrl, kSampleSearchUrl2}) {
    const auto& tab_data =
        std::get<URLVisitAggregate::TabData>(result.data.at(url_key));
    EXPECT_EQ(tab_data.tab_count, 1u);
    EXPECT_EQ(tab_data.last_active_tab.visit.source, source);
  }
  EXPECT_EQ(
      std::get<URLVisitAggregate::TabData>(result.data.at(kSampleSearchUrl))
          .pinned,
      true);
  EXPECT_EQ(
      std::get<URLVisitAggregate::TabData>(result.data.at(kSampleSearchUrl2))
          .pinned,
      false);
}

TEST_F(SessionURLVisitDataFetcherTest,
       FetchURLVisitDataDefaultSourcesSameSession) {
  std::vector<std::unique_ptr<sync_sessions::SyncedSession>> sample_sessions;
  sample_sessions.push_back(GetSampleSession());

  SetSessionSyncServiceExpectations();
  EXPECT_CALL(open_tabs_ui_delegate_, GetLocalSession(testing::_))
      .WillOnce(testing::Invoke(
          [&sample_sessions](const sync_sessions::SyncedSession** local) {
            *local = sample_sessions[0].get();
            return true;
          }));
  EXPECT_CALL(open_tabs_ui_delegate_, GetAllForeignSessions(testing::_))
      .WillOnce(testing::Invoke(
          [&sample_sessions](
              std::vector<raw_ptr<const sync_sessions::SyncedSession,
                                  VectorExperimental>>* sessions) {
            for (auto& sample_session : sample_sessions) {
              sessions->push_back(sample_session.get());
            }
            return true;
          }));

  base::Time yesterday = base::Time::Now() - base::Days(1);
  auto result = FetchAndGetResult(FetchOptions(
      {
          {URLType::kActiveRemoteTab, {.age_limit = base::Days(1)}},
      },
      {
          {Fetcher::kSession, {Source::kForeign}},
      },
      yesterday));
  EXPECT_EQ(result.status, FetchResult::Status::kSuccess);
  EXPECT_EQ(result.data.size(), 0u);
}

}  // namespace visited_url_ranking