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

Unified 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: add back PostMessage undef 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser_tests.isolate » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/plugins/plugin_power_saver_browsertest.cc
diff --git a/chrome/browser/plugins/plugin_power_saver_browsertest.cc b/chrome/browser/plugins/plugin_power_saver_browsertest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..77867ee50c41989dd538665b47bd575b015c9115
--- /dev/null
+++ b/chrome/browser/plugins/plugin_power_saver_browsertest.cc
@@ -0,0 +1,168 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/command_line.h"
+#include "base/strings/stringprintf.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/test/base/in_process_browser_test.h"
+#include "chrome/test/base/ui_test_utils.h"
+#include "content/public/test/browser_test_utils.h"
+#include "content/public/test/ppapi_test_utils.h"
+#include "ppapi/shared_impl/ppapi_switches.h"
+#include "third_party/WebKit/public/web/WebInputEvent.h"
+#include "ui/gfx/geometry/point.h"
+
+class PluginPowerSaverBrowserTest : virtual public InProcessBrowserTest {
+ public:
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ command_line->AppendSwitch(switches::kEnablePluginPowerSaver);
+ command_line->AppendSwitch(switches::kEnablePepperTesting);
+
+ ASSERT_TRUE(ppapi::RegisterPowerSaverTestPlugin(command_line));
+ }
+
+ protected:
+ enum PeripheralTestResult {
+ RESULT_ESSENTIAL = 0,
+ RESULT_PERIPHERAL,
+ RESULT_ERROR
+ };
+
+ void LoadHTML(const char* html) {
+ std::string url_str = "data:text/html;charset=utf-8,";
+ url_str.append(html);
+ ui_test_utils::NavigateToURL(browser(), GURL(url_str));
+ EXPECT_TRUE(content::WaitForRenderFrameReady(
+ GetActiveWebContents()->GetMainFrame()));
+ }
+
+ content::WebContents* GetActiveWebContents() {
+ return browser()->tab_strip_model()->GetActiveWebContents();
+ }
+
+ bool IsPluginPeripheral(const char* element_id) {
+ std::string script = base::StringPrintf(
+ "var plugin = window.document.getElementById('%s');"
+ "function handleEvent() {"
+ " if (event.data.isPeripheral != undefined &&"
+ " event.data.source == 'getPeripheralStatusResponse') {"
+ " window.domAutomationController.send("
+ " event.data.isPeripheral ? %d : %d);"
+ " plugin.removeEventListener('message', handleEvent);"
+ " }"
+ "}"
+ "if (plugin == undefined ||"
+ " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {"
+ " window.domAutomationController.send(%d);"
+ "} else if (plugin.postMessage == undefined) {"
+ " window.domAutomationController.send(%d);"
+ "} else {"
+ " plugin.addEventListener('message', handleEvent);"
+ " plugin.postMessage('getPeripheralStatus');"
+ "}",
+ element_id,
+ 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.
+ RESULT_ESSENTIAL,
+ RESULT_ERROR,
+ RESULT_PERIPHERAL);
+
+ int result = RESULT_ERROR;
+ EXPECT_TRUE(content::ExecuteScriptAndExtractInt(GetActiveWebContents(),
+ script, &result));
+ EXPECT_NE(RESULT_ERROR, result);
+ return result == RESULT_PERIPHERAL;
+ }
+
+ // This sends a simulated click at |point| and waits for test plugin to send
+ // a status message indicating that it is essential. The test plugin sends a
+ // status message during:
+ // - Plugin creation, to handle a plugin freshly created from a poster.
+ // - Peripheral status change.
+ // - 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.
+ // test has missed the above two events.
+ void SimulateClickAndAwaitMarkedEssential(const char* element_id,
+ const gfx::Point& point) {
+ content::SimulateMouseClickAt(GetActiveWebContents(), 0 /* modifiers */,
+ blink::WebMouseEvent::ButtonLeft, point);
+
+ std::string script = base::StringPrintf(
+ "var plugin = window.document.getElementById('%s');"
+ "function handleEvent() {"
+ " if (event.data.isPeripheral == false) {"
+ " window.domAutomationController.send(%d);"
+ " plugin.removeEventListener('message', handleEvent);"
+ " }"
+ "}"
+ "if (plugin == undefined ||"
+ " (plugin.nodeName != 'OBJECT' && plugin.nodeName != 'EMBED')) {"
+ " window.domAutomationController.send(%d);"
+ "} else {"
+ " plugin.addEventListener('message', handleEvent);"
+ " if (plugin.postMessage != undefined) {"
+ " plugin.postMessage('getPeripheralStatus');"
+ " }"
+ "}",
+ element_id,
+ RESULT_ESSENTIAL,
+ RESULT_ERROR);
+
+ int result = RESULT_ERROR;
+ EXPECT_TRUE(content::ExecuteScriptAndExtractInt(GetActiveWebContents(),
+ script, &result));
+ EXPECT_EQ(RESULT_ESSENTIAL, result);
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) {
+ LoadHTML(
+ "<object id='plugin' data='fake.swf' "
+ "type='application/x-ppapi-tests' width='400' height='100'></object>");
+ EXPECT_FALSE(IsPluginPeripheral("plugin"));
+}
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallCrossOrigin) {
+ LoadHTML(
+ "<object id='plugin' data='http://otherorigin.com/fake.swf' "
+ "type='application/x-ppapi-tests' width='400' height='100'></object>");
+ EXPECT_TRUE(IsPluginPeripheral("plugin"));
+
+ SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50));
+}
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) {
+ LoadHTML(
+ "<object id='plugin' data='http://otherorigin.com/fake.swf' "
+ "type='application/x-ppapi-tests' width='400' height='500'></object>");
+ EXPECT_FALSE(IsPluginPeripheral("plugin"));
+}
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePluginsUsePosters) {
+ LoadHTML(
+ "<object id='plugin' type='application/x-ppapi-tests' "
+ " width='400' height='500'>"
+ " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x' />"
+ "</object>");
+ EXPECT_TRUE(IsPluginPeripheral("plugin"));
+}
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) {
+ LoadHTML(
+ "<object id='plugin1' data='http://otherorigin.com/fake1.swf' "
+ "type='application/x-ppapi-tests' width='400' height='100'></object>"
+ "<object id='plugin2' data='http://otherorigin.com/fake2.swf' "
+ "type='application/x-ppapi-tests' width='400' height='500'></object>");
+ EXPECT_FALSE(IsPluginPeripheral("plugin1"));
+ EXPECT_FALSE(IsPluginPeripheral("plugin2"));
+}
+
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) {
+ LoadHTML(
+ "<div style='width: 100px; height: 100px; overflow: hidden;'>"
+ " <object id='plugin' data='http://otherorigin.com/fake.swf' "
+ " type='application/x-ppapi-tests' width='400' height='500'></object>"
+ "</div>");
+ EXPECT_TRUE(IsPluginPeripheral("plugin"));
+}
« 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