File: manifest_update_finalize_command_unittest.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 (114 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download | duplicates (6)
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
// 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/manifest_update_finalize_command.h"

#include <memory>
#include <string>
#include <utility>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/web_applications/manifest_update_utils.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace web_app {

class ManifestUpdateFinalizeCommandTest : public WebAppTest {
 public:
  ManifestUpdateFinalizeCommandTest() = default;
  ManifestUpdateFinalizeCommandTest(const ManifestUpdateFinalizeCommandTest&) =
      delete;
  ManifestUpdateFinalizeCommandTest& operator=(
      const ManifestUpdateFinalizeCommandTest&) = delete;
  ~ManifestUpdateFinalizeCommandTest() override = default;

  void SetUp() override {
    WebAppTest::SetUp();
    test::AwaitStartWebAppProviderAndSubsystems(profile());
  }

 protected:
  std::unique_ptr<ManifestUpdateFinalizeCommand> CreateCommand(
      const GURL& url,
      const webapps::AppId& app_id,
      std::unique_ptr<WebAppInstallInfo> install_info,
      ManifestUpdateFinalizeCommand::ManifestWriteCallback callback) {
    auto keep_alive = std::make_unique<ScopedKeepAlive>(
        KeepAliveOrigin::APP_MANIFEST_UPDATE, KeepAliveRestartOption::DISABLED);
    auto profile_keep_alive = std::make_unique<ScopedProfileKeepAlive>(
        profile(), ProfileKeepAliveOrigin::kWebAppUpdate);
    return std::make_unique<ManifestUpdateFinalizeCommand>(
        url, app_id, std::move(install_info), std::move(callback),
        std::move(keep_alive), std::move(profile_keep_alive));
  }

  ManifestUpdateResult RunCommandAndGetResult(
      const GURL& url,
      const webapps::AppId& app_id,
      std::unique_ptr<WebAppInstallInfo> install_info) {
    ManifestUpdateResult output_result;
    base::RunLoop loop;
    provider().command_manager().ScheduleCommand(CreateCommand(
        url, app_id, std::move(install_info),
        base::BindLambdaForTesting([&](const GURL& url,
                                       const webapps::AppId& output_app_id,
                                       ManifestUpdateResult result) {
          EXPECT_EQ(output_app_id, app_id);
          output_result = result;
          loop.Quit();
        })));
    loop.Run();
    return output_result;
  }


  std::unique_ptr<WebAppInstallInfo> GetNewInstallInfoWithTitle(
      std::u16string new_title) {
    auto info = WebAppInstallInfo::CreateWithStartUrlForTesting(app_url());
    info->scope = app_url().GetWithoutFilename();
    info->user_display_mode = mojom::UserDisplayMode::kStandalone;
    info->title = new_title;
    info->validated_scope_extensions.emplace();
    return info;
  }

  WebAppProvider& provider() { return *WebAppProvider::GetForTest(profile()); }
  WebAppRegistrar& registrar() { return provider().registrar_unsafe(); }
  GURL app_url() { return app_url_; }

 private:
  const GURL app_url_{"http://www.foo.bar/web_apps/basic.html"};
};

TEST_F(ManifestUpdateFinalizeCommandTest, NameUpdate) {
  webapps::AppId app_id =
      test::InstallDummyWebApp(profile(), "Old Name", app_url());
  ManifestUpdateResult expected_result = RunCommandAndGetResult(
      app_url(), app_id, GetNewInstallInfoWithTitle(u"New Name"));
  EXPECT_EQ(expected_result, ManifestUpdateResult::kAppUpdated);
  EXPECT_EQ(registrar().GetAppById(app_id)->untranslated_name(), "New Name");
}

TEST_F(ManifestUpdateFinalizeCommandTest, UpdateFailsOnUnsuccessfulCode) {
  // This should fail because RandomAppId does not exist.
  ManifestUpdateResult expected_result = RunCommandAndGetResult(
      app_url(), "RandomAppId", GetNewInstallInfoWithTitle(u"Name"));
  EXPECT_EQ(expected_result, ManifestUpdateResult::kAppUpdateFailed);
}

}  // namespace web_app