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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// 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 <utility>
#include "base/containers/flat_map.h"
#include "base/scoped_observation.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_decoder.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_factory.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_test_util.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/ash/app_list/arc/arc_app_icon_descriptor.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/browser/extensions/chrome_app_icon.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/experiences/arc/mojom/app.mojom.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "components/services/app_service/public/cpp/features.h"
#include "content/public/test/browser_task_environment.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia_rep.h"
namespace apps {
class ArcAppsIconFactoryTest : public testing::Test {
public:
void SetUp() override {
testing::Test::SetUp();
arc_test_.SetUp(profile());
task_environment_.RunUntilIdle();
}
void TearDown() override {
arc_test_.StopArcInstance();
arc_test_.TearDown();
}
arc::mojom::RawIconPngDataPtr GenerateRawArcAppIcon(
const std::string& app_id,
ui::ResourceScaleFactor scale_factor) {
ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile());
base::test::TestFuture<arc::mojom::RawIconPngDataPtr> result;
prefs->RequestRawIconData(app_id,
ArcAppIconDescriptor(kSizeInDip, scale_factor),
result.GetCallback());
return result.Take();
}
IconValuePtr GetArcAppCompressedIconData(
const std::string& app_id,
ui::ResourceScaleFactor scale_factor) {
base::test::TestFuture<IconValuePtr> result;
apps::GetArcAppCompressedIconData(profile(), app_id, kSizeInDip,
scale_factor, result.GetCallback());
return result.Take();
}
TestingProfile* profile() { return &profile_; }
ArcAppTest* arc_test() { return &arc_test_; }
private:
content::BrowserTaskEnvironment task_environment_;
ArcAppTest arc_test_;
TestingProfile profile_;
};
TEST_F(ArcAppsIconFactoryTest, GetArcAppCompressedIconData) {
const auto& fake_apps = arc_test()->fake_apps();
std::string package_name = fake_apps[0]->package_name;
std::string app_id = ArcAppListPrefs::GetAppId(fake_apps[0]->package_name,
fake_apps[0]->activity);
arc_test()->app_instance()->SendRefreshAppList(fake_apps);
// Generate the source raw icon data for comparing.
auto raw_icon_data =
GenerateRawArcAppIcon(app_id, ui::ResourceScaleFactor::k100Percent);
ASSERT_TRUE(raw_icon_data);
ASSERT_TRUE(raw_icon_data->foreground_icon_png_data.has_value());
ASSERT_TRUE(raw_icon_data->background_icon_png_data.has_value());
// Verify the raw icon data.
auto iv =
GetArcAppCompressedIconData(app_id, ui::ResourceScaleFactor::k100Percent);
ASSERT_TRUE(iv);
ASSERT_EQ(iv->icon_type, IconType::kCompressed);
ASSERT_EQ(raw_icon_data->foreground_icon_png_data.value(),
iv->foreground_icon_png_data);
ASSERT_EQ(raw_icon_data->background_icon_png_data.value(),
iv->background_icon_png_data);
}
class AppServiceArcAppIconTest : public ArcAppsIconFactoryTest,
public ArcAppListPrefs::Observer {
public:
void SetUp() override {
ArcAppsIconFactoryTest::SetUp();
proxy_ = AppServiceProxyFactory::GetForProfile(profile());
}
void GenerateArcAppUncompressedIcon(const std::string& app_id,
gfx::ImageSkia& image_skia) {
gfx::ImageSkia foreground_image_skia;
gfx::ImageSkia background_image_skia;
for (const auto scale_factor : ui::GetSupportedResourceScaleFactors()) {
auto raw_icon_data = GenerateRawArcAppIcon(app_id, scale_factor);
ASSERT_TRUE(raw_icon_data);
ASSERT_TRUE(raw_icon_data->foreground_icon_png_data.has_value());
ASSERT_TRUE(raw_icon_data->background_icon_png_data.has_value());
SkBitmap foreground_bitmap = gfx::PNGCodec::Decode(
raw_icon_data->foreground_icon_png_data.value());
SkBitmap background_bitmap = gfx::PNGCodec::Decode(
raw_icon_data->background_icon_png_data.value());
foreground_image_skia.AddRepresentation(gfx::ImageSkiaRep(
foreground_bitmap, ui::GetScaleForResourceScaleFactor(scale_factor)));
background_image_skia.AddRepresentation(gfx::ImageSkiaRep(
background_bitmap, ui::GetScaleForResourceScaleFactor(scale_factor)));
}
image_skia = apps::CompositeImagesAndApplyMask(foreground_image_skia,
background_image_skia);
image_skia.MakeThreadSafe();
}
std::vector<uint8_t> GenerateArcAppCompressedIcon(const std::string& app_id,
float scale) {
gfx::ImageSkia image_skia;
GenerateArcAppUncompressedIcon(app_id, image_skia);
const gfx::ImageSkiaRep& image_skia_rep =
image_skia.GetRepresentation(scale);
CHECK_EQ(image_skia_rep.scale(), scale);
const SkBitmap& bitmap = image_skia_rep.GetBitmap();
const bool discard_transparency = false;
return gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, discard_transparency)
.value();
}
apps::IconValuePtr LoadIcon(const std::string& app_id, IconType icon_type) {
base::test::TestFuture<apps::IconValuePtr> result;
app_service_proxy().LoadIcon(app_id, icon_type, kSizeInDip,
/*allow_placeholder_icon=*/false,
result.GetCallback());
return result.Take();
}
apps::IconValuePtr LoadIconWithIconEffects(const std::string& app_id,
uint32_t icon_effects,
IconType icon_type) {
base::test::TestFuture<apps::IconValuePtr> result;
app_service_proxy().LoadIconWithIconEffects(
app_id, icon_effects, icon_type, kSizeInDip,
/*allow_placeholder_icon=*/false, result.GetCallback());
return result.Take();
}
AppServiceProxy& app_service_proxy() { return *proxy_; }
// Calls `closure` once any update is made to `app_id`'s icons in
// ArcAppListPrefs.
void AwaitIconUpdate(const std::string& app_id, base::OnceClosure closure) {
waiting_icon_updates_[app_id] = std::move(closure);
}
// ArcAppListPrefs::Observer:
void OnAppIconUpdated(const std::string& app_id,
const ArcAppIconDescriptor& descriptor) override {
if (base::Contains(waiting_icon_updates_, app_id)) {
std::move(waiting_icon_updates_[app_id]).Run();
waiting_icon_updates_.erase(app_id);
}
}
private:
raw_ptr<AppServiceProxy> proxy_;
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
base::flat_map<std::string, base::OnceClosure> waiting_icon_updates_;
};
TEST_F(AppServiceArcAppIconTest, GetCompressedIconDataForUncompressedIcon) {
const auto& fake_apps = arc_test()->fake_apps();
std::string package_name = fake_apps[0]->package_name;
std::string app_id = ArcAppListPrefs::GetAppId(fake_apps[0]->package_name,
fake_apps[0]->activity);
arc_test()->app_instance()->SendRefreshAppList(fake_apps);
// Generate the source uncompressed icon for comparing.
gfx::ImageSkia src_image_skia;
GenerateArcAppUncompressedIcon(app_id, src_image_skia);
// Verify the icon reading and writing function in AppService for the
// uncompressed icon.
IconValuePtr iv = LoadIcon(app_id, IconType::kUncompressed);
ASSERT_TRUE(iv);
ASSERT_EQ(iv->icon_type, IconType::kUncompressed);
VerifyIcon(src_image_skia, iv->uncompressed);
}
TEST_F(AppServiceArcAppIconTest, GetCompressedIconDataForCompressedIcon) {
const auto& fake_apps = arc_test()->fake_apps();
std::string package_name = fake_apps[0]->package_name;
std::string app_id = ArcAppListPrefs::GetAppId(fake_apps[0]->package_name,
fake_apps[0]->activity);
arc_test()->app_instance()->SendRefreshAppList(fake_apps);
// Generate the source compressed icon for comparing.
std::vector<uint8_t> src_data =
GenerateArcAppCompressedIcon(app_id, /*scale=*/1.0);
// Verify the icon reading and writing function in AppService for the
// compressed icon.
VerifyCompressedIcon(src_data, *LoadIcon(app_id, IconType::kCompressed));
}
TEST_F(AppServiceArcAppIconTest, GetCompressedIconDataForStandardIcon) {
const auto& fake_apps = arc_test()->fake_apps();
std::string package_name = fake_apps[0]->package_name;
std::string app_id = ArcAppListPrefs::GetAppId(fake_apps[0]->package_name,
fake_apps[0]->activity);
arc_test()->app_instance()->SendRefreshAppList(fake_apps);
// Generate the source uncompressed icon for comparing.
gfx::ImageSkia src_image_skia;
GenerateArcAppUncompressedIcon(app_id, src_image_skia);
// Apply the paused app badge.
extensions::ChromeAppIcon::ApplyEffects(
kSizeInDip, extensions::ChromeAppIcon::ResizeFunction(),
/*app_launchable=*/false, /*rounded_corners=*/false,
extensions::ChromeAppIcon::Badge::kPaused, &src_image_skia);
src_image_skia.MakeThreadSafe();
// Verify the icon reading and writing function in AppService for the
// kStandard icon.
auto ret = LoadIconWithIconEffects(
app_id, IconEffects::kCrOsStandardIcon | IconEffects::kPaused,
IconType::kStandard);
ASSERT_EQ(apps::IconType::kStandard, ret->icon_type);
VerifyIcon(src_image_skia, ret->uncompressed);
}
TEST_F(AppServiceArcAppIconTest, GetCompressedIconDataFromArcDiskCache) {
const auto& fake_apps = arc_test()->fake_apps();
std::string package_name = fake_apps[0]->package_name;
std::string app_id = ArcAppListPrefs::GetAppId(fake_apps[0]->package_name,
fake_apps[0]->activity);
arc_test()->app_instance()->SendRefreshAppList(fake_apps);
// Ensure that app icons are in the ARC disk cache.
base::ScopedObservation<ArcAppListPrefs, ArcAppListPrefs::Observer>
observation(this);
observation.Observe(arc_test()->arc_app_list_prefs());
for (const auto scale_factor : ui::GetSupportedResourceScaleFactors()) {
base::RunLoop run_loop;
AwaitIconUpdate(app_id, run_loop.QuitClosure());
arc_test()->arc_app_list_prefs()->MaybeRequestIcon(
app_id, ArcAppIconDescriptor(kSizeInDip, scale_factor));
run_loop.Run();
}
// Generate the source compressed icon for comparing.
std::vector<uint8_t> src_data =
GenerateArcAppCompressedIcon(app_id, /*scale=*/1.0);
// Stop ARC from running so that LoadIcon requests must load from the disk
// cache rather than ARC itself.
arc_test()->StopArcInstance();
// Verify the icon reading and writing function in AppService for the
// compressed icon.
VerifyCompressedIcon(src_data, *LoadIcon(app_id, IconType::kCompressed));
}
} // namespace apps
|