File: fake_web_app_provider.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 (262 lines) | stat: -rw-r--r-- 11,302 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
// Copyright 2019 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_WEB_APPLICATIONS_TEST_FAKE_WEB_APP_PROVIDER_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_TEST_FAKE_WEB_APP_PROVIDER_H_

#include <memory>

#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/test/fake_web_contents_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "testing/gmock/include/gmock/gmock.h"

class KeyedService;
class Profile;

namespace content {
class BrowserContext;
}

namespace web_app {

class AbstractWebAppDatabaseFactory;
class ExternallyManagedAppManager;
class FileUtilsWrapper;
class IsolatedWebAppInstallationManager;
class IsolatedWebAppUpdateManager;
class OsIntegrationManager;
class PreinstalledWebAppManager;
class WebAppCommandManager;
class WebAppCommandScheduler;
class WebAppIconManager;
class WebAppInstallFinalizer;
class WebAppInstallManager;
class WebAppOriginAssociationManager;
class WebAppPolicyManager;
class WebAppRegistrarMutable;
class WebAppSyncBridge;
class WebAppTranslationManager;
class WebAppUiManager;
class WebContentsManager;

#if BUILDFLAG(IS_CHROMEOS)
class WebAppRunOnOsLoginManager;
#endif

// This is a tool that allows unit tests (enabled by default) and browser tests
// (disabled by default) to use a 'fake' version of the WebAppProvider system.
// This means that most of the dependencies are faked out. Specifically:
// * The database is in-memory.
// * Integration with Chrome Sync is off.
// * OS integration (saving shortcuts on disk, etc) doesn't execute, but does
//   save its 'expected' state to the database.
// * All access to `WebContents` is redirected to the `FakeWebContentsManager`
//   (accessible via `GetFakeWebContentsManager()`), which stores & returns
//   results for any interaction here.
//
// Other features & notes:
// * FakeWebAppProvider is used by default in unit tests, as the
//   `TestingProfile` hardcodes the usage of this fake version in the
//   `KeyedServiceFactory` for the profile,
// * The system in NOT 'started' by default, which means commands and other
//   operations will not run. This allows tests to do any customization they may
//   want by calling `FakeWebAppProvider::Get()` and interacting with this
//   object before the system is started.
//   * Generally the system is then started by calling
//     `web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());`, which
//     waits for the system to be `ready()`, but also for any external install
//     managers to finish startup as well.
// * To use this in browser tests, you must use the FakeWebAppProviderCreator
//   below.
//
// Future improvements:
// * TODO(http://b/257529391): Isolate the extensions system dependency to
//   allow faking that as well.
// * TODO(http://b/279198384): Make better default for external manager behavior
//   in unittests. Perhaps don't start them by default unless they specified.
// * TODO(http://b/279198562): Create a `FakeWebAppCommandScheduler`, allowing
//   external systems to fully fake out usage of this system if they desire.
class FakeWebAppProvider : public WebAppProvider {
 public:
  // Used by the TestingProfile in unit tests.
  // Builds a stub WebAppProvider which will not fire subsystem startup tasks.
  // Use FakeWebAppProvider::Get() to replace subsystems.
  static std::unique_ptr<KeyedService> BuildDefault(
      content::BrowserContext* context);

  // Gets a FakeWebAppProvider that can have its subsystems set. This should
  // only be called once during SetUp(), and clients must call Start() before
  // using the subsystems.
  static FakeWebAppProvider* Get(Profile* profile);

  explicit FakeWebAppProvider(Profile* profile);
  ~FakeWebAppProvider() override;

  // |run_system_on_start| is false by default, and must be set to true here
  // BEFORE WebAppProvider::Start is called to allow the system to start
  // normally. Otherwise, StartWithSubsystems must be called.
  void SetStartSystemOnStart(bool run_system_on_start);

  // The PreinstalledWebAppManager waits for some dependencies (extensions and
  // device initialization) on startup, and then processes the preinstalled apps
  // configurion to install (or uninstall) apps on the profile. This is disabled
  // by default for unit tests, and can be enabled by setting this flag to true.
  void SetSynchronizePreinstalledAppsOnStartup(bool synchronize_on_startup);

  // Call when the unit tets wants to trigger OS integration, removing the
  // ScopedSuppressOsHooks in FakeOsIntegrationManager (allowing the
  // OsIntegrationTestOverrideBlockingRegistration to work correctly).
  void UseRealOsIntegrationManager();

  enum class AutomaticIwaUpdateStrategy {
    kDefault,
    kForceDisabled,
    kForceEnabled,
  };

  // The `IsolatedWebAppUpdateManager` will check for updates of all installed
  // Isolated Web Apps on startup and in regular time intervals. This is
  // disabled (`kForceDisabled`) by default for unit tests, and can be enabled
  // by setting this flag to `kForceEnabled`. Setting this flag to `kDefault`
  // will retain the default behavior of the `IsolatedWebAppUpdateManager`.
  void SetEnableAutomaticIwaUpdates(
      AutomaticIwaUpdateStrategy automatic_iwa_update_strategy);

