Skip to content

Commit

Permalink
Adds a browser tests for testing icons on Windows and Mac.
Browse files Browse the repository at this point in the history
The test installs a web app, then reads the icon and verify the top left hand order is grey.

Bug: 1232635
Change-Id: I6c7a0d908dd67103e3f26693979c52ce1d5f4a84
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3043686
Commit-Queue: Clifford Cheng <cliffordcheng@chromium.org>
Reviewed-by: Daniel Murphy <dmurph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#907372}
  • Loading branch information
Clifford Cheng authored and Chromium LUCI CQ committed Jul 31, 2021
1 parent 869f3ab commit 9b3e647
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Expand Up @@ -50,6 +50,16 @@
#include "ui/base/models/menu_model.h"
#include "ui/base/page_transition_types.h"

#if defined(OS_MAC)
#include <ImageIO/ImageIO.h>
#import "skia/ext/skia_utils_mac.h"
#endif

#if defined(OS_WIN)
#include <shellapi.h>
#include "ui/gfx/icon_util.h"
#endif

using ui_test_utils::BrowserChangeObserver;

namespace web_app {
Expand Down Expand Up @@ -107,6 +117,33 @@ void AutoAcceptDialogCallback(

} // namespace

SkColor GetIconTopLeftColor(const base::FilePath& shortcut_path) {
#if defined(OS_MAC)
base::FilePath icon_path =
shortcut_path.AppendASCII("Contents/Resources/app.icns");
base::ScopedCFTypeRef<CFDictionaryRef> empty_dict(
CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL));
base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
NULL, (const UInt8*)icon_path.value().c_str(), icon_path.value().length(),
false));
CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
// Get the first icon in the .icns file (index 0)
base::ScopedCFTypeRef<CGImageRef> cg_image(
CGImageSourceCreateImageAtIndex(source, 0, empty_dict));
SkBitmap bitmap = skia::CGImageToSkBitmap(cg_image);
return bitmap.getColor(0, 0);
#elif defined(OS_WIN)
SHFILEINFO file_info = {0};
if (SHGetFileInfo(shortcut_path.value().c_str(), FILE_ATTRIBUTE_NORMAL,
&file_info, sizeof(file_info),
SHGFI_ICON | 0 | SHGFI_USEFILEATTRIBUTES)) {
const SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(file_info.hIcon);
return bitmap.getColor(0, 0);
}
#endif
return 0;
}

AppId InstallWebAppFromPage(Browser* browser, const GURL& app_url) {
NavigateToURLAndWait(browser, app_url);

Expand Down
Expand Up @@ -19,6 +19,10 @@ enum class InstallResultCode;

// For InstallWebAppFromInfo see web_app_install_test_utils.h

// Reads an icon file (.ico/.png/.icns) and returns the color at the
// top left color.
SkColor GetIconTopLeftColor(const base::FilePath& shortcut_path);

// Navigates to |app_url| and installs app without any installability checks.
// Always selects to open app in its own window.
AppId InstallWebAppFromPage(Browser* browser, const GURL& app_url);
Expand Down
50 changes: 50 additions & 0 deletions chrome/browser/ui/web_applications/web_app_browsertest.cc
Expand Up @@ -951,6 +951,56 @@ IN_PROC_BROWSER_TEST_F(WebAppBrowserTest, ReparentWebAppForSecureActiveTab) {
ASSERT_EQ(app_browser->app_controller()->GetAppId(), app_id);
}

#if defined(OS_MAC) || defined(OS_WIN)
IN_PROC_BROWSER_TEST_F(WebAppBrowserTest, ShortcutIconCorrectColor) {
os_hooks_suppress_.reset();
base::ScopedAllowBlockingForTesting allow_blocking;

base::ScopedTempDir temp_dir;
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath application_dir =
temp_dir.GetPath().AppendASCII("application_menu");
base::FilePath desktop_dir = temp_dir.GetPath().AppendASCII("desktop");

ShortcutOverrideForTesting shortcut_override;
#if defined(OS_MAC)
shortcut_override.chrome_apps_folder = application_dir;
#elif defined(OS_WIN)
shortcut_override.desktop = desktop_dir;
shortcut_override.application_menu = application_dir;
#endif
SetShortcutOverrideForTesting(shortcut_override);
NavigateToURLAndWait(
browser(),
https_server()->GetURL(
"/banners/manifest_test_page.html?manifest=manifest_one_icon.json"));

// Wait for OS hooks and installation to complete and the app to launch.
base::RunLoop run_loop_install;
WebAppInstallObserver observer(profile());
observer.SetWebAppInstalledWithOsHooksDelegate(base::BindLambdaForTesting(
[&](const AppId& installed_app_id) { run_loop_install.Quit(); }));
content::WindowedNotificationObserver app_loaded_observer(
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::NotificationService::AllSources());
const AppId app_id = InstallPwaForCurrentUrl();
run_loop_install.Run();
app_loaded_observer.Wait();

base::FilePath shortcut_path;
auto* provider = WebAppProvider::Get(profile());
#if defined(OS_MAC)
shortcut_path = application_dir.Append(
provider->registrar().GetAppShortName(app_id) + ".app");
#elif defined(OS_WIN)
shortcut_path = application_dir.AppendASCII(
provider->registrar().GetAppShortName(app_id) + ".lnk");
#endif
SkColor icon_pixel_color = GetIconTopLeftColor(shortcut_path);
EXPECT_EQ(SkColorSetRGB(92, 92, 92), icon_pixel_color);
}
#endif

#if defined(OS_MAC) || defined(OS_WIN) || defined(OS_LINUX)
IN_PROC_BROWSER_TEST_F(WebAppBrowserTest, WebAppCreateAndDeleteShortcut) {
os_hooks_suppress_.reset();
Expand Down

0 comments on commit 9b3e647

Please sign in to comment.