File: web_app_icon_manager_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 (129 lines) | stat: -rw-r--r-- 5,056 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/app_service_test.h"
#include "chrome/browser/apps/app_service/browser_app_launcher.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_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/test/web_app_install_test_utils.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_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "content/public/test/browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/image/image_skia.h"
#include "url/gurl.h"

namespace web_app {

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

  ~WebAppIconManagerBrowserTest() override = default;

 protected:
  net::EmbeddedTestServer* https_server() { return &https_server_; }

  void SetUpOnMainThread() override {
    Profile* profile = browser()->profile();
    app_service_test_.SetUp(profile);
    web_app::test::WaitUntilReady(WebAppProvider::GetForTest(profile));
  }

  // WebAppBrowserTestBase:
  void SetUp() override {
    https_server_.AddDefaultHandlers(GetChromeTestDataDir());
    WebAppBrowserTestBase::SetUp();
  }

  apps::AppServiceTest& app_service_test() { return app_service_test_; }

 private:
  net::EmbeddedTestServer https_server_;
  apps::AppServiceTest app_service_test_;
};

IN_PROC_BROWSER_TEST_F(WebAppIconManagerBrowserTest, SingleIcon) {
  ASSERT_TRUE(https_server()->Start());
  const GURL start_url =
      https_server()->GetURL("/banners/manifest_test_page.html");

  webapps::AppId app_id;
  {
    std::unique_ptr<WebAppInstallInfo> install_info =
        WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
    install_info->scope = start_url.GetWithoutFilename();
    install_info->title = u"App Name";
    install_info->user_display_mode = mojom::UserDisplayMode::kStandalone;

    {
      SkBitmap bitmap;
      bitmap.allocN32Pixels(icon_size::k32, icon_size::k32, true);
      bitmap.eraseColor(SK_ColorBLUE);
      install_info->icon_bitmaps.any[icon_size::k32] = std::move(bitmap);
    }

    base::RunLoop run_loop;

    auto* provider = WebAppProvider::GetForTest(browser()->profile());
    provider->scheduler().InstallFromInfoNoIntegrationForTesting(
        std::move(install_info),
        /*overwrite_existing_manifest_fields=*/false,
        webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
        base::BindLambdaForTesting(
            [&app_id, &run_loop](const webapps::AppId& installed_app_id,
                                 webapps::InstallResultCode code) {
              EXPECT_EQ(webapps::InstallResultCode::kSuccessNewInstall, code);
              app_id = installed_app_id;
              run_loop.Quit();
            }));

    run_loop.Run();
  }

  base::RunLoop run_loop;
  WebAppBrowserController::SetIconLoadCallbackForTesting(
      run_loop.QuitClosure());
  Browser* app_browser = LaunchWebAppBrowser(app_id);
  run_loop.Run();

  gfx::ImageSkia app_icon =
      app_browser->app_controller()->GetWindowAppIcon().Rasterize(nullptr);

#if BUILDFLAG(IS_CHROMEOS)
  gfx::ImageSkia image_skia =
      app_service_test().LoadAppIconBlocking(app_id, kWebAppIconSmall);
  EXPECT_TRUE(app_service_test().AreIconImageEqual(image_skia, app_icon));
#else
  const SkBitmap* bitmap = app_icon.bitmap();
  EXPECT_EQ(SK_ColorBLUE, bitmap->getColor(0, 0));
  EXPECT_EQ(32, bitmap->width());
  EXPECT_EQ(32, bitmap->height());
#endif
}

}  // namespace web_app