Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/json/json_file_value_serializer.h" | 5 #include "base/json/json_file_value_serializer.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_icon_manager.h" | 9 #include "chrome/browser/extensions/extension_icon_manager.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "content/public/test/test_browser_thread.h" | 13 #include "content/public/test/test_browser_thread.h" |
| 14 #include "extensions/common/id_util.h" | 14 #include "extensions/common/id_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/gfx/skia_util.h" | 16 #include "ui/gfx/skia_util.h" |
| 17 | 17 |
| 18 using content::BrowserThread; | 18 using content::BrowserThread; |
| 19 using extensions::Extension; | 19 using extensions::Extension; |
| 20 using extensions::Manifest; | 20 using extensions::Manifest; |
| 21 | 21 |
| 22 // Our test class that takes care of managing the necessary threads for loading | 22 // Our test class that takes care of managing the necessary threads for loading |
| 23 // extension icons, and waiting for those loads to happen. | 23 // extension icons, and waiting for those loads to happen. |
| 24 class ExtensionIconManagerTest : public testing::Test { | 24 class ExtensionIconManagerTest : public testing::Test { |
|
Yoyo Zhou
2013/04/09 17:19:28
Can you make this inherit from extensions::Extensi
hashimoto
2013/04/10 02:40:14
Done.
| |
| 25 public: | 25 public: |
| 26 ExtensionIconManagerTest() : | 26 ExtensionIconManagerTest() : |
| 27 unwaited_image_loads_(0), | 27 unwaited_image_loads_(0), |
| 28 waiting_(false), | 28 waiting_(false), |
| 29 ui_thread_(BrowserThread::UI, &ui_loop_), | 29 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 30 file_thread_(BrowserThread::FILE), | 30 file_thread_(BrowserThread::FILE), |
| 31 io_thread_(BrowserThread::IO) {} | 31 io_thread_(BrowserThread::IO) {} |
| 32 | 32 |
| 33 virtual ~ExtensionIconManagerTest() {} | 33 virtual ~ExtensionIconManagerTest() {} |
| 34 | 34 |
| 35 void ImageLoadObserved() { | 35 void ImageLoadObserved() { |
| 36 unwaited_image_loads_++; | 36 unwaited_image_loads_++; |
| 37 if (waiting_) { | 37 if (waiting_) { |
| 38 MessageLoop::current()->Quit(); | 38 MessageLoop::current()->Quit(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 void WaitForImageLoad() { | 42 void WaitForImageLoad() { |
| 43 if (unwaited_image_loads_ == 0) { | 43 if (unwaited_image_loads_ == 0) { |
| 44 waiting_ = true; | 44 waiting_ = true; |
| 45 MessageLoop::current()->Run(); | 45 MessageLoop::current()->Run(); |
| 46 waiting_ = false; | 46 waiting_ = false; |
| 47 } | 47 } |
| 48 ASSERT_GT(unwaited_image_loads_, 0); | 48 ASSERT_GT(unwaited_image_loads_, 0); |
| 49 unwaited_image_loads_--; | 49 unwaited_image_loads_--; |
| 50 } | 50 } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 virtual void SetUp() { | 53 virtual void SetUp() { |
|
Yoyo Zhou
2013/04/09 17:19:28
Also call extensions::ExtensionTest::SetUp() here.
hashimoto
2013/04/10 02:40:14
Done.
| |
| 54 file_thread_.Start(); | 54 file_thread_.Start(); |
| 55 io_thread_.Start(); | 55 io_thread_.Start(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // The number of observed image loads that have not been waited for. | 58 // The number of observed image loads that have not been waited for. |
| 59 int unwaited_image_loads_; | 59 int unwaited_image_loads_; |
| 60 | 60 |
| 61 // Whether we are currently waiting for an image load. | 61 // Whether we are currently waiting for an image load. |
| 62 bool waiting_; | 62 bool waiting_; |
| 63 | 63 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 WaitForImageLoad(); | 135 WaitForImageLoad(); |
| 136 SkBitmap second_icon = icon_manager.GetIcon(extension->id()); | 136 SkBitmap second_icon = icon_manager.GetIcon(extension->id()); |
| 137 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon)); | 137 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon)); |
| 138 | 138 |
| 139 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon)); | 139 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 #if defined(FILE_MANAGER_EXTENSION) | 142 #if defined(FILE_MANAGER_EXTENSION) |
| 143 // Tests loading an icon for a component extension. | 143 // Tests loading an icon for a component extension. |
| 144 TEST_F(ExtensionIconManagerTest, LoadComponentExtensionResource) { | 144 TEST_F(ExtensionIconManagerTest, LoadComponentExtensionResource) { |
| 145 scoped_ptr<Profile> profile(new TestingProfile()); | |
| 145 SkBitmap default_icon = GetDefaultIcon(); | 146 SkBitmap default_icon = GetDefaultIcon(); |
| 146 | 147 |
| 147 base::FilePath test_dir; | 148 base::FilePath test_dir; |
| 148 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 149 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
| 149 base::FilePath manifest_path = test_dir.AppendASCII( | 150 base::FilePath manifest_path = test_dir.AppendASCII( |
| 150 "extensions/file_manager/app.json"); | 151 "extensions/file_manager/app.json"); |
| 151 | 152 |
| 152 JSONFileValueSerializer serializer(manifest_path); | 153 JSONFileValueSerializer serializer(manifest_path); |
| 153 scoped_ptr<DictionaryValue> manifest( | 154 scoped_ptr<DictionaryValue> manifest( |
| 154 static_cast<DictionaryValue*>(serializer.Deserialize(NULL, NULL))); | 155 static_cast<DictionaryValue*>(serializer.Deserialize(NULL, NULL))); |
| 155 ASSERT_TRUE(manifest.get() != NULL); | 156 ASSERT_TRUE(manifest.get() != NULL); |
| 156 | 157 |
| 157 std::string error; | 158 std::string error; |
| 158 scoped_refptr<Extension> extension(Extension::Create( | 159 scoped_refptr<Extension> extension(Extension::Create( |
| 159 manifest_path.DirName(), Manifest::COMPONENT, *manifest.get(), | 160 manifest_path.DirName(), Manifest::COMPONENT, *manifest.get(), |
| 160 Extension::NO_FLAGS, &error)); | 161 Extension::NO_FLAGS, &error)); |
| 161 ASSERT_TRUE(extension.get()); | 162 ASSERT_TRUE(extension.get()); |
| 162 | 163 |
| 163 scoped_ptr<Profile> profile(new TestingProfile()); | |
| 164 TestIconManager icon_manager(this); | 164 TestIconManager icon_manager(this); |
| 165 // Load the icon and grab the bitmap. | 165 // Load the icon and grab the bitmap. |
| 166 icon_manager.LoadIcon(profile.get(), extension.get()); | 166 icon_manager.LoadIcon(profile.get(), extension.get()); |
| 167 WaitForImageLoad(); | 167 WaitForImageLoad(); |
| 168 SkBitmap first_icon = icon_manager.GetIcon(extension->id()); | 168 SkBitmap first_icon = icon_manager.GetIcon(extension->id()); |
| 169 EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon)); | 169 EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon)); |
| 170 | 170 |
| 171 // Remove the icon from the manager. | 171 // Remove the icon from the manager. |
| 172 icon_manager.RemoveIcon(extension->id()); | 172 icon_manager.RemoveIcon(extension->id()); |
| 173 | 173 |
| 174 // Now re-load the icon - we should get the same result bitmap (and not the | 174 // Now re-load the icon - we should get the same result bitmap (and not the |
| 175 // default icon). | 175 // default icon). |
| 176 icon_manager.LoadIcon(profile.get(), extension.get()); | 176 icon_manager.LoadIcon(profile.get(), extension.get()); |
| 177 WaitForImageLoad(); | 177 WaitForImageLoad(); |
| 178 SkBitmap second_icon = icon_manager.GetIcon(extension->id()); | 178 SkBitmap second_icon = icon_manager.GetIcon(extension->id()); |
| 179 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon)); | 179 EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon)); |
| 180 | 180 |
| 181 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon)); | 181 EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon)); |
| 182 } | 182 } |
| 183 #endif | 183 #endif |
| OLD | NEW |