File: app_browsertest_util.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 (325 lines) | stat: -rw-r--r-- 11,404 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
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"

#include <memory>
#include <string>

#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/browser_app_launcher.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/apps/chrome_app_delegate.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/app_launch_params.h"
#include "chrome/browser/web_applications/extension_status_utils.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/app_window/app_window_contents.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/app_window/native_app_window.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/constants.h"
#include "extensions/common/switches.h"
#include "extensions/test/extension_test_message_listener.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/apps/app_service/chrome_app_deprecation/chrome_app_deprecation.h"
#include "chrome/browser/ui/ash/cast_config/cast_config_controller_media_router.h"
#include "components/media_router/browser/media_routes_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#endif

using content::WebContents;

namespace {

const char kAppWindowTestApp[] = "app_window/generic";

}  // namespace

namespace extensions {

namespace utils = api_test_utils;

PlatformAppBrowserTest::PlatformAppBrowserTest()
    : enable_chrome_apps_(
          &extensions::testing::g_enable_chrome_apps_for_testing,
          true) {
  ChromeAppDelegate::DisableExternalOpenForTesting();
}

PlatformAppBrowserTest::~PlatformAppBrowserTest() = default;

void PlatformAppBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
  // Skip ExtensionApiTest::SetUpCommandLine.
  // MixinBasedExtensionApiTest::SetUpCommandLine is inlined here, but instead
  // of calling ExtensionApiTest::SetUpCommandLine, we call
  // ExtensionBrowserTest::SetUpCommandLine directly.
  mixin_host_.SetUpCommandLine(command_line);
  ExtensionBrowserTest::SetUpCommandLine(command_line);

  // Make event pages get suspended quicker.
  ProcessManager::SetEventPageIdleTimeForTesting(1000);
  ProcessManager::SetEventPageSuspendingTimeForTesting(1000);
}

void PlatformAppBrowserTest::SetUpOnMainThread() {
  MixinBasedExtensionApiTest::SetUpOnMainThread();
#if BUILDFLAG(IS_CHROMEOS)
  // Mock the Media Router in extension api tests. Several of the
  // PlatformAppBrowserTest suites call RunAllPendingInMessageLoop() when there
  // are mojo messages that will call back into Profile creation through the
  // media router.
  media_router_ = std::make_unique<media_router::MockMediaRouter>();
  ON_CALL(*media_router_, RegisterMediaSinksObserver(::testing::_))
      .WillByDefault(::testing::Return(true));

  CastConfigControllerMediaRouter::SetMediaRouterForTest(media_router_.get());
#endif
}

void PlatformAppBrowserTest::TearDownOnMainThread() {
#if BUILDFLAG(IS_CHROMEOS)
  CastConfigControllerMediaRouter::SetMediaRouterForTest(nullptr);
#endif
  MixinBasedExtensionApiTest::TearDownOnMainThread();
}

// static
AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForBrowser(
    Browser* browser) {
  AppWindowRegistry* app_registry = AppWindowRegistry::Get(browser->profile());
  const AppWindowRegistry::AppWindowList& app_windows =
      app_registry->app_windows();

  auto iter = app_windows.begin();
  if (iter != app_windows.end())
    return *iter;

  return nullptr;
}

const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
    const char* name,
    ExtensionTestMessageListener* listener) {
  DCHECK(listener);
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
  EXPECT_TRUE(extension);

#if BUILDFLAG(IS_CHROMEOS)
  apps::chrome_app_deprecation::ScopedAddAppToAllowlistForTesting allowlist(
      extension->id());
#endif

  LaunchPlatformApp(extension);

  EXPECT_TRUE(listener->WaitUntilSatisfied())
      << "'" << listener->message() << "' message was not receieved";

  return extension;
}

const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
    const char* name,
    const std::string& message) {
  ExtensionTestMessageListener launched_listener(message);
  const Extension* extension =
      LoadAndLaunchPlatformApp(name, &launched_listener);

  return extension;
}

const Extension* PlatformAppBrowserTest::InstallPlatformApp(const char* name) {
  const Extension* extension = InstallExtension(
      test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
  EXPECT_TRUE(extension);

  return extension;
}

const Extension* PlatformAppBrowserTest::InstallHostedApp() {
  const Extension* extension =
      InstallExtension(test_data_dir_.AppendASCII("hosted_app"), 1);
  EXPECT_TRUE(extension);

  return extension;
}

const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
    const char* name) {
  content::CreateAndLoadWebContentsObserver app_loaded_observer;

  const Extension* extension = InstallPlatformApp(name);

  LaunchPlatformApp(extension);

  app_loaded_observer.Wait();

  return extension;
}

