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

Unified Diff: chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js

Issue 2819050: Deal with alpha values when building PNG in chrome.tabs.captureVisibleTab(). (Closed) Base URL: git://codf21.jail/chromium.git
Patch Set: Rerebase. Created 10 years, 5 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
Index: chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js
diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js
index 2a6020a40b8c284a814a1fc2800d625c0328f6c2..68a80d8a349f54c925e7e4e7bde60beae82ddb78 100644
--- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js
+++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js
@@ -9,17 +9,17 @@ var kWidth = 400;
var kHeight = 400;
function pageUrl(base) {
- return chrome.extension.getURL(base + ".html");
+ return chrome.extension.getURL(base + '.html');
}
-function testAllPixelsAreExpectedColor(imgUrl, color) {
- assertEq("string", typeof(imgUrl));
+function getAllPixels(imgUrl, callbackFn) {
+ assertEq('string', typeof(imgUrl));
var img = new Image();
img.width = kWidth;
img.height = kHeight;
img.src = imgUrl;
img.onload = pass(function() {
- var canvas = document.createElement("canvas");
+ var canvas = document.createElement('canvas');
// Comparing pixels is slow enough to hit timeouts. Compare
// a 10x10 region.
@@ -30,20 +30,48 @@ function testAllPixelsAreExpectedColor(imgUrl, color) {
var imageData = context.getImageData(1, 1, 9, 9).data;
- var badPixels = [];
-
+ var pixelColors = [];
for (var i = 0, n = imageData.length; i < n; i += 4) {
- if (color[0] != imageData[i+0] ||
- color[1] != imageData[i+1] ||
- color[2] != imageData[i+2] ||
- color[3] != imageData[i+3] ) {
- badPixels.push({"i": i,
- "color": [imageData[i+0], imageData[i+1],
- imageData[i+2], imageData[i+3]]
+ pixelColors.push([imageData[i+0],
+ imageData[i+1],
+ imageData[i+2],
+ imageData[i+3]].join(','));
+ }
+
+ callbackFn(pixelColors);
+ });
+}
+
+function testAllPixelsAreExpectedColor(imgUrl, expectedColor) {
+ getAllPixels(imgUrl, function(pixelColors) {
+ var badPixels = [];
+ for (var i = 0, ie = pixelColors.length; i < ie; ++i) {
+ if (pixelColors[i] != expectedColor) {
+ badPixels.push({'i': i,
+ 'color': pixelColors[i]
});
}
}
- assertEq("[]", JSON.stringify(badPixels, null, 2))
+ assertEq('[]', JSON.stringify(badPixels, null, 2));
+ });
+}
+
+// Build a count of the number of times the colors in
+// |expectedColors| occur in the image at |imgUrl|.
+function countPixelsWithColors(imgUrl, expectedColors, callback) {
+ colorCounts = new Array(expectedColors.length);
+ for (var i = 0; i < expectedColors.length; ++i) {
+ colorCounts[i] = 0;
+ }
+
+ getAllPixels(imgUrl, function(pixelColors) {
+ for (var i = 0, ie = pixelColors.length; i < ie; ++i) {
+ var colorIdx = expectedColors.indexOf(pixelColors[i]);
+ if (colorIdx != -1)
+ colorCounts[colorIdx]++;
+ }
+ callback(colorCounts, // Mapping from color to # pixels.
+ pixelColors.length); // Total pixels examined.
});
}
@@ -55,19 +83,19 @@ chrome.test.runTests([
// Open a window with one tab, take a snapshot.
function captureVisibleTabWhiteImage() {
// Keep the resulting image small by making the window small.
- createWindow([pageUrl("white")], {"width": kWidth, "height": kHeight},
+ createWindow([pageUrl('white')], {'width': kWidth, 'height': kHeight},
pass(function(winId, tabIds) {
waitForAllTabs(pass(function() {
chrome.tabs.getSelected(winId, pass(function(tab) {
assertEq('complete', tab.status); // waitForAllTabs ensures this.
chrome.tabs.captureVisibleTab(winId, pass(function(imgDataUrl) {
// The URL should be a data URL with has a JPEG mime type.
- assertEq("string", typeof(imgDataUrl));
+ assertEq('string', typeof(imgDataUrl));
assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22));
whiteImageUrl = imgDataUrl;
testAllPixelsAreExpectedColor(whiteImageUrl,
- [255, 255, 255, 255]); // White.
+ '255,255,255,255'); // White.
}));
}));
}));
@@ -76,14 +104,14 @@ chrome.test.runTests([
function captureVisibleTabBlackImage() {
// Keep the resulting image small by making the window small.
- createWindow([pageUrl("black")], {"width": kWidth, "height": kHeight},
+ createWindow([pageUrl('black')], {'width': kWidth, 'height': kHeight},
pass(function(winId, tabIds) {
waitForAllTabs(pass(function() {
chrome.tabs.getSelected(winId, pass(function(tab) {
assertEq('complete', tab.status); // waitForAllTabs ensures this.
chrome.tabs.captureVisibleTab(winId, pass(function(imgDataUrl) {
// The URL should be a data URL with has a JPEG mime type.
- assertEq("string", typeof(imgDataUrl));
+ assertEq('string', typeof(imgDataUrl));
assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22));
blackImageUrl = imgDataUrl;
@@ -93,31 +121,40 @@ chrome.test.runTests([
assertTrue(whiteImageUrl != blackImageUrl);
testAllPixelsAreExpectedColor(blackImageUrl,
- [0, 0, 0, 255]); // Black.
+ '0,0,0,255'); // Black.
}));
}));
}));
}));
},
- function captureVisibleTabRedPng() {
+ function captureVisibleTabText() {
// Keep the resulting image small by making the window small.
- createWindow([pageUrl("red")], {"width": kWidth, "height": kHeight},
+ createWindow([pageUrl('text')], {'width': kWidth, 'height': kHeight},
pass(function(winId, tabIds) {
waitForAllTabs(pass(function() {
chrome.tabs.getSelected(winId, pass(function(tab) {
assertEq('complete', tab.status); // waitForAllTabs ensures this.
chrome.tabs.captureVisibleTab(winId,
- {"format": "png"},
+ {'format': 'png'},
pass(function(imgDataUrl) {
// The URL should be a data URL with has a PNG mime type.
- assertEq("string", typeof(imgDataUrl));
+ assertEq('string', typeof(imgDataUrl));
assertEq('data:image/png;base64,', imgDataUrl.substr(0,22));
- // TODO(skerner): The pixel comparison test fails on XP.
- // Find out why.
- //testAllPixelsAreExpectedColor(imgDataUrl,
- // [255, 0, 0, 255]); // Red.
+ countPixelsWithColors(
+ imgDataUrl,
+ ['255,255,255,255'],
+ pass(function(colorCounts, totalPixelsChecked) {
+ // Some pixels should not be white, because the text
+ // is not white. Can't test for black because
+ // antialiasing makes the pixels slightly different
+ // on each display setting.
+
+ // chrome.test.log(totalPixelsChecked);
+ // chrome.test.log(colorCounts[0]);
+ assertTrue(totalPixelsChecked != colorCounts[0]);
+ }));
}));
}));
}));

Powered by Google App Engine
This is Rietveld 408576698