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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/app_list/model/folder_image.h"
#include <string>
#include <utility>
#include "ash/app_list/model/app_list_item.h"
#include "ash/app_list/model/app_list_item_list.h"
#include "ash/app_list/model/app_list_test_model.h"
#include "ash/public/cpp/app_list/app_list_config.h"
#include "ash/public/cpp/app_list/app_list_config_provider.h"
#include "base/test/icu_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/skia_util.h"
namespace ash {
namespace {
gfx::ImageSkia CreateSquareBitmapWithColor(int size, SkColor color) {
SkBitmap bitmap;
bitmap.allocN32Pixels(size, size);
bitmap.eraseColor(color);
return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
}
bool ImagesAreEqual(const gfx::ImageSkia& image1,
const gfx::ImageSkia& image2) {
return gfx::BitmapsAreEqual(*image1.bitmap(), *image2.bitmap());
}
// Listens for OnFolderImageUpdated and sets a flag upon receiving the signal.
class TestFolderImageObserver : public FolderImageObserver {
public:
TestFolderImageObserver() : updated_flag_(false) {}
TestFolderImageObserver(const TestFolderImageObserver&) = delete;
TestFolderImageObserver& operator=(const TestFolderImageObserver&) = delete;
bool updated() const { return updated_flag_; }
void Reset() { updated_flag_ = false; }
// FolderImageObserver overrides:
void OnFolderImageUpdated(ash::AppListConfigType config_type) override {
updated_flag_ = true;
}
private:
bool updated_flag_;
};
} // namespace
class FolderImageTest : public testing::Test,
public ::testing::WithParamInterface<
std::tuple<AppListConfigType, bool>> {
public:
FolderImageTest() : scoped_locale_(std::get<1>(GetParam()) ? "he" : "") {}
FolderImageTest(const FolderImageTest&) = delete;
FolderImageTest& operator=(const FolderImageTest&) = delete;
~FolderImageTest() override = default;
void SetUp() override {
app_list_model_ = std::make_unique<test::AppListTestModel>();
folder_image_ =
std::make_unique<FolderImage>(GetAppListConfig(/*can_create=*/true),
app_list_model_->top_level_item_list());
// Populate the AppListModel with three items (to test that the FolderImage
// correctly supports having fewer than four icons).
AddAppWithColoredIcon("app1", SK_ColorRED);
AddAppWithColoredIcon("app2", SK_ColorGREEN);
AddAppWithColoredIcon("app3", SK_ColorBLUE);
observer_.Reset();
folder_image_->AddObserver(&observer_);
}
void TearDown() override {
folder_image_->RemoveObserver(&observer_);
AppListConfigProvider::Get().ResetForTesting();
}
AppListConfig* GetAppListConfig(bool can_create) {
return AppListConfigProvider::Get().GetConfigForType(
std::get<0>(GetParam()), can_create);
}
bool is_rtl() { return std::get<1>(GetParam()); }
protected:
void AddAppWithColoredIcon(const std::string& id, SkColor icon_color) {
std::unique_ptr<AppListItem> item(new AppListItem(id));
item->SetDefaultIconAndColor(
CreateSquareBitmapWithColor(
SharedAppListConfig::instance().default_grid_icon_dimension(),
icon_color),
IconColor(), /*is_placeholder_icon=*/false);
static_cast<AppListModel*>(app_list_model_.get())->AddItem(std::move(item));
}
base::test::ScopedRestoreICUDefaultLocale scoped_locale_;
std::unique_ptr<test::AppListTestModel> app_list_model_;
std::unique_ptr<FolderImage> folder_image_;
TestFolderImageObserver observer_;
};
INSTANTIATE_TEST_SUITE_P(
All,
FolderImageTest,
::testing::Combine(::testing::Values(AppListConfigType::kRegular,
AppListConfigType::kDense),
::testing::Bool()));
TEST_P(FolderImageTest, UpdateListTest) {
gfx::ImageSkia icon1 = folder_image_->icon();
// Call UpdateIcon and ensure that the observer event fired.
folder_image_->UpdateIcon();
EXPECT_TRUE(observer_.updated());
observer_.Reset();
// The icon should not have changed.
EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_->icon()));
// Swap two items. Ensure that the observer fired and the icon changed.
app_list_model_->top_level_item_list()->MoveItem(2, 1);
EXPECT_TRUE(observer_.updated());
observer_.Reset();
gfx::ImageSkia icon2 = folder_image_->icon();
EXPECT_FALSE(ImagesAreEqual(icon1, icon2));
// Swap back items. Ensure that the observer fired and the icon changed back.
app_list_model_->top_level_item_list()->MoveItem(2, 1);
EXPECT_TRUE(observer_.updated());
observer_.Reset();
EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_->icon()));
// Add a new item. Ensure that the observer fired and the icon changed.
AddAppWithColoredIcon("app4", SK_ColorYELLOW);
EXPECT_TRUE(observer_.updated());
observer_.Reset();
gfx::ImageSkia icon3 = folder_image_->icon();
EXPECT_FALSE(ImagesAreEqual(icon1, icon3));
// Add a new item. The observer should not fire, nor should the icon change
// (because it does not affect the first four icons).
AddAppWithColoredIcon("app5", SK_ColorCYAN);
EXPECT_FALSE(observer_.updated());
observer_.Reset();
EXPECT_TRUE(ImagesAreEqual(icon3, folder_image_->icon()));
// Delete an item. Ensure that the observer fired and the icon changed.
app_list_model_->DeleteItem("app2");
EXPECT_TRUE(observer_.updated());
observer_.Reset();
gfx::ImageSkia icon4 = folder_image_->icon();
EXPECT_FALSE(ImagesAreEqual(icon3, icon4));
}
TEST_P(FolderImageTest, UpdateItemTest) {
gfx::ImageSkia icon1 = folder_image_->icon();
// Change an item's icon. Ensure that the observer fired and the icon changed.
app_list_model_->FindItem("app2")->SetDefaultIconAndColor(
CreateSquareBitmapWithColor(
SharedAppListConfig::instance().default_grid_icon_dimension(),
SK_ColorMAGENTA),
IconColor(), /*is_placeholder_icon=*/false);
EXPECT_TRUE(observer_.updated());
observer_.Reset();
EXPECT_FALSE(ImagesAreEqual(icon1, folder_image_->icon()));
}
TEST_P(FolderImageTest, GetTargetIconRectInFolderWithSingleItem) {
app_list_model_->DeleteItem("app2");
app_list_model_->DeleteItem("app3");
const AppListConfig* config = GetAppListConfig(/*can_create=*/false);
ASSERT_TRUE(config);
const gfx::Rect test_rects[] = {
gfx::Rect(config->icon_visible_size()),
gfx::Rect(gfx::Point(10, 10), config->icon_visible_size()),
gfx::Rect(config->unclipped_icon_size()),
gfx::Rect(gfx::Point(10, 10), config->unclipped_icon_size()),
};
for (const auto& test_rect : test_rects) {
SCOPED_TRACE(::testing::Message()
<< "Target folder icon bounds: " << test_rect.ToString());
const gfx::Point test_rect_center = test_rect.CenterPoint();
const gfx::Size expected_icon_rect_size(
config->item_icon_in_folder_icon_dimension(),
config->item_icon_in_folder_icon_dimension());
gfx::Rect item_1_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app1"), test_rect);
EXPECT_EQ(expected_icon_rect_size, item_1_bounds.size());
EXPECT_EQ(test_rect_center, item_1_bounds.CenterPoint());
}
}
TEST_P(FolderImageTest, GetTargetIconRectInFolderWithTwoItems) {
app_list_model_->DeleteItem("app3");
const AppListConfig* config = GetAppListConfig(/*can_create=*/false);
ASSERT_TRUE(config);
const gfx::Rect test_rects[] = {
gfx::Rect(config->icon_visible_size()),
gfx::Rect(gfx::Point(10, 10), config->icon_visible_size()),
gfx::Rect(config->unclipped_icon_size()),
gfx::Rect(gfx::Point(10, 10), config->unclipped_icon_size()),
};
for (const auto& test_rect : test_rects) {
SCOPED_TRACE(::testing::Message()
<< "Target folder icon bounds: " << test_rect.ToString());
const gfx::Point test_rect_center = test_rect.CenterPoint();
const gfx::Size expected_icon_rect_size(
config->item_icon_in_folder_icon_dimension(),
config->item_icon_in_folder_icon_dimension());
gfx::Rect item_1_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app1"), test_rect);
gfx::Rect item_2_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app2"), test_rect);
if (is_rtl())
std::swap(item_1_bounds, item_2_bounds);
EXPECT_EQ(expected_icon_rect_size, item_1_bounds.size());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.right());
EXPECT_EQ(test_rect_center.y(), item_1_bounds.CenterPoint().y());
EXPECT_EQ(expected_icon_rect_size, item_2_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.x());
EXPECT_EQ(test_rect_center.y(), item_2_bounds.CenterPoint().y());
}
}
TEST_P(FolderImageTest, GetTargetIconRectInFolderWithThreeItems) {
const AppListConfig* config = GetAppListConfig(/*can_create=*/false);
ASSERT_TRUE(config);
const gfx::Rect test_rects[] = {
gfx::Rect(config->icon_visible_size()),
gfx::Rect(gfx::Point(10, 10), config->icon_visible_size()),
gfx::Rect(config->unclipped_icon_size()),
gfx::Rect(gfx::Point(10, 10), config->unclipped_icon_size()),
};
for (const auto& test_rect : test_rects) {
SCOPED_TRACE(::testing::Message()
<< "Target folder icon bounds: " << test_rect.ToString());
const gfx::Point test_rect_center = test_rect.CenterPoint();
const gfx::Size expected_icon_rect_size(
config->item_icon_in_folder_icon_dimension(),
config->item_icon_in_folder_icon_dimension());
gfx::Rect item_1_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app1"), test_rect);
gfx::Rect item_2_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app2"), test_rect);
gfx::Rect item_3_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app3"), test_rect);
if (is_rtl())
std::swap(item_2_bounds, item_3_bounds);
EXPECT_EQ(expected_icon_rect_size, item_1_bounds.size());
EXPECT_EQ(test_rect_center.x(), item_1_bounds.CenterPoint().x());
EXPECT_EQ(
test_rect_center.y() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.bottom());
EXPECT_EQ(expected_icon_rect_size, item_2_bounds.size());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.right());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.y());
EXPECT_EQ(expected_icon_rect_size, item_3_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.x());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.y());
}
}
TEST_P(FolderImageTest, GetTargetIconRectInFolderWithFourItems) {
AddAppWithColoredIcon("app4", SK_ColorYELLOW);
const AppListConfig* config = GetAppListConfig(/*can_create=*/false);
ASSERT_TRUE(config);
const gfx::Rect test_rects[] = {
gfx::Rect(config->icon_visible_size()),
gfx::Rect(gfx::Point(10, 10), config->icon_visible_size()),
gfx::Rect(config->unclipped_icon_size()),
gfx::Rect(gfx::Point(10, 10), config->unclipped_icon_size()),
};
for (const auto& test_rect : test_rects) {
SCOPED_TRACE(::testing::Message()
<< "Target folder icon bounds: " << test_rect.ToString());
const gfx::Point test_rect_center = test_rect.CenterPoint();
const gfx::Size expected_icon_rect_size(
config->item_icon_in_folder_icon_dimension(),
config->item_icon_in_folder_icon_dimension());
gfx::Rect item_1_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app1"), test_rect);
gfx::Rect item_2_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app2"), test_rect);
gfx::Rect item_3_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app3"), test_rect);
gfx::Rect item_4_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app4"), test_rect);
if (is_rtl()) {
std::swap(item_1_bounds, item_2_bounds);
std::swap(item_3_bounds, item_4_bounds);
}
EXPECT_EQ(expected_icon_rect_size, item_1_bounds.size());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.right());
EXPECT_EQ(
test_rect_center.y() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.bottom());
EXPECT_EQ(expected_icon_rect_size, item_2_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.x());
EXPECT_EQ(
test_rect_center.y() - config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.bottom());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.right());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.y());
EXPECT_EQ(expected_icon_rect_size, item_4_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_4_bounds.x());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_4_bounds.y());
}
}
TEST_P(FolderImageTest, GetTargetIconRectInFolderWithFiveItems) {
AddAppWithColoredIcon("app4", SK_ColorYELLOW);
AddAppWithColoredIcon("app5", SK_ColorYELLOW);
const AppListConfig* config = GetAppListConfig(/*can_create=*/false);
ASSERT_TRUE(config);
const gfx::Rect test_rects[] = {
gfx::Rect(config->icon_visible_size()),
gfx::Rect(gfx::Point(10, 10), config->icon_visible_size()),
gfx::Rect(config->unclipped_icon_size()),
gfx::Rect(gfx::Point(10, 10), config->unclipped_icon_size()),
};
for (const auto& test_rect : test_rects) {
SCOPED_TRACE(::testing::Message()
<< "Target folder icon bounds: " << test_rect.ToString());
const gfx::Point test_rect_center = test_rect.CenterPoint();
const gfx::Size expected_icon_rect_size(
config->item_icon_in_folder_icon_dimension(),
config->item_icon_in_folder_icon_dimension());
gfx::Rect item_1_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app1"), test_rect);
gfx::Rect item_2_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app2"), test_rect);
gfx::Rect item_3_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app3"), test_rect);
gfx::Rect item_4_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app4"), test_rect);
gfx::Rect item_5_bounds = folder_image_->GetTargetIconRectInFolderForItem(
*config, app_list_model_->FindItem("app5"), test_rect);
if (is_rtl()) {
std::swap(item_1_bounds, item_2_bounds);
std::swap(item_3_bounds, item_4_bounds);
}
EXPECT_EQ(expected_icon_rect_size, item_1_bounds.size());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.right());
EXPECT_EQ(
test_rect_center.y() - config->item_icon_in_folder_icon_margin() / 2,
item_1_bounds.bottom());
EXPECT_EQ(expected_icon_rect_size, item_2_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.x());
EXPECT_EQ(
test_rect_center.y() - config->item_icon_in_folder_icon_margin() / 2,
item_2_bounds.bottom());
EXPECT_EQ(
test_rect_center.x() - config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.right());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_3_bounds.y());
EXPECT_EQ(expected_icon_rect_size, item_4_bounds.size());
EXPECT_EQ(
test_rect_center.x() + config->item_icon_in_folder_icon_margin() / 2,
item_4_bounds.x());
EXPECT_EQ(
test_rect_center.y() + config->item_icon_in_folder_icon_margin() / 2,
item_4_bounds.y());
EXPECT_EQ(expected_icon_rect_size, item_5_bounds.size());
EXPECT_EQ(test_rect_center, item_5_bounds.CenterPoint());
}
}
} // namespace ash
|