Disable flaky UnloadTest.CrossSiteInfiniteBeforeUnloadSync on Mac.
[chromium-blink-merge.git] / ui / app_list / folder_image_unittest.cc
blobf9c97b0f4585d876f58b14b408c1631dd094f9b8
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.
5 #include <string>
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"
19 namespace app_list {
21 namespace {
23 gfx::ImageSkia CreateSquareBitmapWithColor(int size, SkColor color) {
24 SkBitmap bitmap;
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 {
37 public:
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; }
47 private:
48 bool updated_flag_;
50 DISALLOW_COPY_AND_ASSIGN(TestFolderImageObserver);
53 } // namespace
55 class FolderImageTest : public testing::Test {
56 public:
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);
68 observer_.Reset();
69 folder_image_.AddObserver(&observer_);
72 void TearDown() override { folder_image_.RemoveObserver(&observer_); }
74 protected:
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 false);
79 app_list_model_.AddItem(item.Pass());
82 AppListModel app_list_model_;
84 FolderImage folder_image_;
86 TestFolderImageObserver observer_;
88 private:
89 DISALLOW_COPY_AND_ASSIGN(FolderImageTest);
92 TEST_F(FolderImageTest, UpdateListTest) {
93 gfx::ImageSkia icon1 = folder_image_.icon();
95 // Call UpdateIcon and ensure that the observer event fired.
96 folder_image_.UpdateIcon();
97 EXPECT_TRUE(observer_.updated());
98 observer_.Reset();
99 // The icon should not have changed.
100 EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_.icon()));
102 // Swap two items. Ensure that the observer fired and the icon changed.
103 app_list_model_.top_level_item_list()->MoveItem(2, 1);
104 EXPECT_TRUE(observer_.updated());
105 observer_.Reset();
106 gfx::ImageSkia icon2 = folder_image_.icon();
107 EXPECT_FALSE(ImagesAreEqual(icon1, icon2));
109 // Swap back items. Ensure that the observer fired and the icon changed back.
110 app_list_model_.top_level_item_list()->MoveItem(2, 1);
111 EXPECT_TRUE(observer_.updated());
112 observer_.Reset();
113 EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_.icon()));
115 // Add a new item. Ensure that the observer fired and the icon changed.
116 AddAppWithColoredIcon("app4", SK_ColorYELLOW);
117 EXPECT_TRUE(observer_.updated());
118 observer_.Reset();
119 gfx::ImageSkia icon3 = folder_image_.icon();
120 EXPECT_FALSE(ImagesAreEqual(icon1, icon3));
122 // Add a new item. The observer should not fire, nor should the icon change
123 // (because it does not affect the first four icons).
124 AddAppWithColoredIcon("app5", SK_ColorCYAN);
125 EXPECT_FALSE(observer_.updated());
126 observer_.Reset();
127 EXPECT_TRUE(ImagesAreEqual(icon3, folder_image_.icon()));
129 // Delete an item. Ensure that the observer fired and the icon changed.
130 app_list_model_.DeleteItem("app2");
131 EXPECT_TRUE(observer_.updated());
132 observer_.Reset();
133 gfx::ImageSkia icon4 = folder_image_.icon();
134 EXPECT_FALSE(ImagesAreEqual(icon3, icon4));
137 TEST_F(FolderImageTest, UpdateItemTest) {
138 gfx::ImageSkia icon1 = folder_image_.icon();
140 // Change an item's icon. Ensure that the observer fired and the icon changed.
141 app_list_model_.FindItem("app2")->SetIcon(
142 CreateSquareBitmapWithColor(kListIconSize, SK_ColorMAGENTA), false);
143 EXPECT_TRUE(observer_.updated());
144 observer_.Reset();
145 EXPECT_FALSE(ImagesAreEqual(icon1, folder_image_.icon()));
148 } // namespace app_list