  // NB: If you replace the Registrar, you also have to replace the SyncBridge
  // accordingly.
  void SetRegistrar(std::unique_ptr<WebAppRegistrarMutable> registrar);
  void SetDatabaseFactory(
      std::unique_ptr<AbstractWebAppDatabaseFactory> database_factory);
  void SetSyncBridge(std::unique_ptr<WebAppSyncBridge> sync_bridge);
  void SetFileUtils(scoped_refptr<FileUtilsWrapper> file_utils);
  void SetIconManager(std::unique_ptr<WebAppIconManager> icon_manager);
  void SetTranslationManager(
      std::unique_ptr<WebAppTranslationManager> translation_manager);
  void SetOsIntegrationManager(
      std::unique_ptr<OsIntegrationManager> os_integration_manager);
  void SetInstallManager(std::unique_ptr<WebAppInstallManager> install_manager);
  void SetInstallFinalizer(
      std::unique_ptr<WebAppInstallFinalizer> install_finalizer);
  void SetExternallyManagedAppManager(
      std::unique_ptr<ExternallyManagedAppManager>
          externally_managed_app_manager);
  void SetWebAppUiManager(std::unique_ptr<WebAppUiManager> ui_manager);
  void SetWebAppPolicyManager(
      std::unique_ptr<WebAppPolicyManager> web_app_policy_manager);
  void SetIsolatedWebAppInstallationManager(
      std::unique_ptr<IsolatedWebAppInstallationManager>
          isolated_web_app_installation_manager);
  void SetIsolatedWebAppUpdateManager(
      std::unique_ptr<IsolatedWebAppUpdateManager> iwa_update_manager);
#if BUILDFLAG(IS_CHROMEOS)
  void SetWebAppRunOnOsLoginManager(std::unique_ptr<WebAppRunOnOsLoginManager>
                                        web_app_run_on_os_login_manager);
#endif
  void SetCommandManager(std::unique_ptr<WebAppCommandManager> command_manager);
  void SetScheduler(std::unique_ptr<WebAppCommandScheduler> scheduler);
  void SetPreinstalledWebAppManager(
      std::unique_ptr<PreinstalledWebAppManager> preinstalled_web_app_manager);
  void SetOriginAssociationManager(
      std::unique_ptr<WebAppOriginAssociationManager>
          origin_association_manager);
  void SetWebContentsManager(
      std::unique_ptr<WebContentsManager> web_contents_manager);

  // These getters can be called at any time: no
  // WebAppProvider::CheckIsConnected() check performed. See
  // WebAppProvider::ConnectSubsystems().
  //
  // A mutable view must be accessible only in tests.
  WebAppRegistrarMutable& GetRegistrarMutable() const;
  WebAppIconManager& GetIconManager() const;
  WebAppCommandManager& GetCommandManager() const;
  AbstractWebAppDatabaseFactory& GetDatabaseFactory() const;
  WebAppUiManager& GetUiManager() const;
  WebAppInstallManager& GetInstallManager() const;
  OsIntegrationManager& GetOsIntegrationManager() const;

  // Starts this WebAppProvider and its subsystems. It does not wait for systems
  // to be ready.
  void StartWithSubsystems();

  // Create and set default fake subsystems.
  void CreateFakeSubsystems();

  // Used to verify shutting down of WebAppUiManager.
  void ShutDownUiManagerForTesting();

  // Shut down subsystems one by one if they are still running.
  // This needs to be done because of functions like
  // ShutDownUiManagerForTesting() which can shut down
  // specific subsystems from tests, and still call
  // FakeWebAppProvider::Shutdown() as part of test teardown.
  void Shutdown() override;

  FakeWebAppProvider* AsFakeWebAppProviderForTesting() override;

  FakeWebContentsManager* GetFakeWebContentsManager() const;

  syncer::MockDataTypeLocalChangeProcessor& processor() {
    return mock_processor_;
  }

 private:
  // CHECK that `Start()` has not been called on this provider, and also
  // disconnect so that clients are forced to call `Start()` before accessing
  // any subsystems.
  void CheckNotStartedAndDisconnect(std::string optional_message = "");

  // WebAppProvider:
  void StartImpl() override;

  // If true, when Start()ed the FakeWebAppProvider will call
  // WebAppProvider::StartImpl() and fire startup tasks like a real
  // WebAppProvider.
  bool run_system_on_start_ = false;
  // If true, preinstalled apps will be processed & installed (or uninstalled)
  // after the system starts.
  bool synchronize_preinstalled_app_on_startup_ = false;
  // If `kForceEnabled`, the `IsolatedWebAppUpdateManager` will automatically
  // search for updates of installed Isolated Web Apps on startup and in regular
  // time intervals. If `kForceDisabled`, then it will not automatically search
  // for updates. If `kDefault`, then it will use its default behavior to
  // determine whether to search for updates (e.g., based feature flags).
  AutomaticIwaUpdateStrategy automatic_iwa_update_strategy_ =
      AutomaticIwaUpdateStrategy::kForceDisabled;

  ::testing::NiceMock<syncer::MockDataTypeLocalChangeProcessor> mock_processor_;
};

// Used in BrowserTests to ensure that the WebAppProvider that is create on
// profile startup is the FakeWebAppProvider. Hooks into the
// BrowserContextKeyedService initialization pipeline.
class FakeWebAppProviderCreator {
 public:
  using CreateWebAppProviderCallback =
      base::RepeatingCallback<std::unique_ptr<KeyedService>(Profile* profile)>;

  // Uses FakeWebAppProvider::BuildDefault to build the FakeWebAppProvider.
  FakeWebAppProviderCreator();
  // Uses the given callback to create the FakeWebAppProvider.
  explicit FakeWebAppProviderCreator(CreateWebAppProviderCallback callback);
  ~FakeWebAppProviderCreator();

 private:
  void OnWillCreateBrowserContextServices(content::BrowserContext* context);
  std::unique_ptr<KeyedService> CreateWebAppProvider(
      content::BrowserContext* context);

  CreateWebAppProviderCallback callback_;

  base::CallbackListSubscription create_services_subscription_;
};

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_TEST_FAKE_WEB_APP_PROVIDER_H_