| OLD | NEW |
| 1 // tabs api test | 1 // tabs api test |
| 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTab | 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTab |
| 3 | 3 |
| 4 var pass = chrome.test.callbackPass; | 4 var pass = chrome.test.callbackPass; |
| 5 var assertEq = chrome.test.assertEq; | 5 var assertEq = chrome.test.assertEq; |
| 6 var assertTrue = chrome.test.assertTrue; | 6 var assertTrue = chrome.test.assertTrue; |
| 7 | 7 |
| 8 function pageUrl(letter) { | 8 var kWidth = 400; |
| 9 return chrome.extension.getURL(letter + ".html"); | 9 var kHeight = 400; |
| 10 |
| 11 function pageUrl(base) { |
| 12 return chrome.extension.getURL(base + ".html"); |
| 10 } | 13 } |
| 11 | 14 |
| 15 function testAllPixelsAreExpectedColor(imgUrl, color) { |
| 16 assertEq("string", typeof(imgUrl)); |
| 17 var img = new Image(); |
| 18 img.width = kWidth; |
| 19 img.height = kHeight; |
| 20 img.src = imgUrl; |
| 21 img.onload = pass(function() { |
| 22 var canvas = document.createElement("canvas"); |
| 23 |
| 24 // Comparing pixels is slow enough to hit timeouts. Compare |
| 25 // a 10x10 region. |
| 26 canvas.setAttribute('width', 10); |
| 27 canvas.setAttribute('height', 10); |
| 28 var context = canvas.getContext('2d'); |
| 29 context.drawImage(img, 0, 0, 10, 10); |
| 30 |
| 31 var imageData = context.getImageData(1, 1, 9, 9).data; |
| 32 |
| 33 var badPixels = []; |
| 34 |
| 35 for (var i = 0, n = imageData.length; i < n; i += 4) { |
| 36 if (color[0] != imageData[i+0] || |
| 37 color[1] != imageData[i+1] || |
| 38 color[2] != imageData[i+2] || |
| 39 color[3] != imageData[i+3] ) { |
| 40 badPixels.push({"i": i, |
| 41 "color": [imageData[i+0], imageData[i+1], |
| 42 imageData[i+2], imageData[i+3]] |
| 43 }); |
| 44 } |
| 45 } |
| 46 assertEq("[]", JSON.stringify(badPixels, null, 2)) |
| 47 }); |
| 48 } |
| 49 |
| 50 // Globals used to allow a test to read data from a previous test. |
| 51 var blackImageUrl; |
| 52 var whiteImageUrl; |
| 53 |
| 12 chrome.test.runTests([ | 54 chrome.test.runTests([ |
| 13 // Open a window with one tab, take a snapshot. | 55 // Open a window with one tab, take a snapshot. |
| 14 function captureVisibleTabSimple() { | 56 function captureVisibleTabWhiteImage() { |
| 15 // Keep the resulting image small by making the window small. | 57 // Keep the resulting image small by making the window small. |
| 16 createWindow([pageUrl("a")], {"width": 300, "height": 150}, | 58 createWindow([pageUrl("white")], {"width": kWidth, "height": kHeight}, |
| 17 pass(function(winId, tabIds) { | 59 pass(function(winId, tabIds) { |
| 18 waitForAllTabs(pass(function() { | 60 waitForAllTabs(pass(function() { |
| 19 chrome.tabs.getSelected(winId, pass(function(tab) { | 61 chrome.tabs.getSelected(winId, pass(function(tab) { |
| 20 assertEq('complete', tab.status); // waitForAllTabs ensures this. | 62 assertEq('complete', tab.status); // waitForAllTabs ensures this. |
| 21 chrome.tabs.captureVisibleTab(winId, pass(function(imgDataUrl) { | 63 chrome.tabs.captureVisibleTab(winId, pass(function(imgDataUrl) { |
| 22 // The URL should be a data URL with has a JPEG mime type. | 64 // The URL should be a data URL with has a JPEG mime type. |
| 23 assertEq("string", typeof(imgDataUrl)); | 65 assertEq("string", typeof(imgDataUrl)); |
| 24 assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); | 66 assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); |
| 67 whiteImageUrl = imgDataUrl; |
| 68 |
| 69 testAllPixelsAreExpectedColor(whiteImageUrl, |
| 70 [255, 255, 255, 255]); // White. |
| 25 })); | 71 })); |
| 26 })); | 72 })); |
| 27 })); | 73 })); |
| 28 })); | 74 })); |
| 29 }, | 75 }, |
| 30 | 76 |
| 31 // Open a window with three tabs, take a snapshot of each. | 77 function captureVisibleTabBlackImage() { |
| 32 function captureVisibleTabMultiTab() { | 78 // Keep the resulting image small by making the window small. |
| 33 var snapshotAndRemoveSelectedTab = function(winId, callback) { | 79 createWindow([pageUrl("black")], {"width": kWidth, "height": kHeight}, |
| 34 chrome.tabs.getSelected(winId, function(tab) { | 80 pass(function(winId, tabIds) { |
| 35 chrome.tabs.captureVisibleTab(winId, function(imgDataUrl) { | 81 waitForAllTabs(pass(function() { |
| 36 // Test that the URL we got is a data URL which encodes a JPEG image. | 82 chrome.tabs.getSelected(winId, pass(function(tab) { |
| 37 assertEq("string", typeof(imgDataUrl)); | 83 assertEq('complete', tab.status); // waitForAllTabs ensures this. |
| 38 assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); | 84 chrome.tabs.captureVisibleTab(winId, pass(function(imgDataUrl) { |
| 85 // The URL should be a data URL with has a JPEG mime type. |
| 86 assertEq("string", typeof(imgDataUrl)); |
| 87 assertEq('data:image/jpg;base64,', imgDataUrl.substr(0,22)); |
| 88 blackImageUrl = imgDataUrl; |
| 39 | 89 |
| 40 // TODO(skerner): Once an option allows captureVisibleTab to | 90 // Check that previous capture was done. |
| 41 // take a lossless snapshot with a set color depth, use | 91 assertEq('string', typeof(whiteImageUrl)); |
| 42 // a canvas to compare |imgDataUrl| to an image of the tab | |
| 43 // we expect. This can't be done with JPEG, as the results | |
| 44 // vary based on the display settings. | |
| 45 chrome.tabs.remove(tab.id, callback); | |
| 46 }); | |
| 47 }); | |
| 48 }; | |
| 49 | 92 |
| 50 createWindow(["a", "b", "c"].map(pageUrl), {"width": 300, "height": 150}, | 93 assertTrue(whiteImageUrl != blackImageUrl); |
| 51 function(winId, tabIds){ | 94 |
| 52 waitForAllTabs(pass(function() { | 95 testAllPixelsAreExpectedColor(blackImageUrl, |
| 53 snapshotAndRemoveSelectedTab(winId, pass(function() { | 96 [0, 0, 0, 255]); // Black. |
| 54 snapshotAndRemoveSelectedTab(winId, pass(function() { | |
| 55 snapshotAndRemoveSelectedTab(winId, pass(function() {})); | |
| 56 })); | |
| 57 })); | 97 })); |
| 58 })); | 98 })); |
| 59 }); | 99 })); |
| 100 })); |
| 101 }, |
| 102 |
| 103 function captureVisibleTabRedPng() { |
| 104 // Keep the resulting image small by making the window small. |
| 105 createWindow([pageUrl("red")], {"width": kWidth, "height": kHeight}, |
| 106 pass(function(winId, tabIds) { |
| 107 waitForAllTabs(pass(function() { |
| 108 chrome.tabs.getSelected(winId, pass(function(tab) { |
| 109 assertEq('complete', tab.status); // waitForAllTabs ensures this. |
| 110 chrome.tabs.captureVisibleTab(winId, |
| 111 {"format": "png"}, |
| 112 pass(function(imgDataUrl) { |
| 113 // The URL should be a data URL with has a PNG mime type. |
| 114 assertEq("string", typeof(imgDataUrl)); |
| 115 assertEq('data:image/png;base64,', imgDataUrl.substr(0,22)); |
| 116 |
| 117 // TODO(skerner): The pixel comparison test fails on XP. |
| 118 // Find out why. |
| 119 //testAllPixelsAreExpectedColor(imgDataUrl, |
| 120 // [255, 0, 0, 255]); // Red. |
| 121 })); |
| 122 })); |
| 123 })); |
| 124 })); |
| 60 } | 125 } |
| 61 ]); | 126 ]); |
| OLD | NEW |