File: web_app_internals_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (561 lines) | stat: -rw-r--r-- 22,651 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
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstddef>
#include <optional>
#include <string>
#include <string_view>

#include "base/functional/callback_helpers.h"
#include "base/strings/string_util.h"
#include "base/test/bind.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/ui/webui/web_app_internals/web_app_internals.mojom.h"
#include "chrome/browser/ui/webui/web_app_internals/web_app_internals_handler.h"
#include "chrome/browser/ui/webui/web_app_internals/web_app_internals_ui.h"
#include "chrome/browser/web_applications/isolated_web_apps/commands/isolated_web_app_install_command_helper.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_update_server_mixin.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/test_signed_web_bundle_builder.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/test/web_app_install_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_install_manager.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_registrar.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/isolated_web_apps/update_channel.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace web_app {

namespace {

using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Field;
using ::testing::HasSubstr;
using ::testing::Pointee;

constexpr char kBadIconErrorTemplate[] = R"({
   "!url": "$1banners/manifest_test_page.html",
   "background_installation": false,
   "install_surface": 15,
   "stages": [ {
      "!stage": "OnIconsRetrieved",
      "icons_downloaded_result": "Completed",
      "icons_http_results": [ {
         "http_code_desc": "Not Found",
         "http_status_code": 404,
         "icon_size": "0x0",
         "icon_url": "$1banners/bad_icon.png"
      }, {
         "http_code_desc": "Not Found",
         "http_status_code": 404,
         "icon_size": "0x0",
         "icon_url": "$1favicon.ico"
      } ],
      "is_generated_icon": true
   } ]
}
)";

// Drops all CR and LF characters.
std::string TrimLineEndings(std::string_view text) {
  return base::CollapseWhitespaceASCII(
      text,
      /*trim_sequences_with_line_breaks=*/true);
}

}  // namespace

class WebAppInternalsBrowserTest : public WebAppBrowserTestBase {
 public:
  WebAppInternalsBrowserTest() = default;
  WebAppInternalsBrowserTest(const WebAppInternalsBrowserTest&) = delete;
  WebAppInternalsBrowserTest& operator=(const WebAppInternalsBrowserTest&) =
      delete;

  ~WebAppInternalsBrowserTest() override = default;

  void SetUp() override {
    embedded_test_server()->AddDefaultHandlers(GetChromeTestDataDir());
    embedded_test_server()->RegisterRequestHandler(
        base::BindRepeating(&WebAppInternalsBrowserTest::RequestHandlerOverride,
                            base::Unretained(this)));
    ASSERT_TRUE(embedded_test_server()->Start());

    WebAppBrowserTestBase::SetUp();
  }

  void SetUpOnMainThread() override {
    test::WaitUntilReady(WebAppProvider::GetForTest(browser()->profile()));
    WebAppBrowserTestBase::SetUpOnMainThread();
  }

  webapps::AppId InstallWebApp(const GURL& app_url) {
    EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), app_url));

    webapps::AppId app_id;
    base::RunLoop run_loop;
    GetProvider().scheduler().FetchManifestAndInstall(
        webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
        browser()->tab_strip_model()->GetActiveWebContents()->GetWeakPtr(),
        base::BindOnce(test::TestAcceptDialogCallback),
        base::BindLambdaForTesting([&](const webapps::AppId& new_app_id,
                                       webapps::InstallResultCode code) {
          EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
          app_id = new_app_id;
          run_loop.Quit();
        }),
        FallbackBehavior::kAllowFallbackDataAlways);

    run_loop.Run();
    return app_id;
  }

  WebAppProvider& GetProvider() {
    return *WebAppProvider::GetForTest(browser()->profile());
  }

  std::unique_ptr<net::test_server::HttpResponse> RequestHandlerOverride(
      const net::test_server::HttpRequest& request) {
    if (request_override_)
      return request_override_.Run(request);
    return nullptr;
  }

  void OverrideHttpRequest(GURL url, net::HttpStatusCode http_status_code) {
    request_override_ = base::BindLambdaForTesting(
        [url = std::move(url),
         http_status_code](const net::test_server::HttpRequest& request)
            -> std::unique_ptr<net::test_server::HttpResponse> {
          if (request.GetURL() != url)
            return nullptr;
          auto http_response =
              std::make_unique<net::test_server::BasicHttpResponse>();
          http_response->set_code(http_status_code);
          return std::move(http_response);
        });
  }

 private:
  net::EmbeddedTestServer::HandleRequestCallback request_override_;

  base::test::ScopedFeatureList scoped_feature_list_{
      features::kRecordWebAppDebugInfo};
};

