1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "ui/app_list/folder_image.h"
9 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/app_list/app_list_constants.h"
14 #include "ui/app_list/app_list_item.h"
15 #include "ui/app_list/app_list_item_list.h"
16 #include "ui/app_list/app_list_model.h"
17 #include "ui/gfx/skia_util.h"
23 gfx::ImageSkia
CreateSquareBitmapWithColor(int size
, SkColor color
) {
25 bitmap
.allocN32Pixels(size
, size
);
26 bitmap
.eraseColor(color
);
27 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap
);
30 bool ImagesAreEqual(const gfx::ImageSkia
& image1
,
31 const gfx::ImageSkia
& image2
) {
32 return gfx::BitmapsAreEqual(*image1
.bitmap(), *image2
.bitmap());
35 // Listens for OnFolderImageUpdated and sets a flag upon receiving the signal.
36 class TestFolderImageObserver
: public FolderImageObserver
{
38 TestFolderImageObserver() : updated_flag_(false) {}
40 bool updated() const { return updated_flag_
; }
42 void Reset() { updated_flag_
= false; }
44 // FolderImageObserver overrides:
45 void OnFolderImageUpdated() override
{ updated_flag_
= true; }
50 DISALLOW_COPY_AND_ASSIGN(TestFolderImageObserver
);
55 class FolderImageTest
: public testing::Test
{
57 FolderImageTest() : folder_image_(app_list_model_
.top_level_item_list()) {}
59 ~FolderImageTest() override
{}
61 void SetUp() override
{
62 // Populate the AppListModel with three items (to test that the FolderImage
63 // correctly supports having fewer than four icons).
64 AddAppWithColoredIcon("app1", SK_ColorRED
);
65 AddAppWithColoredIcon("app2", SK_ColorGREEN
);
66 AddAppWithColoredIcon("app3", SK_ColorBLUE
);
69 folder_image_
.AddObserver(&observer_
);
72 void TearDown() override
{ folder_image_
.RemoveObserver(&observer_
); }
75 void AddAppWithColoredIcon(const std::string
& id
, SkColor icon_color
) {
76 scoped_ptr
<AppListItem
> item(new AppListItem(id
));
77 item
->SetIcon(CreateSquareBitmapWithColor(kListIconSize
, icon_color
));
78 app_list_model_
.AddItem(item
.Pass());
81 AppListModel app_list_model_
;
83 FolderImage folder_image_
;
85 TestFolderImageObserver observer_
;
88 DISALLOW_COPY_AND_ASSIGN(FolderImageTest
);
91 TEST_F(FolderImageTest
, UpdateListTest
) {
92 gfx::ImageSkia icon1
= folder_image_
.icon();
94 // Call UpdateIcon and ensure that the observer event fired.
95 folder_image_
.UpdateIcon();
96 EXPECT_TRUE(observer_
.updated());
98 // The icon should not have changed.
99 EXPECT_TRUE(ImagesAreEqual(icon1
, folder_image_
.icon()));
101 // Swap two items. Ensure that the observer fired and the icon changed.
102 app_list_model_
.top_level_item_list()->MoveItem(2, 1);
103 EXPECT_TRUE(observer_
.updated());
105 gfx::ImageSkia icon2
= folder_image_
.icon();
106 EXPECT_FALSE(ImagesAreEqual(icon1
, icon2
));
108 // Swap back items. Ensure that the observer fired and the icon changed back.
109 app_list_model_
.top_level_item_list()->MoveItem(2, 1);
110 EXPECT_TRUE(observer_
.updated());
112 EXPECT_TRUE(ImagesAreEqual(icon1
, folder_image_
.icon()));
114 // Add a new item. Ensure that the observer fired and the icon changed.
115 AddAppWithColoredIcon("app4", SK_ColorYELLOW
);
116 EXPECT_TRUE(observer_
.updated());
118 gfx::ImageSkia icon3
= folder_image_
.icon();
119 EXPECT_FALSE(ImagesAreEqual(icon1
, icon3
));
121 // Add a new item. The observer should not fire, nor should the icon change
122 // (because it does not affect the first four icons).
123 AddAppWithColoredIcon("app5", SK_ColorCYAN
);
124 EXPECT_FALSE(observer_
.updated());
126 EXPECT_TRUE(ImagesAreEqual(icon3
, folder_image_
.icon()));
128 // Delete an item. Ensure that the observer fired and the icon changed.
129 app_list_model_
.DeleteItem("app2");
130 EXPECT_TRUE(observer_
.updated());
132 gfx::ImageSkia icon4
= folder_image_
.icon();
133 EXPECT_FALSE(ImagesAreEqual(icon3
, icon4
));
136 TEST_F(FolderImageTest
, UpdateItemTest
) {
137 gfx::ImageSkia icon1
= folder_image_
.icon();
139 // Change an item's icon. Ensure that the observer fired and the icon changed.
140 app_list_model_
.FindItem("app2")
141 ->SetIcon(CreateSquareBitmapWithColor(kListIconSize
, SK_ColorMAGENTA
));
142 EXPECT_TRUE(observer_
.updated());
144 EXPECT_FALSE(ImagesAreEqual(icon1
, folder_image_
.icon()));
147 } // namespace app_list