File: kiosk_iwa_version_management_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (685 lines) | stat: -rw-r--r-- 26,350 bytes parent folder | download | duplicates (2)
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/version.h"
#include "chrome/browser/ash/app_mode/isolated_web_app/kiosk_iwa_data.h"
#include "chrome/browser/ash/app_mode/isolated_web_app/kiosk_iwa_manager.h"
#include "chrome/browser/ash/app_mode/kiosk_app_launch_error.h"
#include "chrome/browser/ash/app_mode/kiosk_controller.h"
#include "chrome/browser/ash/app_mode/test/kiosk_mixin.h"
#include "chrome/browser/ash/app_mode/test/kiosk_test_utils.h"
#include "chrome/browser/ash/login/test/test_predicate_waiter.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_update_apply_task.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_update_discovery_task.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/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/ash/components/policy/device_local_account/device_local_account_type.h"
#include "components/account_id/account_id.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "components/web_package/test_support/signed_web_bundles/key_pair.h"
#include "components/webapps/common/web_app_id.h"
#include "components/webapps/isolated_web_apps/update_channel.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_launcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace ash {

using base::test::ErrorIs;
using base::test::HasValue;
using base::test::ValueIs;
using kiosk::test::BlockKioskLaunch;
using kiosk::test::CreateDeviceLocalAccountId;
using kiosk::test::CurrentProfile;
using kiosk::test::LaunchAppManually;
using kiosk::test::TheKioskApp;
using kiosk::test::WaitKioskLaunched;

