File: install_from_info_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 (142 lines) | stat: -rw-r--r-- 5,586 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
// 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/install_from_info_command.h"

#include <map>
#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/fake_os_integration_manager.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_install_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_constants.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_info.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 "components/webapps/browser/install_result_code.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"

using base::BucketsAre;

namespace web_app {

class InstallFromInfoCommandTest : public WebAppBrowserTestBase {
 public:
  InstallFromInfoCommandTest() = default;
  std::map<SquareSizePx, SkBitmap> ReadIcons(const webapps::AppId& app_id,
                                             IconPurpose purpose,
                                             const SortedSizesPx& sizes_px) {
    std::map<SquareSizePx, SkBitmap> result;
    base::RunLoop run_loop;
    provider().icon_manager().ReadIcons(
        app_id, purpose, sizes_px,
        base::BindLambdaForTesting(
            [&](std::map<SquareSizePx, SkBitmap> icon_bitmaps) {
              result = std::move(icon_bitmaps);
              run_loop.Quit();
            }));
    run_loop.Run();
    return result;
  }
};

IN_PROC_BROWSER_TEST_F(InstallFromInfoCommandTest, SuccessInstall) {
  base::HistogramTester tester;
  auto info = WebAppInstallInfo::CreateWithStartUrlForTesting(
      GURL("http://test.com/path"));
  info->title = u"Test name";

  const webapps::WebappInstallSource install_source =
#if BUILDFLAG(IS_CHROMEOS)
      webapps::WebappInstallSource::SYSTEM_DEFAULT;
#else
      webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON;
#endif

  base::RunLoop loop;
  webapps::AppId result_app_id;
  provider().scheduler().InstallFromInfoWithParams(
      std::move(info),
      /*overwrite_existing_manifest_fields=*/false, install_source,
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            result_app_id = app_id;
            loop.Quit();
          }),
      WebAppInstallParams());
  loop.Run();

  EXPECT_EQ(proto::InstallState::INSTALLED_WITH_OS_INTEGRATION,
            provider().registrar_unsafe().GetInstallState(result_app_id));

  // Ensure histogram is only measured once.

  EXPECT_THAT(tester.GetAllSamples("WebApp.Install.Result"),
              BucketsAre(base::Bucket(true, 1)));
  EXPECT_THAT(tester.GetAllSamples("WebApp.Install.Source.Success"),
              BucketsAre(base::Bucket(install_source, 1)));

  const WebApp* web_app =
      provider().registrar_unsafe().GetAppById(result_app_id);
  ASSERT_TRUE(web_app);

  std::map<SquareSizePx, SkBitmap> icon_bitmaps =
      ReadIcons(result_app_id, IconPurpose::ANY,
                web_app->downloaded_icon_sizes(IconPurpose::ANY));

  // Make sure that icons have been generated for all sub sizes.
  EXPECT_TRUE(ContainsOneIconOfEachSize(icon_bitmaps));
}

IN_PROC_BROWSER_TEST_F(InstallFromInfoCommandTest, InstallWithParams) {
  auto info = WebAppInstallInfo::CreateWithStartUrlForTesting(
      GURL("http://test.com/path"));
  info->title = u"Test name";

  WebAppInstallParams install_params;
  install_params.add_to_applications_menu = true;
  install_params.add_to_desktop = true;

  base::RunLoop loop;
  webapps::AppId result_app_id;
  provider().scheduler().InstallFromInfoWithParams(
      std::move(info),
      /*overwrite_existing_manifest_fields=*/false,
      webapps::WebappInstallSource::MENU_BROWSER_TAB,
      base::BindLambdaForTesting(
          [&](const webapps::AppId& app_id, webapps::InstallResultCode code) {
            EXPECT_EQ(code, webapps::InstallResultCode::kSuccessNewInstall);
            EXPECT_EQ(proto::InstallState::INSTALLED_WITH_OS_INTEGRATION,
                      provider().registrar_unsafe().GetInstallState(app_id));
            result_app_id = app_id;
            loop.Quit();
          }),
      install_params);
  loop.Run();
  std::optional<proto::os_state::WebAppOsIntegration> os_state =
      provider().registrar_unsafe().GetAppCurrentOsIntegrationState(
          result_app_id);
  ASSERT_TRUE(os_state.has_value());
  EXPECT_TRUE(os_state->has_shortcut());
  EXPECT_EQ(os_state->run_on_os_login().run_on_os_login_mode(),
            proto::os_state::RunOnOsLogin::MODE_NOT_RUN);
}

}  // namespace web_app