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 "chrome/browser/extensions/extension_icon_image.h" | 5 #include "chrome/browser/extensions/extension_icon_image.h" |
| 6 | 6 |
| 7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/extensions/image_loader.h" | 10 #include "chrome/browser/extensions/image_loader.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 private: | 66 private: |
| 67 gfx::ImageSkia image_; | 67 gfx::ImageSkia image_; |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // Helper class for synchronously loading extension image resource. | 70 // Helper class for synchronously loading extension image resource. |
| 71 class TestImageLoader { | 71 class TestImageLoader { |
| 72 public: | 72 public: |
| 73 explicit TestImageLoader(const Extension* extension) | 73 explicit TestImageLoader(const Extension* extension) |
| 74 : extension_(extension), | 74 : extension_(extension), |
| 75 waiting_(false), | 75 waiting_(false), |
| 76 image_loaded_(false) { | 76 image_loaded_(false), |
| 77 image_loader_(NULL) { | |
| 77 } | 78 } |
| 78 virtual ~TestImageLoader() {} | 79 |
| 80 virtual ~TestImageLoader() { | |
| 81 delete image_loader_; | |
| 82 } | |
| 79 | 83 |
| 80 void OnImageLoaded(const gfx::Image& image) { | 84 void OnImageLoaded(const gfx::Image& image) { |
| 81 image_ = image; | 85 image_ = image; |
| 82 image_loaded_ = true; | 86 image_loaded_ = true; |
| 83 if (waiting_) | 87 if (waiting_) |
| 84 base::MessageLoop::current()->Quit(); | 88 base::MessageLoop::current()->Quit(); |
| 85 } | 89 } |
| 86 | 90 |
| 87 SkBitmap LoadBitmap(const std::string& path, | 91 SkBitmap LoadBitmap(const std::string& path, |
| 88 int size) { | 92 int size) { |
| 89 image_loaded_ = false; | 93 image_loaded_ = false; |
| 90 | 94 |
| 91 image_loader_.LoadImageAsync( | 95 image_loader_ = new extensions::ImageLoader(NULL); |
| 96 image_loader_->LoadImageAsync( | |
| 92 extension_, extension_->GetResource(path), gfx::Size(size, size), | 97 extension_, extension_->GetResource(path), gfx::Size(size, size), |
| 93 base::Bind(&TestImageLoader::OnImageLoaded, | 98 base::Bind(&TestImageLoader::OnImageLoaded, |
| 94 base::Unretained(this))); | 99 base::Unretained(this))); |
| 95 | 100 |
| 96 // If |image_| still hasn't been loaded (i.e. it is being loaded | 101 // If |image_| still hasn't been loaded (i.e. it is being loaded |
| 97 // asynchronously), wait for it. | 102 // asynchronously), wait for it. |
| 98 if (!image_loaded_) { | 103 if (!image_loaded_) { |
| 99 waiting_ = true; | 104 waiting_ = true; |
| 100 base::MessageLoop::current()->Run(); | 105 base::MessageLoop::current()->Run(); |
| 101 waiting_ = false; | 106 waiting_ = false; |
| 102 } | 107 } |
| 103 | 108 |
| 104 EXPECT_TRUE(image_loaded_); | 109 EXPECT_TRUE(image_loaded_); |
| 105 | 110 |
| 106 return image_.IsEmpty() ? SkBitmap() : *image_.ToSkBitmap(); | 111 return image_.IsEmpty() ? SkBitmap() : *image_.ToSkBitmap(); |
| 107 } | 112 } |
| 108 | 113 |
| 109 private: | 114 private: |
| 110 const Extension* extension_; | 115 const Extension* extension_; |
| 111 bool waiting_; | 116 bool waiting_; |
| 112 bool image_loaded_; | 117 bool image_loaded_; |
| 113 gfx::Image image_; | 118 gfx::Image image_; |
| 114 extensions::ImageLoader image_loader_; | 119 extensions::ImageLoader* image_loader_; |
|
Finnur
2013/10/02 09:52:15
nit: scoped_ptr<extensions::ImageLoader> image_loa
| |
| 115 | 120 |
| 116 DISALLOW_COPY_AND_ASSIGN(TestImageLoader); | 121 DISALLOW_COPY_AND_ASSIGN(TestImageLoader); |
| 117 }; | 122 }; |
| 118 | 123 |
| 119 class ExtensionIconImageTest : public testing::Test, | 124 class ExtensionIconImageTest : public testing::Test, |
| 120 public IconImage::Observer { | 125 public IconImage::Observer { |
| 121 public: | 126 public: |
| 122 ExtensionIconImageTest() | 127 ExtensionIconImageTest() |
| 123 : image_loaded_count_(0), | 128 : image_loaded_count_(0), |
| 124 quit_in_image_loaded_(false), | 129 quit_in_image_loaded_(false), |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 EXPECT_EQ(16, representation.pixel_width()); | 568 EXPECT_EQ(16, representation.pixel_width()); |
| 564 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); | 569 EXPECT_TRUE(gfx::BitmapsAreEqual(representation.sk_bitmap(), bitmap_16)); |
| 565 | 570 |
| 566 // When requesting another representation, we should get blank image. | 571 // When requesting another representation, we should get blank image. |
| 567 representation = image_skia.GetRepresentation(2.0f); | 572 representation = image_skia.GetRepresentation(2.0f); |
| 568 | 573 |
| 569 EXPECT_TRUE(gfx::BitmapsAreEqual( | 574 EXPECT_TRUE(gfx::BitmapsAreEqual( |
| 570 representation.sk_bitmap(), | 575 representation.sk_bitmap(), |
| 571 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); | 576 CreateBlankBitmapForScale(16, ui::SCALE_FACTOR_200P))); |
| 572 } | 577 } |
| OLD | NEW |