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 "content/public/test/test_utils.h" |
| 15 #include "ppapi/shared_impl/ppapi_switches.h" |
| 16 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 17 #include "ui/gfx/geometry/point.h" |
| 18 |
| 19 class PluginPowerSaverBrowserTest : virtual public InProcessBrowserTest { |
| 20 public: |
| 21 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 22 command_line->AppendSwitch(switches::kEnablePluginPowerSaver); |
| 23 command_line->AppendSwitch(switches::kEnablePepperTesting); |
| 24 |
| 25 ppapi::RegisterTestPlugin(command_line); |
| 26 } |
| 27 |
| 28 protected: |
| 29 void LoadHTML(const char* html) { |
| 30 std::string url_str = "data:text/html;charset=utf-8,"; |
| 31 url_str.append(html); |
| 32 ui_test_utils::NavigateToURL(browser(), GURL(url_str)); |
| 33 EXPECT_TRUE(content::WaitForRenderFrameReady( |
| 34 GetActiveWebContents()->GetMainFrame())); |
| 35 } |
| 36 |
| 37 content::WebContents* GetActiveWebContents() { |
| 38 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 39 } |
| 40 |
| 41 bool IsPluginPeripheral(const char* element_id) { |
| 42 std::string script = base::StringPrintf( |
| 43 "var plugin = window.document.getElementById('%s');" |
| 44 "function handleEvent() {" |
| 45 " if (event.data.isPeripheral != undefined) {" |
| 46 " window.domAutomationController.send(event.data.isPeripheral);" |
| 47 " plugin.removeEventListener('message', handleEvent);" |
| 48 " }" |
| 49 "}" |
| 50 "if (plugin.postMessage == undefined) {" |
| 51 " window.domAutomationController.send(true);" |
| 52 "} else {" |
| 53 " plugin.addEventListener('message', handleEvent);" |
| 54 " plugin.postMessage('isPeripheral');" |
| 55 "}", |
| 56 element_id); |
| 57 |
| 58 bool result = false; |
| 59 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(GetActiveWebContents(), |
| 60 script, &result)); |
| 61 return result; |
| 62 } |
| 63 |
| 64 // This sends a simulated click at |point| and waits (by polling) for the |
| 65 // plugin to be marked essential and run. Simply cycling the message loops |
| 66 // does not work, as the renderer process must mark the plugin essential. |
| 67 void SimulateClickAndAwaitMarkedEssential(const char* element_id, |
| 68 const gfx::Point& point) { |
| 69 content::SimulateMouseClickAt(GetActiveWebContents(), 0 /* modifiers */, |
| 70 blink::WebMouseEvent::ButtonLeft, point); |
| 71 |
| 72 std::string script = base::StringPrintf( |
| 73 "function checkEssential() {" |
| 74 " var plugin = window.document.getElementById('%s');" |
| 75 " if (plugin.postMessage == undefined) {" |
| 76 " /* Placeholder still up. Try again in 50ms. */" |
| 77 " setTimeout(checkEssential, 50);" |
| 78 " } else {" |
| 79 " function handleEvent() {" |
| 80 " if (event.data.isPeripheral != undefined) {" |
| 81 " if (event.data.isPeripheral) {" |
| 82 " plugin.postMessage('isPeripheral');" |
| 83 " } else {" |
| 84 " window.domAutomationController.send(true);" |
| 85 " plugin.removeEventListener('message', handleEvent);" |
| 86 " }" |
| 87 " }" |
| 88 " }" |
| 89 " plugin.addEventListener('message', handleEvent);" |
| 90 " plugin.postMessage('isPeripheral');" |
| 91 " }" |
| 92 "}" |
| 93 "checkEssential();", |
| 94 element_id); |
| 95 |
| 96 bool result = false; |
| 97 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(GetActiveWebContents(), |
| 98 script, &result)); |
| 99 EXPECT_TRUE(result); |
| 100 } |
| 101 }; |
| 102 |
| 103 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) { |
| 104 LoadHTML( |
| 105 "<object id='plugin' data='fake.swf' " |
| 106 "type='application/x-ppapi-tests' width='400' height='100'></object>"); |
| 107 EXPECT_FALSE(IsPluginPeripheral("plugin")); |
| 108 } |
| 109 |
| 110 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallCrossOrigin) { |
| 111 LoadHTML( |
| 112 "<object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 113 "type='application/x-ppapi-tests' width='400' height='100'></object>"); |
| 114 EXPECT_TRUE(IsPluginPeripheral("plugin")); |
| 115 |
| 116 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); |
| 117 } |
| 118 |
| 119 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { |
| 120 LoadHTML( |
| 121 "<object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 122 "type='application/x-ppapi-tests' width='400' height='500'></object>"); |
| 123 EXPECT_FALSE(IsPluginPeripheral("plugin")); |
| 124 } |
| 125 |
| 126 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePluginsUsePosters) { |
| 127 LoadHTML( |
| 128 "<object id='plugin' type='application/x-ppapi-tests' " |
| 129 " width='400' height='500'>" |
| 130 " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x' />" |
| 131 "</object>"); |
| 132 EXPECT_TRUE(IsPluginPeripheral("plugin")); |
| 133 } |
| 134 |
| 135 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) { |
| 136 LoadHTML( |
| 137 "<object id='plugin1' data='http://otherorigin.com/fake1.swf' " |
| 138 "type='application/x-ppapi-tests' width='400' height='100'></object>" |
| 139 "<object id='plugin2' data='http://otherorigin.com/fake2.swf' " |
| 140 "type='application/x-ppapi-tests' width='400' height='500'></object>"); |
| 141 EXPECT_FALSE(IsPluginPeripheral("plugin1")); |
| 142 EXPECT_FALSE(IsPluginPeripheral("plugin2")); |
| 143 } |
| 144 |
| 145 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) { |
| 146 LoadHTML( |
| 147 "<div style='width: 100px; height: 100px; overflow: hidden;'>" |
| 148 " <object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 149 " type='application/x-ppapi-tests' width='400' height='500'></object>" |
| 150 "</div>"); |
| 151 EXPECT_TRUE(IsPluginPeripheral("plugin")); |
| 152 } |
OLD | NEW |