diff options
author | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 20:21:08 +0000 |
---|---|---|
committer | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-23 20:21:08 +0000 |
commit | ff65139ad41289aa2e62637b16e2dc1a744528c7 (patch) | |
tree | 7b8b620095589c06823952b54ebbb718955535fa /chrome/test/data/extensions | |
parent | 94a1d6a282d6ab09f2388bc99f25faabff14bd31 (diff) | |
download | chromium_src-ff65139ad41289aa2e62637b16e2dc1a744528c7.zip chromium_src-ff65139ad41289aa2e62637b16e2dc1a744528c7.tar.gz chromium_src-ff65139ad41289aa2e62637b16e2dc1a744528c7.tar.bz2 |
Split captureVisibleTab() tests into jpeg and png tests, to avoid timeouts.
BUG=49040
TEST=ExtensionApiTest.CaptureVisibleTab*
Review URL: http://codereview.chromium.org/3050014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53503 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/data/extensions')
15 files changed, 272 insertions, 221 deletions
diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/a.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/a.html deleted file mode 100644 index e69de29..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/a.html +++ /dev/null diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/b.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/b.html deleted file mode 100644 index e69de29..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/b.html +++ /dev/null diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/c.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/c.html deleted file mode 100644 index e69de29..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/c.html +++ /dev/null diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/black.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/black.html index beb85b8..beb85b8 100644 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/black.html +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/black.html diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js new file mode 100644 index 0000000..c2f6e67 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/tabs_util.js @@ -0,0 +1,121 @@ +// Utility functions to help with tabs/windows testing. + +// Creates one window with tabs set to the urls in the array |tabUrls|. +// At least one url must be specified. +// The |callback| should look like function(windowId, tabIds) {...}. +function createWindow(tabUrls, winOptions, callback) { + winOptions["url"] = tabUrls[0]; + chrome.windows.create(winOptions, function(win) { + assertTrue(win.id > 0); + var newTabIds = []; + + // Create tabs and populate newTabIds array. + chrome.tabs.getSelected(win.id, function (tab) { + newTabIds.push(tab.id); + for (var i = 1; i < tabUrls.length; i++) { + chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]}, + function(tab){ + newTabIds.push(tab.id); + if (newTabIds.length == tabUrls.length) + callback(win.id, newTabIds); + }); + } + if (tabUrls.length == 1) + callback(win.id, newTabIds); + }); + }); +} + +// Waits until all tabs (yes, in every window) have status "complete". +// This is useful to prevent test overlap when testing tab events. +// |callback| should look like function() {...}. +function waitForAllTabs(callback) { + // Wait for all tabs to load. + function waitForTabs(){ + chrome.windows.getAll({"populate": true}, function(windows) { + var ready = true; + for (var i in windows){ + for (var j in windows[i].tabs) { + if (windows[i].tabs[j].status != "complete") { + ready = false; + break; + } + } + if (!ready) + break; + } + if (ready) + callback(); + else + window.setTimeout(waitForTabs, 30); + }); + } + waitForTabs(); +} + +function getAllPixels(imgUrl, windowRect, callbackFn) { + assertEq('string', typeof(imgUrl)); + var img = new Image(); + img.width = windowRect.width; + img.height = windowRect.height; + 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 pixelColors = []; + for (var i = 0, n = imageData.length; i < n; i += 4) { + pixelColors.push([imageData[i+0], + imageData[i+1], + imageData[i+2], + imageData[i+3]].join(',')); + } + + callbackFn(pixelColors); + }); +} + +function testAllPixelsAreExpectedColor(imgUrl, windowRect, expectedColor) { + getAllPixels(imgUrl, windowRect, 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)); + }); +} + +// Build a count of the number of times the colors in +// |expectedColors| occur in the image at |imgUrl|. +function countPixelsWithColors(imgUrl, windowRect, expectedColors, callback) { + colorCounts = new Array(expectedColors.length); + for (var i = 0; i < expectedColors.length; ++i) { + colorCounts[i] = 0; + } + + getAllPixels(imgUrl, windowRect, 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. + }); +} + +function pageUrl(base) { + return chrome.extension.getURL('common/' + base + '.html'); +} diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/text.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/text.html index bee19df..bee19df 100644 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/text.html +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/text.html diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/white.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/white.html index a05a8a0..a05a8a0 100644 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/white.html +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/common/white.html diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/manifest.json b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/manifest.json index 779b040..84dc9c3 100644 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/manifest.json +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/manifest.json @@ -1,7 +1,6 @@ { - "name": "chrome.tabs.captureVisibleTab test", + "name": "chrome.tabs.captureVisibleTab test: jpeg", "version": "0.1", "description": "end-to-end browser test for chrome.tabs.captureVisibleTab", - "background_page": "test.html", "permissions": ["tabs"] } diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/tabs_util.js deleted file mode 100644 index f879681..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/tabs_util.js +++ /dev/null @@ -1,54 +0,0 @@ -// Utility functions to help with tabs/windows testing. - -// Creates one window with tabs set to the urls in the array |tabUrls|. -// At least one url must be specified. -// The |callback| should look like function(windowId, tabIds) {...}. -function createWindow(tabUrls, winOptions, callback) { - winOptions["url"] = tabUrls[0]; - chrome.windows.create(winOptions, function(win) { - assertTrue(win.id > 0); - var newTabIds = []; - - // Create tabs and populate newTabIds array. - chrome.tabs.getSelected(win.id, function (tab) { - newTabIds.push(tab.id); - for (var i = 1; i < tabUrls.length; i++) { - chrome.tabs.create({"windowId": win.id, "url": tabUrls[i]}, - function(tab){ - newTabIds.push(tab.id); - if (newTabIds.length == tabUrls.length) - callback(win.id, newTabIds); - }); - } - if (tabUrls.length == 1) - callback(win.id, newTabIds); - }); - }); -} - -// Waits until all tabs (yes, in every window) have status "complete". -// This is useful to prevent test overlap when testing tab events. -// |callback| should look like function() {...}. -function waitForAllTabs(callback) { - // Wait for all tabs to load. - function waitForTabs(){ - chrome.windows.getAll({"populate": true}, function(windows) { - var ready = true; - for (var i in windows){ - for (var j in windows[i].tabs) { - if (windows[i].tabs[j].status != "complete") { - ready = false; - break; - } - } - if (!ready) - break; - } - if (ready) - callback(); - else - window.setTimeout(waitForTabs, 30); - }); - } - waitForTabs(); -} diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.html deleted file mode 100644 index 54f4ab7..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.html +++ /dev/null @@ -1,2 +0,0 @@ -<script src="tabs_util.js"></script> -<script src="test.js"></script> 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 deleted file mode 100644 index 68a80d8..0000000 --- a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test.js +++ /dev/null @@ -1,163 +0,0 @@ -// tabs api test -// browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTab - -var pass = chrome.test.callbackPass; -var assertEq = chrome.test.assertEq; -var assertTrue = chrome.test.assertTrue; - -var kWidth = 400; -var kHeight = 400; - -function pageUrl(base) { - return chrome.extension.getURL(base + '.html'); -} - -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'); - - // 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 pixelColors = []; - for (var i = 0, n = imageData.length; i < n; i += 4) { - 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)); - }); -} - -// 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. - }); -} - -// 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 captureVisibleTabWhiteImage() { - // Keep the resulting image small by making the window small. - 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('data:image/jpg;base64,', imgDataUrl.substr(0,22)); - whiteImageUrl = imgDataUrl; - - testAllPixelsAreExpectedColor(whiteImageUrl, - '255,255,255,255'); // White. - })); - })); - })); - })); - }, - - 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; - - // Check that previous capture was done. - assertEq('string', typeof(whiteImageUrl)); - - assertTrue(whiteImageUrl != blackImageUrl); - - testAllPixelsAreExpectedColor(blackImageUrl, - '0,0,0,255'); // Black. - })); - })); - })); - })); - }, - - function captureVisibleTabText() { - // Keep the resulting image small by making the window small. - 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'}, - 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)); - - 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]); - })); - })); - })); - })); - })); - } -]); diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.html new file mode 100644 index 0000000..39808a6 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.html @@ -0,0 +1,2 @@ +<script src="common/tabs_util.js"></script> +<script src="test_jpeg.js"></script> diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.js new file mode 100644 index 0000000..f5162e2 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_jpeg.js @@ -0,0 +1,69 @@ +// API test for chrome.tabs.captureVisibleTab(), capturing JPEG images. +// browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTabJpeg + +var pass = chrome.test.callbackPass; +var assertEq = chrome.test.assertEq; +var assertTrue = chrome.test.assertTrue; + +var kWindowRect = { + 'width': 400, + 'height': 400 +}; + +// 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 captureVisibleTabWhiteImage() { + // Keep the resulting image small by making the window small. + createWindow([pageUrl('white')], + kWindowRect, + 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)); + whiteImageUrl = imgDataUrl; + + testAllPixelsAreExpectedColor(whiteImageUrl, + kWindowRect, + '255,255,255,255'); // White. + })); + })); + })); + })); + }, + + function captureVisibleTabBlackImage() { + // Keep the resulting image small by making the window small. + createWindow([pageUrl('black')], + kWindowRect, + 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; + + // Check that previous capture was done. + assertEq('string', typeof(whiteImageUrl)); + + assertTrue(whiteImageUrl != blackImageUrl); + + testAllPixelsAreExpectedColor(blackImageUrl, + kWindowRect, + '0,0,0,255'); // Black. + })); + })); + })); + })); + } +]); diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.html b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.html new file mode 100644 index 0000000..f3944eda --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.html @@ -0,0 +1,2 @@ +<script src="common/tabs_util.js"></script> +<script src="test_png.js"></script> diff --git a/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.js b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.js new file mode 100644 index 0000000..5f06d33 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/capture_visible_tab/test_png.js @@ -0,0 +1,77 @@ +// API test for chrome.tabs.captureVisibleTab(), capturing PNG images. +// browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTabPng + +var pass = chrome.test.callbackPass; +var assertEq = chrome.test.assertEq; +var assertTrue = chrome.test.assertTrue; + +var kWindowRect = { + 'width': 400, + 'height': 400 +}; + +var whiteImageUrl; +var textImageUrl; + +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')], + kWindowRect, + 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)); + whiteImageUrl = imgDataUrl; + + testAllPixelsAreExpectedColor(whiteImageUrl, + kWindowRect, + '255,255,255,255'); // White. + })); + })); + })); + })); + }, + + function captureVisibleTabText() { + // Keep the resulting image small by making the window small. + createWindow([pageUrl('text')], + kWindowRect, + 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)); + textImageUrl = imgDataUrl; + assertTrue(whiteImageUrl != textImageUrl); + + countPixelsWithColors( + imgDataUrl, + kWindowRect, + ['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. + + assertTrue(totalPixelsChecked != colorCounts[0]); + })); + })); + })); + })); + })); + } +]); |