-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
web_app_browsertest_util.cc
410 lines (358 loc) · 14.8 KB
/
web_app_browsertest_util.cc
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include <memory>
#include <string>
#include "base/callback_helpers.h"
#include "base/one_shot_event.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/threading/thread_task_runner_handle.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/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/web_applications/components/app_icon_manager.h"
#include "chrome/browser/web_applications/components/external_install_options.h"
#include "chrome/browser/web_applications/components/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/components/install_finalizer.h"
#include "chrome/browser/web_applications/components/web_app_constants.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/components/web_application_info.h"
#include "chrome/browser/web_applications/test/service_worker_registration_waiter.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/models/menu_model.h"
#include "ui/base/page_transition_types.h"
#if defined(OS_MAC)
#include <ImageIO/ImageIO.h>
#import "skia/ext/skia_utils_mac.h"
#endif
#if defined(OS_WIN)
#include <shellapi.h>
#include "ui/gfx/icon_util.h"
#endif
using ui_test_utils::BrowserChangeObserver;
namespace web_app {
namespace {
void WaitUntilReady(WebAppProvider* provider) {
if (provider->on_registry_ready().is_signaled())
return;
base::RunLoop run_loop;
provider->on_registry_ready().Post(FROM_HERE, run_loop.QuitClosure());
run_loop.Run();
}
// Waits for |browser| to be removed from BrowserList and then calls |callback|.
class BrowserRemovedWaiter final : public BrowserListObserver {
public:
explicit BrowserRemovedWaiter(Browser* browser) : browser_(browser) {}
~BrowserRemovedWaiter() override = default;
void Wait() {
BrowserList::AddObserver(this);
run_loop_.Run();
}
// BrowserListObserver
void OnBrowserRemoved(Browser* browser) override {
if (browser != browser_)
return;
BrowserList::RemoveObserver(this);
// Post a task to ensure the Remove event has been dispatched to all
// observers.
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
run_loop_.QuitClosure());
}
private:
Browser* browser_;
base::RunLoop run_loop_;
};
void AutoAcceptDialogCallback(
content::WebContents* initiator_web_contents,
std::unique_ptr<WebApplicationInfo> web_app_info,
ForInstallableSite for_installable_site,
WebAppInstallManager::WebAppInstallationAcceptanceCallback
acceptance_callback) {
web_app_info->open_as_window = true;
std::move(acceptance_callback)
.Run(
/*user_accepted=*/true, std::move(web_app_info));
}
} // namespace
SkColor GetIconTopLeftColor(const base::FilePath& shortcut_path) {
#if defined(OS_MAC)
base::FilePath icon_path =
shortcut_path.AppendASCII("Contents/Resources/app.icns");
base::ScopedCFTypeRef<CFDictionaryRef> empty_dict(
CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL));
base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
NULL, (const UInt8*)icon_path.value().c_str(), icon_path.value().length(),
false));
CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
// Get the first icon in the .icns file (index 0)
base::ScopedCFTypeRef<CGImageRef> cg_image(
CGImageSourceCreateImageAtIndex(source, 0, empty_dict));
SkBitmap bitmap = skia::CGImageToSkBitmap(cg_image);
return bitmap.getColor(0, 0);
#elif defined(OS_WIN)
SHFILEINFO file_info = {0};
if (SHGetFileInfo(shortcut_path.value().c_str(), FILE_ATTRIBUTE_NORMAL,
&file_info, sizeof(file_info),
SHGFI_ICON | 0 | SHGFI_USEFILEATTRIBUTES)) {
const SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(file_info.hIcon);
return bitmap.getColor(0, 0);
}
#endif
return 0;
}
AppId InstallWebAppFromPage(Browser* browser, const GURL& app_url) {
NavigateToURLAndWait(browser, app_url);
AppId app_id;
base::RunLoop run_loop;
auto* provider = WebAppProvider::Get(browser->profile());
DCHECK(provider);
WaitUntilReady(provider);
provider->install_manager().InstallWebAppFromManifestWithFallback(
browser->tab_strip_model()->GetActiveWebContents(),
/*force_shortcut_app=*/false,
webapps::WebappInstallSource::MENU_BROWSER_TAB,
base::BindOnce(&AutoAcceptDialogCallback),
base::BindLambdaForTesting(
[&run_loop, &app_id](const AppId& installed_app_id,
InstallResultCode code) {
DCHECK_EQ(code, InstallResultCode::kSuccessNewInstall);
app_id = installed_app_id;
run_loop.Quit();
}));
run_loop.Run();
return app_id;
}
AppId InstallWebAppFromManifest(Browser* browser, const GURL& app_url) {
ServiceWorkerRegistrationWaiter registration_waiter(browser->profile(),
app_url);
NavigateToURLAndWait(browser, app_url);
registration_waiter.AwaitRegistration();
AppId app_id;
base::RunLoop run_loop;
auto* provider = WebAppProvider::Get(browser->profile());
DCHECK(provider);
WaitUntilReady(provider);
provider->install_manager().InstallWebAppFromManifestWithFallback(
browser->tab_strip_model()->GetActiveWebContents(),
/*force_shortcut_app=*/false,
webapps::WebappInstallSource::MENU_BROWSER_TAB,
base::BindOnce(&AutoAcceptDialogCallback),
base::BindLambdaForTesting(
[&run_loop, &app_id](const AppId& installed_app_id,
InstallResultCode code) {
DCHECK_EQ(code, InstallResultCode::kSuccessNewInstall);
app_id = installed_app_id;
run_loop.Quit();
}));
run_loop.Run();
return app_id;
}
Browser* LaunchWebAppBrowser(Profile* profile, const AppId& app_id) {
BrowserChangeObserver observer(nullptr,
BrowserChangeObserver::ChangeType::kAdded);
EXPECT_TRUE(
apps::AppServiceProxyFactory::GetForProfile(profile)
->BrowserAppLauncher()
->LaunchAppWithParams(apps::AppLaunchParams(
app_id, apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::CURRENT_TAB,
apps::mojom::AppLaunchSource::kSourceTest)));
Browser* browser = observer.Wait();
EXPECT_EQ(browser, chrome::FindLastActive());
bool is_correct_app_browser =
browser && GetAppIdFromApplicationName(browser->app_name()) == app_id;
EXPECT_TRUE(is_correct_app_browser);
return is_correct_app_browser ? browser : nullptr;
}
// Launches the app, waits for the app url to load.
Browser* LaunchWebAppBrowserAndWait(Profile* profile, const AppId& app_id) {
ui_test_utils::UrlLoadObserver url_observer(
WebAppProvider::Get(profile)->registrar().GetAppLaunchUrl(app_id),
content::NotificationService::AllSources());
Browser* const app_browser = LaunchWebAppBrowser(profile, app_id);
url_observer.Wait();
return app_browser;
}
Browser* LaunchBrowserForWebAppInTab(Profile* profile, const AppId& app_id) {
content::WebContents* web_contents =
apps::AppServiceProxyFactory::GetForProfile(profile)
->BrowserAppLauncher()
->LaunchAppWithParams(apps::AppLaunchParams(
app_id, apps::mojom::LaunchContainer::kLaunchContainerTab,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
apps::mojom::AppLaunchSource::kSourceTest));
DCHECK(web_contents);
WebAppTabHelper* tab_helper = WebAppTabHelper::FromWebContents(web_contents);
DCHECK(tab_helper);
EXPECT_EQ(app_id, tab_helper->GetAppId());
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
EXPECT_EQ(browser, chrome::FindLastActive());
EXPECT_EQ(web_contents, browser->tab_strip_model()->GetActiveWebContents());
return browser;
}
ExternalInstallOptions CreateInstallOptions(const GURL& url) {
ExternalInstallOptions install_options(
url, DisplayMode::kStandalone, ExternalInstallSource::kInternalDefault);
// Avoid creating real shortcuts in tests.
install_options.add_to_applications_menu = false;
install_options.add_to_desktop = false;
install_options.add_to_quick_launch_bar = false;
return install_options;
}
InstallResultCode ExternallyManagedAppManagerInstall(
Profile* profile,
ExternalInstallOptions install_options) {
DCHECK(profile);
auto* provider = WebAppProvider::Get(profile);
DCHECK(provider);
WaitUntilReady(provider);
base::RunLoop run_loop;
InstallResultCode result_code;
provider->externally_managed_app_manager().Install(
std::move(install_options),
base::BindLambdaForTesting(
[&result_code, &run_loop](
const GURL& provided_url,
ExternallyManagedAppManager::InstallResult result) {
result_code = result.code;
run_loop.Quit();
}));
run_loop.Run();
return result_code;
}
void NavigateToURLAndWait(Browser* browser,
const GURL& url,
bool proceed_through_interstitial) {
content::WebContents* web_contents =
browser->tab_strip_model()->GetActiveWebContents();
{
content::TestNavigationObserver observer(
web_contents, content::MessageLoopRunner::QuitMode::DEFERRED);
NavigateParams params(browser, url, ui::PAGE_TRANSITION_LINK);
ui_test_utils::NavigateToURL(¶ms);
observer.WaitForNavigationFinished();
}
if (!proceed_through_interstitial)
return;
{
// Need a second TestNavigationObserver; the above one is spent.
content::TestNavigationObserver observer(
web_contents, content::MessageLoopRunner::QuitMode::DEFERRED);
security_interstitials::SecurityInterstitialTabHelper* helper =
security_interstitials::SecurityInterstitialTabHelper::FromWebContents(
browser->tab_strip_model()->GetActiveWebContents());
ASSERT_TRUE(
helper &&
helper->GetBlockingPageForCurrentlyCommittedNavigationForTesting());
std::string javascript = "window.certificateErrorPageController.proceed();";
ASSERT_TRUE(content::ExecuteScript(web_contents, javascript));
observer.Wait();
}
}
// Performs a navigation and then checks that the toolbar visibility is as
// expected.
void NavigateAndCheckForToolbar(Browser* browser,
const GURL& url,
bool expected_visibility,
bool proceed_through_interstitial) {
NavigateToURLAndWait(browser, url, proceed_through_interstitial);
EXPECT_EQ(expected_visibility,
browser->app_controller()->ShouldShowCustomTabBar());
}
AppMenuCommandState GetAppMenuCommandState(int command_id, Browser* browser) {
DCHECK(!browser->app_controller())
<< "This check only applies to regular browser windows.";
auto app_menu_model = std::make_unique<AppMenuModel>(nullptr, browser);
app_menu_model->Init();
ui::MenuModel* model = app_menu_model.get();
int index = -1;
if (!app_menu_model->GetModelAndIndexForCommandId(command_id, &model,
&index)) {
return kNotPresent;
}
return model->IsEnabledAt(index) ? kEnabled : kDisabled;
}
Browser* FindWebAppBrowser(Profile* profile, const AppId& app_id) {
for (auto* browser : *BrowserList::GetInstance()) {
if (browser->profile() != profile)
continue;
if (AppBrowserController::IsForWebApp(browser, app_id))
return browser;
}
return nullptr;
}
void CloseAndWait(Browser* browser) {
BrowserRemovedWaiter waiter(browser);
browser->window()->Close();
waiter.Wait();
}
void WaitForBrowserToBeClosed(Browser* browser) {
BrowserRemovedWaiter waiter(browser);
waiter.Wait();
}
bool IsBrowserOpen(const Browser* test_browser) {
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser == test_browser)
return true;
}
return false;
}
void UninstallWebApp(Profile* profile, const AppId& app_id) {
auto* provider = WebAppProvider::Get(profile);
DCHECK(provider);
DCHECK(provider->install_finalizer().CanUserUninstallWebApp(app_id));
provider->install_finalizer().UninstallWebApp(
app_id, webapps::WebappUninstallSource::kAppMenu, base::DoNothing());
}
void UninstallWebAppWithCallback(Profile* profile,
const AppId& app_id,
UninstallWebAppCallback callback) {
auto* provider = WebAppProvider::Get(profile);
DCHECK(provider);
DCHECK(provider->install_finalizer().CanUserUninstallWebApp(app_id));
provider->install_finalizer().UninstallWebApp(
app_id, webapps::WebappUninstallSource::kAppMenu, std::move(callback));
}
SkColor ReadAppIconPixel(Profile* profile,
const AppId& app_id,
SquareSizePx size,
int x,
int y) {
SkColor result;
base::RunLoop run_loop;
WebAppProvider::Get(profile)->icon_manager().ReadIcons(
app_id, IconPurpose::ANY, {size},
base::BindLambdaForTesting(
[&](std::map<SquareSizePx, SkBitmap> icon_bitmaps) {
run_loop.Quit();
result = icon_bitmaps.at(size).getColor(x, y);
}));
run_loop.Run();
return result;
}
} // namespace web_app