IN_PROC_BROWSER_TEST_F(WebAppInternalsBrowserTest,
                       PRE_InstallManagerErrorsPersist) {
  OverrideHttpRequest(embedded_test_server()->GetURL("/banners/bad_icon.png"),
                      net::HTTP_NOT_FOUND);

  webapps::AppId app_id = InstallWebApp(embedded_test_server()->GetURL(
      "/banners/manifest_test_page.html?manifest=manifest_bad_icon.json"));

  const WebApp* web_app = GetProvider().registrar_unsafe().GetAppById(app_id);
  ASSERT_TRUE(web_app);
  EXPECT_TRUE(web_app->is_generated_icon());

  const std::string expected_error = base::ReplaceStringPlaceholders(
      kBadIconErrorTemplate, {embedded_test_server()->base_url().spec()},
      nullptr);

  ASSERT_TRUE(GetProvider().install_manager().error_log());
  ASSERT_EQ(1u, GetProvider().install_manager().error_log()->size());

  const base::Value& error_log =
      (*GetProvider().install_manager().error_log())[0];
  EXPECT_TRUE(error_log.is_dict());
  EXPECT_EQ(4u, error_log.GetDict().size());

  EXPECT_EQ(TrimLineEndings(expected_error),
            TrimLineEndings(error_log.DebugString()));
}

IN_PROC_BROWSER_TEST_F(WebAppInternalsBrowserTest,
                       InstallManagerErrorsPersist) {
  test::WaitUntilReady(WebAppProvider::GetForTest(browser()->profile()));

  ASSERT_TRUE(GetProvider().install_manager().error_log());
  ASSERT_EQ(1u, GetProvider().install_manager().error_log()->size());

  const base::Value& error_log =
      (*GetProvider().install_manager().error_log())[0];
  EXPECT_TRUE(error_log.is_dict());
  EXPECT_EQ(4u, error_log.GetDict().size());

  // Parses base url from the log: the port for embedded_test_server() changes
  // on every test run.
  const std::string* url_value = error_log.GetDict().FindString("!url");
  ASSERT_TRUE(url_value);
  GURL url{*url_value};
  ASSERT_TRUE(url.is_valid());

  const std::string expected_error = base::ReplaceStringPlaceholders(
      kBadIconErrorTemplate, {url.GetWithEmptyPath().spec()}, nullptr);

  EXPECT_EQ(TrimLineEndings(expected_error),
            TrimLineEndings(error_log.DebugString()));
}

class WebAppInternalsIwaInstallationBrowserTest
    : public IsolatedWebAppBrowserTestHarness {
 protected:
  WebAppInternalsHandler* OpenWebAppInternals() {
    EXPECT_TRUE(ui_test_utils::NavigateToURL(
        browser(), GURL("chrome://web-app-internals")));
    return static_cast<WebAppInternalsUI*>(browser()
                                               ->tab_strip_model()
                                               ->GetActiveWebContents()
                                               ->GetWebUI()
                                               ->GetController())
        ->GetHandlerForTesting();
  }

  IsolatedWebAppUpdateServerMixin update_server_mixin_{&mixin_host_};
};

