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(
92 getLocationInfo: function() { return {}; },
93 getVolumeInfo: function() { return {}; }
96 /* fallbackDir */ null,
97 document.createElement('canvas'),
98 true /* overwrite */)).then(function() {
99 assertEquals(200, item.getMetadataItem().size);
100 assertTrue(entryChanged);
105 * Tests for GalleryItem#saveToFile. In this test case, fileWriter.write fails
108 function testSaveToFileWriteFailCase(callback) {
109 var fileSystem = new MockFileSystem('volumeId');
110 fileSystem.populate(['/test.jpg']);
111 var entry = fileSystem.entries['/test.jpg'];
113 entry.createWriter = function(callback) {
116 Promise.resolve().then(function() {
117 this.onerror(new Error());
120 truncate: function() {
121 Promise.resolve().then(function() {
128 var item = new Gallery.Item(
133 /* original */ true);
135 new Promise(item.saveToFile.bind(
138 getLocationInfo: function() { return {}; },
139 getVolumeInfo: function() { return {}; }
141 getMockMetadataModel(),
142 /* fallbackDir */ null,
143 document.createElement('canvas'),
144 true /* overwrite */)).then(function(result) {
150 * Tests for GalleryItem#saveToFile. In this test case, ImageEncoder.getBlob
151 * fails with an error. This test case confirms that no write operation runs
152 * when it fails to get a blob of new image.
154 function testSaveToFileGetBlobFailCase(callback) {
155 ImageEncoder.getBlob = function() {
159 var fileSystem = new MockFileSystem('volumeId');
160 fileSystem.populate(['/test.jpg']);
161 var entry = fileSystem.entries['/test.jpg'];
163 var writeOperationRun = false;
164 entry.createWriter = function(callback) {
167 Promise.resolve().then(function() {
168 writeOperationRun = true;
172 truncate: function() {
173 Promise.resolve().then(function() {
174 writeOperationRun = true;
181 var item = new Gallery.Item(
186 /* original */ true);
188 new Promise(item.saveToFile.bind(
191 getLocationInfo: function() { return {}; },
192 getVolumeInfo: function() { return {}; }
194 getMockMetadataModel(),
195 /* fallbackDir */ null,
196 document.createElement('canvas'),
197 true /* overwrite*/)).then(function(result) {
199 assertFalse(writeOperationRun);
203 function testSaveToFileRaw(callback) {
204 var fileSystem = new MockFileSystem('volumeId');
205 fileSystem.populate(['/test.arw']);
206 fileSystem.entries['/'].getFile = function(name, options, success, error) {
207 if (options.create) {
208 assertEquals('test - Edited.jpg', name);
209 fileSystem.populate(['/test - Edited.jpg']);
210 var entry = fileSystem.entries['/test - Edited.jpg'];
211 entry.createWriter = function(callback) {
214 Promise.resolve().then(function() {
218 truncate: function() {
224 MockDirectoryEntry.prototype.getFile.apply(this, arguments);
226 var entryChanged = false;
227 var metadataModel = getMockMetadataModel();
228 metadataModel.notifyEntriesChanged = function() {
232 var item = new Gallery.Item(
233 fileSystem.entries['/test.arw'],
237 /* original */ true);
238 assertEquals(100, item.getMetadataItem().size);
239 assertFalse(entryChanged);
241 new Promise(item.saveToFile.bind(
244 getLocationInfo: function() { return {}; },
245 getVolumeInfo: function() { return {}; }
248 /* fallbackDir */ null,
249 document.createElement('canvas'),
250 false /* not overwrite */)).then(function(success) {
252 assertEquals(200, item.getMetadataItem().size);
253 assertTrue(entryChanged);
254 assertFalse(item.isOriginal());
258 function testIsWritableFile() {
259 var downloads = new MockFileSystem('downloads');
260 var removable = new MockFileSystem('removable');
261 var mtp = new MockFileSystem('mtp');
264 downloads: VolumeManagerCommon.VolumeType.DOWNLOADS,
265 removable: VolumeManagerCommon.VolumeType.REMOVABLE,
266 mtp: VolumeManagerCommon.VolumeType.MTP
269 // Mock volume manager.
270 var volumeManager = {
271 getVolumeInfo: function(entry) {
273 volumeType: volumeTypes[entry.filesystem.name]
278 var getGalleryItem = function(path, fileSystem, isReadOnly) {
279 return new Gallery.Item(new MockEntry(fileSystem, path),
280 {isReadOnly: isReadOnly},
283 true /* original */);
286 // Jpeg file on downloads.
287 assertTrue(getGalleryItem(
288 '/test.jpg', downloads, false /* not read only */).
289 isWritableFile(volumeManager));
291 // Png file on downloads.
292 assertTrue(getGalleryItem(
293 '/test.png', downloads, false /* not read only */).
294 isWritableFile(volumeManager));
296 // Webp file on downloads.
297 assertFalse(getGalleryItem(
298 '/test.webp', downloads, false /* not read only */).
299 isWritableFile(volumeManager));
301 // Jpeg file on non-writable volume.
302 assertFalse(getGalleryItem(
303 '/test.jpg', removable, true /* read only */).
304 isWritableFile(volumeManager));
306 // Jpeg file on mtp volume.
307 assertFalse(getGalleryItem(
308 '/test.jpg', mtp, false /* not read only */).
309 isWritableFile(volumeManager));