File: template_url_service_test_util.h

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 (165 lines) | stat: -rw-r--r-- 6,807 bytes parent folder | download | duplicates (3)
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
// 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_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_
#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_

#include <memory>
#include <string>

#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_profile.h"
#include "components/search_engines/enterprise/enterprise_search_manager.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_observer.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"

class KeywordWebDataService;

// Sets the managed preferences for the default search provider. `enabled`
// enables/disables use of the managed engine by `DefaultSearchManager`.
void SetManagedDefaultSearchPreferences(const TemplateURLData& managed_data,
                                        bool enabled,
                                        TestingProfile* profile);

// Removes all the managed preferences for the default search provider.
void RemoveManagedDefaultSearchPreferences(TestingProfile* profile);

// Sets the recommended preferences for the default search provider. `enabled`
// enables/disables use of the managed engine by `DefaultSearchManager`.
void SetRecommendedDefaultSearchPreferences(const TemplateURLData& data,
                                            bool enabled,
                                            TestingProfile* profile);

// Sets the managed preferences for search providers.
void SetManagedSearchSettingsPreference(
    const EnterpriseSearchManager::OwnedTemplateURLDataVector&
        enterprise_search_engines,
    TestingProfile* profile);

// Creates a TemplateURL with some test values. The caller owns the returned
// TemplateURL*.
std::unique_ptr<TemplateURL> CreateTestTemplateURL(
    const std::u16string& keyword,
    const std::string& url,
    const std::string& guid = std::string(),
    base::Time last_modified = base::Time::FromTimeT(100),
    bool safe_for_autoreplace = false,
    TemplateURLData::PolicyOrigin policy_origin =
        TemplateURLData::PolicyOrigin::kNoPolicy,
    int prepopulate_id = 999999,
    int starter_pack_id = 0,
    TemplateURLData::ActiveStatus is_active =
        TemplateURLData::ActiveStatus::kTrue);

class TemplateURLServiceTestUtil : public TemplateURLServiceObserver {
 public:
  TemplateURLServiceTestUtil();

  explicit TemplateURLServiceTestUtil(PrefService& local_state);

  explicit TemplateURLServiceTestUtil(
      TestingProfile::TestingFactories testing_factories,
      PrefService* local_state = nullptr);

  TemplateURLServiceTestUtil(const TemplateURLServiceTestUtil&) = delete;
  TemplateURLServiceTestUtil& operator=(const TemplateURLServiceTestUtil&) =
      delete;

  ~TemplateURLServiceTestUtil() override;

  static BrowserContextKeyedServiceFactory::TestingFactory
  GetTemplateURLServiceTestingFactory();

  // Forwards to the equivalent `TemplateURLService` constructor, pulling
  // services it depends on from `profile`.
  static std::unique_ptr<TemplateURLService> CreateTemplateURLServiceForTesting(
      Profile* profile,
      std::unique_ptr<SearchTermsData> search_terms_data,
      scoped_refptr<KeywordWebDataService> web_data_service,
      std::unique_ptr<TemplateURLServiceClient> client,
      base::RepeatingClosure dsp_change_callback);

  // Forwards to the equivalent `TemplateURLService` constructor, pulling
  // services it depends on from `profile`.
  static std::unique_ptr<TemplateURLService> CreateTemplateURLServiceForTesting(
      Profile* profile,
      base::span<const TemplateURLService::Initializer> initializers = {});

  // TemplateURLServiceObserver implemementation.
  void OnTemplateURLServiceChanged() override;

  // Gets the observer count.
  int GetObserverCount();

  // Sets the observer count to 0.
  void ResetObserverCount();

  // Gets the number of times the DSP has been set to Google.
  int dsp_set_to_google_callback_count() const {
    return dsp_set_to_google_callback_count_;
  }

  // Makes sure the load was successful and sent the correct notification.
  void VerifyLoad();

  // Makes the model believe it has been loaded (without actually doing the
  // load). Since this avoids setting the built-in keyword version, the next
  // load will do a merge from prepopulated data.
  void ChangeModelToLoadState();

  // Deletes the current model (and doesn't create a new one).
  void ClearModel();

  // Creates a new TemplateURLService.
  void ResetModel(bool verify_load);

  // Returns the search term from the last invocation of
  // TemplateURLService::SetKeywordSearchTermsForURL and clears the search term.
  std::u16string GetAndClearSearchTerm();

  // Adds extension controlled TemplateURL to the model and overrides default
  // search pref in an extension controlled preferences, if extension wants to
  // be default.
  TemplateURL* AddExtensionControlledTURL(
      std::unique_ptr<TemplateURL> extension_turl);

  // Removes a TemplateURL controlled by |extension_id| from the model, and,
  // if necessary, from the extension-controlled default search preference.
  // This TemplateURL must exist.
  void RemoveExtensionControlledTURL(const std::string& extension_id);

  KeywordWebDataService* web_data_service() { return web_data_service_.get(); }
  TemplateURLService* model() { return model_.get(); }
  TestingProfile* profile() { return profile_.get(); }

 private:
  static TestingProfile::TestingFactories
  SetUpRequiredServicesWithCustomLocalState(PrefService* local_state_override);

  // Populated only if the calling test did not previously set up a
  // local state. This object would then own the process-global local
  // state.
  // Don't access it directly, prefer using `local_state_` instead.
  std::unique_ptr<ScopedTestingLocalState> owned_local_state_;

  // We pass `local_state_` to the constructor in some cases where we can't
  // or don't want to use `g_browser_process->local_state()`.
  raw_ptr<PrefService> local_state_;
  std::unique_ptr<TestingProfile> profile_;
  int changed_count_ = 0;
  std::u16string search_term_;
  int dsp_set_to_google_callback_count_ = 0;
  scoped_refptr<KeywordWebDataService> web_data_service_;
  std::unique_ptr<TemplateURLService> model_;
  data_decoder::test::InProcessDataDecoder data_decoder_;
};

#endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_TEST_UTIL_H_