1 // Copyright 2014 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.
7 // Tests don't start running until an async call to
8 // chrome.mimeHandlerPrivate.getStreamInfo() completes, so queue any messages
9 // received until that point.
10 var queuedMessages = [];
12 function queueMessage(event) {
13 queuedMessages.push(event);
16 window.addEventListener('message', queueMessage, false);
20 function fetchUrl(url) {
21 return new Promise(function(resolve, reject) {
22 var request = new XMLHttpRequest();
23 request.onreadystatechange = function() {
24 if (request.readyState == 4) {
26 status: request.status,
27 data: request.responseText,
31 request.open('GET', streamDetails.streamUrl, true);
36 function expectSuccessfulRead(response) {
37 chrome.test.assertEq(200, response.status);
38 chrome.test.assertEq('content to read\n', response.data);
41 function expectSuccessfulReadLong(response) {
42 chrome.test.assertEq(200, response.status);
43 chrome.test.assertTrue(response.data.indexOf('content to read\n') === 0);
46 function checkStreamDetails(name, embedded) {
47 checkStreamDetailsNoFile();
48 chrome.test.assertEq(embedded, streamDetails.embedded);
49 chrome.test.assertTrue(streamDetails.originalUrl.indexOf(name) != -1);
50 chrome.test.assertEq('text/csv',
51 streamDetails.responseHeaders['Content-Type']);
54 function checkStreamDetailsNoFile() {
55 chrome.test.assertEq('text/csv', streamDetails.mimeType);
56 chrome.test.assertTrue(streamDetails.tabId != -1);
60 function testBasic() {
61 checkStreamDetails('testBasic.csv', false);
62 fetchUrl(streamDetails.streamUrl)
63 .then(expectSuccessfulRead)
64 .then(chrome.test.succeed);
67 function testEmbedded() {
68 checkStreamDetails('testEmbedded.csv', true);
69 fetchUrl(streamDetails.streamUrl)
70 .then(expectSuccessfulRead)
71 .then(chrome.test.succeed);
74 function testIframe() {
75 checkStreamDetails('testIframe.csv', true);
76 var printMessageArrived = new Promise(function(resolve, reject) {
77 window.addEventListener('message', function(event) {
78 chrome.test.assertEq('print', event.data.type);
82 var contentRead = fetchUrl(streamDetails.streamUrl)
83 .then(expectSuccessfulRead);
84 Promise.all([printMessageArrived, contentRead]).then(chrome.test.succeed);
87 function testAbort() {
88 checkStreamDetails('testAbort.csv', false);
89 chrome.mimeHandlerPrivate.abortStream(function() {
90 fetchUrl(streamDetails.streamUrl).then(function(response) {
91 chrome.test.assertEq(404, response.status);
92 chrome.test.assertEq('', response.data);
93 chrome.test.succeed();
98 function testNonAsciiHeaders() {
99 checkStreamDetails('testNonAsciiHeaders.csv', false);
100 chrome.test.assertEq(undefined,
101 streamDetails.responseHeaders['Content-Disposition']);
102 chrome.test.assertEq(undefined,
103 streamDetails.responseHeaders['ΓΌ']);
104 chrome.test.succeed();
107 function testPostMessage() {
108 var expectedMessages = ['hey', 100, 25.0];
109 var messagesReceived = 0;
110 function handleMessage(event) {
111 if (event.data == 'succeed' &&
112 messagesReceived == expectedMessages.length) {
113 chrome.test.succeed();
114 } else if (event.data == 'fail') {
116 } else if (event.data == expectedMessages[messagesReceived]) {
117 event.source.postMessage(event.data, '*');
120 chrome.test.fail('unexpected message ' + event.data);
123 window.addEventListener('message', handleMessage, false);
124 while (queuedMessages.length) {
125 handleMessage(queuedMessages.shift());
129 function testDataUrl() {
130 // TODO(raymes): have separate checks for embedded/unembedded data URLs.
131 checkStreamDetailsNoFile();
132 fetchUrl(streamDetails.streamUrl)
133 .then(expectSuccessfulRead)
134 .then(chrome.test.succeed);
137 function testDataUrlLong() {
138 checkStreamDetailsNoFile();
139 fetchUrl(streamDetails.streamUrl)
140 .then(expectSuccessfulReadLong)
141 .then(chrome.test.succeed);
144 function testResizeBeforeAttach() {
145 checkStreamDetails('testResizeBeforeAttach.csv', true);
146 chrome.test.succeed();
150 var testsByName = {};
151 for (let i = 0; i < tests.length; i++) {
152 testsByName[tests[i].name] = tests[i];
155 chrome.mimeHandlerPrivate.getStreamInfo(function(streamInfo) {
159 // If the name of the file we're handling matches the name of a test, run that
161 var urlComponents = streamInfo.originalUrl.split('/');
162 var test = urlComponents[urlComponents.length - 1].split('.')[0];
163 streamDetails = streamInfo;
164 if (testsByName[test]) {
165 window.removeEventListener('message', queueMessage);
166 chrome.test.runTests([testsByName[test]]);
169 // Run the test for data URLs.
170 if (streamInfo.originalUrl.indexOf("data:") === 0) {
171 window.removeEventListener('message', queueMessage);
172 // Long data URLs get truncated.
173 if (streamInfo.originalUrl == "data:")
174 chrome.test.runTests([testsByName['testDataUrlLong']]);
176 chrome.test.runTests([testsByName['testDataUrl']]);