namespace {

constexpr char kTestAccountId[] = "kiosk-iwa-test@localhost";

constexpr char kTestIwaTitle1[] = "First app title";
constexpr char kTestIwaTitle2[] = "Changed title";

constexpr char kTestIwaVersion1[] = "1";
constexpr char kTestIwaVersion2[] = "2.0";
constexpr char kTestIwaVersion3[] = "3.0.0";

constexpr char kTestIwaVersionInvalid1[] = "not a version number";
constexpr char kTestIwaVersionInvalid2[] = "2,0";

constexpr char kChannelNameDefault[] = "default";
constexpr char kChannelNameBeta[] = "beta";
constexpr char kChannelNameAlpha[] = "alpha";
constexpr char kChannelNameUnknown[] = "unknown";

constexpr char kUnsetPolicyValue[] = "";

const web_app::UpdateChannel kChannelBeta =
    web_app::UpdateChannel::Create(kChannelNameBeta).value();
const web_app::UpdateChannel kChannelAlpha =
    web_app::UpdateChannel::Create(kChannelNameAlpha).value();

constexpr std::string_view GetTestAccountId() {
  return kTestAccountId;
}

AccountId GetTestDeviceLocalAccountId() {
  return CreateDeviceLocalAccountId(
      GetTestAccountId(), policy::DeviceLocalAccountType::kKioskIsolatedWebApp);
}

const KioskIwaData* GetCurrentKioskIwaData() {
  return CHECK_DEREF(KioskIwaManager::Get())
      .GetApp(GetTestDeviceLocalAccountId());
}

web_package::SignedWebBundleId GetTestWebBundleId() {
  return web_app::test::GetDefaultEd25519WebBundleId();
}

web_package::test::KeyPair GetTestKeyPair() {
  return web_app::test::GetDefaultEd25519KeyPair();
}

webapps::AppId GetTestWebAppId() {
  return web_app::IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(
             GetTestWebBundleId())
      .app_id();
}

web_app::WebAppProvider* GetWebAppProviderPtr() {
  return web_app::WebAppProvider::GetForWebApps(&CurrentProfile());
}

web_app::WebAppProvider& GetWebAppProvider() {
  return CHECK_DEREF(GetWebAppProviderPtr());
}

void WaitForWebAppProvider() {
  test::TestPredicateWaiter(base::BindRepeating([]() {
    return GetWebAppProviderPtr() != nullptr;
  })).Wait();
}

web_app::IsolatedWebAppUpdateDiscoveryTask::CompletionStatus
WaitForTestAppUpdateDiscovery() {
  using UpdateDiscoveryTaskFuture = base::test::TestFuture<
      web_app::IsolatedWebAppUpdateDiscoveryTask::CompletionStatus>;

  UpdateDiscoveryTaskFuture update_discovery_future;
  web_app::UpdateDiscoveryTaskResultWaiter update_discovery_waiter(
      GetWebAppProvider(), GetTestWebAppId(),
      update_discovery_future.GetCallback());
  return update_discovery_future.Take();
}

web_app::IsolatedWebAppUpdateApplyTask::CompletionStatus
WaitForTestAppUpdateApply() {
  using UpdateApplyTaskFuture = base::test::TestFuture<
      web_app::IsolatedWebAppUpdateApplyTask::CompletionStatus>;

  UpdateApplyTaskFuture update_apply_future;
  web_app::UpdateApplyTaskResultWaiter update_apply_waiter(
      GetWebAppProvider(), GetTestWebAppId(),
      update_apply_future.GetCallback());
  return update_apply_future.Take();
}

const web_app::WebApp& GetIsolatedWebApp(const webapps::AppId& app_id) {
  return CHECK_DEREF(GetWebAppProvider().registrar_unsafe().GetAppById(app_id));
}

void ExpectTestAppInstalledAtVersion(const base::Version& expected_version) {
  EXPECT_EQ(GetIsolatedWebApp(GetTestWebAppId()).isolation_data()->version(),
            expected_version);
}

void ExpectTestAppUpdatedToVersion(const base::Version& expected_version) {
  const auto update_apply_status = WaitForTestAppUpdateApply();
  EXPECT_THAT(update_apply_status, HasValue());
  EXPECT_EQ(update_apply_status->updated_version(), expected_version);
}

void ExpectAppUpdateSkipped() {
  ASSERT_THAT(
      WaitForTestAppUpdateDiscovery(),
      ValueIs(
          web_app::IsolatedWebAppUpdateDiscoveryTask::Success::kNoUpdateFound));
}

void ExpectAppUpdateDiscovered() {
  ASSERT_THAT(WaitForTestAppUpdateDiscovery(),
              ValueIs(web_app::IsolatedWebAppUpdateDiscoveryTask::Success::
                          kUpdateFoundAndSavedInDatabase));
}

void ExpectNoApplicableVersion() {
  EXPECT_THAT(WaitForTestAppUpdateDiscovery(),
              ErrorIs(web_app::IsolatedWebAppUpdateDiscoveryTask::Error::
                          kUpdateManifestNoApplicableVersion));
}

// Creates a manual launch IWA kiosk with a custom channel.
KioskMixin::Config CreateManualLaunchConfigWithChannel(
    const std::string& update_channel,
    const GURL& update_manifest_url) {
  KioskMixin::IsolatedWebAppOption iwa_option(
      /*account_id=*/GetTestAccountId(),
      /*web_bundle_id=*/GetTestWebBundleId(), update_manifest_url,
      update_channel);

  return {
      /*name=*/"IsolatedWebApp", /*auto_launch_account_id=*/{}, {iwa_option}};
}

// Creates a manual launch IWA kiosk with version pinning.
KioskMixin::Config CreateManualLaunchConfigWithVersionPinning(
    const std::string& pinned_version,
    bool allow_downgrades,
    const GURL& update_manifest_url) {
  KioskMixin::IsolatedWebAppOption iwa_option(
      /*account_id=*/GetTestAccountId(),
      /*web_bundle_id=*/GetTestWebBundleId(), update_manifest_url,
      /*update_channel=*/kUnsetPolicyValue, pinned_version, allow_downgrades);

  KioskMixin::Config kiosk_iwa_config = {/*name=*/"IsolatedWebApp",
                                         /*auto_launch_account_id=*/{},
                                         {iwa_option}};
  return kiosk_iwa_config;
}

}  // namespace

