Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / media_galleries / common / common_injected.js
blob17db28187fde40f235016b2d98641463f9978254
1 // Copyright 2013 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 // Valid WEBP image.
6 var validWEBPImageCase = {
7 filename: "valid.webp",
8 blobString: "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A" +
9 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0" +
10 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0"
13 // Write an invalid test image and expect failure.
14 var invalidWEBPImageCase = {
15 filename: "invalid.webp",
16 blobString: "abc123"
19 function runCopyToTest(testCase, expectSucceed) {
20 var galleries;
21 var testImageFileEntry;
23 chrome.mediaGalleries.getMediaFileSystems(testGalleries);
25 function testGalleries(results) {
26 galleries = results;
27 chrome.test.assertTrue(galleries.length > 0,
28 "Need at least one media gallery to test copyTo");
30 // Create a temporary file system and an image for copying-into test.
31 window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024,
32 temporaryFileSystemCallback,
33 chrome.test.fail);
36 function temporaryFileSystemCallback(filesystem) {
37 filesystem.root.getFile(testCase.filename, {create:true, exclusive: false},
38 temporaryImageCallback,
39 chrome.test.fail);
42 function temporaryImageCallback(entry) {
43 testImageFileEntry = entry;
44 entry.createWriter(imageWriterCallback, chrome.test.fail);
47 function imageWriterCallback(writer) {
48 // Go through a Uint8Array to avoid UTF-8 control bytes.
49 var blobBytes = new Uint8Array(testCase.blobString.length);
50 for (var i = 0; i < testCase.blobString.length; i++) {
51 blobBytes[i] = testCase.blobString.charCodeAt(i);
53 var blob = new Blob([blobBytes], {type : "image/webp"});
55 writer.onerror = function(e) {
56 chrome.test.fail("Unable to write test image: " + e.toString());
59 writer.onwriteend = testCopyTo(testImageFileEntry, galleries[0],
60 testCase.filename, expectSucceed);
62 writer.write(blob);
65 function testCopyTo(testImageFileEntry, gallery, filename, expectSucceed) {
66 var onSuccess;
67 var onFailure;
68 if (expectSucceed) {
69 onSuccess = chrome.test.succeed;
70 onFailure = chrome.test.fail;
71 } else {
72 onSuccess = chrome.test.fail;
73 onFailure = chrome.test.succeed;
75 return function() {
76 testImageFileEntry.copyTo(gallery.root, filename, onSuccess, onFailure);
81 // The custom callback functions are options.
82 // If they are undefined, the default callbacks and checks will be used.
83 // If passed in, all 3 function arguments need to be valid.
84 function runReadGalleriesTest(expectedGalleryCount, expectSucceed,
85 customReadDirectoryCallback,
86 customReadDirectoryErrorCallback,
87 customGotGalleriesCallback) {
88 var galleries;
89 var readEntriesResults = [];
90 var readDirectoryCallback;
91 var readDirectoryErrorCallback;
92 var gotGalleriesCallback;
94 if (customReadDirectoryCallback && customReadDirectoryErrorCallback &&
95 customGotGalleriesCallback) {
96 chrome.test.assertEq(typeof(customReadDirectoryCallback), 'function');
97 chrome.test.assertEq(typeof(customReadDirectoryErrorCallback), 'function');
98 chrome.test.assertEq(typeof(customGotGalleriesCallback), 'function');
99 readDirectoryCallback = customReadDirectoryCallback;
100 readDirectoryErrorCallback = customReadDirectoryErrorCallback;
101 gotGalleriesCallback = customGotGalleriesCallback;
102 } else {
103 chrome.test.assertTrue(!customReadDirectoryCallback &&
104 !customReadDirectoryErrorCallback &&
105 !customGotGalleriesCallback);
106 readDirectoryCallback = defaultReadDirectoryCallback;
107 readDirectoryErrorCallback = defaultReadDirectoryErrorCallback;
108 gotGalleriesCallback = defaultGotGalleriesCallback;
110 chrome.mediaGalleries.getMediaFileSystems(testGalleries);
112 function testGalleries(results) {
113 gotGalleriesCallback(results);
114 chrome.test.assertEq(expectedGalleryCount, results.length,
115 "Gallery count mismatch");
116 if (expectedGalleryCount == 0) {
117 chrome.test.succeed();
118 return;
121 for (var i = 0; i < results.length; i++) {
122 var dirReader = results[i].root.createReader();
123 dirReader.readEntries(readDirectoryCallback, readDirectoryErrorCallback);
127 function defaultGotGalleriesCallback(entries) {
128 galleries = entries;
131 function defaultReadDirectoryCallback(entries) {
132 var result = "";
133 if (!expectSucceed) {
134 result = "Unexpected readEntries success";
136 readEntriesResults.push(result);
137 checkReadEntriesFinished();
140 function defaultReadDirectoryErrorCallback(err) {
141 var result = "";
142 if (expectSucceed) {
143 result = "Unexpected readEntries failure: " + err;
145 readEntriesResults.push(result);
146 checkReadEntriesFinished();
149 function checkReadEntriesFinished() {
150 if (readEntriesResults.length != galleries.length)
151 return;
152 var success = true;
153 for (var i = 0; i < readEntriesResults.length; i++) {
154 if (readEntriesResults[i]) {
155 success = false;
158 if (success) {
159 chrome.test.succeed();
160 return;
162 chrome.test.fail(readEntriesResults);
166 function checkMetadata(metadata) {
167 chrome.test.assertTrue(metadata != null);
168 chrome.test.assertTrue(metadata.name.length > 0);
169 chrome.test.assertTrue(metadata.galleryId.length > 0);
170 chrome.test.assertTrue("isAvailable" in metadata);
171 chrome.test.assertTrue("isMediaDevice" in metadata);
172 chrome.test.assertTrue("isRemovable" in metadata);
173 if (metadata.isRemovable && metadata.isAvailable) {
174 chrome.test.assertTrue("deviceId" in metadata);
175 chrome.test.assertTrue(metadata.deviceId.length > 0);
179 // Gets the entire listing from directory, then verifies the sorted contents.
180 function verifyDirectoryEntry(directoryEntry, verifyFunction) {
181 var allEntries = [];
182 var reader = directoryEntry.createReader();
184 function readEntries() {
185 reader.readEntries(readEntriesCallback, chrome.test.fail);
188 function readEntriesCallback(entries) {
189 if (entries.length == 0) {
190 // This is the readEntries() is finished case.
191 verifyFunction(directoryEntry, allEntries.sort());
192 return;
195 allEntries = allEntries.concat(entries);
196 readEntries();
199 readEntries();
202 function verifyJPEG(parentDirectoryEntry, filename, expectedFileLength,
203 doneCallback) {
204 function verifyFileEntry(fileEntry) {
205 fileEntry.file(verifyFile, chrome.test.fail);
208 function verifyFile(file) {
209 var reader = new FileReader();
211 reader.onload = function(e) {
212 var arraybuffer = e.target.result;
213 chrome.test.assertEq(expectedFileLength, arraybuffer.byteLength);
214 doneCallback();
217 reader.onerror = function(e) {
218 chrome.test.fail("Unable to read test image: " + filename);
221 reader.readAsArrayBuffer(file);
224 parentDirectoryEntry.getFile(filename, {create: false}, verifyFileEntry,
225 chrome.test.fail);
228 // Create a dummy window to prevent the ProcessManager from suspending the
229 // chrome-test app. Needed because the writer.onerror and writer.onwriteend
230 // events do not qualify as pending callbacks, so the app looks dormant.
231 function CreateDummyWindowToPreventSleep() {
232 chrome.app.runtime.onLaunched.addListener(function() {
233 chrome.app.window.create('dummy.html', {
234 outerBounds: {
235 width: 800,
236 height: 600,
237 left: 100,
238 top: 100,
239 minWidth: 800,
240 minHeight: 600