1 // Copyright (c) 2012 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 "chrome/browser/ui/cocoa/table_row_nsimage_cache.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkBitmap.h"
9 #include "ui/gfx/image/image_skia.h"
13 class TestTable : public TableRowNSImageCache::Table {
16 std::vector<SkBitmap>* rows() {
20 // TableRowNSImageCache::Table overrides.
21 int RowCount() const override { return rows_.size(); }
22 gfx::ImageSkia GetIcon(int index) const override {
23 return gfx::ImageSkia::CreateFrom1xBitmap(rows_[index]);
27 std::vector<SkBitmap> rows_;
30 SkBitmap MakeImage(int width, int height) {
32 image.allocN32Pixels(width, height);
33 image.eraseARGB(255, 255, 0, 0);
37 // Define this as a macro so that the original variable names can be used in
39 #define COMPARE_SK_NS_IMG_SIZES(skia, cocoa) \
40 EXPECT_EQ(skia.width(), [cocoa size].width); \
41 EXPECT_EQ(skia.height(), [cocoa size].height);
43 TEST(TableRowNSImageCacheTest, ModelChanged) {
45 std::vector<SkBitmap>* rows = table.rows();
46 rows->push_back(MakeImage(10, 10));
47 rows->push_back(MakeImage(20, 20));
48 rows->push_back(MakeImage(30, 30));
49 TableRowNSImageCache cache(&table);
51 NSImage* image0 = cache.GetImageForRow(0);
52 NSImage* image1 = cache.GetImageForRow(1);
53 NSImage* image2 = cache.GetImageForRow(2);
55 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
56 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
57 COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
61 rows->push_back(MakeImage(15, 15));
62 rows->push_back(MakeImage(25, 25));
63 rows->push_back(MakeImage(35, 35));
64 rows->push_back(MakeImage(45, 45));
66 // Invalidate the entire model.
67 cache.OnModelChanged();
69 EXPECT_NE(image0, cache.GetImageForRow(0));
70 image0 = cache.GetImageForRow(0);
72 EXPECT_NE(image1, cache.GetImageForRow(1));
73 image1 = cache.GetImageForRow(1);
75 EXPECT_NE(image2, cache.GetImageForRow(2));
76 image2 = cache.GetImageForRow(2);
78 NSImage* image3 = cache.GetImageForRow(3);
80 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
81 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
82 COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
83 COMPARE_SK_NS_IMG_SIZES(rows->at(3), image3);
87 TEST(TableRowNSImageCacheTest, ItemsChanged) {
89 std::vector<SkBitmap>* rows = table.rows();
90 rows->push_back(MakeImage(10, 10));
91 rows->push_back(MakeImage(20, 20));
92 rows->push_back(MakeImage(30, 30));
93 TableRowNSImageCache cache(&table);
95 NSImage* image0 = cache.GetImageForRow(0);
96 NSImage* image1 = cache.GetImageForRow(1);
97 NSImage* image2 = cache.GetImageForRow(2);
99 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
100 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
101 COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
103 // Update the middle image.
104 (*rows)[1] = MakeImage(25, 25);
105 cache.OnItemsChanged(/* start=*/1, /* count=*/1);
107 // Make sure the other items remained the same.
108 EXPECT_EQ(image0, cache.GetImageForRow(0));
109 EXPECT_EQ(image2, cache.GetImageForRow(2));
111 image1 = cache.GetImageForRow(1);
112 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
114 // Update more than one image.
115 (*rows)[0] = MakeImage(15, 15);
116 (*rows)[1] = MakeImage(27, 27);
117 EXPECT_EQ(3U, rows->size());
118 cache.OnItemsChanged(0, 2);
120 image0 = cache.GetImageForRow(0);
121 image1 = cache.GetImageForRow(1);
123 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
124 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
128 TEST(TableRowNSImageCacheTest, ItemsAdded) {
130 std::vector<SkBitmap>* rows = table.rows();
131 rows->push_back(MakeImage(10, 10));
132 rows->push_back(MakeImage(20, 20));
133 TableRowNSImageCache cache(&table);
135 NSImage* image0 = cache.GetImageForRow(0);
136 NSImage* image1 = cache.GetImageForRow(1);
138 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
139 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
141 // Add an item to the end.
142 rows->push_back(MakeImage(30, 30));
143 cache.OnItemsAdded(2, 1);
145 // Make sure image 1 stayed the same.
146 EXPECT_EQ(image1, cache.GetImageForRow(1));
147 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
149 // Check that image 2 got added correctly.
150 NSImage* image2 = cache.GetImageForRow(2);
151 COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
153 // Add two items to the begging.
154 rows->insert(rows->begin(), MakeImage(7, 7));
155 rows->insert(rows->begin(), MakeImage(3, 3));
156 cache.OnItemsAdded(0, 2);
158 NSImage* image00 = cache.GetImageForRow(0);
159 NSImage* image01 = cache.GetImageForRow(1);
161 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image00);
162 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image01);
166 TEST(TableRowNSImageCacheTest, ItemsRemoved) {
168 std::vector<SkBitmap>* rows = table.rows();
169 rows->push_back(MakeImage(10, 10));
170 rows->push_back(MakeImage(20, 20));
171 rows->push_back(MakeImage(30, 30));
172 rows->push_back(MakeImage(40, 40));
173 rows->push_back(MakeImage(50, 50));
174 TableRowNSImageCache cache(&table);
176 NSImage* image0 = cache.GetImageForRow(0);
177 NSImage* image1 = cache.GetImageForRow(1);
178 NSImage* image2 = cache.GetImageForRow(2);
179 NSImage* image3 = cache.GetImageForRow(3);
180 NSImage* image4 = cache.GetImageForRow(4);
182 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
183 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
184 COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
185 COMPARE_SK_NS_IMG_SIZES(rows->at(3), image3);
186 COMPARE_SK_NS_IMG_SIZES(rows->at(4), image4);
188 rows->erase(rows->begin() + 1, rows->begin() + 4); // [20x20, 50x50)
189 cache.OnItemsRemoved(1, 3);
191 image0 = cache.GetImageForRow(0);
192 image1 = cache.GetImageForRow(1);
194 COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
195 COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);