chromium/chrome/browser/sync/test/integration/history_helper.h

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_HISTORY_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_HISTORY_HELPER_H_

#include <map>
#include <vector>

#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "components/history/core/browser/history_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace history {
void PrintTo(const VisitRow& row, std::ostream* os);
}  // namespace history

namespace sync_pb {
void PrintTo(const HistorySpecifics& history, std::ostream* os);
}  // namespace sync_pb

namespace history_helper {

// Matchers for sync_pb::HistorySpecifics.

MATCHER_P(UrlIs, url, "") {}

MATCHER_P2(UrlsAre, url1, url2, "") {}

MATCHER_P(CoreTransitionIs, transition, "") {}

MATCHER(IsChainStart, "") {}

MATCHER(IsChainEnd, "") {}

MATCHER(HasReferringVisit, "") {}

MATCHER(HasOpenerVisit, "") {}

MATCHER(HasReferrerURL, "") {}

MATCHER_P(ReferrerURLIs, referrer_url, "") {}

MATCHER(HasVisitDuration, "") {}

MATCHER(HasHttpResponseCode, "") {}

MATCHER(StandardFieldsArePopulated, "") {}

// Matchers for history::VisitRow.

MATCHER_P(VisitRowIdIs, visit_id, "") {}

MATCHER(VisitRowHasDuration, "") {}

MATCHER_P(VisitRowDurationIs, duration, "") {}

// Gets the URLRow for a specific URL from a specific sync profile. Returns
// false if the URL was not found in the history DB.
bool GetUrlFromClient(int index, const GURL& url, history::URLRow* row);

// Similar, but queries by URL ID rather than URL.
bool GetUrlFromClient(int index, history::URLID url_id, history::URLRow* row);

// Gets the visits for a URL from a specific sync profile.
history::VisitVector GetVisitsFromClient(int index, history::URLID id);

// Gets the visits for a URL from a specific sync profile. Like above, but
// takes a GURL instead of URLID. Returns empty vector if |url| is not returned
// by GetUrlFromClient().
history::VisitVector GetVisitsForURLFromClient(int index, const GURL& url);

// As above, but return `AnnotatedVisit` instead of just `VisitRow`.
std::vector<history::AnnotatedVisit> GetAnnotatedVisitsFromClient(
    int index,
    history::URLID id);
std::vector<history::AnnotatedVisit> GetAnnotatedVisitsForURLFromClient(
    int index,
    const GURL& url);

history::VisitVector GetRedirectChainFromClient(int index,
                                                history::VisitRow final_visit);

// Adds a URL to the history DB for a specific sync profile (just registers a
// new visit if the URL already exists) using a TYPED PageTransition.
void AddUrlToHistory(int index, const GURL& url);

// Adds a URL to the history DB for a specific sync profile (just registers a
// new visit if the URL already exists), using the passed PageTransition.
void AddUrlToHistoryWithTransition(int index,
                                   const GURL& url,
                                   ui::PageTransition transition,
                                   history::VisitSource source);

// Adds a URL to the history DB for a specific sync profile (just registers a
// new visit if the URL already exists), using the passed PageTransition and
// timestamp.
void AddUrlToHistoryWithTimestamp(int index,
                                  const GURL& url,
                                  ui::PageTransition transition,
                                  history::VisitSource source,
                                  const base::Time& timestamp);

// A helper class that waits for entries in the local history DB that match the
// given matchers.
// Note that this only checks URLs that were passed in - any additional URLs in
// the DB (and their corresponding visits) are ignored.
class LocalHistoryMatchChecker : public SingleClientStatusChangeChecker {};

// A helper class that waits for the HISTORY entities on the FakeServer to match
// a given GMock matcher.
class ServerHistoryMatchChecker
    : public fake_server::FakeServerMatchStatusChecker {};

}  // namespace history_helper

#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_HISTORY_HELPER_H_