[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / renderer / pepper / plugin_instance_throttler_impl_unittest.cc
blob0b3e398749d8674c6f06a83be90e1e3fba36a036
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/web/WebInputEvent.h"
16 #include "third_party/WebKit/public/web/WebPluginParams.h"
17 #include "ui/gfx/canvas.h"
19 using testing::_;
20 using testing::Return;
22 class GURL;
24 namespace content {
26 class PluginInstanceThrottlerImplTest
27 : public testing::Test,
28 public PluginInstanceThrottler::Observer {
29 protected:
30 const int kMaximumFramesToExamine =
31 PluginInstanceThrottlerImpl::kMaximumFramesToExamine;
33 PluginInstanceThrottlerImplTest() : change_callback_calls_(0) {}
34 ~PluginInstanceThrottlerImplTest() override {
35 throttler_->RemoveObserver(this);
38 void SetUp() override {
39 blink::WebRect rect;
40 rect.width = 100;
41 rect.height = 100;
42 throttler_.reset(
43 new PluginInstanceThrottlerImpl(true /* power_saver_enabled */));
44 throttler_->Initialize(nullptr, GURL("http://example.com"),
45 "Shockwave Flash", rect);
46 throttler_->AddObserver(this);
49 PluginInstanceThrottlerImpl* throttler() {
50 DCHECK(throttler_.get());
51 return throttler_.get();
54 void DisablePowerSaverByRetroactiveWhitelist() {
55 throttler()->MarkPluginEssential(
56 PluginInstanceThrottlerImpl::UNTHROTTLE_METHOD_BY_WHITELIST);
59 int change_callback_calls() { return change_callback_calls_; }
61 void EngageThrottle() { throttler_->EngageThrottle(); }
63 void SendEventAndTest(blink::WebInputEvent::Type event_type,
64 bool expect_consumed,
65 bool expect_throttled,
66 int expect_change_callback_count) {
67 blink::WebMouseEvent event;
68 event.type = event_type;
69 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown;
70 EXPECT_EQ(expect_consumed, throttler()->ConsumeInputEvent(event));
71 EXPECT_EQ(expect_throttled, throttler()->IsThrottled());
72 EXPECT_EQ(expect_change_callback_count, change_callback_calls());
75 private:
76 // PluginInstanceThrottlerImpl::Observer
77 void OnThrottleStateChange() override { ++change_callback_calls_; }
79 scoped_ptr<PluginInstanceThrottlerImpl> throttler_;
81 int change_callback_calls_;
83 base::MessageLoop loop_;
86 TEST_F(PluginInstanceThrottlerImplTest, ThrottleAndUnthrottleByClick) {
87 EXPECT_FALSE(throttler()->IsThrottled());
88 EXPECT_EQ(0, change_callback_calls());
90 EngageThrottle();
91 EXPECT_TRUE(throttler()->IsThrottled());
92 EXPECT_EQ(1, change_callback_calls());
94 // MouseUp while throttled should be consumed and disengage throttling.
95 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2);
98 TEST_F(PluginInstanceThrottlerImplTest, ThrottleByKeyframe) {
99 EXPECT_FALSE(throttler()->IsThrottled());
100 EXPECT_EQ(0, change_callback_calls());
102 SkBitmap boring_bitmap;
103 gfx::Canvas canvas(gfx::Size(20, 10), 1.0f, true);
104 canvas.FillRect(gfx::Rect(20, 10), SK_ColorBLACK);
105 canvas.FillRect(gfx::Rect(10, 10), SK_ColorWHITE);
106 SkBitmap interesting_bitmap =
107 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
109 // Don't throttle for a boring frame.
110 throttler()->OnImageFlush(&boring_bitmap);
111 EXPECT_FALSE(throttler()->IsThrottled());
112 EXPECT_EQ(0, change_callback_calls());
114 // Don't throttle for non-consecutive interesting frames.
115 throttler()->OnImageFlush(&interesting_bitmap);
116 throttler()->OnImageFlush(&boring_bitmap);
117 throttler()->OnImageFlush(&interesting_bitmap);
118 throttler()->OnImageFlush(&boring_bitmap);
119 throttler()->OnImageFlush(&interesting_bitmap);
120 throttler()->OnImageFlush(&boring_bitmap);
121 EXPECT_FALSE(throttler()->IsThrottled());
122 EXPECT_EQ(0, change_callback_calls());
124 // Throttle after consecutive interesting frames.
125 throttler()->OnImageFlush(&interesting_bitmap);
126 throttler()->OnImageFlush(&interesting_bitmap);
127 throttler()->OnImageFlush(&interesting_bitmap);
128 throttler()->OnImageFlush(&interesting_bitmap);
129 EXPECT_TRUE(throttler()->IsThrottled());
130 EXPECT_EQ(1, change_callback_calls());
133 TEST_F(PluginInstanceThrottlerImplTest, MaximumKeyframesAnalyzed) {
134 EXPECT_FALSE(throttler()->IsThrottled());
135 EXPECT_EQ(0, change_callback_calls());
137 SkBitmap boring_bitmap;
139 // Throttle after tons of boring bitmaps.
140 for (int i = 0; i < kMaximumFramesToExamine; ++i) {
141 throttler()->OnImageFlush(&boring_bitmap);
143 EXPECT_TRUE(throttler()->IsThrottled());
144 EXPECT_EQ(1, change_callback_calls());
146 TEST_F(PluginInstanceThrottlerImplTest, IgnoreThrottlingAfterMouseUp) {
147 EXPECT_FALSE(throttler()->IsThrottled());
148 EXPECT_EQ(0, change_callback_calls());
150 // MouseUp before throttling engaged should not be consumed, but should
151 // prevent subsequent throttling from engaging.
152 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 0);
154 EngageThrottle();
155 EXPECT_FALSE(throttler()->IsThrottled());
156 EXPECT_EQ(0, change_callback_calls());
159 TEST_F(PluginInstanceThrottlerImplTest, FastWhitelisting) {
160 EXPECT_FALSE(throttler()->IsThrottled());
161 EXPECT_EQ(0, change_callback_calls());
163 DisablePowerSaverByRetroactiveWhitelist();
165 EngageThrottle();
166 EXPECT_FALSE(throttler()->IsThrottled());
167 EXPECT_EQ(0, change_callback_calls());
170 TEST_F(PluginInstanceThrottlerImplTest, SlowWhitelisting) {
171 EXPECT_FALSE(throttler()->IsThrottled());
172 EXPECT_EQ(0, change_callback_calls());
174 EngageThrottle();
175 EXPECT_TRUE(throttler()->IsThrottled());
176 EXPECT_EQ(1, change_callback_calls());
178 DisablePowerSaverByRetroactiveWhitelist();
179 EXPECT_FALSE(throttler()->IsThrottled());
180 EXPECT_EQ(2, change_callback_calls());
183 TEST_F(PluginInstanceThrottlerImplTest, EventConsumption) {
184 EXPECT_FALSE(throttler()->IsThrottled());
185 EXPECT_EQ(0, change_callback_calls());
187 EngageThrottle();
188 EXPECT_TRUE(throttler()->IsThrottled());
189 EXPECT_EQ(1, change_callback_calls());
191 // Consume but don't unthrottle on a variety of other events.
192 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, true, true, 1);
193 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, true, true, 1);
194 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, true, true, 1);
195 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, true, true, 1);
196 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, true, true, 1);
198 // Consume and unthrottle on MouseUp
199 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, true, false, 2);
201 // Don't consume events after unthrottle.
202 SendEventAndTest(blink::WebInputEvent::Type::MouseDown, false, false, 2);
203 SendEventAndTest(blink::WebInputEvent::Type::MouseWheel, false, false, 2);
204 SendEventAndTest(blink::WebInputEvent::Type::MouseMove, false, false, 2);
205 SendEventAndTest(blink::WebInputEvent::Type::KeyDown, false, false, 2);
206 SendEventAndTest(blink::WebInputEvent::Type::KeyUp, false, false, 2);
208 // Subsequent MouseUps should also not be consumed.
209 SendEventAndTest(blink::WebInputEvent::Type::MouseUp, false, false, 2);
212 TEST_F(PluginInstanceThrottlerImplTest, ThrottleOnLeftClickOnly) {
213 EXPECT_FALSE(throttler()->IsThrottled());
214 EXPECT_EQ(0, change_callback_calls());
216 EngageThrottle();
217 EXPECT_TRUE(throttler()->IsThrottled());
218 EXPECT_EQ(1, change_callback_calls());
220 blink::WebMouseEvent event;
221 event.type = blink::WebInputEvent::Type::MouseUp;
223 event.modifiers = blink::WebInputEvent::Modifiers::RightButtonDown;
224 EXPECT_FALSE(throttler()->ConsumeInputEvent(event));
225 EXPECT_TRUE(throttler()->IsThrottled());
227 event.modifiers = blink::WebInputEvent::Modifiers::MiddleButtonDown;
228 EXPECT_TRUE(throttler()->ConsumeInputEvent(event));
229 EXPECT_TRUE(throttler()->IsThrottled());
231 event.modifiers = blink::WebInputEvent::Modifiers::LeftButtonDown;
232 EXPECT_TRUE(throttler()->ConsumeInputEvent(event));
233 EXPECT_FALSE(throttler()->IsThrottled());
236 } // namespace content