| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/string_piece.h" | 6 #include "base/strings/string_piece.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 "} else {" | 30 "} else {" |
| 31 " %s" | 31 " %s" |
| 32 "}", | 32 "}", |
| 33 element_id.c_str(), test_script.data()); | 33 element_id.c_str(), test_script.data()); |
| 34 std::string result; | 34 std::string result; |
| 35 EXPECT_TRUE( | 35 EXPECT_TRUE( |
| 36 content::ExecuteScriptAndExtractString(contents, script, &result)); | 36 content::ExecuteScriptAndExtractString(contents, script, &result)); |
| 37 return result; | 37 return result; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void VerifyPluginIsPlaceholderOnly(content::WebContents* contents, | 40 // This also tests that we have JavaScript access to the underlying plugin. |
| 41 const char* element_id) { | 41 bool PluginLoaded(content::WebContents* contents, const char* element_id) { |
| 42 std::string result = RunTestScript( | 42 std::string result = RunTestScript( |
| 43 "if (plugin.postMessage === undefined) {" | 43 "if (plugin.postMessage === undefined) {" |
| 44 " window.domAutomationController.send('placeholder');" | 44 " window.domAutomationController.send('poster_only');" |
| 45 "} else {" | 45 "} else {" |
| 46 " window.domAutomationController.send('plugin_loaded');" | 46 " window.domAutomationController.send('plugin_loaded');" |
| 47 "}", | 47 "}", |
| 48 contents, element_id); | 48 contents, element_id); |
| 49 EXPECT_EQ("placeholder", result); | 49 EXPECT_NE("error", result); |
| 50 return result == "plugin_loaded"; |
| 50 } | 51 } |
| 51 | 52 |
| 52 void VerifyPluginIsPeripheral(content::WebContents* contents, | 53 // Also waits for the placeholder UI overlay to finish loading. |
| 53 const char* element_id) { | 54 void VerifyPluginIsThrottled(content::WebContents* contents, |
| 55 const char* element_id) { |
| 54 std::string result = RunTestScript( | 56 std::string result = RunTestScript( |
| 55 "function handleEvent(event) {" | 57 "function handleEvent(event) {" |
| 56 " if (event.data.isPeripheral !== undefined && " | 58 " if (event.data.isPeripheral && event.data.isThrottled && " |
| 57 " event.data.source === 'getPowerSaverStatusResponse') {" | 59 " event.data.isHiddenForPlaceholder) {" |
| 58 " window.domAutomationController.send(" | 60 " window.domAutomationController.send('throttled');" |
| 59 " event.data.isPeripheral ? 'peripheral' : 'essential');" | |
| 60 " plugin.removeEventListener('message', handleEvent);" | 61 " plugin.removeEventListener('message', handleEvent);" |
| 61 " }" | 62 " }" |
| 62 "}" | 63 "}" |
| 63 "if (plugin.postMessage === undefined) {" | 64 "plugin.addEventListener('message', handleEvent);" |
| 64 " window.domAutomationController.send('peripheral');" | 65 "if (plugin.postMessage !== undefined) {" |
| 65 "} else {" | |
| 66 " plugin.addEventListener('message', handleEvent);" | |
| 67 " plugin.postMessage('getPowerSaverStatus');" | 66 " plugin.postMessage('getPowerSaverStatus');" |
| 68 "}", | 67 "}", |
| 69 contents, element_id); | 68 contents, element_id); |
| 70 EXPECT_EQ("peripheral", result); | 69 EXPECT_EQ("throttled", result); |
| 70 |
| 71 // Page should continue to have JavaScript access to all throttled plugins. |
| 72 EXPECT_TRUE(PluginLoaded(contents, element_id)); |
| 71 } | 73 } |
| 72 | 74 |
| 73 void VerifyPluginMarkedEssential(content::WebContents* contents, | 75 void VerifyPluginMarkedEssential(content::WebContents* contents, |
| 74 const char* element_id) { | 76 const char* element_id) { |
| 75 std::string result = RunTestScript( | 77 std::string result = RunTestScript( |
| 76 "function handleEvent(event) {" | 78 "function handleEvent(event) {" |
| 77 " if (event.data.isPeripheral === false) {" | 79 " if (event.data.isPeripheral === false) {" |
| 78 " window.domAutomationController.send('essential');" | 80 " window.domAutomationController.send('essential');" |
| 79 " plugin.removeEventListener('message', handleEvent);" | 81 " plugin.removeEventListener('message', handleEvent);" |
| 80 " }" | 82 " }" |
| 81 "}" | 83 "}" |
| 82 "plugin.addEventListener('message', handleEvent);" | 84 "plugin.addEventListener('message', handleEvent);" |
| 83 "if (plugin.postMessage !== undefined) {" | 85 "if (plugin.postMessage !== undefined) {" |
| 84 " plugin.postMessage('getPowerSaverStatus');" | 86 " plugin.postMessage('getPowerSaverStatus');" |
| 85 "}", | 87 "}", |
| 86 contents, element_id); | 88 contents, element_id); |
| 87 EXPECT_EQ("essential", result); | 89 EXPECT_EQ("essential", result); |
| 90 EXPECT_TRUE(PluginLoaded(contents, element_id)); |
| 88 } | 91 } |
| 89 | 92 |
| 90 } // namespace | 93 } // namespace |
| 91 | 94 |
| 92 class PluginPowerSaverBrowserTest : virtual public InProcessBrowserTest { | 95 class PluginPowerSaverBrowserTest : virtual public InProcessBrowserTest { |
| 93 public: | 96 public: |
| 94 void SetUpCommandLine(base::CommandLine* command_line) override { | 97 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 95 command_line->AppendSwitch(switches::kEnablePluginPowerSaver); | 98 command_line->AppendSwitch(switches::kEnablePluginPowerSaver); |
| 96 command_line->AppendSwitch(switches::kEnablePepperTesting); | 99 command_line->AppendSwitch(switches::kEnablePepperTesting); |
| 97 | 100 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 128 }; | 131 }; |
| 129 | 132 |
| 130 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) { | 133 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) { |
| 131 LoadHTML( | 134 LoadHTML( |
| 132 "<object id='plugin' data='fake.swf' " | 135 "<object id='plugin' data='fake.swf' " |
| 133 " type='application/x-ppapi-tests' width='400' height='100'>" | 136 " type='application/x-ppapi-tests' width='400' height='100'>" |
| 134 "</object>"); | 137 "</object>"); |
| 135 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin"); | 138 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin"); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // Flaky: https://crbug.com/485160 | 141 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallCrossOrigin) { |
| 139 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, DISABLED_SmallCrossOrigin) { | |
| 140 LoadHTML( | 142 LoadHTML( |
| 141 "<object id='plugin' data='http://otherorigin.com/fake.swf' " | 143 "<object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 142 " type='application/x-ppapi-tests' width='400' height='100'>" | 144 " type='application/x-ppapi-tests' width='400' height='100'>" |
| 143 "</object>"); | 145 "</object>"); |
| 144 VerifyPluginIsPeripheral(GetActiveWebContents(), "plugin"); | 146 VerifyPluginIsThrottled(GetActiveWebContents(), "plugin"); |
| 145 | 147 |
| 146 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); | 148 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); |
| 147 } | 149 } |
| 148 | 150 |
| 149 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { | 151 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { |
| 150 LoadHTML( | 152 LoadHTML( |
| 151 "<object id='plugin' data='http://otherorigin.com/fake.swf' " | 153 "<object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 152 " type='application/x-ppapi-tests' width='400' height='500'>" | 154 " type='application/x-ppapi-tests' width='400' height='500'>" |
| 153 "</object>"); | 155 "</object>"); |
| 154 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin"); | 156 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 165 "<object id='plugin_legacy_syntax' type='application/x-ppapi-tests' " | 167 "<object id='plugin_legacy_syntax' type='application/x-ppapi-tests' " |
| 166 " width='400' height='500'>" | 168 " width='400' height='500'>" |
| 167 " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x'>" | 169 " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x'>" |
| 168 "</object>" | 170 "</object>" |
| 169 "<embed id='plugin_embed_src' type='application/x-ppapi-tests' " | 171 "<embed id='plugin_embed_src' type='application/x-ppapi-tests' " |
| 170 " width='400' height='500' poster='snapshot1x.png'></embed>" | 172 " width='400' height='500' poster='snapshot1x.png'></embed>" |
| 171 "<embed id='plugin_embed_srcset' type='application/x-ppapi-tests' " | 173 "<embed id='plugin_embed_srcset' type='application/x-ppapi-tests' " |
| 172 " width='400' height='500'" | 174 " width='400' height='500'" |
| 173 " poster='snapshot1x.png 1x, snapshot2x.png 2x'></embed>"); | 175 " poster='snapshot1x.png 1x, snapshot2x.png 2x'></embed>"); |
| 174 | 176 |
| 175 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin_src"); | 177 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_src")); |
| 176 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin_srcset"); | 178 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_srcset")); |
| 177 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin_legacy_syntax"); | 179 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_legacy_syntax")); |
| 178 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin_embed_src"); | 180 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_src")); |
| 179 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin_embed_srcset"); | 181 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_srcset")); |
| 180 } | 182 } |
| 181 | 183 |
| 182 // flaky: crbug.com/481687 | 184 // flaky: crbug.com/481687 |
| 183 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, | 185 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, |
| 184 DISABLED_PluginMarkedEssentialAfterPosterClicked) { | 186 DISABLED_PluginMarkedEssentialAfterPosterClicked) { |
| 185 LoadHTML( | 187 LoadHTML( |
| 186 "<object id='plugin' type='application/x-ppapi-tests' " | 188 "<object id='plugin' type='application/x-ppapi-tests' " |
| 187 " width='400' height='100' poster='snapshot1x.png'></object>"); | 189 " width='400' height='100' poster='snapshot1x.png'></object>"); |
| 188 VerifyPluginIsPlaceholderOnly(GetActiveWebContents(), "plugin"); | 190 EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin")); |
| 189 | 191 |
| 190 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); | 192 SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); |
| 191 } | 193 } |
| 192 | 194 |
| 193 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) { | 195 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) { |
| 194 LoadHTML( | 196 LoadHTML( |
| 195 "<object id='plugin1' data='http://otherorigin.com/fake1.swf' " | 197 "<object id='plugin1' data='http://otherorigin.com/fake1.swf' " |
| 196 " type='application/x-ppapi-tests' width='400' height='100'></object>" | 198 " type='application/x-ppapi-tests' width='400' height='100'></object>" |
| 197 "<object id='plugin2' data='http://otherorigin.com/fake2.swf' " | 199 "<object id='plugin2' data='http://otherorigin.com/fake2.swf' " |
| 198 " type='application/x-ppapi-tests' width='400' height='500'>" | 200 " type='application/x-ppapi-tests' width='400' height='500'>" |
| 199 "</object>"); | 201 "</object>"); |
| 200 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin1"); | 202 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin1"); |
| 201 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin2"); | 203 VerifyPluginMarkedEssential(GetActiveWebContents(), "plugin2"); |
| 202 } | 204 } |
| 203 | 205 |
| 204 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) { | 206 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) { |
| 205 LoadHTML( | 207 LoadHTML( |
| 206 "<div style='width: 100px; height: 100px; overflow: hidden;'>" | 208 "<div style='width: 100px; height: 100px; overflow: hidden;'>" |
| 207 " <object id='plugin' data='http://otherorigin.com/fake.swf' " | 209 " <object id='plugin' data='http://otherorigin.com/fake.swf' " |
| 208 " type='application/x-ppapi-tests' width='400' height='500'>" | 210 " type='application/x-ppapi-tests' width='400' height='500'>" |
| 209 " </object>" | 211 " </object>" |
| 210 "</div>"); | 212 "</div>"); |
| 211 VerifyPluginIsPeripheral(GetActiveWebContents(), "plugin"); | 213 VerifyPluginIsThrottled(GetActiveWebContents(), "plugin"); |
| 212 } | 214 } |
| 213 | 215 |
| 214 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, BackgroundTabPlugins) { | 216 IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, BackgroundTabPlugins) { |
| 215 std::string url_str = | 217 std::string url_str = |
| 216 "data:text/html;charset=utf-8," | 218 "data:text/html;charset=utf-8," |
| 217 "<object id='same_origin' type='application/x-ppapi-tests'></object>" | 219 "<object id='same_origin' type='application/x-ppapi-tests'></object>" |
| 218 "<object id='small_cross_origin' data='http://otherorigin.com/fake1.swf' " | 220 "<object id='small_cross_origin' data='http://otherorigin.com/fake1.swf' " |
| 219 " type='application/x-ppapi-tests' width='400' height='100'></object>"; | 221 " type='application/x-ppapi-tests' width='400' height='100'></object>"; |
| 220 ui_test_utils::NavigateToURLWithDisposition( | 222 ui_test_utils::NavigateToURLWithDisposition( |
| 221 browser(), GURL(url_str), NEW_BACKGROUND_TAB, | 223 browser(), GURL(url_str), NEW_BACKGROUND_TAB, |
| 222 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 224 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 223 | 225 |
| 224 ASSERT_EQ(2, browser()->tab_strip_model()->count()); | 226 ASSERT_EQ(2, browser()->tab_strip_model()->count()); |
| 225 content::WebContents* background_contents = | 227 content::WebContents* background_contents = |
| 226 browser()->tab_strip_model()->GetWebContentsAt(1); | 228 browser()->tab_strip_model()->GetWebContentsAt(1); |
| 227 EXPECT_TRUE( | 229 EXPECT_TRUE( |
| 228 content::WaitForRenderFrameReady(background_contents->GetMainFrame())); | 230 content::WaitForRenderFrameReady(background_contents->GetMainFrame())); |
| 229 | 231 |
| 230 VerifyPluginIsPlaceholderOnly(background_contents, "same_origin"); | 232 EXPECT_FALSE(PluginLoaded(background_contents, "same_origin")); |
| 231 VerifyPluginIsPlaceholderOnly(background_contents, "small_cross_origin"); | 233 EXPECT_FALSE(PluginLoaded(background_contents, "small_cross_origin")); |
| 232 | 234 |
| 233 browser()->tab_strip_model()->SelectNextTab(); | 235 browser()->tab_strip_model()->SelectNextTab(); |
| 234 EXPECT_EQ(background_contents, GetActiveWebContents()); | 236 EXPECT_EQ(background_contents, GetActiveWebContents()); |
| 235 | 237 |
| 236 VerifyPluginMarkedEssential(background_contents, "same_origin"); | 238 VerifyPluginMarkedEssential(background_contents, "same_origin"); |
| 237 VerifyPluginIsPeripheral(background_contents, "small_cross_origin"); | 239 VerifyPluginIsThrottled(background_contents, "small_cross_origin"); |
| 238 } | 240 } |
| OLD | NEW |