IN_PROC_BROWSER_TEST_F(WebAppInternalsIwaInstallationBrowserTest,
                       FetchUpdateManifestAndInstallIwaAndUpdate) {
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("1.0.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()));

  auto* handler = OpenWebAppInternals();

  GURL update_manifest_url = update_server_mixin_.GetUpdateManifestUrl(
      test::GetDefaultEd25519WebBundleId());
  base::test::TestFuture<::mojom::ParseUpdateManifestFromUrlResultPtr>
      um_future;
  handler->ParseUpdateManifestFromUrl(update_manifest_url,
                                      um_future.GetCallback());

  auto um_result = um_future.Take();
  ASSERT_TRUE(um_result->is_update_manifest());

  const auto& update_manifest = *um_result->get_update_manifest();

  ASSERT_THAT(update_manifest,
              Field(&::mojom::UpdateManifest::versions,
                    ElementsAre(Pointee(
                        Field(&::mojom::VersionEntry::version, Eq("1.0.0"))))));

  const GURL& web_bundle_url = update_manifest.versions[0]->web_bundle_url;

  base::test::TestFuture<::mojom::InstallIsolatedWebAppResultPtr>
      install_future;
  auto params = ::mojom::InstallFromBundleUrlParams::New();
  params->web_bundle_url = web_bundle_url;
  params->update_info = ::mojom::UpdateInfo::New(
      update_manifest_url, UpdateChannel::default_channel().ToString(),
      /*pinned_version=*/std::nullopt, /*allow_downgrades=*/false);
  handler->InstallIsolatedWebAppFromBundleUrl(std::move(params),
                                              install_future.GetCallback());
  ASSERT_TRUE(install_future.Take()->is_success());

  webapps::AppId app_id = IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(
                              test::GetDefaultEd25519WebBundleId())
                              .app_id();
  {
    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("1.0.0"));
    EXPECT_EQ(iwa.isolation_data()->update_manifest_url(), update_manifest_url);
    EXPECT_EQ(iwa.isolation_data()->update_channel(),
              UpdateChannel::default_channel());
  }

  // Run an update check on the same manifest.
  {
    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(),
                HasSubstr("app is already on the latest version"));
  }

  // Now add a new entry to the manifest and re-run the update check.
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.0.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()));
  {
    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update to v2.0.0 successful"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.0.0"));
    EXPECT_EQ(iwa.isolation_data()->update_manifest_url(), update_manifest_url);
    EXPECT_EQ(iwa.isolation_data()->update_channel(),
              UpdateChannel::default_channel());
  }

  // Set the channel to "beta" and verify that other fields of IsolationData
  // stay intact.
  auto beta_channel = *UpdateChannel::Create("beta");
  {
    base::test::TestFuture<bool> set_channel_future;
    handler->SetUpdateChannelForIsolatedWebApp(
        app_id, beta_channel.ToString(), set_channel_future.GetCallback());
    EXPECT_TRUE(set_channel_future.Get());

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));
    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.0.0"));
    EXPECT_EQ(iwa.isolation_data()->update_manifest_url(), update_manifest_url);
    EXPECT_EQ(iwa.isolation_data()->update_channel(), beta_channel);
  }

  // Now add new entries with v2.1.0 for the `beta` channel and v2.2.0 for the
  // `default` channel and force an update check.
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.1.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()),
      /*update_channels=*/{{beta_channel}});
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.2.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()));

  // The update logic must pick up the v2.1.0 for `beta` instead of a higher
  // v2.2.0 for `default`.
  {
    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update to v2.1.0 successful"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.1.0"));
    EXPECT_EQ(iwa.isolation_data()->update_manifest_url(), update_manifest_url);
    EXPECT_EQ(iwa.isolation_data()->update_channel(), beta_channel);
  }

  // Add v2.3.0 to `beta` channel, pin the app to v2.1.0. Expect no update.
  {
    update_server_mixin_.AddBundle(
        IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.3.0"))
            .BuildBundle(test::GetDefaultEd25519KeyPair()),
        /*update_channels=*/{{beta_channel}});

    base::test::TestFuture<bool> set_pinned_version_future;
    handler->SetPinnedVersionForIsolatedWebApp(
        app_id, "2.1.0", set_pinned_version_future.GetCallback());
    EXPECT_TRUE(set_pinned_version_future.Get());

    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update skipped"));
  }

  // Add v2.4.0 to `beta` channel, pin the app to v2.3.0. Expect an update to
  // v2.3.0.
  {
    update_server_mixin_.AddBundle(
        IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.4.0"))
            .BuildBundle(test::GetDefaultEd25519KeyPair()),
        /*update_channels=*/{{beta_channel}});

    base::test::TestFuture<bool> set_pinned_version_future;
    handler->SetPinnedVersionForIsolatedWebApp(
        app_id, "2.3.0", set_pinned_version_future.GetCallback());
    EXPECT_TRUE(set_pinned_version_future.Get());

    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update to v2.3.0 successful"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.3.0"));
    EXPECT_EQ(iwa.isolation_data()->update_manifest_url(), update_manifest_url);
    EXPECT_EQ(iwa.isolation_data()->update_channel(), beta_channel);
  }

  // Unpin the app. App should be updated to v2.4.0.
  {
    handler->ResetPinnedVersionForIsolatedWebApp(app_id);
    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update to v2.4.0 successful"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.4.0"));
  }

  // Pin to v2.3.0, allow downgrades. Expect update to v2.3.0.
  {
    handler->SetAllowDowngradesForIsolatedWebApp(true, app_id);
    base::test::TestFuture<bool> set_pinned_version_future;
    handler->SetPinnedVersionForIsolatedWebApp(
        app_id, "2.3.0", set_pinned_version_future.GetCallback());
    EXPECT_TRUE(set_pinned_version_future.Get());

    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(update_future.Get(), HasSubstr("Update to v2.3.0 successful"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("2.3.0"));
  }
}

