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

Side by Side Diff: chrome/browser/extensions/extension_action_icon_factory_unittest.cc

Issue 10905005: Change browser/page action default icon defined in manifest to support hidpi. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: missing test for icon factory; need to update comments in browser/ui/ Created 8 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
117 /*
118 scoped_refptr<Extension> extension(CreateExtension(
119 "extension_icon_image", Extension::INVALID));
120 ASSERT_TRUE(extension.get() != NULL);
121
122 gfx::ImageSkia default_icon = GetDefaultIcon();
123
124 ExtensionActionIconFactory icon_factory(extension, this);
125
126 gfx::ImageSkia icon = icon_factory.GetIcon(&extension->icons(),
127 gfx::Size(19, 19));
128
129 // Initially icon factory should return transparent image because icon will be
130 // loaded asynchronously.
131 EXPECT_TRUE(ImageRepsAreEqual(
132 CreateBlankRep(19, ui::SCALE_FACTOR_100P),
133 icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
134
135 WaitForIconUpdate();
136
137 // The icon should have changed.
138 EXPECT_FALSE(ImageRepsAreEqual(
139 CreateBlankRep(19, ui::SCALE_FACTOR_100P),
140 icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
141
142 gfx::ImageSkia updated_icon = icon_factory.GetIcon(&extension->icons(),
143 gfx::Size(19, 19));
144
145 // Icon_factory should return the same icon as before.
146 EXPECT_TRUE(ImageRepsAreEqual(
147 icon.GetRepresentation(ui::SCALE_FACTOR_100P),
148 updated_icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
149
150 ExtensionIconSet invalid_icon_set;
151 invalid_icon_set.Add(19, "invalid.png");
152 icon = icon_factory.GetIcon(&invalid_icon_set, gfx::Size(19, 19));
153
154 // Icon set has changed, so we should get a transparent icon again.
155 EXPECT_TRUE(ImageRepsAreEqual(
156 CreateBlankRep(19, ui::SCALE_FACTOR_100P),
157 icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
158
159 WaitForIconUpdate();
160
161 // Icon set keeps nonexisting resource, so default icon should be loaded
162 // insted.
163 EXPECT_TRUE(ImageRepsAreEqual(
164 default_icon.GetRepresentation(ui::SCALE_FACTOR_100P),
165 icon.GetRepresentation(ui::SCALE_FACTOR_100P)));
166 */
167 };
168
169 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698