OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 <vector> |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/public/cpp/bindings/binding_set.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "ui/gfx/image/image_skia_operations.h" |
| 14 #include "ui/gfx/image/image_skia_source.h" |
| 15 #include "ui/gfx/image/mojo/image_traits_test_service.mojom.h" |
| 16 #include "ui/gfx/skia_util.h" |
| 17 |
| 18 namespace gfx { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Returns true if |reps| matches the representations in |image| except for |
| 23 // the null/empty ones. |
| 24 bool ContainsSameRepresentation(const std::vector<ImageSkiaRep>& reps, |
| 25 const ImageSkia& image) { |
| 26 std::vector<ImageSkiaRep> check_reps = image.image_reps(); |
| 27 |
| 28 // Strip null/empty representations. |
| 29 auto check_it = check_reps.begin(); |
| 30 while (check_it != check_reps.end()) { |
| 31 if (check_it->sk_bitmap().drawsNothing()) |
| 32 check_it = check_reps.erase(check_it); |
| 33 else |
| 34 ++check_it; |
| 35 } |
| 36 |
| 37 if (reps.size() != check_reps.size()) |
| 38 return false; |
| 39 |
| 40 auto it = reps.begin(); |
| 41 check_it = check_reps.begin(); |
| 42 while (it != reps.end() && check_it != check_reps.end()) { |
| 43 if (!gfx::BitmapsAreEqual(it->sk_bitmap(), check_it->sk_bitmap())) |
| 44 return false; |
| 45 |
| 46 ++it; |
| 47 ++check_it; |
| 48 } |
| 49 |
| 50 return it == reps.end() && check_it == check_reps.end(); |
| 51 } |
| 52 |
| 53 // A test ImageSkiaSource that creates an ImageSkiaRep for any scale. |
| 54 class TestImageSkiaSource : public ImageSkiaSource { |
| 55 public: |
| 56 explicit TestImageSkiaSource(const gfx::Size& dip_size) |
| 57 : dip_size_(dip_size) {} |
| 58 ~TestImageSkiaSource() override = default; |
| 59 |
| 60 // ImageSkiaSource: |
| 61 ImageSkiaRep GetImageForScale(float scale) override { |
| 62 return ImageSkiaRep(ScaleToCeiledSize(dip_size_, scale), scale); |
| 63 } |
| 64 |
| 65 private: |
| 66 const gfx::Size dip_size_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(TestImageSkiaSource); |
| 69 }; |
| 70 |
| 71 class ImageTraitsTest : public testing::Test, |
| 72 public mojom::ImageTraitsTestService { |
| 73 public: |
| 74 ImageTraitsTest() = default; |
| 75 |
| 76 // testing::Test: |
| 77 void SetUp() override { |
| 78 service_ = bindings_.CreateInterfacePtrAndBind(this); |
| 79 } |
| 80 |
| 81 mojom::ImageTraitsTestServicePtr& service() { return service_; } |
| 82 |
| 83 private: |
| 84 // mojom::ImageTraitsTestService: |
| 85 void EchoImageSkiaRep(const ImageSkiaRep& in, |
| 86 const EchoImageSkiaRepCallback& callback) override { |
| 87 callback.Run(in); |
| 88 } |
| 89 void EchoImageSkia(const ImageSkia& in, |
| 90 const EchoImageSkiaCallback& callback) override { |
| 91 callback.Run(in); |
| 92 } |
| 93 |
| 94 base::MessageLoop loop_; |
| 95 mojo::BindingSet<ImageTraitsTestService> bindings_; |
| 96 mojom::ImageTraitsTestServicePtr service_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ImageTraitsTest); |
| 99 }; |
| 100 |
| 101 } // namespace |
| 102 |
| 103 TEST_F(ImageTraitsTest, NullImageSkiaRep) { |
| 104 ImageSkiaRep null_rep; |
| 105 ASSERT_TRUE(null_rep.is_null()); |
| 106 |
| 107 ImageSkiaRep output(gfx::Size(1, 1), 1.0f); |
| 108 ASSERT_FALSE(output.is_null()); |
| 109 service()->EchoImageSkiaRep(null_rep, &output); |
| 110 EXPECT_TRUE(output.is_null()); |
| 111 } |
| 112 |
| 113 TEST_F(ImageTraitsTest, EmptyImageSkiaRepIsNull) { |
| 114 SkBitmap empty_bitmap; |
| 115 empty_bitmap.allocN32Pixels(0, 0); |
| 116 // Empty SkBitmap is not null. |
| 117 EXPECT_FALSE(empty_bitmap.isNull()); |
| 118 |
| 119 ImageSkiaRep empty_rep(empty_bitmap, 1.0f); |
| 120 // ImageSkiaRep with empty bitmap is not null. |
| 121 ASSERT_FALSE(empty_rep.is_null()); |
| 122 |
| 123 ImageSkiaRep output(gfx::Size(1, 1), 1.0f); |
| 124 ASSERT_FALSE(output.is_null()); |
| 125 service()->EchoImageSkiaRep(empty_rep, &output); |
| 126 EXPECT_TRUE(output.is_null()); |
| 127 } |
| 128 |
| 129 TEST_F(ImageTraitsTest, ImageSkiaRep) { |
| 130 ImageSkiaRep image_rep(gfx::Size(2, 4), 2.0f); |
| 131 |
| 132 ImageSkiaRep output; |
| 133 service()->EchoImageSkiaRep(image_rep, &output); |
| 134 |
| 135 EXPECT_FALSE(output.is_null()); |
| 136 EXPECT_EQ(image_rep.scale(), output.scale()); |
| 137 EXPECT_EQ(image_rep.pixel_size(), output.pixel_size()); |
| 138 EXPECT_EQ(image_rep.GetWidth(), output.GetWidth()); |
| 139 EXPECT_EQ(image_rep.GetHeight(), output.GetHeight()); |
| 140 EXPECT_TRUE(gfx::BitmapsAreEqual(image_rep.sk_bitmap(), output.sk_bitmap())); |
| 141 } |
| 142 |
| 143 TEST_F(ImageTraitsTest, UnscaledImageSkiaRep) { |
| 144 ImageSkiaRep image_rep(gfx::Size(2, 4), 0.0f); |
| 145 ASSERT_TRUE(image_rep.unscaled()); |
| 146 |
| 147 ImageSkiaRep output(gfx::Size(1, 1), 1.0f); |
| 148 EXPECT_FALSE(output.unscaled()); |
| 149 service()->EchoImageSkiaRep(image_rep, &output); |
| 150 EXPECT_TRUE(output.unscaled()); |
| 151 EXPECT_EQ(image_rep.pixel_size(), output.pixel_size()); |
| 152 EXPECT_TRUE(gfx::BitmapsAreEqual(image_rep.sk_bitmap(), output.sk_bitmap())); |
| 153 } |
| 154 |
| 155 TEST_F(ImageTraitsTest, NullImageSkia) { |
| 156 ImageSkia null_image; |
| 157 ASSERT_TRUE(null_image.isNull()); |
| 158 |
| 159 ImageSkia output(ImageSkiaRep(gfx::Size(1, 1), 1.0f)); |
| 160 ASSERT_FALSE(output.isNull()); |
| 161 service()->EchoImageSkia(null_image, &output); |
| 162 EXPECT_TRUE(output.isNull()); |
| 163 } |
| 164 |
| 165 TEST_F(ImageTraitsTest, ImageSkiaWithNoRepsTreatedAsNull) { |
| 166 const gfx::Size kSize(1, 2); |
| 167 ImageSkia image(new TestImageSkiaSource(kSize), kSize); |
| 168 ASSERT_FALSE(image.isNull()); |
| 169 |
| 170 ImageSkia output(ImageSkiaRep(gfx::Size(1, 1), 1.0f)); |
| 171 ASSERT_FALSE(output.isNull()); |
| 172 service()->EchoImageSkia(image, &output); |
| 173 EXPECT_TRUE(output.isNull()); |
| 174 } |
| 175 |
| 176 TEST_F(ImageTraitsTest, ImageSkia) { |
| 177 const gfx::Size kSize(1, 2); |
| 178 ImageSkia image(new TestImageSkiaSource(kSize), kSize); |
| 179 image.GetRepresentation(1.0f); |
| 180 image.GetRepresentation(2.0f); |
| 181 |
| 182 ImageSkia output; |
| 183 service()->EchoImageSkia(image, &output); |
| 184 |
| 185 EXPECT_FALSE(output.isNull()); |
| 186 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); |
| 187 EXPECT_EQ(2u, output_reps.size()); |
| 188 EXPECT_TRUE(ContainsSameRepresentation(output_reps, image)); |
| 189 } |
| 190 |
| 191 TEST_F(ImageTraitsTest, EmptyRepStripped) { |
| 192 const gfx::Size kSize(1, 2); |
| 193 ImageSkia image(new TestImageSkiaSource(kSize), kSize); |
| 194 image.GetRepresentation(1.0f); |
| 195 |
| 196 SkBitmap empty_bitmap; |
| 197 empty_bitmap.allocN32Pixels(0, 0); |
| 198 image.AddRepresentation(ImageSkiaRep(empty_bitmap, 2.0f)); |
| 199 |
| 200 ImageSkia output; |
| 201 service()->EchoImageSkia(image, &output); |
| 202 |
| 203 EXPECT_FALSE(output.isNull()); |
| 204 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); |
| 205 EXPECT_EQ(1u, output_reps.size()); |
| 206 EXPECT_TRUE(ContainsSameRepresentation(output_reps, image)); |
| 207 } |
| 208 |
| 209 TEST_F(ImageTraitsTest, ImageSkiaWithOperations) { |
| 210 const gfx::Size kSize(32, 32); |
| 211 ImageSkia image(new TestImageSkiaSource(kSize), kSize); |
| 212 |
| 213 const gfx::Size kNewSize(16, 16); |
| 214 ImageSkia resized = ImageSkiaOperations::CreateResizedImage( |
| 215 image, skia::ImageOperations::RESIZE_BEST, kNewSize); |
| 216 resized.GetRepresentation(1.0f); |
| 217 resized.GetRepresentation(2.0f); |
| 218 |
| 219 ImageSkia output; |
| 220 service()->EchoImageSkia(resized, &output); |
| 221 |
| 222 EXPECT_FALSE(output.isNull()); |
| 223 EXPECT_EQ(kNewSize, output.size()); |
| 224 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); |
| 225 EXPECT_EQ(2u, output_reps.size()); |
| 226 EXPECT_TRUE(ContainsSameRepresentation(output_reps, resized)); |
| 227 } |
| 228 |
| 229 } // namespace gfx |
OLD | NEW |