OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "base/command_line.h" | |
6 #include "base/strings/stringprintf.h" | |
7 #include "chrome/browser/ui/browser.h" | |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
9 #include "chrome/common/chrome_switches.h" | |
10 #include "chrome/test/base/in_process_browser_test.h" | |
11 #include "chrome/test/base/ui_test_utils.h" | |
12 #include "content/public/test/browser_test_utils.h" | |
13 #include "content/public/test/ppapi_test_utils.h" | |
14 #include "ppapi/shared_impl/ppapi_switches.h" | |
15 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
16 #include "ui/gfx/geometry/point.h" | |
17 | |
18 class PluginPowerSaverBrowserTest : virtual public InProcessBrowserTest { | |
19 public: | |
20 void SetUpCommandLine(base::CommandLine* command_line) override { | |
21 command_line->AppendSwitch(switches::kEnablePluginPowerSaver); | |
22 command_line->AppendSwitch(switches::kEnablePepperTesting); | |
23 | |
24 ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line)); | |
25 } | |
26 | |
27 protected: | |
28 enum PeripheralTestResult { | |
29 RESULT_ESSENTIAL = 0, | |
30 RESULT_PERIPHERAL, | |
31 RESULT_ERROR | |
32 }; | |
33 | |
34 void LoadHTML(const char* html) { | |
35 std::string url_str = "data:text/html;charset=utf-8,"; | |
36 url_str.append(html); | |
37 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); | |
38 EXPECT_TRUE(content::WaitForRenderFrameReady( | |
39 GetActiveWebContents()->GetMainFrame())); | |
40 } | |
41 | |
42 content::WebContents* GetActiveWebContents() { | |
43 return browser()->tab_strip_model()->GetActiveWebContents(); | |
44 } | |
45 | |
46 bool IsPluginPeripheral(const char* element_id) { | |
47 std::string script = base::StringPrintf( | |
48 "var plugin = window.document.getElementById('%s');" | |
49 "function handleEvent() {" | |
50 " if (event.data.isPeripheral != undefined &&" | |
51 " event.data.source == 'getPeripheralStatusResponse') {" | |
52 " window.domAutomationController.send(" | |
53 " event.data.isPeripheral ? %d : %d);" | |
54 " plugin.removeEventListener('message', handleEvent);" | |
55 " }" | |
56 "}" | |
57 "if (plugin == undefined ||" | |
58 " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {" | |
59 " window.domAutomationController.send(%d);" | |
60 "} else if (plugin.postMessage == undefined) {" | |
61 " window.domAutomationController.send(%d);" | |
62 "} else {" | |
63 " plugin.addEventListener('message', handleEvent);" | |
64 " plugin.postMessage('getPeripheralStatus');" | |
65 "}", | |
66 element_id, | |
67 RESULT_PERIPHERAL, | |
Lei Zhang
2015/04/23 22:38:02
might be easier to write:
var RESULT_PERIPHERAL =
tommycli
2015/04/24 18:08:26
Done. I changed it to just use string literals thr
Lei Zhang
2015/04/24 18:30:36
Ok.
| |
68 RESULT_ESSENTIAL, | |
69 RESULT_ERROR, | |
70 RESULT_PERIPHERAL); | |
71 | |
72 int result = RESULT_ERROR; | |
73 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(GetActiveWebContents(), | |
74 script, &result)); | |
75 EXPECT_NE(RESULT_ERROR, result); | |
76 return result == RESULT_PERIPHERAL; | |
77 } | |
78 | |
79 // This sends a simulated click at |point| and waits for test plugin to send | |
80 // a status message indicating that it is essential. The test plugin sends a | |
81 // status message during: | |
82 // - Plugin creation, to handle a plugin freshly created from a poster. | |
83 // - Peripheral status change. | |
84 // - In response to the explicit 'getPeriheralStatus' request, in case the | |
Lei Zhang
2015/04/23 22:38:02
typo
tommycli
2015/04/24 18:08:26
Done.
| |
85 // test has missed the above two events. | |
86 void SimulateClickAndAwaitMarkedEssential(const char* element_id, | |
87 const gfx::Point& point) { | |
88 content::SimulateMouseClickAt(GetActiveWebContents(), 0 /* modifiers */, | |
89 blink::WebMouseEvent::ButtonLeft, point); | |
90 | |
91 std::string script = base::StringPrintf( | |
92 "var plugin = window.document.getElementById('%s');" | |
93 "function handleEvent() {" | |
94 " if (event.data.isPeripheral == false) {" | |
95 " window.domAutomationController.send(%d);" | |
96 " plugin.removeEventListener('message', handleEvent);" | |
97 " }" | |
98 "}" | |
99 "if (plugin == undefined ||" | |
100 " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {" | |
101 " window.domAutomationController.send(%d);" | |
102 "} else {" | |
103 " plugin.addEventListener('message', handleEvent);" | |
104 " if (plugin.postMessage != undefined) {" | |
105 " plugin.postMessage('getPeripheralStatus');" | |
106 " }" | |
107 "}", | |
108 element_id, | |
109 RESULT_ESSENTIAL, | |
110 RESULT_ERROR); | |
111 | |
112 int result = RESULT_ERROR; | |
113 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(GetActiveWebContents(), | |
114 script, &result)); | |
115 EXPECT_EQ(RESULT_ESSENTIAL, result); | |
116 } | |
117 }; | |
118 | |
119 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) { | |
120 LoadHTML( | |
121 "<object id='plugin' data='fake.swf' " | |
122 "type='application/x-ppapi-tests' width='400' height='100'></object>"); | |
123 EXPECT_FALSE(IsPluginPeripheral("plugin")); | |
124 } | |
125 | |
126 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallCrossOrigin) { | |
127 LoadHTML( | |
128 "<object id='plugin' data='http://otherorigin.com/fake.swf' " | |
129 "type='application/x-ppapi-tests' width='400' height='100'></object>"); | |
130 EXPECT_TRUE(IsPluginPeripheral("plugin")); | |
131 | |
132 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); | |
133 } | |
134 | |
135 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { | |
136 LoadHTML( | |
137 "<object id='plugin' data='http://otherorigin.com/fake.swf' " | |
138 "type='application/x-ppapi-tests' width='400' height='500'></object>"); | |
139 EXPECT_FALSE(IsPluginPeripheral("plugin")); | |
140 } | |
141 | |
142 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePluginsUsePosters) { | |
143 LoadHTML( | |
144 "<object id='plugin' type='application/x-ppapi-tests' " | |
145 " width='400' height='500'>" | |
146 " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x' />" | |
147 "</object>"); | |
148 EXPECT_TRUE(IsPluginPeripheral("plugin")); | |
149 } | |
150 | |
151 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) { | |
152 LoadHTML( | |
153 "<object id='plugin1' data='http://otherorigin.com/fake1.swf' " | |
154 "type='application/x-ppapi-tests' width='400' height='100'></object>" | |
155 "<object id='plugin2' data='http://otherorigin.com/fake2.swf' " | |
156 "type='application/x-ppapi-tests' width='400' height='500'></object>"); | |
157 EXPECT_FALSE(IsPluginPeripheral("plugin1")); | |
158 EXPECT_FALSE(IsPluginPeripheral("plugin2")); | |
159 } | |
160 | |
161 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) { | |
162 LoadHTML( | |
163 "<div style='width: 100px; height: 100px; overflow: hidden;'>" | |
164 " <object id='plugin' data='http://otherorigin.com/fake.swf' " | |
165 " type='application/x-ppapi-tests' width='400' height='500'></object>" | |
166 "</div>"); | |
167 EXPECT_TRUE(IsPluginPeripheral("plugin")); | |
168 } | |
OLD | NEW |