1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/path_service.h"
7 #include "base/run_loop.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/icon_loader.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "content/public/test/browser_test.h"
12 #include "mojo/public/cpp/bindings/remote.h"
13 #include "ui/gfx/image/image.h"
16 #include "ui/display/win/dpi.h"
19 using IconLoaderBrowserTest = InProcessBrowserTest;
21 class TestIconLoader {
23 explicit TestIconLoader(base::OnceClosure quit_closure)
24 : quit_closure_(std::move(quit_closure)) {}
26 TestIconLoader(const TestIconLoader&) = delete;
27 TestIconLoader& operator=(const TestIconLoader&) = delete;
30 if (!quit_closure_.is_null()) {
31 std::move(quit_closure_).Run();
35 bool load_succeeded() const { return load_succeeded_; }
37 bool TryLoadIcon(const base::FilePath& file_path,
38 IconLoader::IconSize size,
41 file_path, size, scale,
42 base::BindOnce(&TestIconLoader::OnIconLoaded, base::Unretained(this)));
47 void OnIconLoaded(gfx::Image img, const IconLoader::IconGroup& group) {
49 load_succeeded_ = true;
54 EXPECT_FALSE(quit_closure_.is_null());
55 std::move(quit_closure_).Run();
58 bool load_succeeded_ = false;
59 base::OnceClosure quit_closure_;
62 // Under GTK, the icon providing functions do not return icons.
63 #if !((BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && \
64 defined(MEMORY_SANITIZER))
66 IN_PROC_BROWSER_TEST_F(IconLoaderBrowserTest, LoadGroup) {
68 constexpr base::FilePath::CharType kGroupOnlyFilename[] =
69 FILE_PATH_LITERAL("unlikely-to-exist-file.txt");
72 scale = display::win::GetDPIScale();
75 // Test that an icon for a file type (group) can be loaded even
76 // where a file does not exist. Should work cross platform.
78 TestIconLoader test_loader(runner.QuitClosure());
79 test_loader.TryLoadIcon(base::FilePath(kGroupOnlyFilename),
80 IconLoader::NORMAL, scale);
83 EXPECT_TRUE(test_loader.load_succeeded());
86 #endif // !((BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) &&
87 // defined(MEMORY_SANITIZER))
90 IN_PROC_BROWSER_TEST_F(IconLoaderBrowserTest, LoadExeIcon) {
91 float scale = display::win::GetDPIScale();
94 TestIconLoader test_loader(runner.QuitClosure());
96 base::FilePath exe_path;
97 base::PathService::Get(base::FILE_EXE, &exe_path);
98 test_loader.TryLoadIcon(exe_path, IconLoader::NORMAL, scale);
101 EXPECT_TRUE(test_loader.load_succeeded());
104 const base::FilePath::CharType kNotExistingExeFile[] =
105 FILE_PATH_LITERAL("unlikely-to-exist-file.exe");
107 IN_PROC_BROWSER_TEST_F(IconLoaderBrowserTest, LoadDefaultExeIcon) {
108 float scale = display::win::GetDPIScale();
110 base::RunLoop runner;
112 TestIconLoader test_loader(runner.QuitClosure());
114 test_loader.TryLoadIcon(base::FilePath(kNotExistingExeFile),
115 IconLoader::NORMAL, scale);
118 EXPECT_TRUE(test_loader.load_succeeded());
120 #endif // BUILDFLAG(IS_WIN)