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/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/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 |
| 30 |
| 31 namespace extensions { |
| 32 |
| 33 namespace { |
| 34 |
| 35 constexpr char kTestAppId[] = "emfkafnhnpcmabnnkckkchdilgeoekbo"; |
| 36 |
| 37 // Receives icon image updates from ExtensionAppIcon. |
| 38 class TestAppIcon : public ExtensionAppIconDelegate { |
| 39 public: |
| 40 TestAppIcon(content::BrowserContext* context, |
| 41 const std::string& app_id, |
| 42 int size) { |
| 43 app_icon_ = |
| 44 ExtensionAppIconService::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 int icon_update_count() const { return icon_update_count_; } |
| 59 ExtensionAppIcon* app_icon() { return app_icon_.get(); } |
| 60 const gfx::ImageSkia& image_skia() const { return app_icon_->image_skia(); } |
| 61 |
| 62 void WaitIconUpdated() { |
| 63 base::RunLoop run_loop; |
| 64 icon_updated_callback_ = run_loop.QuitClosure(); |
| 65 run_loop.Run(); |
| 66 } |
| 67 |
| 68 private: |
| 69 void OnIconUpdated(ExtensionAppIcon* icon) override { |
| 70 ++icon_update_count_; |
| 71 if (!icon_updated_callback_.is_null()) |
| 72 base::ResetAndReturn(&icon_updated_callback_).Run(); |
| 73 } |
| 74 |
| 75 std::unique_ptr<ExtensionAppIcon> app_icon_; |
| 76 |
| 77 int icon_update_count_ = 0; |
| 78 |
| 79 base::OnceClosure icon_updated_callback_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestAppIcon); |
| 82 }; |
| 83 |
| 84 // Receives icon image updates from ExtensionAppIconLoader. |
| 85 class TestAppIconLoader : public AppIconLoaderDelegate { |
| 86 public: |
| 87 TestAppIconLoader() = default; |
| 88 ~TestAppIconLoader() override = default; |
| 89 |
| 90 // AppIconLoaderDelegate: |
| 91 void OnAppImageUpdated(const std::string& app_id, |
| 92 const gfx::ImageSkia& image) override { |
| 93 image_skia_ = image; |
| 94 } |
| 95 |
| 96 const gfx::ImageSkia& image_skia() { return image_skia_; } |
| 97 |
| 98 private: |
| 99 gfx::ImageSkia image_skia_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(TestAppIconLoader); |
| 102 }; |
| 103 |
| 104 // Returns true if provided |image| consists from only empty pixels. |
| 105 bool IsBlankImage(const gfx::ImageSkia& image) { |
| 106 if (!image.width() || !image.height()) |
| 107 return false; |
| 108 |
| 109 const SkBitmap* bitmap = image.bitmap(); |
| 110 DCHECK(bitmap); |
| 111 DCHECK_EQ(bitmap->width(), image.width()); |
| 112 DCHECK_EQ(bitmap->height(), image.height()); |
| 113 |
| 114 for (int x = 0; x < bitmap->width(); ++x) { |
| 115 for (int y = 0; y < bitmap->height(); ++y) { |
| 116 if (*bitmap->getAddr32(x, y)) |
| 117 return false; |
| 118 } |
| 119 } |
| 120 return true; |
| 121 } |
| 122 |
| 123 // Returns true if provided |image| is grayscale. |
| 124 bool IsGrayscaleImage(const gfx::ImageSkia& image) { |
| 125 const SkBitmap* bitmap = image.bitmap(); |
| 126 DCHECK(bitmap); |
| 127 for (int x = 0; x < bitmap->width(); ++x) { |
| 128 for (int y = 0; y < bitmap->height(); ++y) { |
| 129 const unsigned int pixel = *bitmap->getAddr32(x, y); |
| 130 if ((pixel & 0xff) != ((pixel >> 8) & 0xff) || |
| 131 (pixel & 0xff) != ((pixel >> 16) & 0xff)) { |
| 132 return false; |
| 133 } |
| 134 } |
| 135 } |
| 136 return true; |
| 137 } |
| 138 |
| 139 // Returns true if provided |image1| and |image2| are equal. |
| 140 bool AreEqual(const gfx::ImageSkia& image1, const gfx::ImageSkia& image2) { |
| 141 return gfx::test::AreImagesEqual(gfx::Image(image1), gfx::Image(image2)); |
| 142 } |
| 143 |
| 144 } // namespace |
| 145 |
| 146 class ExtensionAppIconTest : public ExtensionServiceTestBase { |
| 147 public: |
| 148 ExtensionAppIconTest() = default; |
| 149 ~ExtensionAppIconTest() override = default; |
| 150 |
| 151 // ExtensionServiceTestBase: |
| 152 void SetUp() override { |
| 153 ExtensionServiceTestBase::SetUp(); |
| 154 |
| 155 const base::FilePath source_install_dir = |
| 156 data_dir().AppendASCII("app_list").AppendASCII("Extensions"); |
| 157 const base::FilePath pref_path = |
| 158 source_install_dir.DirName().Append(chrome::kPreferencesFilename); |
| 159 InitializeInstalledExtensionService(pref_path, source_install_dir); |
| 160 service_->Init(); |
| 161 } |
| 162 |
| 163 private: |
| 164 DISALLOW_COPY_AND_ASSIGN(ExtensionAppIconTest); |
| 165 }; |
| 166 |
| 167 TEST_F(ExtensionAppIconTest, IconLifeCycle) { |
| 168 TestAppIcon reference_icon(profile(), kTestAppId, |
| 169 extension_misc::EXTENSION_ICON_MEDIUM); |
| 170 EXPECT_EQ(1, reference_icon.icon_update_count()); |
| 171 // By default no representation in image. |
| 172 EXPECT_FALSE(reference_icon.image_skia().HasRepresentation(1.0f)); |
| 173 |
| 174 // Defualt blank image must be provided without an update. |
| 175 EXPECT_FALSE(reference_icon.image_skia().GetRepresentation(1.0f).is_null()); |
| 176 EXPECT_EQ(1, reference_icon.icon_update_count()); |
| 177 EXPECT_TRUE(reference_icon.image_skia().HasRepresentation(1.0f)); |
| 178 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, |
| 179 reference_icon.image_skia().width()); |
| 180 EXPECT_EQ(extension_misc::EXTENSION_ICON_MEDIUM, |
| 181 reference_icon.image_skia().height()); |
| 182 EXPECT_TRUE(IsBlankImage(reference_icon.image_skia())); |
| 183 |
| 184 // Wait until real image is loaded. |
| 185 reference_icon.WaitIconUpdated(); |
| 186 EXPECT_EQ(2, reference_icon.icon_update_count()); |
| 187 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); |
| 188 EXPECT_FALSE(IsGrayscaleImage(reference_icon.image_skia())); |
| 189 |
| 190 const gfx::ImageSkia image_before_disable = reference_icon.image_skia(); |
| 191 // Disable extension. This should update icon and provide grayed image inline. |
| 192 // Note update might be sent twice in CrOS. |
| 193 service()->DisableExtension(kTestAppId, Extension::DISABLE_CORRUPTED); |
| 194 const int update_count_after_disable = reference_icon.icon_update_count(); |
| 195 EXPECT_NE(2, update_count_after_disable); |
| 196 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); |
| 197 EXPECT_TRUE(IsGrayscaleImage(reference_icon.image_skia())); |
| 198 |
| 199 // Reenable extension. It should match previous enabled image |
| 200 service()->EnableExtension(kTestAppId); |
| 201 EXPECT_NE(update_count_after_disable, reference_icon.icon_update_count()); |
| 202 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_disable)); |
| 203 } |
| 204 |
| 205 // Validates that icon release is safe. |
| 206 TEST_F(ExtensionAppIconTest, IconRelease) { |
| 207 TestAppIcon test_icon1(profile(), kTestAppId, |
| 208 extension_misc::EXTENSION_ICON_MEDIUM); |
| 209 TestAppIcon test_icon2(profile(), kTestAppId, |
| 210 extension_misc::EXTENSION_ICON_MEDIUM); |
| 211 EXPECT_FALSE(test_icon1.image_skia().GetRepresentation(1.0f).is_null()); |
| 212 EXPECT_FALSE(test_icon2.image_skia().GetRepresentation(1.0f).is_null()); |
| 213 |
| 214 // Reset before service is stopped. |
| 215 test_icon1.Reset(); |
| 216 |
| 217 // Reset after service is stopped. |
| 218 profile_.reset(); |
| 219 test_icon2.Reset(); |
| 220 } |
| 221 |
| 222 #if defined(OS_CHROMEOS) |
| 223 class ExtensionAppIconWithModelTest : public ExtensionAppIconTest { |
| 224 public: |
| 225 ExtensionAppIconWithModelTest() = default; |
| 226 ~ExtensionAppIconWithModelTest() override = default; |
| 227 |
| 228 protected: |
| 229 void CreateBuilder() { |
| 230 model_.reset(new app_list::AppListModel); |
| 231 controller_.reset(new test::TestAppListControllerDelegate); |
| 232 builder_.reset(new ExtensionAppModelBuilder(controller_.get())); |
| 233 builder_->InitializeWithProfile(profile(), model_.get()); |
| 234 } |
| 235 |
| 236 void ResetBuilder() { |
| 237 builder_.reset(); |
| 238 controller_.reset(); |
| 239 model_.reset(); |
| 240 } |
| 241 |
| 242 app_list::AppListItem* FindAppListItem(const std::string& app_id) { |
| 243 return model_->FindItem(app_id); |
| 244 } |
| 245 |
| 246 test::TestAppListControllerDelegate* app_list_controller() { |
| 247 return controller_.get(); |
| 248 } |
| 249 |
| 250 private: |
| 251 std::unique_ptr<app_list::AppListModel> model_; |
| 252 std::unique_ptr<test::TestAppListControllerDelegate> controller_; |
| 253 std::unique_ptr<ExtensionAppModelBuilder> builder_; |
| 254 |
| 255 DISALLOW_COPY_AND_ASSIGN(ExtensionAppIconWithModelTest); |
| 256 }; |
| 257 |
| 258 // Validates that icons are the same for different consumers. |
| 259 TEST_F(ExtensionAppIconWithModelTest, IconsTheSame) { |
| 260 CreateBuilder(); |
| 261 |
| 262 TestAppIcon reference_icon(profile(), kTestAppId, |
| 263 extension_misc::EXTENSION_ICON_MEDIUM); |
| 264 |
| 265 // Wait until reference data is loaded. |
| 266 EXPECT_FALSE(reference_icon.image_skia().GetRepresentation(1.0f).is_null()); |
| 267 reference_icon.WaitIconUpdated(); |
| 268 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); |
| 269 |
| 270 app_list::ExtensionAppResult search(profile(), kTestAppId, |
| 271 app_list_controller(), true); |
| 272 while (!AreEqual(reference_icon.image_skia(), search.icon())) |
| 273 base::RunLoop().RunUntilIdle(); |
| 274 |
| 275 app_list::AppListItem* app_list_item = FindAppListItem(kTestAppId); |
| 276 ASSERT_TRUE(app_list_item); |
| 277 while (!AreEqual(reference_icon.image_skia(), app_list_item->icon())) |
| 278 base::RunLoop().RunUntilIdle(); |
| 279 |
| 280 TestAppIconLoader loader_delegate; |
| 281 ExtensionAppIconLoader loader( |
| 282 profile(), extension_misc::EXTENSION_ICON_MEDIUM, &loader_delegate); |
| 283 loader.FetchImage(kTestAppId); |
| 284 while (!AreEqual(reference_icon.image_skia(), loader_delegate.image_skia())) |
| 285 base::RunLoop().RunUntilIdle(); |
| 286 |
| 287 ResetBuilder(); |
| 288 } |
| 289 |
| 290 TEST_F(ExtensionAppIconWithModelTest, ChromeBadging) { |
| 291 ArcAppTest arc_test; |
| 292 arc_test.SetUp(profile()); |
| 293 |
| 294 TestAppIcon reference_icon(profile(), kTestAppId, |
| 295 extension_misc::EXTENSION_ICON_MEDIUM); |
| 296 // Wait until reference data is loaded. |
| 297 EXPECT_FALSE(reference_icon.image_skia().GetRepresentation(1.0f).is_null()); |
| 298 reference_icon.WaitIconUpdated(); |
| 299 EXPECT_FALSE(IsBlankImage(reference_icon.image_skia())); |
| 300 |
| 301 reference_icon.GetIconUpdateCountAndReset(); |
| 302 const gfx::ImageSkia image_before_badging = reference_icon.image_skia(); |
| 303 |
| 304 // Badging should be applied once package is installed. |
| 305 arc_test.app_instance()->RefreshAppList(); |
| 306 std::vector<arc::mojom::AppInfo> fake_apps = arc_test.fake_apps(); |
| 307 fake_apps[0].package_name = arc_test.fake_packages()[0].package_name; |
| 308 arc_test.app_instance()->SendRefreshAppList(fake_apps); |
| 309 arc_test.app_instance()->SendRefreshPackageList(arc_test.fake_packages()); |
| 310 EXPECT_EQ(1, reference_icon.icon_update_count()); |
| 311 EXPECT_FALSE(AreEqual(reference_icon.image_skia(), image_before_badging)); |
| 312 |
| 313 // Opts out the Play Store. Badge should be gone and icon image is the same |
| 314 // as it was before badging. |
| 315 arc::SetArcPlayStoreEnabledForProfile(profile(), false); |
| 316 EXPECT_EQ(2, reference_icon.icon_update_count()); |
| 317 EXPECT_TRUE(AreEqual(reference_icon.image_skia(), image_before_badging)); |
| 318 } |
| 319 #endif |
| 320 |
| 321 } // namespace extensions |
OLD | NEW |