Index: chrome/browser/extensions/extension_action_icon_factory_unittest.cc |
diff --git a/chrome/browser/extensions/extension_action_icon_factory_unittest.cc b/chrome/browser/extensions/extension_action_icon_factory_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c99e2b8755383b09be4155652dfb81329740bee3 |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_action_icon_factory_unittest.cc |
@@ -0,0 +1,166 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/extension_action_icon_factory.h" |
+ |
+#include "base/json/json_file_value_serializer.h" |
+#include "base/message_loop.h" |
+#include "base/path_service.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/extensions/extension_action.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "grit/theme_resources.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/base/resource/resource_bundle.h" |
+#include "ui/gfx/image/image_skia.h" |
+#include "ui/gfx/skia_util.h" |
+ |
+using content::BrowserThread; |
+using extensions::Extension; |
+ |
+namespace { |
+ |
+bool ImageRepsAreEqual(const gfx::ImageSkiaRep& image_rep1, |
+ const gfx::ImageSkiaRep& image_rep2) { |
+ return image_rep1.scale_factor() == image_rep2.scale_factor() && |
+ gfx::BitmapsAreEqual(image_rep1.sk_bitmap(), image_rep2.sk_bitmap()); |
+} |
+ |
+gfx::ImageSkiaRep CreateBlankRep(int size_dip, ui::ScaleFactor scale_factor) { |
+ SkBitmap bitmap; |
+ const float scale = ui::GetScaleFactorScale(scale_factor); |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
+ static_cast<int>(size_dip * scale), |
+ static_cast<int>(size_dip * scale)); |
+ bitmap.allocPixels(); |
+ bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0)); |
+ return gfx::ImageSkiaRep(bitmap, scale_factor); |
+} |
+ |
+class ExtensionActionIconFactoryTest |
+ : public testing::Test, |
+ public ExtensionActionIconFactory::Observer { |
+ public: |
+ ExtensionActionIconFactoryTest() |
+ : quit_in_icon_updated_(false), |
+ ui_thread_(BrowserThread::UI, &ui_loop_), |
+ file_thread_(BrowserThread::FILE), |
+ io_thread_(BrowserThread::IO) { |
+ } |
+ |
+ virtual ~ExtensionActionIconFactoryTest() {} |
+ |
+ void WaitForIconUpdate() { |
+ quit_in_icon_updated_ = true; |
+ MessageLoop::current()->Run(); |
+ quit_in_icon_updated_ = false; |
+ } |
+ |
+ scoped_refptr<Extension> CreateExtension(const char* name, |
+ Extension::Location location) { |
+ // Create and load an extension. |
+ FilePath test_file; |
+ if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
+ EXPECT_FALSE(true); |
+ return NULL; |
+ } |
+ test_file = test_file.AppendASCII("extensions").AppendASCII(name); |
+ int error_code = 0; |
+ std::string error; |
+ JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
+ scoped_ptr<DictionaryValue> valid_value( |
+ static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
+ &error))); |
+ EXPECT_EQ(0, error_code) << error; |
+ if (error_code != 0) |
+ return NULL; |
+ |
+ EXPECT_TRUE(valid_value.get()); |
+ if (!valid_value.get()) |
+ return NULL; |
+ |
+ return Extension::Create(test_file, location, *valid_value, |
+ Extension::NO_FLAGS, &error); |
+ } |
+ |
+ // testing::Test overrides: |
+ virtual void SetUp() OVERRIDE { |
+ file_thread_.Start(); |
+ io_thread_.Start(); |
+ } |
+ |
+ // ExtensionActionIconFactory::Observer overrides: |
+ virtual void OnIconUpdated() OVERRIDE { |
+ if (quit_in_icon_updated_) |
+ MessageLoop::current()->Quit(); |
+ } |
+ |
+ gfx::ImageSkia GetDefaultIcon() { |
+ return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
+ IDR_EXTENSIONS_FAVICON); |
+ } |
+ |
+ private: |
+ bool quit_in_icon_updated_; |
+ MessageLoop ui_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ content::TestBrowserThread file_thread_; |
+ content::TestBrowserThread io_thread_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExtensionActionIconFactoryTest); |
+}; |
+ |
+TEST_F(ExtensionActionIconFactoryTest, Basic) { |
+ scoped_refptr<Extension> extension(CreateExtension( |
+ "extension_icon_image", Extension::INVALID)); |
+ ASSERT_TRUE(extension.get() != NULL); |
+ |
+ gfx::ImageSkia default_icon = GetDefaultIcon(); |
+ |
+ ExtensionActionIconFactory icon_factory(extension, this); |
+ |
+ gfx::ImageSkia icon = icon_factory.GetIcon(&extension->icons(), |
+ gfx::Size(19, 19)); |
+ |
+ // Initially icon factory should return transparent image because icon will be |
+ // loaded asynchronously. |
+ EXPECT_TRUE(ImageRepsAreEqual( |
+ CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
+ icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
+ |
+ WaitForIconUpdate(); |
+ |
+ // The icon should have changed. |
+ EXPECT_FALSE(ImageRepsAreEqual( |
+ CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
+ icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
+ |
+ gfx::ImageSkia updated_icon = icon_factory.GetIcon(&extension->icons(), |
+ gfx::Size(19, 19)); |
+ |
+ // Icon_factory should return the same icon as before. |
+ EXPECT_TRUE(ImageRepsAreEqual( |
+ icon.GetRepresentation(ui::SCALE_FACTOR_100P), |
+ updated_icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
+ |
+ ExtensionIconSet invalid_icon_set; |
+ invalid_icon_set.Add(19, "invalid.png"); |
+ icon = icon_factory.GetIcon(&invalid_icon_set, gfx::Size(19, 19)); |
+ |
+ // Icon set has changed, so we should get a transparent icon again. |
+ EXPECT_TRUE(ImageRepsAreEqual( |
+ CreateBlankRep(19, ui::SCALE_FACTOR_100P), |
+ icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
+ |
+ WaitForIconUpdate(); |
+ |
+ // Icon set keeps nonexisting resource, so default icon should be loaded |
+ // insted. |
+ EXPECT_TRUE(ImageRepsAreEqual( |
+ default_icon.GetRepresentation(ui::SCALE_FACTOR_100P), |
+ icon.GetRepresentation(ui::SCALE_FACTOR_100P))); |
+}; |
+ |
+} // namespace |