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

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

Issue 10701087: chromeos: Fix pixelated icons in app list and launcher (part 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and refactor Created 8 years, 5 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/browser/extensions/extension_icon_image_delegate.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using content::BrowserThread;
17 using extensions::Extension;
18
19 class ExtensionIconImageTest : public testing::Test,
20 public ExtensionIconImageDelegate {
21 public:
22 ExtensionIconImageTest()
23 : image_loaded_count_(0),
24 quit_in_image_loaded_(false),
25 ui_thread_(BrowserThread::UI, &ui_loop_),
26 file_thread_(BrowserThread::FILE),
27 io_thread_(BrowserThread::IO) {
28 }
29
30 void WaitForImageLoad() {
31 quit_in_image_loaded_ = true;
32 MessageLoop::current()->Run();
33 quit_in_image_loaded_ = false;
34 }
35
36 int image_loaded_count() {
37 int result = image_loaded_count_;
38 image_loaded_count_ = 0;
39 return result;
40 }
41
42 scoped_refptr<Extension> CreateExtension(const char* name,
43 Extension::Location location) {
44 // Create and load an extension.
45 FilePath test_file;
46 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) {
47 EXPECT_FALSE(true);
48 return NULL;
49 }
50 test_file = test_file.AppendASCII("extensions")
51 .AppendASCII(name);
52 int error_code = 0;
53 std::string error;
54 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json"));
55 scoped_ptr<DictionaryValue> valid_value(
56 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code,
57 &error)));
58 EXPECT_EQ(0, error_code) << error;
59 if (error_code != 0)
60 return NULL;
61
62 EXPECT_TRUE(valid_value.get());
63 if (!valid_value.get())
64 return NULL;
65
66 return Extension::Create(test_file, location, *valid_value,
67 Extension::NO_FLAGS, &error);
68 }
69
70 // testing::Test overrides:
71 virtual void SetUp() OVERRIDE {
72 file_thread_.Start();
73 io_thread_.Start();
74 }
75
76 // ExtensionIconImageDelegate overrides:
77 virtual void OnExtensionIconImageChanged(ExtensionIconImage* image) OVERRIDE {
78 image_loaded_count_++;
79 if (quit_in_image_loaded_)
80 MessageLoop::current()->Quit();
81 }
82
83 private:
84 int image_loaded_count_;
85 bool quit_in_image_loaded_;
86 MessageLoop ui_loop_;
87 content::TestBrowserThread ui_thread_;
88 content::TestBrowserThread file_thread_;
89 content::TestBrowserThread io_thread_;
90
91 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest);
92 };
93
94 TEST_F(ExtensionIconImageTest, Basic) {
95 scoped_refptr<Extension> extension(CreateExtension(
96 "image_loading_tracker", Extension::INVALID));
97 ASSERT_TRUE(extension.get() != NULL);
98
99 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage(
100 extension,
101 ExtensionIconSet::EXTENSION_ICON_BITTY,
102 ExtensionIconSet::MATCH_SMALLER,
103 gfx::Size(ExtensionIconSet::EXTENSION_ICON_BITTY,
104 ExtensionIconSet::EXTENSION_ICON_BITTY),
105 ImageLoadingTracker::DONT_CACHE,
106 this));
107
108 EXPECT_FALSE(image->IsEmpty());
109
110 // No representations in |image_| yet.
111 gfx::ImageSkia::ImageSkiaReps image_reps = image->image_skia().image_reps();
112 ASSERT_EQ(0u, image_reps.size());
113
114 // Gets representation for a scale factor.
115 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
116 WaitForImageLoad();
117 EXPECT_EQ(1, image_loaded_count());
118
119 gfx::ImageSkiaRep image_rep =
120 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
121 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY,
122 image_rep.pixel_width());
123
124 // Gets representation for an additional scale factor.
125 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
126 WaitForImageLoad();
127 EXPECT_EQ(1, image_loaded_count());
128
129 image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
130 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALL,
131 image_rep.pixel_width());
132 }
133
134 TEST_F(ExtensionIconImageTest, BadResource) {
135 scoped_refptr<Extension> extension(CreateExtension(
136 "image_loading_tracker", Extension::INVALID));
137 ASSERT_TRUE(extension.get() != NULL);
138
139 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage(
140 extension,
141 ExtensionIconSet::EXTENSION_ICON_GIGANTOR,
142 ExtensionIconSet::MATCH_BIGGER,
143 gfx::Size(ExtensionIconSet::EXTENSION_ICON_GIGANTOR,
144 ExtensionIconSet::EXTENSION_ICON_GIGANTOR),
145 ImageLoadingTracker::DONT_CACHE,
146 this));
147
148 EXPECT_TRUE(image->IsEmpty());
149 }
150
151 TEST_F(ExtensionIconImageTest, Missing2x) {
152 scoped_refptr<Extension> extension(CreateExtension(
153 "image_loading_tracker", Extension::INVALID));
154 ASSERT_TRUE(extension.get() != NULL);
155
156 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage(
157 extension,
158 ExtensionIconSet::EXTENSION_ICON_SMALLISH,
159 ExtensionIconSet::MATCH_BIGGER,
160 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
161 ExtensionIconSet::EXTENSION_ICON_SMALLISH),
162 ImageLoadingTracker::DONT_CACHE,
163 this));
164
165 // Gets representation for 1x.
166 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
167 WaitForImageLoad();
168 EXPECT_EQ(1, image_loaded_count());
169
170 gfx::ImageSkiaRep image_rep =
171 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
172 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
173 image_rep.pixel_width());
174
175 // Get representation for 2x.
176 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
177
178 image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
179
180 // 1x representation would be returned since there is no 2x resource.
181 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
182 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
183 image_rep.pixel_width());
184 }
185
186 // Similar to Missing2x test but go directly for the missing 2x resource first.
187 TEST_F(ExtensionIconImageTest, FallbackTo1x) {
188 scoped_refptr<Extension> extension(CreateExtension(
189 "image_loading_tracker", Extension::INVALID));
190 ASSERT_TRUE(extension.get() != NULL);
191
192 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage(
193 extension,
194 ExtensionIconSet::EXTENSION_ICON_SMALLISH,
195 ExtensionIconSet::MATCH_BIGGER,
196 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
197 ExtensionIconSet::EXTENSION_ICON_SMALLISH),
198 ImageLoadingTracker::DONT_CACHE,
199 this));
200
201 // Attempt to get representation for 2x.
202 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
203
204 WaitForImageLoad();
205 EXPECT_EQ(1, image_loaded_count());
206
207 // 1x representation would be returned since there is no 2x resource.
208 gfx::ImageSkiaRep image_rep =
209 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
210 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
211 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
212 image_rep.pixel_width());
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698