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

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

Powered by Google App Engine
This is Rietveld 408576698