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 |
index 8a3a0252b05fcfc801f82d963c016a4dc4919133..3adef0250d2f620fa805482e21b3be8673eab3a4 100644 |
--- a/chrome/browser/plugins/plugin_power_saver_browsertest.cc |
+++ b/chrome/browser/plugins/plugin_power_saver_browsertest.cc |
@@ -2,15 +2,24 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <string> |
+ |
#include "base/command_line.h" |
+#include "base/stl_util.h" |
#include "base/strings/string_piece.h" |
#include "base/strings/stringprintf.h" |
#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_window.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/test_switches.h" |
#include "chrome/test/base/ui_test_utils.h" |
#include "components/ui/zoom/zoom_controller.h" |
+#include "content/public/browser/readback_types.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "content/public/browser/render_widget_host.h" |
#include "content/public/common/content_switches.h" |
#include "content/public/test/browser_test_utils.h" |
#include "content/public/test/ppapi_test_utils.h" |
@@ -19,11 +28,28 @@ |
#include "net/test/embedded_test_server/http_response.h" |
#include "ppapi/shared_impl/ppapi_switches.h" |
#include "third_party/WebKit/public/web/WebInputEvent.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
#include "ui/base/window_open_disposition.h" |
+#include "ui/gfx/codec/png_codec.h" |
#include "ui/gfx/geometry/point.h" |
+#include "ui/gfx/screen.h" |
namespace { |
+// Use fixed browser dimensions for pixel tests. |
+const int kBrowserWidth = 600; |
+const int kBrowserHeight = 700; |
+ |
+// Compare only a portion of the snapshots, as different platforms will |
+// capture different sized snapshots (due to differences in browser chrome). |
+const int kComparisonWidth = 500; |
+const int kComparisonHeight = 600; |
+ |
+// Different platforms have slightly different pixel output, due to different |
+// graphics implementations. Slightly different pixels (in BGR space) are still |
+// counted as a matching pixel by this simple manhattan distance threshold. |
+const int kPixelManhattanDistanceTolerance = 8; |
+ |
std::string RunTestScript(base::StringPiece test_script, |
content::WebContents* contents, |
const std::string& element_id) { |
@@ -43,7 +69,8 @@ std::string RunTestScript(base::StringPiece test_script, |
} |
// This also tests that we have JavaScript access to the underlying plugin. |
-bool PluginLoaded(content::WebContents* contents, const char* element_id) { |
+bool PluginLoaded(content::WebContents* contents, |
+ const std::string& element_id) { |
std::string result = RunTestScript( |
"if (plugin.postMessage === undefined) {" |
" window.domAutomationController.send('poster_only');" |
@@ -55,9 +82,28 @@ bool PluginLoaded(content::WebContents* contents, const char* element_id) { |
return result == "plugin_loaded"; |
} |
+// Blocks until the placeholder is ready. |
+void WaitForPlaceholderReady(content::WebContents* contents, |
+ const std::string& element_id) { |
+ std::string result = RunTestScript( |
+ "function handleEvent(event) {" |
+ " if (event.data === 'placeholderReady') {" |
+ " window.domAutomationController.send('ready');" |
+ " plugin.removeEventListener('message', handleEvent);" |
+ " }" |
+ "}" |
+ "plugin.addEventListener('message', handleEvent);" |
+ "if (plugin.hasAttribute('placeholderReady')) {" |
+ " window.domAutomationController.send('ready');" |
+ " plugin.removeEventListener('message', handleEvent);" |
+ "}", |
+ contents, element_id); |
+ ASSERT_EQ("ready", result); |
+} |
+ |
// Also waits for the placeholder UI overlay to finish loading. |
void VerifyPluginIsThrottled(content::WebContents* contents, |
- const char* element_id) { |
+ const std::string& element_id) { |
std::string result = RunTestScript( |
"function handleEvent(event) {" |
" if (event.data.isPeripheral && event.data.isThrottled && " |
@@ -75,10 +121,12 @@ void VerifyPluginIsThrottled(content::WebContents* contents, |
// Page should continue to have JavaScript access to all throttled plugins. |
EXPECT_TRUE(PluginLoaded(contents, element_id)); |
+ |
+ WaitForPlaceholderReady(contents, element_id); |
} |
void VerifyPluginMarkedEssential(content::WebContents* contents, |
- const char* element_id) { |
+ const std::string& element_id) { |
std::string result = RunTestScript( |
"function handleEvent(event) {" |
" if (event.data.isPeripheral === false) {" |
@@ -105,13 +153,116 @@ scoped_ptr<net::test_server::HttpResponse> RespondWithHTML( |
return response.Pass(); |
} |
+void VerifyVisualStateUpdated(const base::Closure& done_cb, |
+ bool visual_state_updated) { |
+ ASSERT_TRUE(visual_state_updated); |
+ done_cb.Run(); |
+} |
+ |
+bool SnapshotMatches(const base::FilePath& reference, const SkBitmap& bitmap) { |
+ if (bitmap.width() < kComparisonWidth || |
+ bitmap.height() < kComparisonHeight) { |
+ return false; |
+ } |
+ |
+ std::string reference_data; |
+ if (!base::ReadFileToString(reference, &reference_data)) |
+ return false; |
+ |
+ int w = 0; |
+ int h = 0; |
+ std::vector<unsigned char> decoded; |
+ if (!gfx::PNGCodec::Decode( |
+ reinterpret_cast<unsigned char*>(string_as_array(&reference_data)), |
+ reference_data.size(), gfx::PNGCodec::FORMAT_BGRA, &decoded, &w, |
+ &h)) { |
+ return false; |
+ } |
+ |
+ if (w < kComparisonWidth || h < kComparisonHeight) |
+ return false; |
+ |
+ int32_t* ref_pixels = reinterpret_cast<int32_t*>(vector_as_array(&decoded)); |
+ SkAutoLockPixels lock_image(bitmap); |
+ int32_t* pixels = static_cast<int32_t*>(bitmap.getPixels()); |
+ |
+ int stride = bitmap.rowBytes(); |
+ for (int y = 0; y < kComparisonHeight; ++y) { |
+ for (int x = 0; x < kComparisonWidth; ++x) { |
+ int32_t pixel = pixels[y * stride / sizeof(int32_t) + x]; |
+ int pixel_b = pixel & 0xFF; |
+ int pixel_g = (pixel >> 8) & 0xFF; |
+ int pixel_r = (pixel >> 16) & 0xFF; |
+ |
+ int32_t ref_pixel = ref_pixels[y * w + x]; |
+ int ref_pixel_b = ref_pixel & 0xFF; |
+ int ref_pixel_g = (ref_pixel >> 8) & 0xFF; |
+ int ref_pixel_r = (ref_pixel >> 16) & 0xFF; |
+ |
+ int manhattan_distance = abs(pixel_b - ref_pixel_b) + |
+ abs(pixel_g - ref_pixel_g) + |
+ abs(pixel_r - ref_pixel_r); |
+ |
+ if (manhattan_distance > kPixelManhattanDistanceTolerance) |
+ return false; |
+ } |
+ } |
+ |
+ return true; |
+} |
+ |
+// |snapshot_matches| is set to true if the snapshot matches the reference and |
+// the test passes. Otherwise, set to false. |
+void CompareSnapshotToReference(const base::FilePath& reference, |
+ bool* snapshot_matches, |
+ const base::Closure& done_cb, |
+ const SkBitmap& bitmap, |
+ content::ReadbackResponse response) { |
+ DCHECK(snapshot_matches); |
+ if (response != content::READBACK_SUCCESS) { |
+ *snapshot_matches = false; |
+ done_cb.Run(); |
+ return; |
+ } |
+ |
+ *snapshot_matches = SnapshotMatches(reference, bitmap); |
+ |
+ // When rebaselining the pixel test, the test will fail, and we will |
+ // overwrite the reference file. On the next try through, the test will then |
+ // pass, since we just overwrote the reference file. A bit wonky. |
+ if (!(*snapshot_matches) && |
+ base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kRebaselinePixelTests)) { |
+ SkBitmap clipped_bitmap; |
+ bitmap.extractSubset(&clipped_bitmap, |
+ SkIRect::MakeWH(kComparisonWidth, kComparisonHeight)); |
+ std::vector<unsigned char> png_data; |
+ gfx::PNGCodec::EncodeBGRASkBitmap(clipped_bitmap, false, &png_data); |
Lei Zhang
2015/10/26 23:15:47
Check return result?
tommycli
2015/10/26 23:29:51
Done.
|
+ base::WriteFile(reference, |
Lei Zhang
2015/10/26 23:15:47
Also check return result?
tommycli
2015/10/26 23:29:51
Done.
|
+ reinterpret_cast<const char*>(vector_as_array(&png_data)), |
+ png_data.size()); |
+ } |
+ |
+ done_cb.Run(); |
+} |
+ |
} // namespace |
class PluginPowerSaverBrowserTest : public InProcessBrowserTest { |
public: |
+ void SetUp() override { |
+ EnablePixelOutput(); |
+ InProcessBrowserTest::SetUp(); |
+ } |
+ |
void SetUpOnMainThread() override { |
InProcessBrowserTest::SetUpOnMainThread(); |
ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ |
+ embedded_test_server()->ServeFilesFromDirectory( |
+ ui_test_utils::GetTestFilePath( |
+ base::FilePath(FILE_PATH_LITERAL("plugin_power_saver")), |
+ base::FilePath())); |
} |
void SetUpCommandLine(base::CommandLine* command_line) override { |
@@ -126,6 +277,13 @@ class PluginPowerSaverBrowserTest : public InProcessBrowserTest { |
protected: |
void LoadHTML(const std::string& html) { |
+ gfx::Rect bounds(gfx::Rect(0, 0, kBrowserWidth, kBrowserHeight)); |
+ gfx::Rect screen_bounds = |
+ gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().bounds(); |
+ ASSERT_GT(screen_bounds.width(), kBrowserWidth); |
+ ASSERT_GT(screen_bounds.height(), kBrowserHeight); |
+ browser()->window()->SetBounds(bounds); |
+ |
ASSERT_TRUE(embedded_test_server()->Started()); |
embedded_test_server()->RegisterRequestHandler( |
base::Bind(&RespondWithHTML, html)); |
@@ -145,29 +303,51 @@ class PluginPowerSaverBrowserTest : public InProcessBrowserTest { |
// - Peripheral status change. |
// - In response to the explicit 'getPowerSaverStatus' request, in case the |
// test has missed the above two events. |
- void SimulateClickAndAwaitMarkedEssential(const char* element_id, |
+ void SimulateClickAndAwaitMarkedEssential(const std::string& element_id, |
const gfx::Point& point) { |
- // Waits for the placeholder to be ready to be clicked first. |
- std::string result = RunTestScript( |
- "function handleEvent(event) {" |
- " if (event.data === 'placeholderLoaded') {" |
- " window.domAutomationController.send('ready');" |
- " plugin.removeEventListener('message', handleEvent);" |
- " }" |
- "}" |
- "plugin.addEventListener('message', handleEvent);" |
- "if (plugin.hasAttribute('placeholderLoaded')) {" |
- " window.domAutomationController.send('ready');" |
- " plugin.removeEventListener('message', handleEvent);" |
- "}", |
- GetActiveWebContents(), element_id); |
- ASSERT_EQ("ready", result); |
- |
+ WaitForPlaceholderReady(GetActiveWebContents(), element_id); |
content::SimulateMouseClickAt(GetActiveWebContents(), 0 /* modifiers */, |
blink::WebMouseEvent::ButtonLeft, point); |
VerifyPluginMarkedEssential(GetActiveWebContents(), element_id); |
} |
+ |
+ // |element_id| must be an element on the foreground tab. |
+ void VerifyPluginIsPosterOnly(const std::string& element_id) { |
+ EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), element_id)); |
+ WaitForPlaceholderReady(GetActiveWebContents(), element_id); |
+ } |
+ |
+ bool VerifySnapshot(const base::FilePath::StringType& expected_filename) { |
+ base::FilePath reference = ui_test_utils::GetTestFilePath( |
+ base::FilePath(FILE_PATH_LITERAL("plugin_power_saver")), |
+ base::FilePath().Append(expected_filename)); |
Lei Zhang
2015/10/26 23:15:47
Can't this be base::FilePath(expected_filename) no
tommycli
2015/10/26 23:29:51
Done.
|
+ |
+ GetActiveWebContents()->GetMainFrame()->InsertVisualStateCallback( |
+ base::Bind(&VerifyVisualStateUpdated, |
+ base::MessageLoop::QuitWhenIdleClosure())); |
+ content::RunMessageLoop(); |
+ |
+ content::RenderWidgetHost* rwh = |
+ GetActiveWebContents()->GetRenderViewHost()->GetWidget(); |
+ |
+ // When a widget is first shown, it can take some time before it is ready |
+ // for copying from its backing store. This is a transient condition, and |
+ // so it is not being treated as a test failure. |
+ if (!rwh->CanCopyFromBackingStore()) |
+ return false; |
+ |
+ bool snapshot_matches = false; |
+ rwh->CopyFromBackingStore( |
+ gfx::Rect(), gfx::Size(), |
+ base::Bind(&CompareSnapshotToReference, reference, &snapshot_matches, |
+ base::MessageLoop::QuitWhenIdleClosure()), |
+ kN32_SkColorType); |
+ |
+ content::RunMessageLoop(); |
+ |
+ return snapshot_matches; |
+ } |
}; |
IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallSameOrigin) { |
@@ -182,10 +362,21 @@ 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>" |
+ "<br>" |
+ "<object id='plugin_poster' data='http://otherorigin.com/fake.swf' " |
+ " type='application/x-ppapi-tests' width='400' height='100' " |
+ " poster='click_me.png'>" |
"</object>"); |
+ |
VerifyPluginIsThrottled(GetActiveWebContents(), "plugin"); |
+ VerifyPluginIsPosterOnly("plugin_poster"); |
+ |
+ EXPECT_TRUE( |
+ VerifySnapshot(FILE_PATH_LITERAL("small_cross_origin_expected.png"))); |
SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); |
+ SimulateClickAndAwaitMarkedEssential("plugin_poster", gfx::Point(50, 150)); |
} |
IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { |
@@ -200,30 +391,94 @@ IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { |
VerifyPluginMarkedEssential(GetActiveWebContents(), "medium_16_9"); |
} |
-// Test extremely flaky, see https://crbug.com/547224 |
Lei Zhang
2015/10/26 23:15:47
So the flakiness should be fixed by this too?
tommycli
2015/10/26 23:29:51
Broken out into separate patch here: https://coder
|
-IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, |
- DISABLED_LargePluginsPeripheralWhenPosterSpecified) { |
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, SmallerThanPlayIcon) { |
+ LoadHTML( |
+ "<object id='plugin_16' type='application/x-ppapi-tests' " |
+ " width='16' height='16'></object>" |
+ "<object id='plugin_32' type='application/x-ppapi-tests' " |
+ " width='32' height='32'></object>" |
+ "<object id='plugin_16_64' type='application/x-ppapi-tests' " |
+ " width='16' height='64'></object>" |
+ "<object id='plugin_64_16' type='application/x-ppapi-tests' " |
+ " width='64' height='16'></object>"); |
+ |
+ VerifyPluginIsThrottled(GetActiveWebContents(), "plugin_16"); |
+ VerifyPluginIsThrottled(GetActiveWebContents(), "plugin_32"); |
+ VerifyPluginIsThrottled(GetActiveWebContents(), "plugin_16_64"); |
+ VerifyPluginIsThrottled(GetActiveWebContents(), "plugin_64_16"); |
+ |
+ EXPECT_TRUE( |
+ VerifySnapshot(FILE_PATH_LITERAL("smaller_than_play_icon_expected.png"))); |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, PosterTests) { |
+ // This test simultaneously verifies the varied supported poster syntaxes, |
+ // as well as verifies that the poster is rendered correctly with various |
+ // mismatched aspect ratios and sizes, following the same rules as VIDEO. |
LoadHTML( |
"<object id='plugin_src' type='application/x-ppapi-tests' " |
- " width='400' height='500' poster='snapshot1x.png'></object>" |
+ " width='100' height='100' poster='click_me.png'></object>" |
"<object id='plugin_srcset' type='application/x-ppapi-tests' " |
- " width='400' height='500' " |
- " poster='snapshot1x.png 1x, snapshot2x.png 2x'></object>" |
- "<object id='plugin_legacy_syntax' type='application/x-ppapi-tests' " |
- " width='400' height='500'>" |
- " <param name='poster' value='snapshot1x.png 1x, snapshot2x.png 2x'>" |
+ " width='100' height='100' " |
+ " poster='click_me.png 1x, click_me.png 2x'></object>" |
+ "<br>" |
+ |
+ "<object id='plugin_poster_param' type='application/x-ppapi-tests' " |
+ " width='100' height='100'>" |
+ " <param name='poster' value='click_me.png 1x, click_me.png 2x'>" |
"</object>" |
"<embed id='plugin_embed_src' type='application/x-ppapi-tests' " |
- " width='400' height='500' poster='snapshot1x.png'></embed>" |
+ " width='100' height='100' poster='click_me.png'></embed>" |
"<embed id='plugin_embed_srcset' type='application/x-ppapi-tests' " |
- " width='400' height='500'" |
- " poster='snapshot1x.png 1x, snapshot2x.png 2x'></embed>"); |
- |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_src")); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_srcset")); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_legacy_syntax")); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_src")); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_srcset")); |
+ " width='100' height='100'" |
+ " poster='click_me.png 1x, click_me.png 2x'></embed>" |
+ "<br>" |
+ |
+ "<object id='poster_missing' type='application/x-ppapi-tests' " |
+ " width='100' height='100' poster='missing.png'></object>" |
+ "<object id='poster_too_small' type='application/x-ppapi-tests' " |
+ " width='100' height='50' poster='click_me.png'></object>" |
+ "<object id='poster_too_big' type='application/x-ppapi-tests' " |
+ " width='100' height='150' poster='click_me.png'></object>" |
+ "<br>" |
+ |
+ "<object id='poster_16' type='application/x-ppapi-tests' " |
+ " width='16' height='16' poster='click_me.png'></object>" |
+ "<object id='poster_32' type='application/x-ppapi-tests' " |
+ " width='32' height='32' poster='click_me.png'></object>" |
+ "<object id='poster_16_64' type='application/x-ppapi-tests' " |
+ " width='16' height='64' poster='click_me.png'></object>" |
+ "<object id='poster_64_16' type='application/x-ppapi-tests' " |
+ " width='64' height='16' poster='click_me.png'></object>" |
+ "<br>" |
+ |
+ "<div id='container' " |
+ " style='width: 400px; height: 100px; overflow: hidden;'>" |
+ " <object id='poster_obscured' data='http://otherorigin.com/fake.swf' " |
+ " type='application/x-ppapi-tests' width='400' height='500' " |
+ " poster='click_me.png'>" |
+ " </object>" |
+ "</div>"); |
+ |
+ VerifyPluginIsPosterOnly("plugin_src"); |
+ VerifyPluginIsPosterOnly("plugin_srcset"); |
+ |
+ VerifyPluginIsPosterOnly("plugin_poster_param"); |
+ VerifyPluginIsPosterOnly("plugin_embed_src"); |
+ VerifyPluginIsPosterOnly("plugin_embed_srcset"); |
+ |
+ VerifyPluginIsPosterOnly("poster_missing"); |
+ VerifyPluginIsPosterOnly("poster_too_small"); |
+ VerifyPluginIsPosterOnly("poster_too_big"); |
+ |
+ VerifyPluginIsPosterOnly("poster_16"); |
+ VerifyPluginIsPosterOnly("poster_32"); |
+ VerifyPluginIsPosterOnly("poster_16_64"); |
+ VerifyPluginIsPosterOnly("poster_64_16"); |
+ |
+ VerifyPluginIsPosterOnly("poster_obscured"); |
+ |
+ EXPECT_TRUE(VerifySnapshot(FILE_PATH_LITERAL("poster_tests_expected.png"))); |
} |
IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePostersNotThrottled) { |
@@ -243,7 +498,7 @@ IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargePostersNotThrottled) { |
" type='application/x-ppapi-tests' width='400' height='300' " |
" poster='click_me.png'></object>"); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_small")); |
+ VerifyPluginIsPosterOnly("poster_small"); |
VerifyPluginMarkedEssential(GetActiveWebContents(), |
"plugin_whitelisted_origin"); |
VerifyPluginMarkedEssential(GetActiveWebContents(), "poster_large"); |
@@ -254,7 +509,7 @@ IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, |
LoadHTML( |
"<object id='plugin' type='application/x-ppapi-tests' " |
" width='400' height='100' poster='snapshot1x.png'></object>"); |
- EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin")); |
+ VerifyPluginIsPosterOnly("plugin"); |
SimulateClickAndAwaitMarkedEssential("plugin", gfx::Point(50, 50)); |
} |
@@ -277,12 +532,15 @@ IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, OriginWhitelisting) { |
IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOriginObscured) { |
LoadHTML( |
"<div id='container' " |
- " style='width: 400px; height: 100px; overflow: hidden;'>" |
+ " style='width: 100px; height: 400px; overflow: hidden;'>" |
" <object id='plugin' data='http://otherorigin.com/fake.swf' " |
- " type='application/x-ppapi-tests' width='400' height='500'>" |
+ " type='application/x-ppapi-tests' width='400' height='500' " |
+ " style='float: right;'>" |
" </object>" |
"</div>"); |
VerifyPluginIsThrottled(GetActiveWebContents(), "plugin"); |
+ EXPECT_TRUE(VerifySnapshot( |
+ FILE_PATH_LITERAL("large_cross_origin_obscured_expected.png"))); |
// Test that's unthrottled if it is unobscured. |
std::string script = |