OLD | NEW |
(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_icon_image.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/browser/extensions/extension_icon_image_delegate.h" |
| 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "content/public/test/test_browser_thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 using extensions::Extension; |
| 18 |
| 19 class ExtensionIconImageTest : public testing::Test, |
| 20 public ExtensionIconImageDelegate { |
| 21 public: |
| 22 ExtensionIconImageTest() |
| 23 : image_loaded_count_(0), |
| 24 quit_in_image_loaded_(false), |
| 25 ui_thread_(BrowserThread::UI, &ui_loop_), |
| 26 file_thread_(BrowserThread::FILE), |
| 27 io_thread_(BrowserThread::IO) { |
| 28 } |
| 29 |
| 30 void WaitForImageLoad() { |
| 31 quit_in_image_loaded_ = true; |
| 32 MessageLoop::current()->Run(); |
| 33 quit_in_image_loaded_ = false; |
| 34 } |
| 35 |
| 36 int image_loaded_count() { |
| 37 int result = image_loaded_count_; |
| 38 image_loaded_count_ = 0; |
| 39 return result; |
| 40 } |
| 41 |
| 42 scoped_refptr<Extension> CreateExtension(const char* name, |
| 43 Extension::Location location) { |
| 44 // Create and load an extension. |
| 45 FilePath test_file; |
| 46 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
| 47 EXPECT_FALSE(true); |
| 48 return NULL; |
| 49 } |
| 50 test_file = test_file.AppendASCII("extensions") |
| 51 .AppendASCII(name); |
| 52 int error_code = 0; |
| 53 std::string error; |
| 54 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
| 55 scoped_ptr<DictionaryValue> valid_value( |
| 56 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
| 57 &error))); |
| 58 EXPECT_EQ(0, error_code) << error; |
| 59 if (error_code != 0) |
| 60 return NULL; |
| 61 |
| 62 EXPECT_TRUE(valid_value.get()); |
| 63 if (!valid_value.get()) |
| 64 return NULL; |
| 65 |
| 66 return Extension::Create(test_file, location, *valid_value, |
| 67 Extension::NO_FLAGS, &error); |
| 68 } |
| 69 |
| 70 // testing::Test overrides: |
| 71 virtual void SetUp() OVERRIDE { |
| 72 file_thread_.Start(); |
| 73 io_thread_.Start(); |
| 74 } |
| 75 |
| 76 // ExtensionIconImageDelegate overrides: |
| 77 virtual void OnExtensionIconImageChanged(ExtensionIconImage* image) OVERRIDE { |
| 78 image_loaded_count_++; |
| 79 if (quit_in_image_loaded_) |
| 80 MessageLoop::current()->Quit(); |
| 81 } |
| 82 |
| 83 private: |
| 84 int image_loaded_count_; |
| 85 bool quit_in_image_loaded_; |
| 86 MessageLoop ui_loop_; |
| 87 content::TestBrowserThread ui_thread_; |
| 88 content::TestBrowserThread file_thread_; |
| 89 content::TestBrowserThread io_thread_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(ExtensionIconImageTest); |
| 92 }; |
| 93 |
| 94 TEST_F(ExtensionIconImageTest, Basic) { |
| 95 scoped_refptr<Extension> extension(CreateExtension( |
| 96 "image_loading_tracker", Extension::INVALID)); |
| 97 ASSERT_TRUE(extension.get() != NULL); |
| 98 |
| 99 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage( |
| 100 extension, |
| 101 extension->icons(), |
| 102 ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 103 ExtensionIconSet::MATCH_SMALLER, |
| 104 gfx::Size(ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 105 ExtensionIconSet::EXTENSION_ICON_BITTY), |
| 106 ImageLoadingTracker::DONT_CACHE, |
| 107 this)); |
| 108 |
| 109 EXPECT_FALSE(image->IsNull()); |
| 110 |
| 111 // No representations in |image_| yet. |
| 112 gfx::ImageSkia::ImageSkiaReps image_reps = image->image_skia().image_reps(); |
| 113 ASSERT_EQ(0u, image_reps.size()); |
| 114 |
| 115 // Gets representation for a scale factor. |
| 116 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 117 WaitForImageLoad(); |
| 118 EXPECT_EQ(1, image_loaded_count()); |
| 119 |
| 120 gfx::ImageSkiaRep image_rep = |
| 121 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 122 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY, |
| 123 image_rep.pixel_width()); |
| 124 |
| 125 // Gets representation for an additional scale factor. |
| 126 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 127 WaitForImageLoad(); |
| 128 EXPECT_EQ(1, image_loaded_count()); |
| 129 |
| 130 image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 131 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALL, |
| 132 image_rep.pixel_width()); |
| 133 } |
| 134 |
| 135 TEST_F(ExtensionIconImageTest, BadResource) { |
| 136 scoped_refptr<Extension> extension(CreateExtension( |
| 137 "image_loading_tracker", Extension::INVALID)); |
| 138 ASSERT_TRUE(extension.get() != NULL); |
| 139 |
| 140 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage( |
| 141 extension, |
| 142 extension->icons(), |
| 143 ExtensionIconSet::EXTENSION_ICON_GIGANTOR, |
| 144 ExtensionIconSet::MATCH_BIGGER, |
| 145 gfx::Size(ExtensionIconSet::EXTENSION_ICON_GIGANTOR, |
| 146 ExtensionIconSet::EXTENSION_ICON_GIGANTOR), |
| 147 ImageLoadingTracker::DONT_CACHE, |
| 148 this)); |
| 149 |
| 150 EXPECT_TRUE(image->IsNull()); |
| 151 } |
| 152 |
| 153 TEST_F(ExtensionIconImageTest, Missing2x) { |
| 154 scoped_refptr<Extension> extension(CreateExtension( |
| 155 "image_loading_tracker", Extension::INVALID)); |
| 156 ASSERT_TRUE(extension.get() != NULL); |
| 157 |
| 158 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage( |
| 159 extension, |
| 160 extension->icons(), |
| 161 ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 162 ExtensionIconSet::MATCH_BIGGER, |
| 163 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 164 ExtensionIconSet::EXTENSION_ICON_SMALLISH), |
| 165 ImageLoadingTracker::DONT_CACHE, |
| 166 this)); |
| 167 |
| 168 // Gets representation for 1x. |
| 169 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 170 WaitForImageLoad(); |
| 171 EXPECT_EQ(1, image_loaded_count()); |
| 172 |
| 173 gfx::ImageSkiaRep image_rep = |
| 174 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_100P); |
| 175 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 176 image_rep.pixel_width()); |
| 177 |
| 178 // Get representation for 2x. |
| 179 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 180 |
| 181 image_rep = image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 182 |
| 183 // 1x representation would be returned since there is no 2x resource. |
| 184 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); |
| 185 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 186 image_rep.pixel_width()); |
| 187 } |
| 188 |
| 189 // Similar to Missing2x test but go directly for the missing 2x resource first. |
| 190 TEST_F(ExtensionIconImageTest, FallbackTo1x) { |
| 191 scoped_refptr<Extension> extension(CreateExtension( |
| 192 "image_loading_tracker", Extension::INVALID)); |
| 193 ASSERT_TRUE(extension.get() != NULL); |
| 194 |
| 195 scoped_ptr<ExtensionIconImage> image(new ExtensionIconImage( |
| 196 extension, |
| 197 extension->icons(), |
| 198 ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 199 ExtensionIconSet::MATCH_BIGGER, |
| 200 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 201 ExtensionIconSet::EXTENSION_ICON_SMALLISH), |
| 202 ImageLoadingTracker::DONT_CACHE, |
| 203 this)); |
| 204 |
| 205 // Attempt to get representation for 2x. |
| 206 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 207 |
| 208 WaitForImageLoad(); |
| 209 EXPECT_EQ(1, image_loaded_count()); |
| 210 |
| 211 // 1x representation would be returned since there is no 2x resource. |
| 212 gfx::ImageSkiaRep image_rep = |
| 213 image->image_skia().GetRepresentation(ui::SCALE_FACTOR_200P); |
| 214 EXPECT_EQ(ui::SCALE_FACTOR_100P, image_rep.scale_factor()); |
| 215 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
| 216 image_rep.pixel_width()); |
| 217 } |
OLD | NEW |