OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/app_list/folder_image.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "third_party/skia/include/core/SkColor.h" |
| 11 #include "ui/app_list/app_list_constants.h" |
| 12 #include "ui/app_list/app_list_item.h" |
| 13 #include "ui/app_list/app_list_item_list.h" |
| 14 #include "ui/app_list/app_list_model.h" |
| 15 #include "ui/gfx/skia_util.h" |
| 16 |
| 17 namespace app_list { |
| 18 |
| 19 namespace { |
| 20 |
| 21 gfx::ImageSkia CreateSquareBitmapWithColor(int size, SkColor color) { |
| 22 SkBitmap bitmap; |
| 23 bitmap.allocN32Pixels(size, size); |
| 24 bitmap.eraseColor(color); |
| 25 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 26 } |
| 27 |
| 28 bool ImagesAreEqual(const gfx::ImageSkia& image1, |
| 29 const gfx::ImageSkia& image2) { |
| 30 return gfx::BitmapsAreEqual(*image1.bitmap(), *image2.bitmap()); |
| 31 } |
| 32 |
| 33 // Listens for OnFolderImageUpdated and sets a flag upon receiving the signal. |
| 34 class TestFolderImageObserver : public FolderImageObserver { |
| 35 public: |
| 36 TestFolderImageObserver() : updated_flag_(false) {} |
| 37 |
| 38 bool updated() const { return updated_flag_; } |
| 39 |
| 40 void Reset() { updated_flag_ = false; } |
| 41 |
| 42 // FolderImageObserver overrides: |
| 43 void OnFolderImageUpdated() override { updated_flag_ = true; } |
| 44 |
| 45 private: |
| 46 bool updated_flag_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(TestFolderImageObserver); |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 class FolderImageTest : public testing::Test { |
| 54 public: |
| 55 FolderImageTest() : folder_image_(app_list_model_.top_level_item_list()) {} |
| 56 |
| 57 ~FolderImageTest() {} |
| 58 |
| 59 void SetUp() { |
| 60 // Populate the AppListModel with three items (to test that the FolderImage |
| 61 // correctly supports having fewer than four icons). |
| 62 AddAppWithColoredIcon("app1", SK_ColorRED); |
| 63 AddAppWithColoredIcon("app2", SK_ColorGREEN); |
| 64 AddAppWithColoredIcon("app3", SK_ColorBLUE); |
| 65 |
| 66 observer_.Reset(); |
| 67 folder_image_.AddObserver(&observer_); |
| 68 } |
| 69 |
| 70 void TearDown() { folder_image_.RemoveObserver(&observer_); } |
| 71 |
| 72 protected: |
| 73 void AddAppWithColoredIcon(const std::string& id, SkColor icon_color) { |
| 74 scoped_ptr<AppListItem> item(new AppListItem(id)); |
| 75 item->SetIcon(CreateSquareBitmapWithColor(kListIconSize, icon_color), |
| 76 false); |
| 77 app_list_model_.AddItem(item.Pass()); |
| 78 } |
| 79 |
| 80 AppListModel app_list_model_; |
| 81 |
| 82 FolderImage folder_image_; |
| 83 |
| 84 TestFolderImageObserver observer_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(FolderImageTest); |
| 87 }; |
| 88 |
| 89 TEST_F(FolderImageTest, UpdateListTest) { |
| 90 gfx::ImageSkia icon1 = folder_image_.icon(); |
| 91 |
| 92 // Call UpdateIcon and ensure that the observer event fired. |
| 93 folder_image_.UpdateIcon(); |
| 94 EXPECT_TRUE(observer_.updated()); |
| 95 observer_.Reset(); |
| 96 // The icon should not have changed. |
| 97 EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_.icon())); |
| 98 |
| 99 // Swap two items. Ensure that the observer fired and the icon changed. |
| 100 app_list_model_.top_level_item_list()->MoveItem(2, 1); |
| 101 EXPECT_TRUE(observer_.updated()); |
| 102 observer_.Reset(); |
| 103 gfx::ImageSkia icon2 = folder_image_.icon(); |
| 104 EXPECT_FALSE(ImagesAreEqual(icon1, icon2)); |
| 105 |
| 106 // Swap back items. Ensure that the observer fired and the icon changed back. |
| 107 app_list_model_.top_level_item_list()->MoveItem(2, 1); |
| 108 EXPECT_TRUE(observer_.updated()); |
| 109 observer_.Reset(); |
| 110 EXPECT_TRUE(ImagesAreEqual(icon1, folder_image_.icon())); |
| 111 |
| 112 // Add a new item. Ensure that the observer fired and the icon changed. |
| 113 AddAppWithColoredIcon("app4", SK_ColorYELLOW); |
| 114 EXPECT_TRUE(observer_.updated()); |
| 115 observer_.Reset(); |
| 116 gfx::ImageSkia icon3 = folder_image_.icon(); |
| 117 EXPECT_FALSE(ImagesAreEqual(icon1, icon3)); |
| 118 |
| 119 // Add a new item. The observer should not fire, nor should the icon change |
| 120 // (because it does not affect the first four icons). |
| 121 AddAppWithColoredIcon("app5", SK_ColorCYAN); |
| 122 EXPECT_FALSE(observer_.updated()); |
| 123 observer_.Reset(); |
| 124 EXPECT_TRUE(ImagesAreEqual(icon3, folder_image_.icon())); |
| 125 |
| 126 // Delete an item. Ensure that the observer fired and the icon changed. |
| 127 app_list_model_.DeleteItem("app2"); |
| 128 EXPECT_TRUE(observer_.updated()); |
| 129 observer_.Reset(); |
| 130 gfx::ImageSkia icon4 = folder_image_.icon(); |
| 131 EXPECT_FALSE(ImagesAreEqual(icon3, icon4)); |
| 132 } |
| 133 |
| 134 TEST_F(FolderImageTest, UpdateItemTest) { |
| 135 gfx::ImageSkia icon1 = folder_image_.icon(); |
| 136 |
| 137 // Change an item's icon. Ensure that the observer fired and the icon changed. |
| 138 app_list_model_.FindItem("app2")->SetIcon( |
| 139 CreateSquareBitmapWithColor(kListIconSize, SK_ColorMAGENTA), false); |
| 140 EXPECT_TRUE(observer_.updated()); |
| 141 observer_.Reset(); |
| 142 EXPECT_FALSE(ImagesAreEqual(icon1, folder_image_.icon())); |
| 143 } |
| 144 |
| 145 } // namespace app_list |
OLD | NEW |