File: fetch_manifest_and_install_command_browsertest.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 (629 lines) | stat: -rw-r--r-- 26,980 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright 2022 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/web_applications/commands/fetch_manifest_and_install_command.h"

#include <algorithm>
#include <cstdlib>
#include <memory>
#include <string>
#include <utility>

#include "base/numerics/safe_conversions.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_future.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/custom_tab_bar_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/fake_web_app_ui_manager.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test_utils.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "content/public/browser/manifest_icon_downloader.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom-shared.h"
#include "third_party/skia/include/core/SkColor.h"

namespace web_app {

namespace {

// Mock of a WebContentsDelegate that catches messages sent to the console.
class WebContentsErrorDelegate : public content::WebContentsDelegate {
 public:
  WebContentsErrorDelegate() = default;

  bool DidAddMessageToConsole(content::WebContents* source,
                              blink::mojom::ConsoleMessageLevel log_level,
                              const std::u16string& message,
                              int32_t line_no,
                              const std::u16string& source_id) override {
    CHECK_EQ(source->GetDelegate(), this);

    if (log_level == blink::mojom::ConsoleMessageLevel::kError) {
      console_errors_.push_back(message);
    }
    return false;
  }

  bool NoConsoleErrors() { return console_errors_.empty(); }

 private:
  std::vector<std::u16string> console_errors_;
};

}  // namespace

class FetchManifestAndInstallCommandTest : public WebAppBrowserTestBase {
 public:
  WebAppInstallDialogCallback CreateDialogCallback(
      bool accept = true,
      mojom::UserDisplayMode user_display_mode =
          mojom::UserDisplayMode::kStandalone) {
    return base::BindLambdaForTesting(
        [accept, user_display_mode](
            base::WeakPtr<WebAppScreenshotFetcher>,
            content::WebContents* initiator_web_contents,
            std::unique_ptr<WebAppInstallInfo> web_app_info,
            WebAppInstallationAcceptanceCallback acceptance_callback) {
          web_app_info->user_display_mode = user_display_mode;
          std::move(acceptance_callback).Run(accept, std::move(web_app_info));
        });
  }
};

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, SuccessInstall) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
                      provider().registrar_unsafe().GetInstallState(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, ReparentInTab) {
  GURL test_url = https_server()->GetURL("/web_apps/minimal_ui/basic.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
      install_future;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(), install_future.GetCallback(),
      FallbackBehavior::kCraftedManifestOnly);
  base::HistogramTester tester;
  ASSERT_TRUE(install_future.Wait());
  tester.ExpectUniqueSample("WebApp.LaunchSource",
                            apps::LaunchSource::kFromReparenting, 1);
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       WontReparentFromDevtools) {
  GURL test_url = https_server()->GetURL("/web_apps/minimal_ui/basic.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
      install_future;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::DEVTOOLS,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(), install_future.GetCallback(),
      FallbackBehavior::kCraftedManifestOnly);
  base::HistogramTester tester;
  ASSERT_TRUE(install_future.Wait());
  tester.ExpectUniqueSample("WebApp.LaunchSource",
                            apps::LaunchSource::kFromReparenting, 0);
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, MultipleManifests) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "multiple_manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
      install_future;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(), install_future.GetCallback(),
      FallbackBehavior::kCraftedManifestOnly);
  ASSERT_TRUE(install_future.Wait());
  EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
            webapps::InstallResultCode::kSuccessNewInstall);
  webapps::AppId app_id = install_future.Get<webapps::AppId>();
  EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
            provider().registrar_unsafe().GetInstallState(app_id));

  // multiple_manifest_test_page.html includes both manifest_with_id.json and
  // manifest.json. Section 4.6.7.10 of the HTML spec says the first manifest
  // should be used.
  EXPECT_EQ(provider().registrar_unsafe().GetAppManifestId(app_id),
            https_server()->GetURL("/some_id"));
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, MultipleInstalls) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  // Schedule two installs. The second should fail because the first will cause
  // a navigation (because reparenting somehow changes visiblity, which is
  // wrong, but fine).
  base::RunLoop loop;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
                      provider().registrar_unsafe().GetInstallState(app_id));
          }),
      FallbackBehavior::kCraftedManifestOnly);

  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting([&](const webapps::AppId& app_id,
                                     webapps::InstallResultCode code) {
        EXPECT_EQ(
            code,
            webapps::InstallResultCode::kCancelledDueToMainFrameNavigation);
        EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(app_id));
        loop.Quit();
      }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, InvalidManifest) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "no_manifest_test_page.html");
  EXPECT_FALSE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code,
                      webapps::InstallResultCode::kNotValidManifestForWebApp);
            EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest, UserDeclineInstall) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),

      CreateDialogCallback(/*accept=*/false),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kUserInstallDeclined);
            EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       HandleWebContentsDestroyed) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      web_contents->GetWeakPtr(), CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kWebContentsDestroyed);
            EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kCraftedManifestOnly);

  // Create a new tab to ensure that the browser isn't destroyed with the web
  // contents closing.
  chrome::NewTab(browser());
  web_contents->Close();

  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       InstallWithFallback) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "no_manifest_test_page.html");
  EXPECT_FALSE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      web_contents->GetWeakPtr(), CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
                      provider().registrar_unsafe().GetInstallState(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kAllowFallbackDataAlways);
  loop.Run();
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       InstallWithFallbackOverwriteInstalled) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "no_manifest_test_page.html");
  auto web_app = test::CreateWebApp(test_url);
  const webapps::AppId app_id = web_app->app_id();

  {
    ScopedRegistryUpdate update = provider().sync_bridge_unsafe().BeginUpdate();
    web_app->SetUserDisplayMode(mojom::UserDisplayMode::kStandalone);
    web_app->SetInstallState(
        proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE);
    update->CreateApp(std::move(web_app));
  }

  EXPECT_EQ(proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE,
            provider().registrar_unsafe().GetInstallState(app_id));
  EXPECT_EQ(provider().registrar_unsafe().GetAppUserDisplayMode(app_id).value(),
            mojom::UserDisplayMode::kBrowser);

  EXPECT_FALSE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::RunLoop loop;
  auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      web_contents->GetWeakPtr(),

      CreateDialogCallback(
          /*accept=*/true,
          /*user_display_mode=*/mojom::UserDisplayMode::kStandalone),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            loop.Quit();
          }),
      FallbackBehavior::kAllowFallbackDataAlways);
  loop.Run();
  EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
            provider().registrar_unsafe().GetInstallState(app_id));

  EXPECT_EQ(provider().registrar_unsafe().GetAppUserDisplayMode(app_id).value(),
            mojom::UserDisplayMode::kStandalone);
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       InstallFromOutsideScopeToolbarHasBackButton) {
  GURL test_url = https_server()->GetURL("/banners/app_with_nested/index.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
      install_future;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(), install_future.GetCallback(),
      FallbackBehavior::kCraftedManifestOnly);
  ASSERT_TRUE(install_future.Wait());
  EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
            webapps::InstallResultCode::kSuccessNewInstall);
  webapps::AppId app_id = install_future.Get<webapps::AppId>();
  EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
            provider().registrar_unsafe().GetInstallState(app_id));
  EXPECT_EQ(provider().registrar_unsafe().GetAppUserDisplayMode(app_id),
            mojom::UserDisplayMode::kStandalone);

  Browser* app_browser =
      AppBrowserController::FindForWebApp(*profile(), app_id);
  ASSERT_TRUE(app_browser);
  EXPECT_TRUE(app_browser->app_controller()->ShouldShowCustomTabBar());

  BrowserView* app_view = BrowserView::GetBrowserViewForBrowser(app_browser);
  ASSERT_TRUE(app_view);
  EXPECT_TRUE(
      app_view->toolbar()->custom_tab_bar()->IsShowingOriginForTesting());
  EXPECT_TRUE(
      app_view->toolbar()->custom_tab_bar()->IsShowingCloseButtonForTesting());
}

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandTest,
                       InstallFromPwaWindowDoesNotReparent) {
  GURL test_url = https_server()->GetURL("/banners/manifest_test_page.html");
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::WeakPtr<content::WebContents> active_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr();
  // Install the app that will be used to try to install from.
  webapps::AppId app_id;
  {
    base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
        install_future;
    provider().scheduler().FetchManifestAndInstall(
        webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON, active_web_contents,
        CreateDialogCallback(), install_future.GetCallback(),
        FallbackBehavior::kCraftedManifestOnly);
    ASSERT_TRUE(install_future.Wait());
    EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
              webapps::InstallResultCode::kSuccessNewInstall);
    app_id = install_future.Get<webapps::AppId>();
    EXPECT_EQ(WebAppTabHelper::FromWebContents(active_web_contents.get())
                  ->window_app_id(),
              app_id);
  }
  Browser* app_browser =
      AppBrowserController::FindForWebApp(*profile(), app_id);
  ASSERT_TRUE(app_browser);

  // Navigate to another installable page, and verify that installing from this
  // page (which shouldn't be possible in our UX, but is 'valid' for the
  // command) does not reparent the current web contents into the installed app.
  webapps::AppId other_app_id;
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(
      app_browser, https_server()->GetURL("/web_apps/simple/index.html")));
  {
    base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
        install_future;
    ASSERT_TRUE(active_web_contents);
    // Ensure the web contents is visible & focused. This can be flaky, so
    // bypass visibility checks. See Mac flakiness example at
    // https://bit.ly/4gL5Eks.
    active_web_contents->Focus();
    auto bypass_visibility_check =
        FetchManifestAndInstallCommand::BypassVisibilityCheckForTesting();

    provider().scheduler().FetchManifestAndInstall(
        webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON, active_web_contents,
        CreateDialogCallback(), install_future.GetCallback(),
        FallbackBehavior::kCraftedManifestOnly);
    ASSERT_TRUE(install_future.Wait());
    EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
              webapps::InstallResultCode::kSuccessNewInstall);
    other_app_id = install_future.Get<webapps::AppId>();
  }

  EXPECT_NE(other_app_id, app_id);
  EXPECT_FALSE(AppBrowserController::FindForWebApp(*profile(), other_app_id));
  ASSERT_TRUE(active_web_contents);
  EXPECT_EQ(WebAppTabHelper::FromWebContents(active_web_contents.get())
                ->window_app_id(),
            app_id);
}

