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 gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap()); |
| 28 } |
| 29 |
| 30 gfx::ImageSkiaRep CreateBlankRep(int size_dip, ui::ScaleFactor scale_factor) { |
| 31 SkBitmap bitmap; |
| 32 const float scale = ui::GetScaleFactorScale(scale_factor); |
| 33 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 34 static_cast<int>(size_dip * scale), |
| 35 static_cast<int>(size_dip * scale)); |
| 36 bitmap.allocPixels(); |
| 37 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
| 38 return gfx::ImageSkiaRep(bitmap, scale_factor); |
| 39 } |
| 40 |
| 41 class ExtensionActionIconFactoryTest |
| 42 : public testing::Test, |
| 43 public ExtensionActionIconFactory::Observer { |
| 44 public: |
| 45 ExtensionActionIconFactoryTest() |
| 46 : quit_in_icon_updated_(false), |
| 47 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 48 file_thread_(BrowserThread::FILE), |
| 49 io_thread_(BrowserThread::IO) { |
| 50 } |
| 51 |
| 52 virtual ~ExtensionActionIconFactoryTest() {} |
| 53 |
| 54 void WaitForIconUpdate() { |
| 55 quit_in_icon_updated_ = true; |
| 56 MessageLoop::current()->Run(); |
| 57 quit_in_icon_updated_ = false; |
| 58 } |
| 59 |
| 60 scoped_refptr<Extension> CreateExtension(const char* name, |
| 61 Extension::Location location) { |
| 62 // Create and load an extension. |
| 63 FilePath test_file; |
| 64 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
| 65 EXPECT_FALSE(true); |
| 66 return NULL; |
| 67 } |
| 68 test_file = test_file.AppendASCII("extensions").AppendASCII(name); |
| 69 int error_code = 0; |
| 70 std::string error; |
| 71 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
| 72 scoped_ptr<DictionaryValue> valid_value( |
| 73 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
| 74 &error))); |
| 75 EXPECT_EQ(0, error_code) << error; |
| 76 if (error_code != 0) |
| 77 return NULL; |
| 78 |
| 79 EXPECT_TRUE(valid_value.get()); |
| 80 if (!valid_value.get()) |
| 81 return NULL; |
| 82 |
| 83 return Extension::Create(test_file, location, *valid_value, |
| 84 Extension::NO_FLAGS, &error); |
| 85 } |
| 86 |
| 87 // testing::Test overrides: |
| 88 virtual void SetUp() OVERRIDE { |
| 89 file_thread_.Start(); |
| 90 io_thread_.Start(); |
| 91 } |
| 92 |
| 93 // ExtensionActionIconFactory::Observer overrides: |
| 94 virtual void OnIconUpdated() OVERRIDE { |
| 95 if (quit_in_icon_updated_) |
| 96 MessageLoop::current()->Quit(); |
| 97 } |
| 98 |
| 99 gfx::ImageSkia GetDefaultIcon() { |
| 100 return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 101 IDR_EXTENSIONS_FAVICON); |
| 102 } |
| 103 |
| 104 private: |
| 105 bool quit_in_icon_updated_; |
| 106 MessageLoop ui_loop_; |
| 107 content::TestBrowserThread ui_thread_; |
| 108 content::TestBrowserThread file_thread_; |
| 109 content::TestBrowserThread io_thread_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(ExtensionActionIconFactoryTest); |
| 112 }; |
| 113 |
| 114 TEST_F(ExtensionActionIconFactoryTest, Basic) { |
| 115 scoped_refptr<Extension> extension(CreateExtension( |
| 116 "extension_icon_image", Extension::INVALID)); |
| 117 ASSERT_TRUE(extension.get() != NULL); |
| 118 |
| 119 gfx::ImageSkia default_icon = GetDefaultIcon(); |
| 120 |
| 121 ExtensionActionIconFactory icon_factory(extension, this); |
| 122 |
| 123 gfx::ImageSkia icon = icon_factory.GetIcon(&extension->icons(), |
| 124 ExtensionAction::TYPE_BROWSER); |
| 125 |
| 126 // Initially icon factory should return transparent image because icon will be |
| 127 // loaded asynchronously. |
| 128 EXPECT_TRUE(ImageRepsAreEqual( |
| 129 CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
| 130 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 131 |
| 132 WaitForIconUpdate(); |
| 133 |
| 134 // The icon should have changed. |
| 135 EXPECT_FALSE(ImageRepsAreEqual( |
| 136 CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
| 137 icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
| 138 |
| 139 gfx::ImageSkia updated_icon = |
| 140 icon_factory.GetIcon(&extension->icons(), |
| 141 ExtensionAction::TYPE_BROWSER); |
| 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, ExtensionAction::TYPE_BROWSER); |
| 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 |