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
|
// Copyright 2012 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_SEARCH_ENGINES_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_SEARCH_ENGINES_HELPER_H_
#include <map>
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/status_change_checker.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_observer.h"
#include "components/sync/protocol/search_engine_specifics.pb.h"
class TemplateURL;
using GUIDToTURLMap = std::map<std::string, const TemplateURL*>;
namespace search_engines_helper {
// Used to access the search engines within a particular sync profile.
TemplateURLService* GetServiceForBrowserContext(int profile_index);
// Used to access the search engines within the verifier sync profile.
TemplateURLService* GetVerifierService();
// Compared a single TemplateURLService for a given profile to the verifier.
// Retrns true iff their user-visible fields match.
bool ServiceMatchesVerifier(int profile_index);
// Returns true iff all TemplateURLServices match with the verifier.
bool AllServicesMatch();
bool AllServicesMatch(std::ostream* os);
// Builder class that by default infers all fields from |keyword| and allows
// overriding those default values.
class TemplateURLBuilder {
public:
explicit TemplateURLBuilder(const std::string& keyword);
~TemplateURLBuilder();
TemplateURLData* data() { return &data_; }
std::unique_ptr<TemplateURL> Build();
private:
TemplateURLData data_;
};
// Add a search engine based on a keyword to the service at index
// |profile_index| and the verifier if it is used.
void AddSearchEngine(int profile_index, const std::string& keyword);
// Retrieves a search engine from the service at index |profile_index| with
// original keyword |keyword| and changes its user-visible fields. Does the same
// to the verifier, if it is used.
void EditSearchEngine(int profile_index,
const std::string& keyword,
const std::u16string& short_name,
const std::string& new_keyword,
const std::string& url);
// Deletes a search engine from the service at index |profile_index| with
// |keyword|.
void DeleteSearchEngine(int profile_index, const std::string& keyword);
// Changes the search engine with |keyword| to be the new default for
// |profile_index|. Does the same to the verifier, if it is used.
void ChangeDefaultSearchProvider(int profile_index, const std::string& keyword);
// Returns true if the profile at |profile_index| has a search engine matching
// |keyword|.
bool HasSearchEngine(int profile_index, const std::string& keyword);
// Returns the keyword for the default search engine at |profile_index|.
std::string GetDefaultSearchEngineKeyword(int profile_index);
// Return true if the fake server has a search engine matching `keyword`.
bool HasSearchEngineInFakeServer(const std::string& keyword,
fake_server::FakeServer* fake_server);
// Returns the first search engine matching `keyword` in the fake server.
std::optional<sync_pb::SearchEngineSpecifics>
GetSearchEngineInFakeServerWithKeyword(const std::string& keyword,
fake_server::FakeServer* fake_server);
// Checker that blocks until all services have the same search engine data.
class SearchEnginesMatchChecker : public StatusChangeChecker,
public TemplateURLServiceObserver {
public:
SearchEnginesMatchChecker();
~SearchEnginesMatchChecker() override;
// StatusChangeChecker overrides.
bool IsExitConditionSatisfied(std::ostream* os) override;
// TemplateURLServiceObserver overrides.
void OnTemplateURLServiceChanged() override;
private:
base::ScopedMultiSourceObservation<TemplateURLService,
TemplateURLServiceObserver>
observations_{this};
};
// Checker that blocks until |profile_index| has a search engine matching the
// search engine generated with |keyword|.
class HasSearchEngineChecker : public StatusChangeChecker,
public TemplateURLServiceObserver {
public:
HasSearchEngineChecker(int profile_index, const std::string& keyword);
~HasSearchEngineChecker() override;
// StatusChangeChecker overrides.
bool IsExitConditionSatisfied(std::ostream* os) override;
// TemplateURLServiceObserver overrides.
void OnTemplateURLServiceChanged() override;
private:
const raw_ptr<TemplateURLService> service_;
const std::u16string keyword_;
base::ScopedMultiSourceObservation<TemplateURLService,
TemplateURLServiceObserver>
observations_{this};
};
// Waits until the fake server has an expected search engine.
class FakeServerHasSearchEngineChecker
: public fake_server::FakeServerMatchStatusChecker {
public:
explicit FakeServerHasSearchEngineChecker(const std::string& keyword);
protected:
// StatusChangeChecker overrides.
bool IsExitConditionSatisfied(std::ostream* os) override;
private:
const std::string keyword_;
};
} // namespace search_engines_helper
#endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SEARCH_ENGINES_HELPER_H_
|