1 // Copyright 2015 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.
9 getMetricName: function() {},
11 recordEnum: function() {},
12 recordInterval: function() {},
13 startInterval: function() {}
18 * Mock of ImageEncoder. Since some test changes the behavior of ImageEncoder,
19 * this is initialized in setUp().
27 DRIVE_DIRECTORY_LABEL: '',
28 DOWNLOADS_DIRECTORY_LABEL: ''
33 encodeMetadata: function() {},
34 getBlob: function() {}
39 * Returns a mock of metadata model.
41 * @return {!MetadataModel}
43 function getMockMetadataModel() {
45 get: function(entries, names) {
46 return Promise.resolve([
50 notifyEntriesChanged: function() {
56 * Tests for GalleryItem#saveToFile.
58 function testSaveToFile(callback) {
59 var fileSystem = new MockFileSystem('volumeId');
60 fileSystem.populate(['/test.jpg']);
61 var entry = fileSystem.entries['/test.jpg'];
62 entry.createWriter = function(callback) {
65 Promise.resolve().then(function() {
69 truncate: function() {
74 var entryChanged = false;
75 var metadataModel = getMockMetadataModel();
76 metadataModel.notifyEntriesChanged = function() {
80 var item = new Gallery.Item(
86 assertEquals(100, item.getMetadataItem().size);
87 assertFalse(entryChanged);
89 new Promise(item.saveToFile.bind(
91 {getLocationInfo: function() { return {}; }},
93 /* fallbackDir */ null,
95 document.createElement('canvas'))).then(function() {
96 assertEquals(200, item.getMetadataItem().size);
97 assertTrue(entryChanged);
102 * Tests for GalleryItem#saveToFile. In this test case, fileWriter.write fails
105 function testSaveToFileWriteFailCase(callback) {
106 var fileSystem = new MockFileSystem('volumeId');
107 fileSystem.populate(['/test.jpg']);
108 var entry = fileSystem.entries['/test.jpg'];
110 entry.createWriter = function(callback) {
113 Promise.resolve().then(function() {
114 this.onerror(new Error());
117 truncate: function() {
118 Promise.resolve().then(function() {
125 var item = new Gallery.Item(
130 /* original */ true);
132 new Promise(item.saveToFile.bind(
134 {getLocationInfo: function() { return {}; }},
135 getMockMetadataModel(),
136 /* fallbackDir */ null,
137 /* overwrite */ true,
138 document.createElement('canvas'))).then(function(result) {
144 * Tests for GalleryItem#saveToFile. In this test case, ImageEncoder.getBlob
145 * fails with an error. This test case confirms that no write operation runs
146 * when it fails to get a blob of new image.
148 function testSaveToFileGetBlobFailCase(callback) {
149 ImageEncoder.getBlob = function() {
153 var fileSystem = new MockFileSystem('volumeId');
154 fileSystem.populate(['/test.jpg']);
155 var entry = fileSystem.entries['/test.jpg'];
157 var writeOperationRun = false;
158 entry.createWriter = function(callback) {
161 Promise.resolve().then(function() {
162 writeOperationRun = true;
166 truncate: function() {
167 Promise.resolve().then(function() {
168 writeOperationRun = true;
175 var item = new Gallery.Item(
180 /* original */ true);
182 new Promise(item.saveToFile.bind(
184 {getLocationInfo: function() { return {}; }},
185 getMockMetadataModel(),
186 /* fallbackDir */ null,
187 /* overwrite */ true,
188 document.createElement('canvas'))).then(function(result) {
190 assertFalse(writeOperationRun);
194 function testSaveToFileRaw(callback) {
195 var fileSystem = new MockFileSystem('volumeId');
196 fileSystem.populate(['/test.arw']);
197 fileSystem.entries['/'].getFile = function(name, options, success, error) {
198 if (options.create) {
199 assertEquals('test - Edited.jpg', name);
200 fileSystem.populate(['/test - Edited.jpg']);
201 var entry = fileSystem.entries['/test - Edited.jpg'];
202 entry.createWriter = function(callback) {
205 Promise.resolve().then(function() {
209 truncate: function() {
215 MockDirectoryEntry.prototype.getFile.apply(this, arguments);
217 var entryChanged = false;
218 var metadataModel = getMockMetadataModel();
219 metadataModel.notifyEntriesChanged = function() {
223 var item = new Gallery.Item(
224 fileSystem.entries['/test.arw'],
228 /* original */ true);
229 assertEquals(100, item.getMetadataItem().size);
230 assertFalse(entryChanged);
232 new Promise(item.saveToFile.bind(
234 {getLocationInfo: function() { return {}; }},
236 /* fallbackDir */ null,
237 /* overwrite is true but ignored */ true,
238 document.createElement('canvas'))).then(function(success) {
240 assertEquals(200, item.getMetadataItem().size);
241 assertTrue(entryChanged);
242 assertFalse(item.isOriginal());