This source file includes following definitions.
- io_thread_
- ImageLoadObserved
- WaitForImageLoad
- SetUp
- OnImageLoaded
- GetDefaultIcon
- TEST_F
- TEST_F
#include "base/json/json_file_value_serializer.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_icon_manager.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/common/extension.h"
#include "extensions/common/id_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/skia_util.h"
using content::BrowserThread;
using extensions::Extension;
using extensions::Manifest;
class ExtensionIconManagerTest : public testing::Test {
public:
ExtensionIconManagerTest() :
unwaited_image_loads_(0),
waiting_(false),
ui_thread_(BrowserThread::UI, &ui_loop_),
file_thread_(BrowserThread::FILE),
io_thread_(BrowserThread::IO) {}
virtual ~ExtensionIconManagerTest() {}
void ImageLoadObserved() {
unwaited_image_loads_++;
if (waiting_) {
base::MessageLoop::current()->Quit();
}
}
void WaitForImageLoad() {
if (unwaited_image_loads_ == 0) {
waiting_ = true;
base::MessageLoop::current()->Run();
waiting_ = false;
}
ASSERT_GT(unwaited_image_loads_, 0);
unwaited_image_loads_--;
}
private:
virtual void SetUp() {
file_thread_.Start();
io_thread_.Start();
}
int unwaited_image_loads_;
bool waiting_;
base::MessageLoop ui_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
content::TestBrowserThread io_thread_;
DISALLOW_COPY_AND_ASSIGN(ExtensionIconManagerTest);
};
class TestIconManager : public ExtensionIconManager {
public:
explicit TestIconManager(ExtensionIconManagerTest* test) : test_(test) {}
virtual ~TestIconManager() {}
virtual void OnImageLoaded(const std::string& extension_id,
const gfx::Image& image) OVERRIDE {
ExtensionIconManager::OnImageLoaded(extension_id, image);
test_->ImageLoadObserved();
}
private:
ExtensionIconManagerTest* test_;
DISALLOW_COPY_AND_ASSIGN(TestIconManager);
};
SkBitmap GetDefaultIcon() {
std::string dummy_id = extensions::id_util::GenerateId("whatever");
ExtensionIconManager manager;
return manager.GetIcon(dummy_id);
}
TEST_F(ExtensionIconManagerTest, LoadRemoveLoad) {
scoped_ptr<Profile> profile(new TestingProfile());
SkBitmap default_icon = GetDefaultIcon();
base::FilePath test_dir;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
base::FilePath manifest_path = test_dir.AppendASCII(
"extensions/image_loading_tracker/app.json");
JSONFileValueSerializer serializer(manifest_path);
scoped_ptr<base::DictionaryValue> manifest(
static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, NULL)));
ASSERT_TRUE(manifest.get() != NULL);
std::string error;
scoped_refptr<Extension> extension(Extension::Create(
manifest_path.DirName(), Manifest::INVALID_LOCATION, *manifest.get(),
Extension::NO_FLAGS, &error));
ASSERT_TRUE(extension.get());
TestIconManager icon_manager(this);
icon_manager.LoadIcon(profile.get(), extension.get());
WaitForImageLoad();
SkBitmap first_icon = icon_manager.GetIcon(extension->id());
EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon));
icon_manager.RemoveIcon(extension->id());
icon_manager.LoadIcon(profile.get(), extension.get());
WaitForImageLoad();
SkBitmap second_icon = icon_manager.GetIcon(extension->id());
EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon));
EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon));
}
#if defined(OS_CHROMEOS)
TEST_F(ExtensionIconManagerTest, LoadComponentExtensionResource) {
scoped_ptr<Profile> profile(new TestingProfile());
SkBitmap default_icon = GetDefaultIcon();
base::FilePath test_dir;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
base::FilePath manifest_path = test_dir.AppendASCII(
"extensions/file_manager/app.json");
JSONFileValueSerializer serializer(manifest_path);
scoped_ptr<base::DictionaryValue> manifest(
static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, NULL)));
ASSERT_TRUE(manifest.get() != NULL);
std::string error;
scoped_refptr<Extension> extension(Extension::Create(
manifest_path.DirName(), Manifest::COMPONENT, *manifest.get(),
Extension::NO_FLAGS, &error));
ASSERT_TRUE(extension.get());
TestIconManager icon_manager(this);
icon_manager.LoadIcon(profile.get(), extension.get());
WaitForImageLoad();
SkBitmap first_icon = icon_manager.GetIcon(extension->id());
EXPECT_FALSE(gfx::BitmapsAreEqual(first_icon, default_icon));
icon_manager.RemoveIcon(extension->id());
icon_manager.LoadIcon(profile.get(), extension.get());
WaitForImageLoad();
SkBitmap second_icon = icon_manager.GetIcon(extension->id());
EXPECT_FALSE(gfx::BitmapsAreEqual(second_icon, default_icon));
EXPECT_TRUE(gfx::BitmapsAreEqual(first_icon, second_icon));
}
#endif