void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
  apps::AppServiceProxyFactory::GetForProfile(profile())
      ->BrowserAppLauncher()
      ->LaunchAppWithParamsForTesting(apps::AppLaunchParams(
          extension->id(), apps::LaunchContainer::kLaunchContainerNone,
          WindowOpenDisposition::NEW_WINDOW, apps::LaunchSource::kFromTest));
}

void PlatformAppBrowserTest::LaunchHostedApp(const Extension* extension) {
  apps::AppServiceProxyFactory::GetForProfile(profile())
      ->BrowserAppLauncher()
      ->LaunchAppWithParamsForTesting(CreateAppLaunchParamsUserContainer(
          browser()->profile(), extension,
          WindowOpenDisposition::NEW_FOREGROUND_TAB,
          apps::LaunchSource::kFromCommandLine));
}

WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
  AppWindow* window = GetFirstAppWindow();
  if (window)
    return window->web_contents();

  return nullptr;
}

AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
  return GetFirstAppWindowForBrowser(browser());
}

AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
    const std::string& app_id) {
  AppWindowRegistry* app_registry =
      AppWindowRegistry::Get(browser()->profile());
  const AppWindowRegistry::AppWindowList& app_windows =
      app_registry->GetAppWindowsForApp(app_id);

  auto iter = app_windows.begin();
  if (iter != app_windows.end())
    return *iter;

  return nullptr;
}

size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
    const Extension* extension) {
  scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
  function->set_extension(extension);
  base::Value::List result(
      utils::ToList(utils::RunFunctionAndReturnSingleResult(
          function.get(), "[]", browser()->profile())));
  return result.size();
}

bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
    int window_id,
    const Extension* extension) {
  scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
  function->set_extension(extension);
  utils::RunFunction(function.get(), base::StringPrintf("[%u]", window_id),
                     browser()->profile(), api_test_utils::FunctionMode::kNone);
  return *function->response_type() ==
         ExtensionFunction::ResponseType::kSucceeded;
}

size_t PlatformAppBrowserTest::GetAppWindowCount() {
  return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
}

size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
    const std::string& app_id) {
  return AppWindowRegistry::Get(browser()->profile())
      ->GetAppWindowsForApp(app_id)
      .size();
}

AppWindow* PlatformAppBrowserTest::CreateAppWindow(
    content::BrowserContext* context,
    const Extension* extension) {
  return CreateAppWindowFromParams(context, extension,
                                   AppWindow::CreateParams());
}

AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
    content::BrowserContext* context,
    const Extension* extension,
    const AppWindow::CreateParams& params) {
  AppWindow* window = new AppWindow(
      browser()->profile(),
      std::make_unique<ChromeAppDelegate>(browser()->profile(), true),
      extension);
  ProcessManager* process_manager = ProcessManager::Get(context);
  ExtensionHost* background_host =
      process_manager->GetBackgroundHostForExtension(extension->id());
  window->Init(GURL(std::string()),
               std::make_unique<AppWindowContentsImpl>(window),
               background_host->host_contents()->GetPrimaryMainFrame(), params);
  return window;
}

void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
  content::WebContentsDestroyedWatcher destroyed_watcher(
      window->web_contents());
  window->GetBaseWindow()->Close();
  destroyed_watcher.Wait();
}

void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
    AppWindow* window,
    const gfx::Rect& cached_bounds,
    const gfx::Rect& cached_screen_bounds,
    const gfx::Rect& current_screen_bounds,
    const gfx::Size& minimum_size,
    gfx::Rect* bounds) {
  window->AdjustBoundsToBeVisibleOnScreen(cached_bounds, cached_screen_bounds,
                                          current_screen_bounds, minimum_size,
                                          bounds);
}

AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
    const std::string& window_create_options) {
  ExtensionTestMessageListener launched_listener("launched",
                                                 ReplyBehavior::kWillReply);
  ExtensionTestMessageListener loaded_listener("window_loaded");

  // Load and launch the test app.
  const Extension* extension =
      LoadAndLaunchPlatformApp(kAppWindowTestApp, &launched_listener);
  EXPECT_TRUE(extension);
  EXPECT_TRUE(launched_listener.WaitUntilSatisfied());

  // Send the options for window creation.
  launched_listener.Reply(window_create_options);

  // Wait for the window to be opened and loaded.
  EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());

  EXPECT_EQ(1U, GetAppWindowCount());
  AppWindow* app_window = GetFirstAppWindow();
  return app_window;
}

NativeAppWindow* PlatformAppBrowserTest::GetNativeAppWindowForAppWindow(
    AppWindow* window) {
  return window->native_app_window_.get();
}

void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
    base::CommandLine* command_line) {
  PlatformAppBrowserTest::SetUpCommandLine(command_line);
  command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
}

}  // namespace extensions