Add ability for NetLogLogger to gather data from more than just NetLog
[chromium-blink-merge.git] / ui / file_manager / gallery / js / gallery_item_unittest.js
bloba5f487cd112e29a39cea5f4d986846f0801722b6
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.
5 /**
6 * Mock of ImageUtil.
7 */
8 var ImageUtil = {
9 getMetricName: function() {},
10 metrics: {
11 recordEnum: function() {},
12 recordInterval: function() {},
13 startInterval: function() {}
17 /**
18 * Mock of ImageEncoder. Since some test changes the behavior of ImageEncoder,
19 * this is initialized in setUp().
21 var ImageEncoder;
23 /**
24 * Load time data.
26 loadTimeData.data = {
27 DRIVE_DIRECTORY_LABEL: '',
28 DOWNLOADS_DIRECTORY_LABEL: ''
31 function setUp() {
32 ImageEncoder = {
33 encodeMetadata: function() {},
34 getBlob: function() {}
38 /**
39 * Returns a mock of metadata model.
40 * @private
41 * @return {!MetadataModel}
43 function getMockMetadataModel() {
44 return {
45 get: function(entries, names) {
46 return Promise.resolve([
47 {size: 200}
48 ]);
50 notifyEntriesChanged: function() {
55 /**
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) {
63 callback({
64 write: function() {
65 Promise.resolve().then(function() {
66 this.onwriteend();
67 }.bind(this));
69 truncate: function() {
70 this.write();
72 });
74 var entryChanged = false;
75 var metadataModel = getMockMetadataModel();
76 metadataModel.notifyEntriesChanged = function() {
77 entryChanged = true;
80 var item = new Gallery.Item(
81 entry,
82 {isReadOnly: false},
83 {size: 100},
84 {},
85 /* original */ true);
86 assertEquals(100, item.getMetadataItem().size);
87 assertFalse(entryChanged);
88 reportPromise(
89 new Promise(item.saveToFile.bind(
90 item,
91 {getLocationInfo: function() { return {}; }},
92 metadataModel,
93 /* fallbackDir */ null,
94 /* overwrite */ true,
95 document.createElement('canvas'))).then(function() {
96 assertEquals(200, item.getMetadataItem().size);
97 assertTrue(entryChanged);
98 }), callback);
102 * Tests for GalleryItem#saveToFile. In this test case, fileWriter.write fails
103 * with an error.
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) {
111 callback({
112 write: function() {
113 Promise.resolve().then(function() {
114 this.onerror(new Error());
115 }.bind(this));
117 truncate: function() {
118 Promise.resolve().then(function() {
119 this.onwriteend();
120 }.bind(this));
125 var item = new Gallery.Item(
126 entry,
127 {isReadOnly: false},
128 {size: 100},
130 /* original */ true);
131 reportPromise(
132 new Promise(item.saveToFile.bind(
133 item,
134 {getLocationInfo: function() { return {}; }},
135 getMockMetadataModel(),
136 /* fallbackDir */ null,
137 /* overwrite */ true,
138 document.createElement('canvas'))).then(function(result) {
139 assertFalse(result);
140 }), callback);
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() {
150 throw new Error();
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) {
159 callback({
160 write: function() {
161 Promise.resolve().then(function() {
162 writeOperationRun = true;
163 this.onwriteend();
164 }.bind(this));
166 truncate: function() {
167 Promise.resolve().then(function() {
168 writeOperationRun = true;
169 this.onwriteend();
170 }.bind(this));
175 var item = new Gallery.Item(
176 entry,
177 {isReadOnly: false},
178 {size: 100},
180 /* original */ true);
181 reportPromise(
182 new Promise(item.saveToFile.bind(
183 item,
184 {getLocationInfo: function() { return {}; }},
185 getMockMetadataModel(),
186 /* fallbackDir */ null,
187 /* overwrite */ true,
188 document.createElement('canvas'))).then(function(result) {
189 assertFalse(result);
190 assertFalse(writeOperationRun);
191 }), callback);