// Base class for Kiosk IWA version management fixtures.
class KioskIwaVersionManagementBaseTest
    : public MixinBasedInProcessBrowserTest {
 public:
  // Factory method to create kiosk mixin configurations based on the url from
  // IsolatedWebAppUpdateServerMixin.
  using ConfigCreator =
      base::OnceCallback<KioskMixin::Config(const GURL& update_manifest_url)>;

  explicit KioskIwaVersionManagementBaseTest(ConfigCreator config_creator)
      : feature_list_(ash::features::kIsolatedWebAppKiosk),
        iwa_server_mixin_(&mixin_host_),
        kiosk_mixin_(&mixin_host_,
                     std::move(config_creator).Run(GetUpdateManifestUrl())) {}

  ~KioskIwaVersionManagementBaseTest() override = default;
  KioskIwaVersionManagementBaseTest(const KioskIwaVersionManagementBaseTest&) =
      delete;
  KioskIwaVersionManagementBaseTest& operator=(
      const KioskIwaVersionManagementBaseTest&) = delete;

  void AddTestBundle(std::string_view version,
                     std::optional<std::vector<web_app::UpdateChannel>>
                         channels = std::nullopt) {
    iwa_server_mixin_.AddBundle(
        web_app::IsolatedWebAppBuilder(
            web_app::ManifestBuilder().SetVersion(version))
            .BuildBundle(GetTestKeyPair()),
        std::move(channels));
  }

  void AddTestBundle(std::string_view name,
                     std::string_view version,
                     std::optional<std::vector<web_app::UpdateChannel>>
                         channels = std::nullopt) {
    iwa_server_mixin_.AddBundle(
        web_app::IsolatedWebAppBuilder(
            web_app::ManifestBuilder().SetName(name).SetVersion(version))
            .BuildBundle(GetTestKeyPair()),
        std::move(channels));
  }

  void RunUnableToInstallChecks() {
    RunUntilBrowserProcessQuits();
    EXPECT_EQ(KioskAppLaunchError::Error::kUnableToInstall,
              KioskAppLaunchError::Get());
    EXPECT_FALSE(KioskController::Get().IsSessionStarting());
  }

 private:
  GURL GetUpdateManifestUrl() const {
    return iwa_server_mixin_.GetUpdateManifestUrl(GetTestWebBundleId());
  }

  base::test::ScopedFeatureList feature_list_;
  web_app::IsolatedWebAppUpdateServerMixin iwa_server_mixin_;
  KioskMixin kiosk_mixin_;
};

struct KioskIwaUpdateChannelTestParams {
  std::string input_channel_name;
  std::optional<base::Version> expected_version;
};

// Tests how the first Kiosk IWA installation processes update channels.
class KioskIwaUpdateChannelTest
    : public KioskIwaVersionManagementBaseTest,
      public testing::WithParamInterface<KioskIwaUpdateChannelTestParams> {
 public:
  KioskIwaUpdateChannelTest()
      : KioskIwaVersionManagementBaseTest(
            KioskIwaWithCustomChannel(GetChannelName())) {
    AddTestBundle(kTestIwaVersion1);
    AddTestBundle(kTestIwaVersion2, {{kChannelBeta, kChannelAlpha}});
    AddTestBundle(kTestIwaVersion3, {{kChannelAlpha}});
  }

 protected:
  static ConfigCreator KioskIwaWithCustomChannel(
      const std::string& channel_name) {
    return base::BindOnce(&CreateManualLaunchConfigWithChannel, channel_name);
  }

  static const std::string& GetChannelName() {
    return GetParam().input_channel_name;
  }

  static const base::Version& GetExpectedVersion() {
    CHECK(GetParam().expected_version.has_value());
    return GetParam().expected_version.value();
  }
};

using KioskIwaUpdateChannelTestInstallSuccess = KioskIwaUpdateChannelTest;
IN_PROC_BROWSER_TEST_P(KioskIwaUpdateChannelTestInstallSuccess,
                       InstallsCorrectVersion) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(GetExpectedVersion());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskIwaUpdateChannelTestInstallSuccess,
    testing::Values(
        // Uses 'default' channel with unset policy.
        KioskIwaUpdateChannelTestParams{
            .input_channel_name = kUnsetPolicyValue,
            .expected_version = base::Version(kTestIwaVersion1)},
        // Explicitly set 'default' channel.
        KioskIwaUpdateChannelTestParams{
            .input_channel_name = kChannelNameDefault,
            .expected_version = base::Version(kTestIwaVersion1)},
        // Installs a different version for 'beta'
        KioskIwaUpdateChannelTestParams{
            .input_channel_name = kChannelNameBeta,
            .expected_version = base::Version(kTestIwaVersion2)},
        // Selects the latest version from multiple in 'alpha'.
        KioskIwaUpdateChannelTestParams{
            .input_channel_name = kChannelNameAlpha,
            .expected_version = base::Version(kTestIwaVersion3)}));