IN_PROC_BROWSER_TEST_F(
    WebAppInternalsIwaInstallationBrowserTest,
    FetchUpdateManifestAndInstallIwaAndUpdateInvalidPinnedVersion) {
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("1.0.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()));

  auto* handler = OpenWebAppInternals();

  GURL update_manifest_url = update_server_mixin_.GetUpdateManifestUrl(
      test::GetDefaultEd25519WebBundleId());
  base::test::TestFuture<::mojom::ParseUpdateManifestFromUrlResultPtr>
      um_future;
  handler->ParseUpdateManifestFromUrl(update_manifest_url,
                                      um_future.GetCallback());

  auto um_result = um_future.Take();
  ASSERT_TRUE(um_result->is_update_manifest());

  const auto& update_manifest = *um_result->get_update_manifest();

  ASSERT_THAT(update_manifest,
              Field(&::mojom::UpdateManifest::versions,
                    ElementsAre(Pointee(
                        Field(&::mojom::VersionEntry::version, Eq("1.0.0"))))));

  const GURL& web_bundle_url = update_manifest.versions[0]->web_bundle_url;

  base::test::TestFuture<::mojom::InstallIsolatedWebAppResultPtr>
      install_future;
  auto params = ::mojom::InstallFromBundleUrlParams::New();
  params->web_bundle_url = web_bundle_url;
  params->update_info = ::mojom::UpdateInfo::New(
      update_manifest_url, UpdateChannel::default_channel().ToString(),
      /*pinned_version=*/std::nullopt, /*allow_downgrades=*/false);
  handler->InstallIsolatedWebAppFromBundleUrl(std::move(params),
                                              install_future.GetCallback());
  ASSERT_TRUE(install_future.Take()->is_success());

  webapps::AppId app_id = IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(
                              test::GetDefaultEd25519WebBundleId())
                              .app_id();

  // Add v2.0.0 to the update manifest.
  update_server_mixin_.AddBundle(
      IsolatedWebAppBuilder(ManifestBuilder().SetVersion("2.0.0"))
          .BuildBundle(test::GetDefaultEd25519KeyPair()));

  // Pin the app to an non-existent but valid version.
  {
    base::test::TestFuture<bool> set_pinned_version_future;
    handler->SetPinnedVersionForIsolatedWebApp(
        app_id, "1.1.1", set_pinned_version_future.GetCallback());
    EXPECT_TRUE(set_pinned_version_future.Get());

    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(
        update_future.Get(),
        HasSubstr("Update failed: Error::kUpdateManifestNoApplicableVersion"));

    ASSERT_OK_AND_ASSIGN(
        const WebApp& iwa,
        GetIsolatedWebAppById(provider().registrar_unsafe(), app_id));

    // Expect the app to stay at v1.0.0.
    EXPECT_EQ(iwa.isolation_data()->version(), base::Version("1.0.0"));
  }

  // Fails to pin the app to invalid version.
  {
    base::test::TestFuture<bool> set_pinned_version_future;
    handler->SetPinnedVersionForIsolatedWebApp(
        app_id, "invalid_version", set_pinned_version_future.GetCallback());
    EXPECT_THAT(set_pinned_version_future.Get(), 0);

    base::test::TestFuture<std::string> update_future;
    handler->UpdateManifestInstalledIsolatedWebApp(
        app_id, update_future.GetCallback<const std::string&>());
    EXPECT_THAT(
        update_future.Get(),
        HasSubstr("Update failed: Error::kUpdateManifestNoApplicableVersion"));
  }
}

IN_PROC_BROWSER_TEST_F(WebAppInternalsIwaInstallationBrowserTest,
                       ParseUpdateManifestFromUrlFailsWithIncorrectUrl) {
  auto* handler = OpenWebAppInternals();

  base::test::TestFuture<::mojom::ParseUpdateManifestFromUrlResultPtr>
      um_future;

  // Select some dummy URL that certainly doesn't host an update manifest.
  handler->ParseUpdateManifestFromUrl(GURL("https://example.com"),
                                      um_future.GetCallback());
  ASSERT_TRUE(um_future.Take()->is_error());
}

IN_PROC_BROWSER_TEST_F(
    WebAppInternalsIwaInstallationBrowserTest,
    InstallIsolatedWebAppFromBundleUrlFailsWithIncorrectUrl) {
  auto* handler = OpenWebAppInternals();

  base::test::TestFuture<::mojom::InstallIsolatedWebAppResultPtr>
      install_future;
  auto params = ::mojom::InstallFromBundleUrlParams::New();

  // Select some dummy URL that certainly doesn't host a web bundle.
  params->web_bundle_url = GURL("https://example.com");
  handler->InstallIsolatedWebAppFromBundleUrl(std::move(params),
                                              install_future.GetCallback());
  ASSERT_TRUE(install_future.Take()->is_error());
}

}  // namespace web_app