Index: chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js |
=================================================================== |
--- chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js (revision 44660) |
+++ chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js (working copy) |
@@ -5,15 +5,57 @@ |
var assertEq = chrome.test.assertEq; |
var assertTrue = chrome.test.assertTrue; |
-function pageUrl(letter) { |
- return chrome.extension.getURL(letter + ".html"); |
+var kWidth = 400; |
+var kHeight = 400; |
+ |
+function pageUrl(base) { |
+ return chrome.extension.getURL(base + ".html"); |
} |
+function testAllPixelsAreExpectedColor(imgUrl, color) { |
+ 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"); |
+ |
+ // Comparing pixels is slow enough to hit timeouts. Compare |
+ // a 10x10 region. |
+ canvas.setAttribute('width', 10); |
+ canvas.setAttribute('height', 10); |
+ var context = canvas.getContext('2d'); |
+ context.drawImage(img, 0, 0, 10, 10); |
+ |
+ var imageData = context.getImageData(1, 1, 9, 9).data; |
+ |
+ var badPixels = []; |
+ |
+ 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]] |
+ }); |
+ } |
+ } |
+ assertEq("[]", JSON.stringify(badPixels, null, 2)) |
+ }); |
+} |
+ |
+// Globals used to allow a test to read data from a previous test. |
+var blackImageUrl; |
+var whiteImageUrl; |
+ |
chrome.test.runTests([ |
// Open a window with one tab, take a snapshot. |
- function captureVisibleTabSimple() { |
+ function captureVisibleTabWhiteImage() { |
// Keep the resulting image small by making the window small. |
- createWindow([pageUrl("a")], {"width": 300, "height": 150}, |
+ createWindow([pageUrl("white")], {"width": kWidth, "height": kHeight}, |
pass(function(winId, tabIds) { |
waitForAllTabs(pass(function() { |
chrome.tabs.getSelected(winId, pass(function(tab) { |
@@ -22,40 +64,63 @@ |
// The URL should be a data URL with has a JPEG mime type. |
assertEq("string", typeof(imgDataUrl)); |
assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); |
+ whiteImageUrl = imgDataUrl; |
+ |
+ testAllPixelsAreExpectedColor(whiteImageUrl, |
+ [255, 255, 255, 255]); // White. |
})); |
})); |
})); |
})); |
}, |
- // Open a window with three tabs, take a snapshot of each. |
- function captureVisibleTabMultiTab() { |
- var snapshotAndRemoveSelectedTab = function(winId, callback) { |
- chrome.tabs.getSelected(winId, function(tab) { |
- chrome.tabs.captureVisibleTab(winId, function(imgDataUrl) { |
- // Test that the URL we got is a data URL which encodes a JPEG image. |
- assertEq("string", typeof(imgDataUrl)); |
- assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); |
+ function captureVisibleTabBlackImage() { |
+ // Keep the resulting image small by making the window small. |
+ 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('data:image/jpg;base64,', imgDataUrl.substr(0,22)); |
+ blackImageUrl = imgDataUrl; |
- // TODO(skerner): Once an option allows captureVisibleTab to |
- // take a lossless snapshot with a set color depth, use |
- // a canvas to compare |imgDataUrl| to an image of the tab |
- // we expect. This can't be done with JPEG, as the results |
- // vary based on the display settings. |
- chrome.tabs.remove(tab.id, callback); |
- }); |
- }); |
- }; |
+ // Check that previous capture was done. |
+ assertEq('string', typeof(whiteImageUrl)); |
- createWindow(["a", "b", "c"].map(pageUrl), {"width": 300, "height": 150}, |
- function(winId, tabIds){ |
- waitForAllTabs(pass(function() { |
- snapshotAndRemoveSelectedTab(winId, pass(function() { |
- snapshotAndRemoveSelectedTab(winId, pass(function() { |
- snapshotAndRemoveSelectedTab(winId, pass(function() {})); |
- })); |
+ assertTrue(whiteImageUrl != blackImageUrl); |
+ |
+ testAllPixelsAreExpectedColor(blackImageUrl, |
+ [0, 0, 0, 255]); // Black. |
})); |
})); |
- }); |
+ })); |
+ })); |
+ }, |
+ |
+ function captureVisibleTabRedPng() { |
+ // Keep the resulting image small by making the window small. |
+ createWindow([pageUrl("red")], {"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"}, |
+ pass(function(imgDataUrl) { |
+ // The URL should be a data URL with has a PNG mime type. |
+ 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. |
+ })); |
+ })); |
+ })); |
+ })); |
} |
]); |