using KioskIwaUpdateChannelTestInstallFail = KioskIwaUpdateChannelTest;
IN_PROC_BROWSER_TEST_P(KioskIwaUpdateChannelTestInstallFail, CannotInstall) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  RunUnableToInstallChecks();
}

INSTANTIATE_TEST_SUITE_P(All,
                         KioskIwaUpdateChannelTestInstallFail,
                         testing::Values(
                             // Cannot install when channel not found.
                             KioskIwaUpdateChannelTestParams{
                                 .input_channel_name = kChannelNameUnknown,
                                 .expected_version = std::nullopt}));

struct KioskIwaVersionPinningTestParams {
  std::string input_pinned_version;
  bool input_allow_downgrades;
  std::optional<base::Version> expected_version;
};

// Tests how the first Kiosk IWA installation processes version pinning.
class KioskIwaVersionPinningTest
    : public KioskIwaVersionManagementBaseTest,
      public testing::WithParamInterface<KioskIwaVersionPinningTestParams> {
 public:
  KioskIwaVersionPinningTest()
      : KioskIwaVersionManagementBaseTest(
            KioskIwaWithPinning(GetPinnedVersion(), GetAllowedDowngrades())) {
    AddTestBundle(kTestIwaVersion1);
    AddTestBundle(kTestIwaVersion2);
  }

 protected:
  static ConfigCreator KioskIwaWithPinning(const std::string& pinned_version,
                                           bool allow_downgrades) {
    return base::BindOnce(&CreateManualLaunchConfigWithVersionPinning,
                          pinned_version, allow_downgrades);
  }

  static const std::string& GetPinnedVersion() {
    return GetParam().input_pinned_version;
  }

  static bool GetAllowedDowngrades() {
    return GetParam().input_allow_downgrades;
  }

  static const base::Version& GetExpectedVersion() {
    CHECK(GetParam().expected_version.has_value());
    return GetParam().expected_version.value();
  }
};

using KioskIwaVersionPinningTestInstallSuccess = KioskIwaVersionPinningTest;
IN_PROC_BROWSER_TEST_P(KioskIwaVersionPinningTestInstallSuccess,
                       InstallsCorrectVersion) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(GetExpectedVersion());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskIwaVersionPinningTestInstallSuccess,
    testing::Values(
        // Installs the latest version when pinning is not set.
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kUnsetPolicyValue,
            .input_allow_downgrades = false,
            .expected_version = base::Version(kTestIwaVersion2)},
        // Installs the exact pinned version, downgrading has no effect.
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion2,
            .input_allow_downgrades = false,
            .expected_version = base::Version(kTestIwaVersion2)},
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion2,
            .input_allow_downgrades = true,
            .expected_version = base::Version(kTestIwaVersion2)},
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion1,
            .input_allow_downgrades = false,
            .expected_version = base::Version(kTestIwaVersion1)},
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion1,
            .input_allow_downgrades = true,
            .expected_version = base::Version(kTestIwaVersion1)}));

using KioskIwaVersionPinningTestInstallFail = KioskIwaVersionPinningTest;
IN_PROC_BROWSER_TEST_P(KioskIwaVersionPinningTestInstallFail,
                       CannotInstallUnknownVersion) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  RunUnableToInstallChecks();
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskIwaVersionPinningTestInstallFail,
    testing::Values(
        // Cannot install when pinned version is not found in the manifest.
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion3,
            .input_allow_downgrades = false,
            .expected_version = std::nullopt},
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersion3,
            .input_allow_downgrades = true,
            .expected_version = std::nullopt}));

