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

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

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: Xiyuan's comments, extra unit tests + fix one more issue with badging Created 3 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2017 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 <memory>
6
7 #include "base/macros.h"
8 #include "chrome/browser/extensions/extension_app_icon.h"
9 #include "chrome/browser/extensions/extension_app_icon_delegate.h"
10 #include "chrome/browser/extensions/extension_app_icon_loader.h"
11 #include "chrome/browser/extensions/extension_app_icon_service.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/app_icon_loader_delegate.h"
15 #include "chrome/browser/ui/app_list/app_list_test_util.h"
16 #include "chrome/browser/ui/app_list/extension_app_model_builder.h"
17 #include "chrome/browser/ui/app_list/search/extension_app_result.h"
18 #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "extensions/common/constants.h"
21 #include "ui/app_list/app_list_item.h"
22
23 #if defined(OS_CHROMEOS)
24 #include "chrome/browser/chromeos/arc/arc_util.h"
25 #include "chrome/browser/ui/app_list/arc/arc_app_test.h"
26 #include "components/arc/test/fake_app_instance.h"
27 #endif
28
29 namespace extensions {
30
31 namespace {
32
33 // Receives icon image updates from ExtensionAppIcon.
34 class TestAppIcon : public ExtensionAppIconDelegate {
35 public:
36 TestAppIcon(content::BrowserContext* context,
37 const std::string& app_id,
38 int size) {
39 app_icon_ =
40 ExtensionAppIconService::Get(context)->CreateIcon(this, app_id, size);
41 DCHECK(app_icon_);
42 }
43
44 ~TestAppIcon() override = default;
45
46 int icon_update_count() const { return icon_update_count_; }
47 ExtensionAppIcon* app_icon() { return app_icon_.get(); }
48 const gfx::ImageSkia& image_skia() const { return app_icon_->image_skia(); }
49
50 void WaitIconUpdated() {
51 base::RunLoop run_loop;
52 icon_updated_callback_ = run_loop.QuitClosure();
53 run_loop.Run();
54 icon_updated_callback_.Reset();
55 }
56
57 private:
58 void OnIconUpdated(ExtensionAppIcon* icon) override {
59 ++icon_update_count_;
60 if (!icon_updated_callback_.is_null())
61 icon_updated_callback_.Run();
62 }
63
64 std::unique_ptr<ExtensionAppIcon> app_icon_;
65
66 int icon_update_count_ = 0;
67
68 base::Closure icon_updated_callback_;
69
70 DISALLOW_COPY_AND_ASSIGN(TestAppIcon);
71 };
72
73 // Receives icon image updates from ExtensionAppIconLoader.
74 class TestAppIconLoader : public AppIconLoaderDelegate {
75 public:
76 TestAppIconLoader() = default;
77 ~TestAppIconLoader() override = default;
78
79 // AppIconLoaderDelegate:
80 void OnAppImageUpdated(const std::string& app_id,
81 const gfx::ImageSkia& image) override {
82 image_skia_ = image;
83 }
84
85 const gfx::ImageSkia& image_skia() { return image_skia_; }
86
87 private:
88 gfx::ImageSkia image_skia_;
89
90 DISALLOW_COPY_AND_ASSIGN(TestAppIconLoader);
91 };
92
93 // Returns true if provided |image| consists from only empty pixels.
94 bool IsBlankImage(const gfx::ImageSkia& image) {
95 if (!image.width() || !image.height())
96 return false;
97
98 const SkBitmap* bitmap = image.bitmap();
99 DCHECK(bitmap);
100 DCHECK_EQ(bitmap->width(), image.width());
101 DCHECK_EQ(bitmap->height(), image.height());
102
103 for (int x = 0; x < bitmap->width(); ++x) {
104 for (int y = 0; y < bitmap->height(); ++y) {
105 if (*bitmap->getAddr32(x, y))
106 return false;
107 }
108 }
109 return true;
110 }
111
112 // Returns true if provided |image| is grayscale.
113 bool IsGrayscaleImage(const gfx::ImageSkia& image) {
114 const SkBitmap* bitmap = image.bitmap();
115 DCHECK(bitmap);
116 for (int x = 0; x < bitmap->width(); ++x) {
117 for (int y = 0; y < bitmap->height(); ++y) {
118 const unsigned int pixel = *bitmap->getAddr32(x, y);
119 if ((pixel & 0xff) != ((pixel >> 8) & 0xff) ||
120 (pixel & 0xff) != ((pixel >> 16) & 0xff)) {
121 return false;
122 }
123 }
124 }
125 return true;
126 }
127
128 // Returns true if provided |image1| and |image2| are equal.
129 bool AreEqual(const gfx::ImageSkia& image1, const gfx::ImageSkia& image2) {
130 const SkBitmap* bitmap1 = image1.bitmap();
131 DCHECK(bitmap1);
132 const SkBitmap* bitmap2 = image2.bitmap();
133 DCHECK(bitmap2);
134
135 if (bitmap1->width() != bitmap2->width() ||
136 bitmap1->height() != bitmap2->height()) {
137 return false;
138 }
139
140 for (int x = 0; x < bitmap1->width(); ++x) {
141 for (int y = 0; y < bitmap2->height(); ++y) {
142 if (*bitmap1->getAddr32(x, y) != *bitmap2->getAddr32(x, y))
143 return false;
144 }
145 }
146 return true;
147 }
148
149 } // namespace
150
151 class ExtensionAppIconTest : public AppListTestBase {
152 public:
153 ExtensionAppIconTest() = default;
154 ~ExtensionAppIconTest() override = default;
155
156 protected:
157 void CreateBuilder() {
158 model_.reset(new app_list::AppListModel);
159 controller_.reset(new test::TestAppListControllerDelegate);
160 builder_.reset(new ExtensionAppModelBuilder(controller_.get()));
161 builder_->InitializeWithProfile(profile(), model_.get());
162 }
163
164 void ResetBuilder() {
165 builder_.reset();
166 controller_.reset();
167 model_.reset();
168 }
169
170 app_list::AppListItem* FindAppListItem(const std::string& app_id) {
171 return model_->FindItem(app_id);
172 }
173
174 test::TestAppListControllerDelegate* app_list_controller() {
175 return controller_.get();
176 }
177
178 private:
179 std::unique_ptr<app_list::AppListModel> model_;
180 std::unique_ptr<test::TestAppListControllerDelegate> controller_;
181 std::unique_ptr<ExtensionAppModelBuilder> builder_;
182
183 DISALLOW_COPY_AND_ASSIGN(ExtensionAppIconTest);
184 };
185
186 TEST_F(ExtensionAppIconTest, IconLifeCycle) {
187 TestAppIcon reference_icon(profile(), AppListTestBase::kPackagedApp1Id,
188 extension_misc::EXTENSION_ICON_MEDIUM);
189 EXPECT_EQ(1, reference_icon.icon_update_count());
190 // By default no representation in image.
191 EXPECT_FALSE(reference_icon.image_skia().HasRepresentation(1.0f));
192
193 // Accessing to bitmap requests default representations.
194 // Defualt blank image must be provided without an update.
195 EXPECT_TRUE(reference_icon.image_skia().bitmap());
196 EXPECT_EQ(1, reference_icon.icon_update_count());
197 EXPECT_TRUE(reference_icon.image_skia().HasRepresentation(1.0f));
198 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM,
199 reference_icon.image_skia().width());
200 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM,
201 reference_icon.image_skia().height());
202 EXPECT_TRUE(IsBlankImage(reference_icon.image_skia()));
203
204 // Wait until real image is loaded.
205 reference_icon.WaitIconUpdated();
206 EXPECT_EQ(2, reference_icon.icon_update_count());
207 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia()));
208 EXPECT_FALSE(IsGrayscaleImage(reference_icon.image_skia()));
209
210 const gfx::ImageSkia image_before_disable = reference_icon.image_skia();
211 // Disable extension. This should update icon and provide grayed image inline.
212 // Note update might be sent twice in CrOS.
213 service()->DisableExtension(AppListTestBase::kPackagedApp1Id,
214 Extension::DISABLE_CORRUPTED);
215 const int update_count_after_disable = reference_icon.icon_update_count();
216 EXPECT_NE(2, update_count_after_disable);
217 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia()));
218 EXPECT_TRUE(IsGrayscaleImage(reference_icon.image_skia()));
219
220 // Reenable extension. It should match previous enabled image
221 service()->EnableExtension(AppListTestBase::kPackagedApp1Id);
222 EXPECT_NE(update_count_after_disable, reference_icon.icon_update_count());
223 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_disable));
224 }
225
226 // Validates that icons are the same for different consumers.
227 TEST_F(ExtensionAppIconTest, IconsTheSame) {
228 CreateBuilder();
229
230 TestAppIcon reference_icon(profile(), AppListTestBase::kPackagedApp1Id,
231 extension_misc::EXTENSION_ICON_MEDIUM);
232
233 // Wait until reference data is loaded.
234 EXPECT_TRUE(reference_icon.image_skia().bitmap());
235 while (IsBlankImage(reference_icon.image_skia()))
236 base::RunLoop().RunUntilIdle();
237
238 app_list::ExtensionAppResult search(
239 profile(), AppListTestBase::kPackagedApp1Id, app_list_controller(), true);
240 while (!AreEqual(reference_icon.image_skia(), search.icon()))
241 base::RunLoop().RunUntilIdle();
242
243 app_list::AppListItem* app_list_item =
244 FindAppListItem(AppListTestBase::kPackagedApp1Id);
245 ASSERT_TRUE(app_list_item);
246 while (!AreEqual(reference_icon.image_skia(), app_list_item->icon()))
247 base::RunLoop().RunUntilIdle();
248
249 TestAppIconLoader loader_delegate;
250 ExtensionAppIconLoader loader(
251 profile(), extension_misc::EXTENSION_ICON_MEDIUM, &loader_delegate);
252 loader.FetchImage(AppListTestBase::kPackagedApp1Id);
253 while (!AreEqual(reference_icon.image_skia(), loader_delegate.image_skia()))
254 base::RunLoop().RunUntilIdle();
255
256 ResetBuilder();
257 }
258
259 #if defined(OS_CHROMEOS)
260 TEST_F(ExtensionAppIconTest, ChromeBadging) {
261 ArcAppTest arc_test;
262 arc_test.SetUp(profile());
263
264 TestAppIcon reference_icon(profile(), AppListTestBase::kPackagedApp1Id,
265 extension_misc::EXTENSION_ICON_MEDIUM);
266 // Wait until reference data is loaded.
267 EXPECT_TRUE(reference_icon.image_skia().bitmap());
268 while (IsBlankImage(reference_icon.image_skia()))
269 base::RunLoop().RunUntilIdle();
270
271 const int icon_update_count_before_badging =
272 reference_icon.icon_update_count();
273 const gfx::ImageSkia image_before_badging = reference_icon.image_skia();
274
275 // Badging should be applied once package is installed.
276 arc_test.app_instance()->RefreshAppList();
277 std::vector<arc::mojom::AppInfo> fake_apps = arc_test.fake_apps();
278 fake_apps[0].package_name = arc_test.fake_packages()[0].package_name;
279 arc_test.app_instance()->SendRefreshAppList(fake_apps);
280 arc_test.app_instance()->SendRefreshPackageList(arc_test.fake_packages());
281 EXPECT_EQ(icon_update_count_before_badging + 1,
282 reference_icon.icon_update_count());
283 EXPECT_FALSE(AreEqual(reference_icon.image_skia(), image_before_badging));
284
285 // Opts out the Play Store. Badge should be gone and icon image is the same
286 // as it was before badging.
287 arc::SetArcPlayStoreEnabledForProfile(profile(), false);
288 EXPECT_EQ(icon_update_count_before_badging + 2,
289 reference_icon.icon_update_count());
290 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_badging));
291 }
292 #endif
293
294 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698