Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4596)

Unified Diff: chrome/browser/extensions/extension_icon_image_unittest.cc

Issue 10825012: chromeos: Fix pixelated icons in app list and launcher (part 2) (by xiyuan) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_icon_image_unittest.cc
diff --git a/chrome/browser/extensions/extension_icon_image_unittest.cc b/chrome/browser/extensions/extension_icon_image_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5b416af0335e286654aca4d562a34f6341ff1a15
--- /dev/null
+++ b/chrome/browser/extensions/extension_icon_image_unittest.cc
@@ -0,0 +1,201 @@
+// 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_icon_image.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 "content/public/test/test_browser_thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using content::BrowserThread;
+using extensions::Extension;
+using extensions::IconImage;
+
+namespace {
+
+class ExtensionIconImageTest : public testing::Test,
+ public IconImage::Delegate {
+ public:
+ ExtensionIconImageTest()
+ : image_loaded_count_(0),
+ quit_in_image_loaded_(false),
+ ui_thread_(BrowserThread::UI, &ui_loop_),
+ file_thread_(BrowserThread::FILE),
+ io_thread_(BrowserThread::IO) {
+ }
+
+ void WaitForImageLoad() {
+ quit_in_image_loaded_ = true;
+ MessageLoop::current()->Run();
+ quit_in_image_loaded_ = false;
+ }
+
+ int image_loaded_count() {
+ int result = image_loaded_count_;
+ image_loaded_count_ = 0;
+ return result;
+ }
+
+ 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();
+ }
+
+ // IconImage::Delegate overrides:
+ virtual void OnExtensionIconImageChanged(IconImage* image) OVERRIDE {
+ image_loaded_count_++;
+ if (quit_in_image_loaded_)
+ MessageLoop::current()->Quit();
+ }
+
+ private:
+ int image_loaded_count_;
+ bool quit_in_image_loaded_;
+ MessageLoop ui_loop_;
+ content::TestBrowserThread ui_thread_;
+ content::TestBrowserThread file_thread_;
+ content::TestBrowserThread io_thread_;
+
+ DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest);
+};
+
+} // namespace
+
+TEST_F(ExtensionIconImageTest, Basic) {
+ scoped_refptr<Extension> extension(CreateExtension(
+ "image_loading_tracker", Extension::INVALID));
+ ASSERT_TRUE(extension.get() != NULL);
+
+ scoped_ptr<IconImage> image(new IconImage(
+ extension,
+ extension->icons(),
+ ExtensionIconSet::EXTENSION_ICON_BITTY,
+ ExtensionIconSet::MATCH_SMALLER,
+ gfx::Size(ExtensionIconSet::EXTENSION_ICON_BITTY,
+ ExtensionIconSet::EXTENSION_ICON_BITTY),
+ ImageLoadingTracker::DONT_CACHE,
+ this));
+
+ // No representations in |image_| yet.
+ gfx::ImageSkia::ImageSkiaReps image_reps = image->image_skia().image_reps();
+ ASSERT_EQ(0u, image_reps.size());
+
+ // Gets representation for a scale factor.
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
+ WaitForImageLoad();
+ EXPECT_EQ(1, image_loaded_count());
+
+ gfx::ImageSkiaRep image_rep =
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
+ EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY,
+ image_rep.pixel_width());
+
+ // Gets representation for an additional scale factor.
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+ WaitForImageLoad();
+ EXPECT_EQ(1, image_loaded_count());
+
+ image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+ EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALL,
+ image_rep.pixel_width());
+}
+
+TEST_F(ExtensionIconImageTest, Missing2x) {
+ scoped_refptr<Extension> extension(CreateExtension(
+ "image_loading_tracker", Extension::INVALID));
+ ASSERT_TRUE(extension.get() != NULL);
+
+ scoped_ptr<IconImage> image(new IconImage(
+ extension,
+ extension->icons(),
+ ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::MATCH_BIGGER,
+ gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::EXTENSION_ICON_SMALLISH),
+ ImageLoadingTracker::DONT_CACHE,
+ this));
+
+ // Gets representation for 1x.
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
+ WaitForImageLoad();
+ EXPECT_EQ(1, image_loaded_count());
+
+ gfx::ImageSkiaRep image_rep =
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P);
+ EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ image_rep.pixel_width());
+
+ // Get representation for 2x.
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+
+ image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+
+ // 1x representation would be returned since there is no 2x resource.
+ EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
+ EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ image_rep.pixel_width());
+}
+
+// Similar to Missing2x test but go directly for the missing 2x resource first.
+TEST_F(ExtensionIconImageTest, FallbackTo1x) {
+ scoped_refptr<Extension> extension(CreateExtension(
+ "image_loading_tracker", Extension::INVALID));
+ ASSERT_TRUE(extension.get() != NULL);
+
+ scoped_ptr<IconImage> image(new IconImage(
+ extension,
+ extension->icons(),
+ ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::MATCH_BIGGER,
+ gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::EXTENSION_ICON_SMALLISH),
+ ImageLoadingTracker::DONT_CACHE,
+ this));
+
+ // Attempt to get representation for 2x.
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+
+ WaitForImageLoad();
+ EXPECT_EQ(1, image_loaded_count());
+
+ // 1x representation would be returned since there is no 2x resource.
+ gfx::ImageSkiaRep image_rep =
+ image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P);
+ EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor());
+ EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH,
+ image_rep.pixel_width());
+}

Powered by Google App Engine
This is Rietveld 408576698