Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_icon_manager_unittest.cc
blob004c1964fa013393d050cbb418336ee0e3094233
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.
5 #include "base/json/json_file_value_serializer.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_icon_manager.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/crx_file/id_util.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "extensions/common/extension.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/skia_util.h"
18 using content::BrowserThread;
19 using extensions::Extension;
20 using extensions::Manifest;
22 // Our test class that takes care of managing the necessary threads for loading
23 // extension icons, and waiting for those loads to happen.
24 class ExtensionIconManagerTest : public testing::Test {
25 public:
26 ExtensionIconManagerTest() :
27 unwaited_image_loads_(0),
28 waiting_(false),
29 ui_thread_(BrowserThread::UI, &ui_loop_),
30 file_thread_(BrowserThread::FILE),
31 io_thread_(BrowserThread::IO) {}
33 ~ExtensionIconManagerTest() override {}
35 void ImageLoadObserved() {
36 unwaited_image_loads_++;
37 if (waiting_) {
38 base::MessageLoop::current()->Quit();
42 void WaitForImageLoad() {
43 if (unwaited_image_loads_ == 0) {
44 waiting_ = true;
45 base::MessageLoop::current()->Run();
46 waiting_ = false;
48 ASSERT_GT(unwaited_image_loads_, 0);
49 unwaited_image_loads_--;
52 private:
53 void SetUp() override {
54 file_thread_.Start();
55 io_thread_.Start();
58 // The number of observed image loads that have not been waited for.
59 int unwaited_image_loads_;
61 // Whether we are currently waiting for an image load.
62 bool waiting_;
64 base::MessageLoop ui_loop_;
65 content::TestBrowserThread ui_thread_;
66 content::TestBrowserThread file_thread_;
67 content::TestBrowserThread io_thread_;
69 DISALLOW_COPY_AND_ASSIGN(ExtensionIconManagerTest);
72 // This is a specialization of ExtensionIconManager, with a special override to
73 // call back to the test when an icon has completed loading.
74 class TestIconManager : public ExtensionIconManager {
75 public:
76 explicit TestIconManager(ExtensionIconManagerTest* test) : test_(test) {}
77 ~TestIconManager() override {}
79 // Overrides the ImageLoader callback, and calls through to the base class'
80 // implementation. Then it lets the test know that an image load was observed.
81 void OnImageLoaded(const std::string& extension_id,
82 const gfx::Image& image) override {
83 ExtensionIconManager::OnImageLoaded(extension_id, image);
84 test_->ImageLoadObserved();
87 private:
88 ExtensionIconManagerTest* test_;
90 DISALLOW_COPY_AND_ASSIGN(TestIconManager);
93 // Returns the default icon that ExtensionIconManager gives when an extension
94 // doesn't have an icon.
95 SkBitmap GetDefaultIcon() {
96 std::string dummy_id = crx_file::id_util::GenerateId("whatever");
97 ExtensionIconManager manager;
98 return manager.GetIcon(dummy_id);
101 // Tests loading an icon for an extension, removing it, then re-loading it.
102 TEST_F(ExtensionIconManagerTest, LoadRemoveLoad) {
103 scoped_ptr<Profile> profile(new TestingProfile());
104 SkBitmap default_icon = GetDefaultIcon();
106 base::FilePath test_dir;
107 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
108 base::FilePath manifest_path = test_dir.AppendASCII(
109 "extensions/image_loading_tracker/app.json");
111 JSONFileValueDeserializer deserializer(manifest_path);
112 scoped_ptr<base::DictionaryValue> manifest(
113 static_cast<base::DictionaryValue*>(deserializer.Deserialize(NULL,
114 NULL)));
115 ASSERT_TRUE(manifest.get() != NULL);
117 std::string error;
118 scoped_refptr<Extension> extension(Extension::Create(
119 manifest_path.DirName(), Manifest::INVALID_LOCATION, *manifest.get(),
120 Extension::NO_FLAGS, &error));
121 ASSERT_TRUE(extension.get());
122 TestIconManager icon_manager(this);
124 // Load the icon and grab the bitmap.
125 icon_manager.LoadIcon(profile.get(), extension.get());
126 WaitForImageLoad();
127 SkBitmap first_icon = icon_manager.GetIcon(extension->id());
128 EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon));
130 // Remove the icon from the manager.
131 icon_manager.RemoveIcon(extension->id());
133 // Now re-load the icon - we should get the same result bitmap (and not the
134 // default icon).
135 icon_manager.LoadIcon(profile.get(), extension.get());
136 WaitForImageLoad();
137 SkBitmap second_icon = icon_manager.GetIcon(extension->id());
138 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon));
140 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon));
143 #if defined(OS_CHROMEOS)
144 // Tests loading an icon for a component extension.
145 TEST_F(ExtensionIconManagerTest, LoadComponentExtensionResource) {
146 scoped_ptr<Profile> profile(new TestingProfile());
147 SkBitmap default_icon = GetDefaultIcon();
149 base::FilePath test_dir;
150 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
151 base::FilePath manifest_path = test_dir.AppendASCII(
152 "extensions/file_manager/app.json");
154 JSONFileValueDeserializer deserializer(manifest_path);
155 scoped_ptr<base::DictionaryValue> manifest(
156 static_cast<base::DictionaryValue*>(deserializer.Deserialize(NULL,
157 NULL)));
158 ASSERT_TRUE(manifest.get() != NULL);
160 std::string error;
161 scoped_refptr<Extension> extension(Extension::Create(
162 manifest_path.DirName(), Manifest::COMPONENT, *manifest.get(),
163 Extension::NO_FLAGS, &error));
164 ASSERT_TRUE(extension.get());
166 TestIconManager icon_manager(this);
167 // Load the icon and grab the bitmap.
168 icon_manager.LoadIcon(profile.get(), extension.get());
169 WaitForImageLoad();
170 SkBitmap first_icon = icon_manager.GetIcon(extension->id());
171 EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon));
173 // Remove the icon from the manager.
174 icon_manager.RemoveIcon(extension->id());
176 // Now re-load the icon - we should get the same result bitmap (and not the
177 // default icon).
178 icon_manager.LoadIcon(profile.get(), extension.get());
179 WaitForImageLoad();
180 SkBitmap second_icon = icon_manager.GetIcon(extension->id());
181 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon));
183 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon));
185 #endif