OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
6 #include "core/layout/ImageQualityController.h" | 6 #include "core/layout/ImageQualityController.h" |
7 | 7 |
8 #include "core/layout/LayoutImage.h" | 8 #include "core/layout/LayoutImage.h" |
9 #include "core/layout/LayoutTestHelper.h" | 9 #include "core/layout/LayoutTestHelper.h" |
10 #include "platform/graphics/GraphicsContext.h" | 10 #include "platform/graphics/GraphicsContext.h" |
11 #include "platform/graphics/paint/DisplayItemList.h" | 11 #include "platform/graphics/paint/PaintController.h" |
12 | 12 |
13 #include <gtest/gtest.h> | 13 #include <gtest/gtest.h> |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 | 16 |
17 class ImageQualityControllerTest : public RenderingTest { | 17 class ImageQualityControllerTest : public RenderingTest { |
18 protected: | 18 protected: |
19 ImageQualityController* controller() { return m_controller; } | 19 ImageQualityController* controller() { return m_controller; } |
20 | 20 |
21 private: | 21 private: |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 bool isBitmapImage() const override { return true; } | 99 bool isBitmapImage() const override { return true; } |
100 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } | 100 PassRefPtr<SkImage> imageForCurrentFrame() override { return nullptr; } |
101 }; | 101 }; |
102 | 102 |
103 TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage) | 103 TEST_F(ImageQualityControllerTest, MediumQualityFilterForUnscaledImage) |
104 { | 104 { |
105 setBodyInnerHTML("<img src='myimage'></img>"); | 105 setBodyInnerHTML("<img src='myimage'></img>"); |
106 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 106 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
107 | 107 |
108 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 108 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
109 OwnPtr<DisplayItemList> displayItemList = DisplayItemList::create(); | 109 OwnPtr<PaintController> paintController = PaintController::create(); |
110 GraphicsContext context(displayItemList.get()); | 110 GraphicsContext context(paintController.get()); |
111 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(1, 1))); | 111 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(1, 1))); |
112 } | 112 } |
113 | 113 |
114 class MockTimer : public Timer<ImageQualityController> { | 114 class MockTimer : public Timer<ImageQualityController> { |
115 typedef void (ImageQualityController::*TimerFiredFunction)(Timer*); | 115 typedef void (ImageQualityController::*TimerFiredFunction)(Timer*); |
116 public: | 116 public: |
117 MockTimer(ImageQualityController* o, TimerFiredFunction f) | 117 MockTimer(ImageQualityController* o, TimerFiredFunction f) |
118 : Timer<ImageQualityController>(o, f) | 118 : Timer<ImageQualityController>(o, f) |
119 { | 119 { |
120 } | 120 } |
121 | 121 |
122 void fire() | 122 void fire() |
123 { | 123 { |
124 this->Timer<ImageQualityController>::fired(); | 124 this->Timer<ImageQualityController>::fired(); |
125 stop(); | 125 stop(); |
126 } | 126 } |
127 }; | 127 }; |
128 | 128 |
129 TEST_F(ImageQualityControllerTest, LowQualityFilterForLiveResize) | 129 TEST_F(ImageQualityControllerTest, LowQualityFilterForLiveResize) |
130 { | 130 { |
131 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 131 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
132 controller()->setTimer(mockTimer); | 132 controller()->setTimer(mockTimer); |
133 setBodyInnerHTML("<img src='myimage'></img>"); | 133 setBodyInnerHTML("<img src='myimage'></img>"); |
134 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 134 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
135 | 135 |
136 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 136 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
137 OwnPtr<DisplayItemList> displayItemList = DisplayItemList::create(); | 137 OwnPtr<PaintController> paintController = PaintController::create(); |
138 GraphicsContext context(displayItemList.get()); | 138 GraphicsContext context(paintController.get()); |
139 | 139 |
140 // Start a resize | 140 // Start a resize |
141 document().frame()->view()->willStartLiveResize(); | 141 document().frame()->view()->willStartLiveResize(); |
142 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 142 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); |
143 | 143 |
144 document().frame()->view()->willEndLiveResize(); | 144 document().frame()->view()->willEndLiveResize(); |
145 | 145 |
146 // End of live resize, but timer has not fired. Therefore paint at non-low q
uality. | 146 // End of live resize, but timer has not fired. Therefore paint at non-low q
uality. |
147 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 147 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); |
148 | 148 |
(...skipping 10 matching lines...) Expand all Loading... |
159 } | 159 } |
160 | 160 |
161 TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage) | 161 TEST_F(ImageQualityControllerTest, LowQualityFilterForResizingImage) |
162 { | 162 { |
163 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 163 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
164 controller()->setTimer(mockTimer); | 164 controller()->setTimer(mockTimer); |
165 setBodyInnerHTML("<img src='myimage'></img>"); | 165 setBodyInnerHTML("<img src='myimage'></img>"); |
166 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 166 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
167 | 167 |
168 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 168 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
169 OwnPtr<DisplayItemList> displayItemList = DisplayItemList::create(); | 169 OwnPtr<PaintController> paintController = PaintController::create(); |
170 GraphicsContext context(displayItemList.get()); | 170 GraphicsContext context(paintController.get()); |
171 | 171 |
172 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. | 172 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. |
173 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 173 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); |
174 | 174 |
175 // Go into low-quality mode now that the size changed. | 175 // Go into low-quality mode now that the size changed. |
176 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 176 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); |
177 | 177 |
178 // Stay in low-quality mode since the size changed again. | 178 // Stay in low-quality mode since the size changed again. |
179 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 179 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
180 | 180 |
181 mockTimer->fire(); | 181 mockTimer->fire(); |
182 // The timer fired before painting at another size, so this doesn't count as
animation. Therefore not painting at low quality. | 182 // The timer fired before painting at another size, so this doesn't count as
animation. Therefore not painting at low quality. |
183 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 183 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
184 } | 184 } |
185 | 185 |
186 TEST_F(ImageQualityControllerTest, DontKickTheAnimationTimerWhenPaintingAtTheSam
eSize) | 186 TEST_F(ImageQualityControllerTest, DontKickTheAnimationTimerWhenPaintingAtTheSam
eSize) |
187 { | 187 { |
188 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); | 188 MockTimer* mockTimer = new MockTimer(controller(), &ImageQualityController::
highQualityRepaintTimerFired); |
189 controller()->setTimer(mockTimer); | 189 controller()->setTimer(mockTimer); |
190 setBodyInnerHTML("<img src='myimage'></img>"); | 190 setBodyInnerHTML("<img src='myimage'></img>"); |
191 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); | 191 LayoutImage* img = toLayoutImage(document().body()->firstChild()->layoutObje
ct()); |
192 | 192 |
193 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); | 193 RefPtr<TestImageLowQuality> testImage = adoptRef(new TestImageLowQuality); |
194 OwnPtr<DisplayItemList> displayItemList = DisplayItemList::create(); | 194 OwnPtr<PaintController> paintController = PaintController::create(); |
195 GraphicsContext context(displayItemList.get()); | 195 GraphicsContext context(paintController.get()); |
196 | 196 |
197 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. | 197 // Paint once. This will kick off a timer to see if we resize it during that
timer's execution. |
198 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); | 198 EXPECT_EQ(InterpolationMedium, controller()->chooseInterpolationQuality(&con
text, img, testImage.get(), testImage.get(), LayoutSize(2, 2))); |
199 | 199 |
200 // Go into low-quality mode now that the size changed. | 200 // Go into low-quality mode now that the size changed. |
201 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); | 201 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(3, 3))); |
202 | 202 |
203 // Stay in low-quality mode since the size changed again. | 203 // Stay in low-quality mode since the size changed again. |
204 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 204 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
205 | 205 |
206 mockTimer->stop(); | 206 mockTimer->stop(); |
207 EXPECT_FALSE(mockTimer->isActive()); | 207 EXPECT_FALSE(mockTimer->isActive()); |
208 // Painted at the same size, so even though timer is still executing, don't
go to low quality. | 208 // Painted at the same size, so even though timer is still executing, don't
go to low quality. |
209 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); | 209 EXPECT_EQ(InterpolationLow, controller()->chooseInterpolationQuality(&contex
t, img, testImage.get(), testImage.get(), LayoutSize(4, 4))); |
210 // Check that the timer was not kicked. It should not have been, since the i
mage was painted at the same size as last time. | 210 // Check that the timer was not kicked. It should not have been, since the i
mage was painted at the same size as last time. |
211 EXPECT_FALSE(mockTimer->isActive()); | 211 EXPECT_FALSE(mockTimer->isActive()); |
212 } | 212 } |
213 | 213 |
214 #endif | 214 #endif |
215 | 215 |
216 } // namespace blink | 216 } // namespace blink |
OLD | NEW |