OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 'use strict'; | 4 'use strict'; |
5 | 5 |
6 /** | 6 /** |
7 * Encodes an image to data URL by using ImageEncoder.getBlob. | 7 * Encodes an image to data URL by using ImageEncoder.getBlob. |
8 */ | 8 */ |
9 function encodeAnImageAsDataURL(canvas, metadata, imageQuality) { | 9 function encodeAnImageAsDataURL(canvas, metadata, imageQuality) { |
10 return new Promise(function(resolve, reject) { | 10 return new Promise(function(resolve, reject) { |
(...skipping 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 | 25 |
26 /** | 26 /** |
27 * Test case for png image. | 27 * Test case for png image. |
28 * Png image should be saved as a png image. | 28 * Png image should be saved as a png image. |
29 */ | 29 */ |
30 function testPngImage(callback) { | 30 function testPngImage(callback) { |
31 var canvas = getSampleCanvas(); | 31 var canvas = getSampleCanvas(); |
32 | 32 |
33 var metadata = { | 33 var metadata = { |
34 media: { | 34 mediaMimeType: 'image/png' |
35 mimeType: 'image/png', | |
36 } | |
37 }; | 35 }; |
38 | 36 |
39 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( | 37 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( |
40 function(result) { | 38 function(result) { |
41 assertEquals(canvas.toDataURL('image/png'), result); | 39 assertEquals(canvas.toDataURL('image/png'), result); |
42 }), callback); | 40 }), callback); |
43 } | 41 } |
44 | 42 |
45 /** | 43 /** |
46 * Test case for jpeg image. | 44 * Test case for jpeg image. |
47 * Jpeg image should be saved as a jpeg image. Since we don't include | 45 * 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. | 46 * exif_encoder.js in this test, no metadata is added to the blob. |
49 */ | 47 */ |
50 function testJpegImage(callback) { | 48 function testJpegImage(callback) { |
51 var canvas = getSampleCanvas(); | 49 var canvas = getSampleCanvas(); |
52 | 50 |
53 var metadata = { | 51 var metadata = { |
54 media: { | 52 mediaMimeType: 'image/jpeg' |
55 mimeType: 'image/jpeg', | |
56 } | |
57 }; | 53 }; |
58 | 54 |
59 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( | 55 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( |
60 function(result) { | 56 function(result) { |
61 assertEquals(canvas.toDataURL('image/jpeg', 0.9), result); | 57 assertEquals(canvas.toDataURL('image/jpeg', 0.9), result); |
62 }), callback); | 58 }), callback); |
63 } | 59 } |
64 | 60 |
65 | 61 |
66 /** | 62 /** |
67 * Test case of webp image. | 63 * Test case of webp image. |
68 * Image should be saved as a image/png since chrome doesn't support to | 64 * Image should be saved as a image/png since chrome doesn't support to |
69 * encode other than image/jpeg or image/png. | 65 * encode other than image/jpeg or image/png. |
70 */ | 66 */ |
71 function testWebpImage(callback) { | 67 function testWebpImage(callback) { |
72 var canvas = getSampleCanvas(); | 68 var canvas = getSampleCanvas(); |
73 | 69 |
74 var metadata = { | 70 var metadata = { |
75 media: { | 71 mediaMimeType: 'image/webp' |
76 mimeType: 'image/webp' | |
77 } | |
78 }; | 72 }; |
79 | 73 |
80 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( | 74 reportPromise(encodeAnImageAsDataURL(canvas, metadata, 0.9).then( |
81 function(result) { | 75 function(result) { |
82 assertEquals(canvas.toDataURL('image/png'), result); | 76 assertEquals(canvas.toDataURL('image/png'), result); |
83 }), callback); | 77 }), callback); |
84 } | 78 } |
85 | 79 |
86 /** | 80 /** |
87 * Test case for broken metadata. | 81 * Test case for broken metadata. |
88 */ | 82 */ |
89 function testWithBrokenMetadata() { | 83 function testWithBrokenMetadata() { |
90 var canvas = getSampleCanvas(); | 84 var canvas = getSampleCanvas(); |
91 | 85 |
92 var metadata = { | 86 var metadata = { |
93 media: { | 87 // No mimetype field. |
94 // No mimetype field. | |
95 } | |
96 }; | 88 }; |
97 | 89 |
98 // An exception should be thrown if metadata is broken. | 90 // An exception should be thrown if metadata is broken. |
99 assertThrows(function() { | 91 assertThrows(function() { |
100 var encoder = ImageEncoder.encodeMetadata(metadata, canvas); | 92 var encoder = ImageEncoder.encodeMetadata(metadata, canvas); |
101 }); | 93 }); |
102 } | 94 } |
OLD | NEW |