Skip to content

Commit

Permalink
Fix chrome automation snapshotting for pages with plugins and transpa…
Browse files Browse the repository at this point in the history
…rent

overlays. Instead of using FORMAT_SkBitmap, which assumes an alpha premultiplied
color, just use FORMAT_BGRA.
BUG=96317
TEST=none


Review URL: http://codereview.chromium.org/7870010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@101399 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kkania@chromium.org committed Sep 15, 2011
1 parent 9008ea7 commit db83b4d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
25 changes: 19 additions & 6 deletions chrome/browser/automation/automation_provider_observers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include "content/common/json_value_serializer.h"
#include "content/common/notification_service.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/rect.h"

Expand Down Expand Up @@ -1894,13 +1895,25 @@ void PageSnapshotTaker::OnModalDialogShown() {
void PageSnapshotTaker::OnSnapshotTaken(const SkBitmap& bitmap) {
base::ThreadRestrictions::ScopedAllowIO allow_io;
std::vector<unsigned char> png_data;
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data);
int bytes_written = file_util::WriteFile(image_path_,
reinterpret_cast<char*>(&png_data[0]), png_data.size());
bool success = bytes_written == static_cast<int>(png_data.size());
SkAutoLockPixels lock_input(bitmap);
bool success = gfx::PNGCodec::Encode(
reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
gfx::PNGCodec::FORMAT_BGRA,
gfx::Size(bitmap.width(), bitmap.height()),
bitmap.rowBytes(),
true, // discard_transparency
std::vector<gfx::PNGCodec::Comment>(),
&png_data);
std::string error_msg;
if (!success)
error_msg = "could not write snapshot to disk";
if (!success) {
error_msg = "could not encode bitmap as PNG";
} else {
int bytes_written = file_util::WriteFile(image_path_,
reinterpret_cast<char*>(&png_data[0]), png_data.size());
success = bytes_written == static_cast<int>(png_data.size());
if (!success)
error_msg = "could not write snapshot to disk";
}
SendMessage(success, error_msg);
}

Expand Down
24 changes: 16 additions & 8 deletions chrome/test/webdriver/test/chromedriver_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ class ScreenshotTest(ChromeDriverTest):

REDBOX = "automation_proxy_snapshot/set_size.html"

def setUp(self):
super(ScreenshotTest, self).setUp()
self._driver = self.GetNewDriver()

def testScreenCaptureAgainstReference(self):
# This has regressed on linux because of tighter sandbox restrictions.
# See crbug.com/89777.
Expand All @@ -401,14 +397,26 @@ def testScreenCaptureAgainstReference(self):
# Create a red square of 2000x2000 pixels.
url = GetFileURLForPath(os.path.join(test_paths.DataDir(),
self.REDBOX))
url += "?2000,2000"
self._driver.get(url)
s = self._driver.get_screenshot_as_base64();
self._driver.get_screenshot_as_file("/tmp/foo.png")
url += '?2000,2000'
driver = self.GetNewDriver()
driver.get(url)
s = driver.get_screenshot_as_base64()
h = hashlib.md5(s).hexdigest()
# Compare the PNG created to the reference hash.
self.assertEquals(h, '12c0ade27e3875da3d8866f52d2fa84f')

# This test requires Flash and must be run on a VM or via remote desktop.
# See crbug.com/96317.
def testSnapshotWithWindowlessFlashAndTransparentOverlay(self):
if not IsWindows():
return

driver = self.GetNewDriver()
driver.get(GetTestDataUrl() + '/plugin_transparency_test.html')
snapshot = driver.get_screenshot_as_base64()
self.assertEquals(hashlib.md5(snapshot).hexdigest(),
'72e5b8525e48758bae59997472f27f14')


class SessionTest(ChromeDriverTest):
"""Tests dealing with WebDriver sessions."""
Expand Down
25 changes: 25 additions & 0 deletions chrome/test/webdriver/test/plugin_transparency_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<html>
<style>
.translucent_div {
position:relative;
top:-100px;
left:50px;
opacity:0.5;
width:100px;
height:100px;
background-color:black;
}
* {
margin:0px;
padding:0px;
}
</style>
<!-- Make the body 2000x2000 so that regardless of the window size, the
document should be the same size-->
<body style='background-color:red; width:2000px; height:2000px'>
<embed src="does_not_exist" width="100" height="100" wmode="opaque"
type="application/x-shockwave-flash" allowscriptaccess="never">
</embed>
<div class='translucent_div'></div>
</body>
</html>

0 comments on commit db83b4d

Please sign in to comment.