[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / table_row_nsimage_cache_unittest.mm
blobf27e9351efa88fdea91f06fe5406bc7e3883b051
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"
11 namespace {
13 class TestTable : public TableRowNSImageCache::Table {
14  public:
16   std::vector<SkBitmap>* rows() {
17     return &rows_;
18   }
20   // TableRowNSImageCache::Table overrides.
21   virtual int RowCount() const OVERRIDE {
22     return rows_.size();
23   }
24   virtual gfx::ImageSkia GetIcon(int index) const OVERRIDE {
25     return gfx::ImageSkia::CreateFrom1xBitmap(rows_[index]);
26   }
28  private:
29   std::vector<SkBitmap> rows_;
32 SkBitmap MakeImage(int width, int height) {
33   SkBitmap image;
34   image.setConfig(SkBitmap::kARGB_8888_Config, width, height);
35   EXPECT_TRUE(image.allocPixels());
36   image.eraseARGB(255, 255, 0, 0);
37   return image;
40 // Define this as a macro so that the original variable names can be used in
41 // test output.
42 #define COMPARE_SK_NS_IMG_SIZES(skia, cocoa) \
43   EXPECT_EQ(skia.width(), [cocoa size].width); \
44   EXPECT_EQ(skia.height(), [cocoa size].height);
46 TEST(TableRowNSImageCacheTest, ModelChanged) {
47   TestTable table;
48   std::vector<SkBitmap>* rows = table.rows();
49   rows->push_back(MakeImage(10, 10));
50   rows->push_back(MakeImage(20, 20));
51   rows->push_back(MakeImage(30, 30));
52   TableRowNSImageCache cache(&table);
54   NSImage* image0 = cache.GetImageForRow(0);
55   NSImage* image1 = cache.GetImageForRow(1);
56   NSImage* image2 = cache.GetImageForRow(2);
58   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
59   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
60   COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
62   rows->clear();
64   rows->push_back(MakeImage(15, 15));
65   rows->push_back(MakeImage(25, 25));
66   rows->push_back(MakeImage(35, 35));
67   rows->push_back(MakeImage(45, 45));
69   // Invalidate the entire model.
70   cache.OnModelChanged();
72   EXPECT_NE(image0, cache.GetImageForRow(0));
73   image0 = cache.GetImageForRow(0);
75   EXPECT_NE(image1, cache.GetImageForRow(1));
76   image1 = cache.GetImageForRow(1);
78   EXPECT_NE(image2, cache.GetImageForRow(2));
79   image2 = cache.GetImageForRow(2);
81   NSImage* image3 = cache.GetImageForRow(3);
83   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
84   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
85   COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
86   COMPARE_SK_NS_IMG_SIZES(rows->at(3), image3);
90 TEST(TableRowNSImageCacheTest, ItemsChanged) {
91   TestTable table;
92   std::vector<SkBitmap>* rows = table.rows();
93   rows->push_back(MakeImage(10, 10));
94   rows->push_back(MakeImage(20, 20));
95   rows->push_back(MakeImage(30, 30));
96   TableRowNSImageCache cache(&table);
98   NSImage* image0 = cache.GetImageForRow(0);
99   NSImage* image1 = cache.GetImageForRow(1);
100   NSImage* image2 = cache.GetImageForRow(2);
102   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
103   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
104   COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
106   // Update the middle image.
107   (*rows)[1] = MakeImage(25, 25);
108   cache.OnItemsChanged(/* start=*/1, /* count=*/1);
110   // Make sure the other items remained the same.
111   EXPECT_EQ(image0, cache.GetImageForRow(0));
112   EXPECT_EQ(image2, cache.GetImageForRow(2));
114   image1 = cache.GetImageForRow(1);
115   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
117   // Update more than one image.
118   (*rows)[0] = MakeImage(15, 15);
119   (*rows)[1] = MakeImage(27, 27);
120   EXPECT_EQ(3U, rows->size());
121   cache.OnItemsChanged(0, 2);
123   image0 = cache.GetImageForRow(0);
124   image1 = cache.GetImageForRow(1);
126   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
127   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
131 TEST(TableRowNSImageCacheTest, ItemsAdded) {
132   TestTable table;
133   std::vector<SkBitmap>* rows = table.rows();
134   rows->push_back(MakeImage(10, 10));
135   rows->push_back(MakeImage(20, 20));
136   TableRowNSImageCache cache(&table);
138   NSImage* image0 = cache.GetImageForRow(0);
139   NSImage* image1 = cache.GetImageForRow(1);
141   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
142   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
144   // Add an item to the end.
145   rows->push_back(MakeImage(30, 30));
146   cache.OnItemsAdded(2, 1);
148   // Make sure image 1 stayed the same.
149   EXPECT_EQ(image1, cache.GetImageForRow(1));
150   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
152   // Check that image 2 got added correctly.
153   NSImage* image2 = cache.GetImageForRow(2);
154   COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
156   // Add two items to the begging.
157   rows->insert(rows->begin(), MakeImage(7, 7));
158   rows->insert(rows->begin(), MakeImage(3, 3));
159   cache.OnItemsAdded(0, 2);
161   NSImage* image00 = cache.GetImageForRow(0);
162   NSImage* image01 = cache.GetImageForRow(1);
164   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image00);
165   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image01);
169 TEST(TableRowNSImageCacheTest, ItemsRemoved) {
170   TestTable table;
171   std::vector<SkBitmap>* rows = table.rows();
172   rows->push_back(MakeImage(10, 10));
173   rows->push_back(MakeImage(20, 20));
174   rows->push_back(MakeImage(30, 30));
175   rows->push_back(MakeImage(40, 40));
176   rows->push_back(MakeImage(50, 50));
177   TableRowNSImageCache cache(&table);
179   NSImage* image0 = cache.GetImageForRow(0);
180   NSImage* image1 = cache.GetImageForRow(1);
181   NSImage* image2 = cache.GetImageForRow(2);
182   NSImage* image3 = cache.GetImageForRow(3);
183   NSImage* image4 = cache.GetImageForRow(4);
185   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
186   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
187   COMPARE_SK_NS_IMG_SIZES(rows->at(2), image2);
188   COMPARE_SK_NS_IMG_SIZES(rows->at(3), image3);
189   COMPARE_SK_NS_IMG_SIZES(rows->at(4), image4);
191   rows->erase(rows->begin() + 1, rows->begin() + 4);  // [20x20, 50x50)
192   cache.OnItemsRemoved(1, 3);
194   image0 = cache.GetImageForRow(0);
195   image1 = cache.GetImageForRow(1);
197   COMPARE_SK_NS_IMG_SIZES(rows->at(0), image0);
198   COMPARE_SK_NS_IMG_SIZES(rows->at(1), image1);
201 }  // namespace