using KioskIwaVersionPinningTestInvalidPolicy = KioskIwaVersionPinningTest;
IN_PROC_BROWSER_TEST_P(KioskIwaVersionPinningTestInvalidPolicy,
                       CannotCreateKioskAccount) {
  EXPECT_TRUE(KioskController::Get().GetApps().empty());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskIwaVersionPinningTestInvalidPolicy,
    testing::Values(
        // allow_downgrades is set without a pinned version.
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kUnsetPolicyValue,
            .input_allow_downgrades = true,
            .expected_version = std::nullopt},
        // Version names that cannot be parsed.
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersionInvalid1,
            .input_allow_downgrades = false,
            .expected_version = std::nullopt},
        KioskIwaVersionPinningTestParams{
            .input_pinned_version = kTestIwaVersionInvalid2,
            .input_allow_downgrades = false,
            .expected_version = std::nullopt}));

// Tests an app update to the new latest version in the same channel.
class KioskIwaSimpleUpdateTest : public KioskIwaVersionManagementBaseTest {
 public:
  KioskIwaSimpleUpdateTest()
      : KioskIwaVersionManagementBaseTest(KioskIwaWithDefaultChannel()) {
    AddTestBundle(kTestIwaTitle1, kTestIwaVersion1);
  }

 protected:
  static ConfigCreator KioskIwaWithDefaultChannel() {
    return base::BindOnce(&CreateManualLaunchConfigWithChannel,
                          kChannelNameDefault);
  }
};

IN_PROC_BROWSER_TEST_F(KioskIwaSimpleUpdateTest,
                       PRE_UpdatesToLatestBeforeLaunch) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(base::Version(kTestIwaVersion1));
}

IN_PROC_BROWSER_TEST_F(KioskIwaSimpleUpdateTest, UpdatesToLatestBeforeLaunch) {
  EXPECT_EQ(TheKioskApp().name(), kTestIwaTitle1);

  AddTestBundle(kTestIwaTitle2, kTestIwaVersion2);
  const base::Version expected_version(kTestIwaVersion2);

  // Prevents the app launch to let the update apply.
  auto scoped_launch_blocker = BlockKioskLaunch();
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));

  WaitForWebAppProvider();
  ExpectTestAppUpdatedToVersion(expected_version);

  scoped_launch_blocker.reset();
  ASSERT_TRUE(WaitKioskLaunched());

  ExpectTestAppInstalledAtVersion(expected_version);
  EXPECT_EQ(TheKioskApp().name(), kTestIwaTitle2);
}

IN_PROC_BROWSER_TEST_F(KioskIwaSimpleUpdateTest, PRE_UpdatesToLatestAtExit) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(base::Version(kTestIwaVersion1));
}

IN_PROC_BROWSER_TEST_F(KioskIwaSimpleUpdateTest, UpdatesToLatestAtExit) {
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  // Wait for the first update discovery to finish.
  ExpectAppUpdateSkipped();

  AddTestBundle(kTestIwaVersion2);
  EXPECT_EQ(GetWebAppProvider().iwa_update_manager().DiscoverUpdatesNow(), 1UL);

  ExpectAppUpdateDiscovered();
  kiosk::test::CloseAppWindow(TheKioskApp());
  ExpectTestAppUpdatedToVersion(base::Version(kTestIwaVersion2));
}

struct KioskIwaUpdateChannelChangeTestParams {
  enum class TestCase { kUpdateApplied, kUpdateSkipped, kUpdateError };

  TestCase test_case;
  std::string initial_channel_name;
  base::Version expected_initial_version;
  std::string new_channel_name;
  base::Version expected_new_version;
};

