Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/plugins/plugin_power_saver_browsertest.cc

Issue 1088763002: Plugin Power Saver: Add comprehensive browser tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0260-plugins-overhaul-prerender-tests
Patch Set: x Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser_tests.isolate » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 void LoadHTML(const char* html) {
29 std::string url_str = "data:text/html;charset=utf-8,";
30 url_str.append(html);
31 ui_test_utils::NavigateToURL(browser(), GURL(url_str));
32 EXPECT_TRUE(content::WaitForRenderFrameReady(
33 GetActiveWebContents()->GetMainFrame()));
34 }
35
36 content::WebContents* GetActiveWebContents() {
37 return browser()->tab_strip_model()->GetActiveWebContents();
38 }
39
40 bool IsPluginPeripheral(const char* element_id) {
41 std::string script = base::StringPrintf(
42 "var plugin = window.document.getElementById('%s');"
43 "function handleEvent() {"
44 " if (event.data.isPeripheral != undefined &&"
45 " event.data.source == 'getPeripheralStatusResponse') {"
46 " window.domAutomationController.send("
47 " event.data.isPeripheral ? 'peripheral' : 'essential');"
48 " plugin.removeEventListener('message', handleEvent);"
49 " }"
50 "}"
51 "if (plugin == undefined ||"
52 " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {"
53 " window.domAutomationController.send('error');"
54 "} else if (plugin.postMessage == undefined) {"
55 " window.domAutomationController.send('peripheral');"
56 "} else {"
57 " plugin.addEventListener('message', handleEvent);"
58 " plugin.postMessage('getPeripheralStatus');"
59 "}",
60 element_id);
61
62 std::string result;
63 EXPECT_TRUE(content::ExecuteScriptAndExtractString(GetActiveWebContents(),
64 script, &result));
65 EXPECT_NE("error", result);
66 return result == "peripheral";
67 }
68
69 // This sends a simulated click at |point| and waits for test plugin to send
70 // a status message indicating that it is essential. The test plugin sends a
71 // status message during:
72 // - Plugin creation, to handle a plugin freshly created from a poster.
73 // - Peripheral status change.
74 // - In response to the explicit 'getPeripheralStatus' request, in case the
75 // test has missed the above two events.
76 void SimulateClickAndAwaitMarkedEssential(const char* element_id,
77 const gfx::Point& point) {
78 content::SimulateMouseClickAt(GetActiveWebContents(), 0 /* modifiers */,
79 blink::WebMouseEvent::ButtonLeft, point);
80
81 std::string script = base::StringPrintf(
82 "var plugin = window.document.getElementById('%s');"
83 "function handleEvent() {"
84 " if (event.data.isPeripheral == false) {"
85 " window.domAutomationController.send('essential');"
86 " plugin.removeEventListener('message', handleEvent);"
87 " }"
88 "}"
89 "if (plugin == undefined ||"
90 " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {"
91 " window.domAutomationController.send('error');"
92 "} else {"
93 " plugin.addEventListener('message', handleEvent);"
94 " if (plugin.postMessage != undefined) {"
95 " plugin.postMessage('getPeripheralStatus');"
96 " }"
97 "}",
98 element_id);
99
100 std::string result;
101 EXPECT_TRUE(content::ExecuteScriptAndExtractString(GetActiveWebContents(),
102 script, &result));
103 EXPECT_EQ("essential", result);
104 }
105 };
106
107 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) {
108 LoadHTML(
109 "<object id='plugin' data='fake.swf' "
110 "type='application/x-ppapi-tests' width='400' height='100'></object>");
111 EXPECT_FALSE(IsPluginPeripheral("plugin"));
112 }
113
114 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallCrossOrigin) {
115 LoadHTML(
116 "<object id='plugin' data='http://otherorigin.com/fake.swf' "
117 "type='application/x-ppapi-tests' width='400' height='100'></object>");
118 EXPECT_TRUE(IsPluginPeripheral("plugin"));
119
120 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50));
121 }
122
123 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) {
124 LoadHTML(
125 "<object id='plugin' data='http://otherorigin.com/fake.swf' "
126 "type='application/x-ppapi-tests' width='400' height='500'></object>");
127 EXPECT_FALSE(IsPluginPeripheral("plugin"));
128 }
129
130 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePluginsUsePosters) {
131 LoadHTML(
132 "<object id='plugin' type='application/x-ppapi-tests' "
133 " width='400' height='500'>"
134 " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x' />"
135 "</object>");
136 EXPECT_TRUE(IsPluginPeripheral("plugin"));
137 }
138
139 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) {
140 LoadHTML(
141 "<object id='plugin1' data='http://otherorigin.com/fake1.swf' "
142 "type='application/x-ppapi-tests' width='400' height='100'></object>"
143 "<object id='plugin2' data='http://otherorigin.com/fake2.swf' "
144 "type='application/x-ppapi-tests' width='400' height='500'></object>");
145 EXPECT_FALSE(IsPluginPeripheral("plugin1"));
146 EXPECT_FALSE(IsPluginPeripheral("plugin2"));
147 }
148
149 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) {
150 LoadHTML(
151 "<div style='width: 100px; height: 100px; overflow: hidden;'>"
152 " <object id='plugin' data='http://otherorigin.com/fake.swf' "
153 " type='application/x-ppapi-tests' width='400' height='500'></object>"
154 "</div>");
155 EXPECT_TRUE(IsPluginPeripheral("plugin"));
156 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser_tests.isolate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698