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