File: favicon_utils_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 (162 lines) | stat: -rw-r--r-- 6,314 bytes parent folder | download | duplicates (7)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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/favicon/favicon_utils.h"

#include "content/public/browser/navigation_entry.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_key.h"
#include "ui/color/color_provider_manager.h"
#include "ui/color/color_recipe.h"
#include "ui/resources/grit/ui_resources.h"

namespace favicon {

namespace {

constexpr SkColor kLightColor = SK_ColorWHITE;
constexpr SkColor kDarkColor = SK_ColorBLACK;

}  // namespace

TEST(FaviconUtilsTest, ShouldThemifyFavicon) {
  std::unique_ptr<content::NavigationEntry> entry =
      content::NavigationEntry::Create();
  const GURL unthemeable_url("http://mail.google.com");
  const GURL themeable_virtual_url("chrome://feedback/");
  const GURL themeable_url("chrome://new-tab-page/");

  entry->SetVirtualURL(themeable_virtual_url);
  entry->SetURL(themeable_url);
  // Entry should be themefied if both its virtual and actual URLs are
  // themeable.
  EXPECT_TRUE(ShouldThemifyFaviconForEntry(entry.get()));

  entry->SetVirtualURL(unthemeable_url);
  // Entry should be themefied if only its actual URL is themeable.
  EXPECT_TRUE(ShouldThemifyFaviconForEntry(entry.get()));

  entry->SetURL(unthemeable_url);
  // Entry should not be themefied if both its virtual and actual URLs are
  // not themeable.
  EXPECT_FALSE(ShouldThemifyFaviconForEntry(entry.get()));

  entry->SetVirtualURL(themeable_virtual_url);
  // Entry should be themefied if only its virtual URL is themeable.
  EXPECT_TRUE(ShouldThemifyFaviconForEntry(entry.get()));
}

class DefaultFaviconModelTest : public ::testing::Test {
 protected:
  using InitializerType =
      ui::ColorProviderManager::ColorProviderInitializerList::CallbackType;

  // testing::Test:
  void SetUp() override {
    Test::SetUp();
    AddDefaultInitializer();
  }
  void TearDown() override {
    ui::ColorProviderManager::ResetForTesting();
    Test::TearDown();
  }

  void AddDefaultInitializer() {
    const auto initializer = [&](ui::ColorProvider* provider,
                                 const ui::ColorProviderKey& key) {
      ui::ColorMixer& mixer = provider->AddMixer();
      mixer[ui::kColorWindowBackground] = {
          key.color_mode == ui::ColorProviderKey::ColorMode::kDark
              ? kDarkColor
              : kLightColor};
    };
    AddInitializer(base::BindRepeating(initializer));
  }

  void AddInitializer(InitializerType initializer) {
    ui::ColorProviderManager& manager =
        ui::ColorProviderManager::GetForTesting();
    manager.AppendColorProviderInitializer(base::BindRepeating(initializer));
  }

  ui::ColorProvider* GetColorProvider(
      ui::ColorProviderKey::ColorMode color_mode) {
    ui::ColorProviderKey key;
    key.color_mode = color_mode;
    return ui::ColorProviderManager::GetForTesting().GetColorProviderFor(key);
  }

  gfx::ImageSkia GetDefaultFaviconForColorScheme(bool is_dark) {
    const int resource_id =
        is_dark ? IDR_DEFAULT_FAVICON_DARK : IDR_DEFAULT_FAVICON;
    return ui::ResourceBundle::GetSharedInstance()
        .GetNativeImageNamed(resource_id)
        .AsImageSkia();
  }
};

TEST_F(DefaultFaviconModelTest, UsesCorrectIcon_LightBackground) {
  auto* color_provider =
      GetColorProvider(ui::ColorProviderKey::ColorMode::kLight);
  const auto favicon_image = GetDefaultFaviconModel().Rasterize(color_provider);
  EXPECT_TRUE(GetDefaultFaviconForColorScheme(/*is_dark=*/false)
                  .BackedBySameObjectAs(favicon_image));
}

TEST_F(DefaultFaviconModelTest, UsesCorrectIcon_DarkBackground) {
  auto* color_provider =
      GetColorProvider(ui::ColorProviderKey::ColorMode::kDark);
  const auto favicon_image = GetDefaultFaviconModel().Rasterize(color_provider);
  EXPECT_TRUE(GetDefaultFaviconForColorScheme(/*is_dark=*/true)
                  .BackedBySameObjectAs(favicon_image));
}

TEST_F(DefaultFaviconModelTest, UsesCorrectIcon_LightBackground_Custom) {
  // Flip the background for a new color id such that it is light in dark mode
  // and vice versa.
  const auto initializer = [&](ui::ColorProvider* provider,
                               const ui::ColorProviderKey& key) {
    ui::ColorMixer& mixer = provider->AddMixer();
    mixer[ui::kColorBubbleBackground] = {
        key.color_mode == ui::ColorProviderKey::ColorMode::kDark ? kLightColor
                                                                 : kDarkColor};
  };
  AddInitializer(base::BindRepeating(initializer));

  // Even if the color provider is configured for light mode the default favicon
  // model should match the dark variant when rasterized.
  auto* color_provider =
      GetColorProvider(ui::ColorProviderKey::ColorMode::kLight);
  const auto favicon_image = GetDefaultFaviconModel(ui::kColorBubbleBackground)
                                 .Rasterize(color_provider);
  EXPECT_TRUE(GetDefaultFaviconForColorScheme(/*is_dark=*/true)
                  .BackedBySameObjectAs(favicon_image));
}

TEST_F(DefaultFaviconModelTest, UsesCorrectIcon_DarkBackground_Custom) {
  // Flip the background for a new color id such that it is light in dark mode
  // and vice versa.
  const auto initializer = [&](ui::ColorProvider* provider,
                               const ui::ColorProviderKey& key) {
    ui::ColorMixer& mixer = provider->AddMixer();
    mixer[ui::kColorBubbleBackground] = {
        key.color_mode == ui::ColorProviderKey::ColorMode::kDark ? kLightColor
                                                                 : kDarkColor};
  };
  AddInitializer(base::BindRepeating(initializer));

  // Even if the color provider is configured for dark mode the default favicon
  // model should match the light variant when rasterized.
  auto* color_provider =
      GetColorProvider(ui::ColorProviderKey::ColorMode::kDark);
  const auto favicon_image = GetDefaultFaviconModel(ui::kColorBubbleBackground)
                                 .Rasterize(color_provider);
  EXPECT_TRUE(GetDefaultFaviconForColorScheme(/*is_dark=*/false)
                  .BackedBySameObjectAs(favicon_image));
}

}  // namespace favicon