Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/chrome_app_icon.h" | |
| 9 #include "chrome/browser/extensions/chrome_app_icon_delegate.h" | |
| 10 #include "chrome/browser/extensions/chrome_app_icon_loader.h" | |
| 11 #include "chrome/browser/extensions/chrome_app_icon_service.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_service_test_base.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/browser/ui/app_icon_loader_delegate.h" | |
| 16 #include "chrome/common/chrome_constants.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "extensions/common/constants.h" | |
| 19 #include "ui/app_list/app_list_item.h" | |
| 20 #include "ui/gfx/image/image_unittest_util.h" | |
| 21 | |
| 22 #if defined(OS_CHROMEOS) | |
| 23 #include "chrome/browser/chromeos/arc/arc_util.h" | |
| 24 #include "chrome/browser/ui/app_list/arc/arc_app_test.h" | |
| 25 #include "chrome/browser/ui/app_list/extension_app_model_builder.h" | |
| 26 #include "chrome/browser/ui/app_list/search/extension_app_result.h" | |
| 27 #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h" | |
| 28 #include "components/arc/test/fake_app_instance.h" | |
| 29 #endif // defined(OS_CHROMEOS) | |
| 30 | |
| 31 namespace extensions { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 constexpr char kTestAppId[] = "emfkafnhnpcmabnnkckkchdilgeoekbo"; | |
| 36 | |
| 37 // Receives icon image updates from ChromeAppIcon. | |
| 38 class TestAppIcon : public ChromeAppIconDelegate { | |
| 39 public: | |
| 40 TestAppIcon(content::BrowserContext* context, | |
| 41 const std::string& app_id, | |
| 42 int size) { | |
| 43 app_icon_ = | |
| 44 ChromeAppIconService::Get(context)->CreateIcon(this, app_id, size); | |
| 45 DCHECK(app_icon_); | |
| 46 } | |
| 47 | |
| 48 ~TestAppIcon() override = default; | |
| 49 | |
| 50 void Reset() { app_icon_.reset(); } | |
| 51 | |
| 52 int GetIconUpdateCountAndReset() { | |
| 53 int icon_update_count = icon_update_count_; | |
| 54 icon_update_count_ = 0; | |
| 55 return icon_update_count; | |
| 56 } | |
| 57 | |
| 58 size_t icon_update_count() const { return icon_update_count_; } | |
| 59 ChromeAppIcon* app_icon() { return app_icon_.get(); } | |
| 60 const gfx::ImageSkia& image_skia() const { return app_icon_->image_skia(); } | |
| 61 | |
| 62 void WaitForIconUpdates() { | |
| 63 base::RunLoop run_loop; | |
| 64 icon_update_count_expected_ = | |
| 65 icon_update_count_ + image_skia().image_reps().size(); | |
| 66 icon_updated_callback_ = run_loop.QuitClosure(); | |
| 67 run_loop.Run(); | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 void OnIconUpdated(ChromeAppIcon* icon) override { | |
| 72 ++icon_update_count_; | |
| 73 if ((icon_update_count_ == icon_update_count_expected_) && | |
|
msw
2017/05/11 17:00:31
I meant that the parentheses "()" are not needed a
khmel
2017/05/11 17:47:46
Sorry, misunderstood you.
| |
| 74 (!icon_updated_callback_.is_null())) | |
| 75 base::ResetAndReturn(&icon_updated_callback_).Run(); | |
| 76 } | |
| 77 | |
| 78 std::unique_ptr<ChromeAppIcon> app_icon_; | |
| 79 | |
| 80 size_t icon_update_count_ = 0; | |
| 81 size_t icon_update_count_expected_ = 0; | |
| 82 | |
| 83 base::OnceClosure icon_updated_callback_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(TestAppIcon); | |
| 86 }; | |
| 87 | |
| 88 // Receives icon image updates from ChromeAppIconLoader. | |
| 89 class TestAppIconLoader : public AppIconLoaderDelegate { | |
| 90 public: | |
| 91 TestAppIconLoader() = default; | |
| 92 ~TestAppIconLoader() override = default; | |
| 93 | |
| 94 // AppIconLoaderDelegate: | |
| 95 void OnAppImageUpdated(const std::string& app_id, | |
| 96 const gfx::ImageSkia& image) override { | |
| 97 image_skia_ = image; | |
| 98 } | |
| 99 | |
| 100 gfx::ImageSkia& icon() { return image_skia_; } | |
| 101 const gfx::ImageSkia& icon() const { return image_skia_; } | |
| 102 | |
| 103 private: | |
| 104 gfx::ImageSkia image_skia_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(TestAppIconLoader); | |
| 107 }; | |
| 108 | |
| 109 // Returns true if provided |image| consists from only empty pixels. | |
| 110 bool IsBlankImage(const gfx::ImageSkia& image) { | |
| 111 if (!image.width() || !image.height()) | |
| 112 return false; | |
| 113 | |
| 114 const SkBitmap* bitmap = image.bitmap(); | |
| 115 DCHECK(bitmap); | |
| 116 DCHECK_EQ(bitmap->width(), image.width()); | |
| 117 DCHECK_EQ(bitmap->height(), image.height()); | |
| 118 | |
| 119 for (int x = 0; x < bitmap->width(); ++x) { | |
| 120 for (int y = 0; y < bitmap->height(); ++y) { | |
| 121 if (*bitmap->getAddr32(x, y)) | |
| 122 return false; | |
| 123 } | |
| 124 } | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 // Returns true if provided |image| is grayscale. | |
| 129 bool IsGrayscaleImage(const gfx::ImageSkia& image) { | |
| 130 const SkBitmap* bitmap = image.bitmap(); | |
| 131 DCHECK(bitmap); | |
| 132 for (int x = 0; x < bitmap->width(); ++x) { | |
| 133 for (int y = 0; y < bitmap->height(); ++y) { | |
| 134 const unsigned int pixel = *bitmap->getAddr32(x, y); | |
| 135 if ((pixel & 0xff) != ((pixel >> 8) & 0xff) || | |
| 136 (pixel & 0xff) != ((pixel >> 16) & 0xff)) { | |
| 137 return false; | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 // Returns true if provided |image1| and |image2| are equal. | |
| 145 bool AreEqual(const gfx::ImageSkia& image1, const gfx::ImageSkia& image2) { | |
| 146 return gfx::test::AreImagesEqual(gfx::Image(image1), gfx::Image(image2)); | |
| 147 } | |
| 148 | |
| 149 #if defined(OS_CHROMEOS) | |
| 150 | |
| 151 bool AreAllImageRepresentationsDifferent(const gfx::ImageSkia& image1, | |
| 152 const gfx::ImageSkia& image2) { | |
| 153 const std::vector<gfx::ImageSkiaRep> image1_reps = image1.image_reps(); | |
| 154 const std::vector<gfx::ImageSkiaRep> image2_reps = image2.image_reps(); | |
| 155 DCHECK_EQ(image1_reps.size(), image2_reps.size()); | |
| 156 for (size_t i = 0; i < image1_reps.size(); ++i) { | |
| 157 const float scale = image1_reps[i].scale(); | |
| 158 const gfx::ImageSkiaRep& image_rep2 = image2.GetRepresentation(scale); | |
| 159 if (gfx::test::AreBitmapsClose(image1_reps[i].sk_bitmap(), | |
| 160 image_rep2.sk_bitmap(), 0)) { | |
| 161 return false; | |
| 162 } | |
| 163 } | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 // Waits until icon content of the item is changed for all representations. | |
| 168 template <typename T> | |
| 169 void WaitForIconUpdates(const T& item) { | |
| 170 item.icon().EnsureRepsForSupportedScales(); | |
| 171 std::unique_ptr<gfx::ImageSkia> reference_image = item.icon().DeepCopy(); | |
| 172 while (!AreAllImageRepresentationsDifferent(*reference_image, item.icon())) | |
| 173 base::RunLoop().RunUntilIdle(); | |
| 174 } | |
| 175 | |
| 176 #endif // defined(OS_CHROMEOS) | |
| 177 | |
| 178 } // namespace | |
| 179 | |
| 180 class ChromeAppIconTest : public ExtensionServiceTestBase { | |
| 181 public: | |
| 182 ChromeAppIconTest() = default; | |
| 183 ~ChromeAppIconTest() override = default; | |
| 184 | |
| 185 // ExtensionServiceTestBase: | |
| 186 void SetUp() override { | |
| 187 ExtensionServiceTestBase::SetUp(); | |
| 188 | |
| 189 const base::FilePath source_install_dir = | |
| 190 data_dir().AppendASCII("app_list").AppendASCII("Extensions"); | |
| 191 const base::FilePath pref_path = | |
| 192 source_install_dir.DirName().Append(chrome::kPreferencesFilename); | |
| 193 InitializeInstalledExtensionService(pref_path, source_install_dir); | |
| 194 service_->Init(); | |
| 195 } | |
| 196 | |
| 197 private: | |
| 198 DISALLOW_COPY_AND_ASSIGN(ChromeAppIconTest); | |
| 199 }; | |
| 200 | |
| 201 TEST_F(ChromeAppIconTest, IconLifeCycle) { | |
| 202 TestAppIcon reference_icon(profile(), kTestAppId, | |
| 203 extension_misc::EXTENSION_ICON_MEDIUM); | |
| 204 EXPECT_EQ(1U, reference_icon.icon_update_count()); | |
| 205 // By default no representation in image. | |
| 206 EXPECT_FALSE(reference_icon.image_skia().HasRepresentation(1.0f)); | |
| 207 | |
| 208 // Default blank image must be provided without an update. | |
| 209 EXPECT_FALSE(reference_icon.image_skia().GetRepresentation(1.0f).is_null()); | |
| 210 EXPECT_EQ(1U, reference_icon.icon_update_count()); | |
| 211 EXPECT_TRUE(reference_icon.image_skia().HasRepresentation(1.0f)); | |
| 212 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, | |
| 213 reference_icon.image_skia().width()); | |
| 214 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, | |
| 215 reference_icon.image_skia().height()); | |
| 216 EXPECT_TRUE(IsBlankImage(reference_icon.image_skia())); | |
| 217 | |
| 218 // Wait until real image is loaded. | |
| 219 reference_icon.WaitForIconUpdates(); | |
| 220 EXPECT_EQ(2U, reference_icon.icon_update_count()); | |
| 221 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); | |
| 222 EXPECT_FALSE(IsGrayscaleImage(reference_icon.image_skia())); | |
| 223 | |
| 224 const gfx::ImageSkia image_before_disable = reference_icon.image_skia(); | |
| 225 // Disable extension. This should update icon and provide grayed image inline. | |
| 226 // Note update might be sent twice in CrOS. | |
| 227 service()->DisableExtension(kTestAppId, Extension::DISABLE_CORRUPTED); | |
| 228 const size_t update_count_after_disable = reference_icon.icon_update_count(); | |
| 229 EXPECT_NE(2U, update_count_after_disable); | |
| 230 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); | |
| 231 EXPECT_TRUE(IsGrayscaleImage(reference_icon.image_skia())); | |
| 232 | |
| 233 // Reenable extension. It should match previous enabled image | |
| 234 service()->EnableExtension(kTestAppId); | |
| 235 EXPECT_NE(update_count_after_disable, reference_icon.icon_update_count()); | |
| 236 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_disable)); | |
| 237 } | |
| 238 | |
| 239 // Validates that icon release is safe. | |
| 240 TEST_F(ChromeAppIconTest, IconRelease) { | |
| 241 TestAppIcon test_icon1(profile(), kTestAppId, | |
| 242 extension_misc::EXTENSION_ICON_MEDIUM); | |
| 243 TestAppIcon test_icon2(profile(), kTestAppId, | |
| 244 extension_misc::EXTENSION_ICON_MEDIUM); | |
| 245 EXPECT_FALSE(test_icon1.image_skia().GetRepresentation(1.0f).is_null()); | |
| 246 EXPECT_FALSE(test_icon2.image_skia().GetRepresentation(1.0f).is_null()); | |
| 247 | |
| 248 // Reset before service is stopped. | |
| 249 test_icon1.Reset(); | |
| 250 | |
| 251 // Reset after service is stopped. | |
| 252 profile_.reset(); | |
| 253 test_icon2.Reset(); | |
| 254 } | |
| 255 | |
| 256 #if defined(OS_CHROMEOS) | |
| 257 | |
| 258 class ChromeAppIconWithModelTest : public ChromeAppIconTest { | |
| 259 public: | |
| 260 ChromeAppIconWithModelTest() = default; | |
| 261 ~ChromeAppIconWithModelTest() override = default; | |
| 262 | |
| 263 protected: | |
| 264 void CreateBuilder() { | |
| 265 model_.reset(new app_list::AppListModel); | |
| 266 controller_.reset(new test::TestAppListControllerDelegate); | |
| 267 builder_.reset(new ExtensionAppModelBuilder(controller_.get())); | |
| 268 builder_->InitializeWithProfile(profile(), model_.get()); | |
| 269 } | |
| 270 | |
| 271 void ResetBuilder() { | |
| 272 builder_.reset(); | |
| 273 controller_.reset(); | |
| 274 model_.reset(); | |
| 275 } | |
| 276 | |
| 277 app_list::AppListItem* FindAppListItem(const std::string& app_id) { | |
| 278 return model_->FindItem(app_id); | |
| 279 } | |
| 280 | |
| 281 test::TestAppListControllerDelegate* app_list_controller() { | |
| 282 return controller_.get(); | |
| 283 } | |
| 284 | |
| 285 private: | |
| 286 std::unique_ptr<app_list::AppListModel> model_; | |
| 287 std::unique_ptr<test::TestAppListControllerDelegate> controller_; | |
| 288 std::unique_ptr<ExtensionAppModelBuilder> builder_; | |
| 289 | |
| 290 DISALLOW_COPY_AND_ASSIGN(ChromeAppIconWithModelTest); | |
| 291 }; | |
| 292 | |
| 293 // Validates that icons are the same for different consumers. | |
| 294 TEST_F(ChromeAppIconWithModelTest, IconsTheSame) { | |
| 295 CreateBuilder(); | |
| 296 | |
| 297 // App list item is already created. Wait until all image representations are | |
| 298 // updated and take image snapshot. | |
| 299 app_list::AppListItem* app_list_item = FindAppListItem(kTestAppId); | |
| 300 ASSERT_TRUE(app_list_item); | |
| 301 WaitForIconUpdates<app_list::AppListItem>(*app_list_item); | |
| 302 std::unique_ptr<gfx::ImageSkia> app_list_item_image = | |
| 303 app_list_item->icon().DeepCopy(); | |
| 304 | |
| 305 // Load reference icon. | |
| 306 TestAppIcon reference_icon(profile(), kTestAppId, | |
| 307 extension_misc::EXTENSION_ICON_MEDIUM); | |
| 308 | |
| 309 // Wait until reference data is loaded. | |
| 310 reference_icon.image_skia().EnsureRepsForSupportedScales(); | |
| 311 reference_icon.WaitForIconUpdates(); | |
| 312 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); | |
| 313 | |
| 314 // Now compare with app list icon snapshot. | |
| 315 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), *app_list_item_image)); | |
| 316 | |
| 317 app_list::ExtensionAppResult search(profile(), kTestAppId, | |
| 318 app_list_controller(), true); | |
| 319 WaitForIconUpdates<app_list::ExtensionAppResult>(search); | |
| 320 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), search.icon())); | |
| 321 | |
| 322 TestAppIconLoader loader_delegate; | |
| 323 ChromeAppIconLoader loader(profile(), extension_misc::EXTENSION_ICON_MEDIUM, | |
| 324 &loader_delegate); | |
| 325 loader.FetchImage(kTestAppId); | |
| 326 WaitForIconUpdates<TestAppIconLoader>(loader_delegate); | |
| 327 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), loader_delegate.icon())); | |
| 328 | |
| 329 ResetBuilder(); | |
| 330 } | |
| 331 | |
| 332 TEST_F(ChromeAppIconTest, ChromeBadging) { | |
| 333 ArcAppTest arc_test; | |
| 334 arc_test.SetUp(profile()); | |
| 335 | |
| 336 TestAppIcon reference_icon(profile(), kTestAppId, | |
| 337 extension_misc::EXTENSION_ICON_MEDIUM); | |
| 338 // Wait until reference data is loaded. | |
| 339 EXPECT_FALSE(reference_icon.image_skia().GetRepresentation(1.0f).is_null()); | |
| 340 reference_icon.WaitForIconUpdates(); | |
| 341 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); | |
| 342 | |
| 343 reference_icon.GetIconUpdateCountAndReset(); | |
| 344 const gfx::ImageSkia image_before_badging = reference_icon.image_skia(); | |
| 345 | |
| 346 // Badging should be applied once package is installed. | |
| 347 arc_test.app_instance()->RefreshAppList(); | |
| 348 std::vector<arc::mojom::AppInfo> fake_apps = arc_test.fake_apps(); | |
| 349 fake_apps[0].package_name = arc_test.fake_packages()[0].package_name; | |
| 350 arc_test.app_instance()->SendRefreshAppList(fake_apps); | |
| 351 arc_test.app_instance()->SendRefreshPackageList(arc_test.fake_packages()); | |
| 352 EXPECT_EQ(1U, reference_icon.icon_update_count()); | |
| 353 EXPECT_FALSE(AreEqual(reference_icon.image_skia(), image_before_badging)); | |
| 354 | |
| 355 // Opts out the Play Store. Badge should be gone and icon image is the same | |
| 356 // as it was before badging. | |
| 357 arc::SetArcPlayStoreEnabledForProfile(profile(), false); | |
| 358 EXPECT_EQ(2U, reference_icon.icon_update_count()); | |
| 359 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_badging)); | |
| 360 } | |
| 361 | |
| 362 #endif // defined(OS_CHROMEOS) | |
| 363 | |
| 364 } // namespace extensions | |
| OLD | NEW |