// Tests that when an SVG icon is passed in the manifest with a size of |any|,
// the icon is downloaded correctly for the web app during installation.
// Parameterized to work with SVG icons that have an intrinsic size (width and
// height defined) as well as no intrinsic size.
class FetchManifestAndInstallCommandTestWithSVG
    : public FetchManifestAndInstallCommandTest,
      public ::testing::WithParamInterface<bool> {
 protected:
  GURL GetSiteUrlBasedOnSVGParams() {
    if (GetParam()) {
      return https_server()->GetURL(
          "/banners/"
          "manifest_test_page.html?manifest=manifest_svg_icon_any.json");
    } else {
      return https_server()->GetURL(
          "/banners/"
          "manifest_test_page.html?manifest=manifest_svg_icon_no_intrinsic_"
          "size.json");
    }
  }

  // There is a minor loss in image quality during resizing that leads to colors
  // being not equal for precision for 2-3 decimal places. This is a helper
  // function to work around that behavior by verifying that the difference is
  // within some error margin.
  bool VerifyColorsMatchWithinErrorPrecision(SkColor read_color,
                                             SkColor expected_color) {
    bool colors_match = true;
    std::array<float, 4> read_colors = SkColor4f::FromColor(read_color).array();
    std::array<float, 4> expected_colors =
        SkColor4f::FromColor(expected_color).array();
    float precision = 0.02f;
    for (int i = 0; i < 4; i++) {
      colors_match =
          colors_match &&
          (std::abs(read_colors[i] - expected_colors[i]) < precision);
    }

    return colors_match;
  }
};

