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 all reps in |reps| are present in |image|. | |
23 bool ContainsSameRepresentation(const std::vector<ImageSkiaRep>& reps, | |
msw
2017/03/23 20:47:52
nit: look at gfx::test::ImageSkiaStructureMatches,
xiyuan
2017/03/24 05:45:31
Acknowledged.
| |
24 const ImageSkia& image) { | |
25 for (const auto& rep : reps) { | |
msw
2017/03/23 20:47:52
Should we check that image.image_reps().size() ==
xiyuan
2017/03/24 05:45:27
Done. Revised code to get image_reps() from |image
| |
26 if (!gfx::BitmapsAreEqual( | |
27 rep.sk_bitmap(), | |
28 image.GetRepresentation(rep.scale()).sk_bitmap())) { | |
msw
2017/03/23 20:47:52
Should we be checking HasRepresentation before cal
xiyuan
2017/03/24 05:45:24
Good point. GetRepresentation should not be used.
| |
29 return false; | |
30 } | |
31 } | |
32 return true; | |
33 } | |
34 | |
35 class TestImageSkiaSource : public ImageSkiaSource { | |
msw
2017/03/23 20:47:52
nit: add a comment
xiyuan
2017/03/24 05:45:26
Done.
| |
36 public: | |
37 explicit TestImageSkiaSource(const gfx::Size& dip_size) | |
38 : dip_size_(dip_size) {} | |
39 ~TestImageSkiaSource() override = default; | |
40 | |
41 // ImageSkiaSource: | |
42 ImageSkiaRep GetImageForScale(float scale) override { | |
43 return ImageSkiaRep(ScaleToCeiledSize(dip_size_, scale), scale); | |
44 } | |
45 | |
46 private: | |
47 const gfx::Size dip_size_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(TestImageSkiaSource); | |
50 }; | |
51 | |
52 class ImageTraitsTest : public testing::Test, | |
msw
2017/03/23 20:47:52
See my earlier comment; hopefully the test service
xiyuan
2017/03/24 05:45:28
Tried it and handles are not supported in Deserial
| |
53 public mojom::ImageTraitsTestService { | |
54 public: | |
55 ImageTraitsTest() = default; | |
56 | |
57 // testing::Test: | |
58 void SetUp() override { | |
59 service_ = bindings_.CreateInterfacePtrAndBind(this); | |
60 } | |
61 | |
62 mojom::ImageTraitsTestServicePtr& service() { return service_; } | |
63 | |
64 private: | |
65 // mojom::ImageTraitsTestService: | |
66 void EchoImageSkiaRep(const ImageSkiaRep& in, | |
67 const EchoImageSkiaRepCallback& callback) override { | |
68 callback.Run(in); | |
69 } | |
70 void EchoImageSkia(const ImageSkia& in, | |
71 const EchoImageSkiaCallback& callback) override { | |
72 callback.Run(in); | |
73 } | |
74 | |
75 base::MessageLoop loop_; | |
76 mojo::BindingSet<ImageTraitsTestService> bindings_; | |
77 mojom::ImageTraitsTestServicePtr service_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(ImageTraitsTest); | |
80 }; | |
81 | |
82 } // namespace | |
83 | |
84 TEST_F(ImageTraitsTest, NullImageSkiaRep) { | |
85 ImageSkiaRep null_rep; | |
86 ASSERT_TRUE(null_rep.is_null()); | |
87 | |
88 ImageSkiaRep output(gfx::Size(1, 1), 1.0f); | |
89 ASSERT_FALSE(output.is_null()); | |
90 service()->EchoImageSkiaRep(null_rep, &output); | |
91 EXPECT_TRUE(output.is_null()); | |
92 } | |
93 | |
94 TEST_F(ImageTraitsTest, EmptyImageSkiaRepIsNull) { | |
95 SkBitmap empty_bitmap; | |
96 empty_bitmap.allocN32Pixels(0, 0); | |
msw
2017/03/23 20:47:52
optional nit: check if the bitmap is null?
xiyuan
2017/03/24 05:45:31
Done.
| |
97 ImageSkiaRep image_rep(empty_bitmap, 1.0f); | |
msw
2017/03/23 20:47:52
nit: after this, ASSERT_TRUE(image_rep.is_null());
xiyuan
2017/03/24 05:45:23
This is the tricky part, might be a bug with curre
| |
98 | |
99 ImageSkiaRep output(gfx::Size(1, 1), 1.0f); | |
100 ASSERT_FALSE(output.is_null()); | |
101 service()->EchoImageSkiaRep(image_rep, &output); | |
102 EXPECT_TRUE(output.is_null()); | |
103 } | |
104 | |
105 TEST_F(ImageTraitsTest, ImageSkiaRep) { | |
106 ImageSkiaRep image_rep(gfx::Size(2, 4), 2.0f); | |
107 | |
108 ImageSkiaRep output; | |
109 service()->EchoImageSkiaRep(image_rep, &output); | |
110 | |
111 EXPECT_FALSE(output.is_null()); | |
112 EXPECT_EQ(image_rep.scale(), output.scale()); | |
113 EXPECT_EQ(image_rep.pixel_size(), output.pixel_size()); | |
114 EXPECT_EQ(image_rep.GetWidth(), output.GetWidth()); | |
115 EXPECT_EQ(image_rep.GetHeight(), output.GetHeight()); | |
116 EXPECT_TRUE(gfx::BitmapsAreEqual(image_rep.sk_bitmap(), output.sk_bitmap())); | |
117 } | |
118 | |
119 TEST_F(ImageTraitsTest, NullImageSkia) { | |
120 ImageSkia null_image; | |
121 ASSERT_TRUE(null_image.isNull()); | |
122 | |
123 ImageSkia output(ImageSkiaRep(gfx::Size(1, 1), 1.0f)); | |
124 ASSERT_FALSE(output.isNull()); | |
125 service()->EchoImageSkia(null_image, &output); | |
126 EXPECT_TRUE(output.isNull()); | |
127 } | |
128 | |
129 TEST_F(ImageTraitsTest, ImageSkiaWithNoRepsTreatedAsNull) { | |
msw
2017/03/23 20:47:52
Hmm, I wonder if this behavior will bite us later.
xiyuan
2017/03/24 05:45:30
Possible. But there is not much we could do at thi
| |
130 const gfx::Size kSize(1, 2); | |
131 ImageSkia image(new TestImageSkiaSource(kSize), kSize); | |
132 ASSERT_FALSE(image.isNull()); | |
133 | |
134 ImageSkia output(ImageSkiaRep(gfx::Size(1, 1), 1.0f)); | |
135 ASSERT_FALSE(output.isNull()); | |
136 service()->EchoImageSkia(image, &output); | |
137 | |
msw
2017/03/23 20:47:52
optional nit: remove blank line
xiyuan
2017/03/24 05:45:29
Done.
| |
138 EXPECT_TRUE(output.isNull()); | |
139 } | |
140 | |
141 TEST_F(ImageTraitsTest, ImageSkia) { | |
142 const gfx::Size kSize(1, 2); | |
143 ImageSkia image(new TestImageSkiaSource(kSize), kSize); | |
144 image.GetRepresentation(1.0f); | |
145 image.GetRepresentation(2.0f); | |
146 | |
147 ImageSkia output; | |
148 service()->EchoImageSkia(image, &output); | |
149 | |
150 EXPECT_FALSE(output.isNull()); | |
151 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); | |
152 EXPECT_EQ(2u, output_reps.size()); | |
153 EXPECT_TRUE(ContainsSameRepresentation(output_reps, image)); | |
154 } | |
155 | |
156 TEST_F(ImageTraitsTest, EmptyRepStripped) { | |
157 const gfx::Size kSize(1, 2); | |
158 ImageSkia image(new TestImageSkiaSource(kSize), kSize); | |
159 image.GetRepresentation(1.0f); | |
160 | |
161 SkBitmap empty_bitmap; | |
162 empty_bitmap.allocN32Pixels(0, 0); | |
163 image.AddRepresentation(ImageSkiaRep(empty_bitmap, 2.0f)); | |
164 | |
165 ImageSkia output; | |
166 service()->EchoImageSkia(image, &output); | |
167 | |
168 EXPECT_FALSE(output.isNull()); | |
169 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); | |
170 EXPECT_EQ(1u, output_reps.size()); | |
171 EXPECT_TRUE(ContainsSameRepresentation(output_reps, image)); | |
172 } | |
173 | |
174 TEST_F(ImageTraitsTest, ImageSkiaWithOperations) { | |
175 const gfx::Size kSize(32, 32); | |
176 ImageSkia image(new TestImageSkiaSource(kSize), kSize); | |
177 | |
178 const gfx::Size kNewSize(16, 16); | |
179 ImageSkia resized = ImageSkiaOperations::CreateResizedImage( | |
180 image, skia::ImageOperations::RESIZE_BEST, kNewSize); | |
181 resized.GetRepresentation(1.0f); | |
182 resized.GetRepresentation(2.0f); | |
183 | |
184 ImageSkia output; | |
185 service()->EchoImageSkia(resized, &output); | |
186 | |
187 EXPECT_FALSE(output.isNull()); | |
188 EXPECT_EQ(kNewSize, output.size()); | |
189 const std::vector<ImageSkiaRep> output_reps = output.image_reps(); | |
190 EXPECT_EQ(2u, output_reps.size()); | |
191 EXPECT_TRUE(ContainsSameRepresentation(output_reps, resized)); | |
192 } | |
193 | |
194 } // namespace gfx | |
OLD | NEW |