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 * Encodes an image to data URL by using ImageEncoder.getBlob.
9 function encodeAnImageAsDataURL(canvas, metadata, imageQuality) {
10 return new Promise(function(resolve, reject) {
11 // Thumbnail quality is fixed to 1.0 in this test.
12 var encoder = ImageEncoder.encodeMetadata(metadata, canvas, 1.0);
13 var blob = ImageEncoder.getBlob(canvas, encoder, imageQuality);
15 var fileReader = new FileReader();
16 fileReader.onerror = function() {
17 reject(fileReader.error);
19 fileReader.onloadend = function() {
20 resolve(fileReader.result);
22 fileReader.readAsDataURL(blob);
27 * Test case for png image.
28 * Png image should be saved as a png image.
30 function testPngImage(callback) {
31 var canvas = getSampleCanvas();
35 mimeType: 'image/png',
39 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then(
41 assertEquals(canvas.toDataURL('image/png'), result);
46 * Test case for jpeg image.
47 * Jpeg image should be saved as a jpeg image. Since we don't include
48 * exif_encoder.js in this test, no metadata is added to the blob.
50 function testJpegImage(callback) {
51 var canvas = getSampleCanvas();
55 mimeType: 'image/jpeg',
59 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then(
61 assertEquals(canvas.toDataURL('image/jpeg', 0.9), result);
67 * Test case of webp image.
68 * Image should be saved as a image/png since chrome doesn't support to
69 * encode other than image/jpeg or image/png.
71 function testWebpImage(callback) {
72 var canvas = getSampleCanvas();
76 mimeType: 'image/webp'
80 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then(
82 assertEquals(canvas.toDataURL('image/png'), result);
87 * Test case for broken metadata.
89 function testWithBrokenMetadata() {
90 var canvas = getSampleCanvas();
98 // An exception should be thrown if metadata is broken.
99 assertThrows(function() {
100 var encoder = ImageEncoder.encodeMetadata(metadata, canvas);