OLD | NEW |
(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_action_icon_factory.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 "chrome/common/extensions/extension_action.h" |
| 13 #include "content/public/test/test_browser_thread.h" |
| 14 #include "grit/theme_resources.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/image/image_skia.h" |
| 18 #include "ui/gfx/skia_util.h" |
| 19 |
| 20 using content::BrowserThread; |
| 21 using extensions::Extension; |
| 22 |
| 23 namespace { |
| 24 |
| 25 bool ImageRepsAreEqual(const gfx::ImageSkiaRep& image_rep1, |
| 26 const gfx::ImageSkiaRep& image_rep2) { |
| 27 return image_rep1.scale_factor() == image_rep2.scale_factor() && |
| 28 gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap()); |
| 29 } |
| 30 |
| 31 gfx::ImageSkiaRep CreateBlankRep(int size_dip, ui::ScaleFactor scale_factor) { |
| 32 SkBitmap bitmap; |
| 33 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 34 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 35 static_cast<int>(size_dip * scale), |
| 36 static_cast<int>(size_dip * scale)); |
| 37 bitmap.allocPixels(); |
| 38 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
| 39 return gfx::ImageSkiaRep(bitmap, scale_factor); |
| 40 } |
| 41 |
| 42 class ExtensionActionIconFactoryTest |
| 43 : public testing::Test, |
| 44 public ExtensionActionIconFactory::Observer { |
| 45 public: |
| 46 ExtensionActionIconFactoryTest() |
| 47 : quit_in_icon_updated_(false), |
| 48 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 49 file_thread_(BrowserThread::FILE), |
| 50 io_thread_(BrowserThread::IO) { |
| 51 } |
| 52 |
| 53 virtual ~ExtensionActionIconFactoryTest() {} |
| 54 |
| 55 void WaitForIconUpdate() { |
| 56 quit_in_icon_updated_ = true; |
| 57 MessageLoop::current()->Run(); |
| 58 quit_in_icon_updated_ = false; |
| 59 } |
| 60 |
| 61 scoped_refptr<Extension> CreateExtension(const char* name, |
| 62 Extension::Location location) { |
| 63 // Create and load an extension. |
| 64 FilePath test_file; |
| 65 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
| 66 EXPECT_FALSE(true); |
| 67 return NULL; |
| 68 } |
| 69 test_file = test_file.AppendASCII("extensions").AppendASCII(name); |
| 70 int error_code = 0; |
| 71 std::string error; |
| 72 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
| 73 scoped_ptr<DictionaryValue> valid_value( |
| 74 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
| 75 &error))); |
| 76 EXPECT_EQ(0, error_code) << error; |
| 77 if (error_code != 0) |
| 78 return NULL; |
| 79 |
| 80 EXPECT_TRUE(valid_value.get()); |
| 81 if (!valid_value.get()) |
| 82 return NULL; |
| 83 |
| 84 return Extension::Create(test_file, location, *valid_value, |
| 85 Extension::NO_FLAGS, &error); |
| 86 } |
| 87 |
| 88 // testing::Test overrides: |
| 89 virtual void SetUp() OVERRIDE { |
| 90 file_thread_.Start(); |
| 91 io_thread_.Start(); |
| 92 } |
| 93 |
| 94 // ExtensionActionIconFactory::Observer overrides: |
| 95 virtual void OnIconUpdated() OVERRIDE { |
| 96 if (quit_in_icon_updated_) |
| 97 MessageLoop::current()->Quit(); |
| 98 } |
| 99 |
| 100 gfx::ImageSkia GetDefaultIcon() { |
| 101 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 102 IDR_EXTENSIONS_FAVICON); |
| 103 } |
| 104 |
| 105 private: |
| 106 bool quit_in_icon_updated_; |
| 107 MessageLoop ui_loop_; |
| 108 content::TestBrowserThread ui_thread_; |
| 109 content::TestBrowserThread file_thread_; |
| 110 content::TestBrowserThread io_thread_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(ExtensionActionIconFactoryTest); |
| 113 }; |
| 114 |
| 115 TEST_F(ExtensionActionIconFactoryTest, Basic) { |
| 116 scoped_refptr<Extension> extension(CreateExtension( |
| 117 "extension_icon_image", Extension::INVALID)); |
| 118 ASSERT_TRUE(extension.get() != NULL); |
| 119 |
| 120 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 121 |
| 122 ExtensionActionIconFactory icon_factory(extension, this); |
| 123 |
| 124 gfx::ImageSkia icon = icon_factory.GetIcon(&extension->icons(), |
| 125 gfx::Size(19, 19)); |
| 126 |
| 127 // Initially icon factory should return transparent image because icon will be |
| 128 // loaded asynchronously. |
| 129 EXPECT_TRUE(ImageRepsAreEqual( |
| 130 CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
| 131 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 132 |
| 133 WaitForIconUpdate(); |
| 134 |
| 135 // The icon should have changed. |
| 136 EXPECT_FALSE(ImageRepsAreEqual( |
| 137 CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
| 138 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 139 |
| 140 gfx::ImageSkia updated_icon = icon_factory.GetIcon(&extension->icons(), |
| 141 gfx::Size(19, 19)); |
| 142 |
| 143 // Icon_factory should return the same icon as before. |
| 144 EXPECT_TRUE(ImageRepsAreEqual( |
| 145 icon.GetRepresentation(ui::SCALE_FACTOR_100P), |
| 146 updated_icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 147 |
| 148 ExtensionIconSet invalid_icon_set; |
| 149 invalid_icon_set.Add(19, "invalid.png"); |
| 150 icon = icon_factory.GetIcon(&invalid_icon_set, gfx::Size(19, 19)); |
| 151 |
| 152 // Icon set has changed, so we should get a transparent icon again. |
| 153 EXPECT_TRUE(ImageRepsAreEqual( |
| 154 CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
| 155 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 156 |
| 157 WaitForIconUpdate(); |
| 158 |
| 159 // Icon set keeps nonexisting resource, so default icon should be loaded |
| 160 // insted. |
| 161 EXPECT_TRUE(ImageRepsAreEqual( |
| 162 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P), |
| 163 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 164 }; |
| 165 |
| 166 } // namespace |
OLD | NEW |