Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: chrome/browser/extensions/extension_icon_image_unittest.cc

Issue 10825012: chromeos: Fix pixelated icons in app list and launcher (part 2) (by xiyuan) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/extensions/extension_icon_image.h"
6
7 #include "base/json/json_file_value_serializer.h"
8 #include "base/message_loop.h"
9 #include "base/path_service.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/extensions/extension.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using content::BrowserThread;
16 using extensions::Extension;
17 using extensions::IconImage;
18
19 namespace {
20
21 class ExtensionIconImageTest : public testing::Test,
22 public IconImage::Observer {
23 public:
24 ExtensionIconImageTest()
25 : image_loaded_count_(0),
26 image_load_failure_count_(0),
27 quit_in_image_loaded_(false),
28 ui_thread_(BrowserThread::UI, &ui_loop_),
29 file_thread_(BrowserThread::FILE),
30 io_thread_(BrowserThread::IO) {
31 }
32
33 virtual ~ExtensionIconImageTest() {}
34
35 void WaitForImageLoad() {
36 // ExtensionIconImage may return synchronously, in which case there's
37 // nothing to wait for.
38 if (image_loaded_count_ > 0 || image_load_failure_count_ > 0)
39 return;
40 quit_in_image_loaded_ = true;
41 MessageLoop::current()->Run();
42 quit_in_image_loaded_ = false;
43 }
44
45 int ImageLoadedCount() {
46 int result = image_loaded_count_;
47 image_loaded_count_ = 0;
48 return result;
49 }
50
51 int ImageLoadFailureCount() {
52 int result = image_load_failure_count_;
53 image_load_failure_count_ = 0;
54 return result;
55 }
56
57 scoped_refptr<Extension> CreateExtension(const char* name,
58 Extension::Location location) {
59 // Create and load an extension.
60 FilePath test_file;
61 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) {
62 EXPECT_FALSE(true);
63 return NULL;
64 }
65 test_file = test_file.AppendASCII("extensions").AppendASCII(name);
66 int error_code = 0;
67 std::string error;
68 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json"));
69 scoped_ptr<DictionaryValue> valid_value(
70 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code,
71 &error)));
72 EXPECT_EQ(0, error_code) << error;
73 if (error_code != 0)
74 return NULL;
75
76 EXPECT_TRUE(valid_value.get());
77 if (!valid_value.get())
78 return NULL;
79
80 return Extension::Create(test_file, location, *valid_value,
81 Extension::NO_FLAGS, &error);
82 }
83
84 // testing::Test overrides:
85 virtual void SetUp() OVERRIDE {
86 file_thread_.Start();
87 io_thread_.Start();
88 }
89
90 // IconImage::Delegate overrides:
91 virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE {
92 image_loaded_count_++;
93 if (quit_in_image_loaded_)
94 MessageLoop::current()->Quit();
95 }
96
97 virtual void OnIconImageLoadFailed(IconImage* image,
98 ui::ScaleFactor scale_factor) OVERRIDE {
99 image_load_failure_count_++;
100 if (quit_in_image_loaded_)
101 MessageLoop::current()->Quit();
102 }
103
104 private:
105 int image_loaded_count_;
106 int image_load_failure_count_;
107 bool quit_in_image_loaded_;
108 MessageLoop ui_loop_;
109 content::TestBrowserThread ui_thread_;
110 content::TestBrowserThread file_thread_;
111 content::TestBrowserThread io_thread_;
112
113 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest);
114 };
115
116 } // namespace
117
118 TEST_F(ExtensionIconImageTest, Basic) {
119 scoped_refptr<Extension> extension(CreateExtension(
120 "extension_icon_image", Extension::INVALID));
121 ASSERT_TRUE(extension.get() != NULL);
122
123 IconImage image(
124 extension,
125 extension->icons(),
126 extension_misc::EXTENSION_ICON_BITTY,
127 this);
128
129 // No representations in |image_| yet.
130 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_skia().image_reps();
131 ASSERT_EQ(0u, image_reps.size());
132
133 // Gets representation for a scale factor.
134 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
135 WaitForImageLoad();
136 EXPECT_EQ(1, ImageLoadedCount());
137 EXPECT_EQ(0, ImageLoadFailureCount());
138
139 // Gets representation for an additional scale factor.
140 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
141 WaitForImageLoad();
142 EXPECT_EQ(1, ImageLoadedCount());
143 EXPECT_EQ(0, ImageLoadFailureCount());
144
145 gfx::ImageSkiaRep image_rep =
146 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
147 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
148 image_rep.pixel_width());
149
150 image_rep = image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
151 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALL,
152 image_rep.pixel_width());
153 }
154
155 // If we can't load icon with the exact size, but a bigger resource is
156 // available.
157 TEST_F(ExtensionIconImageTest, FallbackToBigger) {
158 scoped_refptr<Extension> extension(CreateExtension(
159 "extension_icon_image", Extension::INVALID));
160 ASSERT_TRUE(extension.get() != NULL);
161
162 IconImage image(
163 extension,
164 extension->icons(),
165 extension_misc::EXTENSION_ICON_BITTY,
166 this);
167
168 // Get representation for 2x.
169 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
170
171 WaitForImageLoad();
172 EXPECT_EQ(1, ImageLoadedCount());
173 EXPECT_EQ(0, ImageLoadFailureCount());
174
175 gfx::ImageSkiaRep image_rep =
176 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
177
178 // We should have found a bigger resource and it should have been resized.
179 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
180 EXPECT_EQ(2 * extension_misc::EXTENSION_ICON_BITTY,
181 image_rep.pixel_width());
182 }
183
184 // There is no resource with either exact or bigger size, but there is a smaller
185 // resource.
186 TEST_F(ExtensionIconImageTest, FallbackToSmallerWhenNoBigger) {
187 scoped_refptr<Extension> extension(CreateExtension(
188 "extension_icon_image", Extension::INVALID));
189 ASSERT_TRUE(extension.get() != NULL);
190
191 IconImage image(
192 extension,
193 extension->icons(),
194 extension_misc::EXTENSION_ICON_SMALL,
195 this);
196
197 // Attempt to get representation for 2x.
198 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
199
200 WaitForImageLoad();
201 EXPECT_EQ(1, ImageLoadedCount());
202 EXPECT_EQ(0, ImageLoadFailureCount());
203
204 gfx::ImageSkiaRep image_rep =
205 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
206
207 // We should have loaded the biggest smaller resource. In this case the
208 // loaded resource should not be resized.
209 EXPECT_EQ(ui::SCALE_FACTOR_200P, image_rep.scale_factor());
210 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM,
211 image_rep.pixel_width());
212 }
213
214 // There is no resource with exact size, but there is a smaller and a bigger
215 // one. Requested size is smaller than 32 though, so the smaller resource should
216 // be loaded.
217 TEST_F(ExtensionIconImageTest, FallbackToSmaller) {
218 scoped_refptr<Extension> extension(CreateExtension(
219 "extension_icon_image", Extension::INVALID));
220 ASSERT_TRUE(extension.get() != NULL);
221
222 IconImage image(
223 extension,
224 extension->icons(),
225 17,
226 this);
227
228 // Attempt to get representation for 1x.
229 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
230
231 WaitForImageLoad();
232 EXPECT_EQ(1, ImageLoadedCount());
233 EXPECT_EQ(0, ImageLoadFailureCount());
234
235 gfx::ImageSkiaRep image_rep =
236 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
237
238 // We should have loaded smaller (not resized) resource.
239 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
240 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
241 image_rep.pixel_width());
242 }
243
244 // If resource set is empty, failure should be reported.
245 TEST_F(ExtensionIconImageTest, NoResources) {
246 scoped_refptr<Extension> extension(CreateExtension(
247 "extension_icon_image", Extension::INVALID));
248 ASSERT_TRUE(extension.get() != NULL);
249
250 ExtensionIconSet empty_icon_set;
251
252 IconImage image(
253 extension,
254 empty_icon_set,
255 extension_misc::EXTENSION_ICON_SMALLISH,
256 this);
257
258 // Attempt to get representation for 2x.
259 image.image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
260
261 WaitForImageLoad();
262 EXPECT_EQ(0, ImageLoadedCount());
263 EXPECT_EQ(1, ImageLoadFailureCount());
264 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_icon_image.cc ('k') | chrome/browser/extensions/image_loading_tracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698