Chromium Code Reviews| 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 333720d38cf3728abeb7a5bffab2b9efd6ca013e..27f0e426a0d4d819b7d2221a5466ef8e90506f13 100644 |
| --- a/chrome/browser/plugins/plugin_power_saver_browsertest.cc |
| +++ b/chrome/browser/plugins/plugin_power_saver_browsertest.cc |
| @@ -3,14 +3,20 @@ |
| // found in the LICENSE file. |
| #include "base/command_line.h" |
| +#include "base/stl_util.h" |
| #include "base/strings/string_piece.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/test/test_switches.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/ui_test_utils.h" |
| #include "components/ui/zoom/zoom_controller.h" |
| +#include "content/public/browser/readback_types.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 +25,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) { |
| @@ -105,13 +128,110 @@ scoped_ptr<net::test_server::HttpResponse> RespondWithHTML( |
| return response.Pass(); |
| } |
| +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*>(&decoded[0]); |
|
Lei Zhang
2015/10/21 23:25:33
vector_as_array
tommycli
2015/10/21 23:57:22
Done.
|
| + 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); |
| + base::WriteFile(reference, |
| + 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 +246,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)); |
| @@ -168,6 +295,53 @@ class PluginPowerSaverBrowserTest : public InProcessBrowserTest { |
| VerifyPluginMarkedEssential(GetActiveWebContents(), element_id); |
| } |
| + |
| + bool VerifySnapshot(const std::string& expected_filename) { |
| + // If we are rebaselining, give the placeholders an extra 2 seconds to |
| + // settle, so we write the correct reference images. See comment below. |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kRebaselinePixelTests)) { |
| + base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| + FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| + base::TimeDelta::FromSeconds(2)); |
| + content::RunMessageLoop(); |
| + } |
| + |
| + base::FilePath reference = ui_test_utils::GetTestFilePath( |
| + base::FilePath(FILE_PATH_LITERAL("plugin_power_saver")), |
| + base::FilePath().AppendASCII(expected_filename)); |
| + |
| + // Because of how Pepper painting works, even if the plugin is ready, the |
| + // image data may not have arrived in the backing store. Retry verifying |
| + // the snapshot with a timeout. |
| + return ui_test_utils::RunLoopUntil( |
| + base::Bind(&PluginPowerSaverBrowserTest::TryVerifySnapshot, |
| + base::Unretained(this), reference)); |
| + } |
| + |
| + private: |
| + bool TryVerifySnapshot(const base::FilePath& reference) { |
| + bool snapshot_matches = false; |
| + |
| + 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; |
| + |
| + 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 +356,20 @@ 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"); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_poster")); |
| + |
| + EXPECT_TRUE(VerifySnapshot("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,29 +384,93 @@ IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, LargeCrossOrigin) { |
| VerifyPluginMarkedEssential(GetActiveWebContents(), "medium_16_9"); |
| } |
| -IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, |
| - 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("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>"); |
| + " 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>"); |
| EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_src")); |
| EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_srcset")); |
| - EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_legacy_syntax")); |
| + |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_poster_param")); |
| EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_src")); |
| EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "plugin_embed_srcset")); |
| + |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_missing")); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_too_small")); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_too_big")); |
| + |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_16")); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_32")); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_16_64")); |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_64_16")); |
| + |
| + EXPECT_FALSE(PluginLoaded(GetActiveWebContents(), "poster_obscured")); |
| + |
| + EXPECT_TRUE(VerifySnapshot("poster_tests_expected.png")); |
| } |
| IN_PROC_BROWSER_TEST_F(PluginPowerSaverBrowserTest, |
| @@ -249,12 +497,14 @@ 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("large_cross_origin_obscured_expected.png")); |
| // Test that's unthrottled if it is unobscured. |
| std::string script = |