// Tests how the Kiosk IWA update processes update channels.
class KioskIwaUpdateChannelChangeTest
    : public KioskIwaVersionManagementBaseTest,
      public testing::WithParamInterface<
          KioskIwaUpdateChannelChangeTestParams> {
 public:
  KioskIwaUpdateChannelChangeTest()
      : KioskIwaVersionManagementBaseTest(KioskIwaWithChannelSwitch()) {
    AddTestBundle(kTestIwaVersion1);
    AddTestBundle(kTestIwaVersion2, {{kChannelBeta}});
    AddTestBundle(kTestIwaVersion3, {{kChannelAlpha}});
  }

 protected:
  static ConfigCreator KioskIwaWithChannelSwitch() {
    const std::string& channel_name =
        content::IsPreTest() ? GetInitialChannelName() : GetNewChannelName();
    return base::BindOnce(&CreateManualLaunchConfigWithChannel, channel_name);
  }

  static KioskIwaUpdateChannelChangeTestParams::TestCase GetTestCase() {
    return GetParam().test_case;
  }

  static const std::string& GetInitialChannelName() {
    return GetParam().initial_channel_name;
  }

  static const base::Version& GetExpectedInitialVersion() {
    return GetParam().expected_initial_version;
  }

  static const std::string& GetNewChannelName() {
    return GetParam().new_channel_name;
  }

  static const base::Version& GetExpectedNewVersion() {
    return GetParam().expected_new_version;
  }

  static void CheckUpdateStatusForTestCase() {
    switch (GetTestCase()) {
      case KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateApplied:
        ExpectTestAppUpdatedToVersion(GetExpectedNewVersion());
        break;
      case KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateSkipped:
        ExpectAppUpdateSkipped();
        break;
      case KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateError:
        ExpectNoApplicableVersion();
        break;
    }
  }
};

IN_PROC_BROWSER_TEST_P(KioskIwaUpdateChannelChangeTest,
                       PRE_ProcessChannelChange) {
  EXPECT_EQ(GetCurrentKioskIwaData()->update_channel().ToString(),
            GetInitialChannelName());
  ASSERT_TRUE(LaunchAppManually(TheKioskApp()));
  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(GetExpectedInitialVersion());
}

IN_PROC_BROWSER_TEST_P(KioskIwaUpdateChannelChangeTest, ProcessChannelChange) {
  EXPECT_EQ(GetCurrentKioskIwaData()->update_channel().ToString(),
            GetNewChannelName());

  {
    // Prevents the app launch to let the app update apply.
    auto scoped_launch_blocker = BlockKioskLaunch();
    ASSERT_TRUE(LaunchAppManually(TheKioskApp()));

    WaitForWebAppProvider();
    CheckUpdateStatusForTestCase();
  }

  ASSERT_TRUE(WaitKioskLaunched());
  ExpectTestAppInstalledAtVersion(GetExpectedNewVersion());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    KioskIwaUpdateChannelChangeTest,
    testing::Values(
        // Switching to a channel with a newer version updates the app.
        // Switch from "default" to "beta".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateApplied,
            .initial_channel_name = kChannelNameDefault,
            .expected_initial_version = base::Version(kTestIwaVersion1),
            .new_channel_name = kChannelNameBeta,
            .expected_new_version = base::Version(kTestIwaVersion2)},
        // Switch from "default" to "alpha".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateApplied,
            .initial_channel_name = kChannelNameDefault,
            .expected_initial_version = base::Version(kTestIwaVersion1),
            .new_channel_name = kChannelNameAlpha,
            .expected_new_version = base::Version(kTestIwaVersion3)},
        // Switch from "beta" to "alpha".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateApplied,
            .initial_channel_name = kChannelNameBeta,
            .expected_initial_version = base::Version(kTestIwaVersion2),
            .new_channel_name = kChannelNameAlpha,
            .expected_new_version = base::Version(kTestIwaVersion3)},

        // Switching to a channel with an older version skips the update.
        // Switch from "beta" to "default".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateSkipped,
            .initial_channel_name = kChannelNameBeta,
            .expected_initial_version = base::Version(kTestIwaVersion2),
            .new_channel_name = kChannelNameDefault,
            .expected_new_version = base::Version(kTestIwaVersion2)},
        // Switch from "alpha" to "default".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateSkipped,
            .initial_channel_name = kChannelNameAlpha,
            .expected_initial_version = base::Version(kTestIwaVersion3),
            .new_channel_name = kChannelNameDefault,
            .expected_new_version = base::Version(kTestIwaVersion3)},

        // Switching to an unknown channel skips the update with an error.
        // Switch from "beta" to "unknown".
        KioskIwaUpdateChannelChangeTestParams{
            .test_case =
                KioskIwaUpdateChannelChangeTestParams::TestCase::kUpdateError,
            .initial_channel_name = kChannelNameBeta,
            .expected_initial_version = base::Version(kTestIwaVersion2),
            .new_channel_name = kChannelNameUnknown,
            .expected_new_version = base::Version(kTestIwaVersion2)}));

}  // namespace ash