IN_PROC_BROWSER_TEST_P(FetchManifestAndInstallCommandTestWithSVG,
                       SuccessInstallSVGIcons) {
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(
      browser(), GetSiteUrlBasedOnSVGParams()));

  base::RunLoop loop;
  webapps::AppId installed_app_id;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting([&](const webapps::AppId& app_id,
                                     webapps::InstallResultCode code) {
        EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
        EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
                  provider().registrar_unsafe().GetInstallState(app_id));
        installed_app_id = app_id;
        loop.Quit();
      }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();

  for (const int& icon_size : web_app::SizesToGenerate()) {
    SkColor small_icon_color = IconManagerReadAppIconPixel(
        provider().icon_manager(), installed_app_id, icon_size,
        /*x=*/icon_size / 2, /*y=*/icon_size / 2);
    EXPECT_TRUE(
        VerifyColorsMatchWithinErrorPrecision(small_icon_color, SK_ColorRED));
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         FetchManifestAndInstallCommandTestWithSVG,
                         ::testing::Bool(),
                         [](const testing::TestParamInfo<bool>& info) {
                           return info.param ? "SVGIconIntrinsicSize"
                                             : "SVGIconNoIntrinsicSize";
                         });

using FetchManifestAndInstallCommandUniversalInstallTest =
    FetchManifestAndInstallCommandTest;

IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallCommandUniversalInstallTest,
                       NoManifest) {
  GURL test_url = https_server()->GetURL(
      "/banners/"
      "no_manifest_test_page.html");
  EXPECT_FALSE(NavigateAndAwaitInstallabilityCheck(browser(), test_url));

  base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
      install_future;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(), install_future.GetCallback(),
      FallbackBehavior::kUseFallbackInfoWhenNotInstallable);
  ASSERT_TRUE(install_future.Wait());
  EXPECT_EQ(install_future.Get<webapps::InstallResultCode>(),
            webapps::InstallResultCode::kSuccessNewInstall);
  webapps::AppId app_id = install_future.Get<webapps::AppId>();
  EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
            provider().registrar_unsafe().GetInstallState(app_id));

  EXPECT_EQ("Web app banner test page",
            provider().registrar_unsafe().GetAppShortName(app_id));
  auto os_integration =
      provider().registrar_unsafe().GetAppCurrentOsIntegrationState(app_id);
  ASSERT_TRUE(os_integration);
  EXPECT_TRUE(os_integration->has_shortcut());
  // TODO(crbug.com/291778116): Add more checks once DIY apps are supported.
  EXPECT_TRUE(provider().registrar_unsafe().IsDiyApp(app_id));
}

// Test for crbug.com/381069204, where triggering an install command on
// chrome://password-manager does not throw any console errors.
using FetchManifestAndInstallTestNoConsoleErrors =
    FetchManifestAndInstallCommandTest;
IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallTestNoConsoleErrors,
                       PasswordManager) {
  std::unique_ptr<WebContentsErrorDelegate> delegate =
      std::make_unique<WebContentsErrorDelegate>();
  browser()->tab_strip_model()->GetActiveWebContents()->SetDelegate(
      delegate.get());

  GURL chrome_password_manager("chrome://password-manager/");
  EXPECT_TRUE(
      NavigateAndAwaitInstallabilityCheck(browser(), chrome_password_manager));

  base::RunLoop loop;
  provider().scheduler().FetchManifestAndInstall(
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
      CreateDialogCallback(),
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            EXPECT_EQ(proto::INSTALLED_WITH_OS_INTEGRATION,
                      provider().registrar_unsafe().GetInstallState(app_id));
            loop.Quit();
          }),
      FallbackBehavior::kCraftedManifestOnly);
  loop.Run();

  EXPECT_TRUE(delegate->NoConsoleErrors());
}

// Valid icon measures the `kSuccess` histogram for chrome urls. This can't
// exist closer to `ManifestIconBrowserTest`, because the `shell()` does not
// load chrome urls.
IN_PROC_BROWSER_TEST_F(FetchManifestAndInstallTestNoConsoleErrors,
                       ChromeUrlHistograms) {
  base::HistogramTester tester;
  GURL password_manager("chrome://password-manager/");

  // This involves loading and fetching the icons specified in the manifest, so
  // histograms are automatically measured.
  EXPECT_TRUE(NavigateAndAwaitInstallabilityCheck(browser(), password_manager));

  tester.ExpectBucketCount("WebApp.ManifestIconDownloader.Result",
                           content::ManifestIconDownloader::Result::kSuccess,
                           1);
  tester.ExpectBucketCount("WebApp.ManifestIconDownloader.ChromeUrl.Result",
                           content::ManifestIconDownloader::Result::kSuccess,
                           1);
}

}  // namespace web_app