1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // API test for chrome.tabs.captureVisibleTab(), capturing PNG images.
6 // browser_tests.exe --gtest_filter=ExtensionApiTest.CaptureVisibleTabPng
8 var pass = chrome.test.callbackPass;
9 var fail = chrome.test.callbackFail;
10 var assertEq = chrome.test.assertEq;
11 var assertTrue = chrome.test.assertTrue;
18 var kTestDir = '/extensions/api_test/tabs/capture_visible_tab/common/';
19 var kURLBaseA = 'http://a.com:PORT' + kTestDir;
20 var kURLBaseB = 'http://b.com:PORT' + kTestDir;
25 chrome.test.getConfig(function(config) {
26 var fixPort = function(url) {
27 return url.replace(/PORT/, config.testServer.port);
30 chrome.test.runTests([
31 // Open a window with one tab, take a snapshot.
32 function captureVisibleTabWhiteImage() {
33 // Keep the resulting image small by making the window small.
34 createWindow([fixPort(kURLBaseA + 'white.html')],
36 pass(function(winId, tabIds) {
37 waitForAllTabs(pass(function() {
38 chrome.tabs.getSelected(winId, pass(function(tab) {
39 assertEq('complete', tab.status); // waitForAllTabs ensures this.
40 chrome.tabs.captureVisibleTab(winId,
42 pass(function(imgDataUrl) {
43 // The URL should be a data URL with has a PNG mime type.
44 assertIsStringWithPrefix('data:image/png;base64,', imgDataUrl);
45 whiteImageUrl = imgDataUrl;
47 testPixelsAreExpectedColor(whiteImageUrl,
49 '255,255,255,255'); // White.
56 function captureVisibleTabText() {
57 // Keep the resulting image small by making the window small.
58 createWindow([fixPort(kURLBaseA + 'text.html')],
60 pass(function(winId, tabIds) {
61 waitForAllTabs(pass(function() {
62 chrome.tabs.getSelected(winId, pass(function(tab) {
63 assertEq('complete', tab.status); // waitForAllTabs ensures this.
64 chrome.tabs.captureVisibleTab(winId,
66 pass(function(imgDataUrl) {
67 // The URL should be a data URL with has a PNG mime type.
68 assertIsStringWithPrefix('data:image/png;base64,', imgDataUrl);
70 textImageUrl = imgDataUrl;
71 assertTrue(whiteImageUrl != textImageUrl);
73 countPixelsWithColors(
77 pass(function(colorCounts, totalPixelsChecked) {
78 // Some pixels should not be white, because the text
79 // is not white. Can't test for black because
80 // antialiasing makes the pixels slightly different
81 // on each display setting. Test that all pixels are
82 // not the same color.
83 assertTrue(totalPixelsChecked > colorCounts[0],
85 message: 'Some pixels should not be white.',
86 totalPixelsChecked: totalPixelsChecked,
87 numWhitePixels: colorCounts[0]
96 function captureVisibleTabNoPermissions() {
97 var fail_url = fixPort(kURLBaseB + 'text.html');
98 createWindow([fail_url], kWindowRect, pass(function(winId, tabIds) {
99 waitForAllTabs(pass(function() {
100 chrome.tabs.getSelected(winId, pass(function(tab) {
101 assertEq('complete', tab.status); // waitForAllTabs ensures this.
102 chrome.tabs.captureVisibleTab(winId, {'format': 'png'}, fail(
103 'Cannot access contents of url "' + fail_url +
104 '". Extension manifest must request permission ' +
105 'to access this host.'));
111 function captureVisibleTabChromeExtensionScheme() {
112 var url = chrome.extension.getURL("/common/white.html");
113 createWindow([url], kWindowRect, pass(function(winId, tabIds) {
114 waitForAllTabs(pass(function() {
115 chrome.tabs.getSelected(winId, pass(function(tab) {
116 assertEq('complete', tab.status); // waitForAllTabs ensures this.
117 chrome.tabs.captureVisibleTab(winId,
119 pass(function(imgDataUrl) {
120 // The URL should be a data URL with has a PNG mime type.
121 assertIsStringWithPrefix('data:image/png;base64,', imgDataUrl);
122 testPixelsAreExpectedColor(imgDataUrl,
124 '255,255,255,255'); // White.