Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(805)

Unified Diff: ui/file_manager/gallery/js/gallery_item_unittest.js

Issue 988273002: Gallery.app : Truncates the file after a blob of new image is created. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add test cases. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/file_manager/gallery/js/gallery_item.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/file_manager/gallery/js/gallery_item_unittest.js
diff --git a/ui/file_manager/gallery/js/gallery_item_unittest.js b/ui/file_manager/gallery/js/gallery_item_unittest.js
index 613bbb2be38808dd401ec1f57610026cd0145495..a5f487cd112e29a39cea5f4d986846f0801722b6 100644
--- a/ui/file_manager/gallery/js/gallery_item_unittest.js
+++ b/ui/file_manager/gallery/js/gallery_item_unittest.js
@@ -15,12 +15,10 @@ var ImageUtil = {
};
/**
- * Mock of ImageEncoder
+ * Mock of ImageEncoder. Since some test changes the behavior of ImageEncoder,
+ * this is initialized in setUp().
*/
-var ImageEncoder = {
- encodeMetadata: function() {},
- getBlob: function() {}
-};
+var ImageEncoder;
/**
* Load time data.
@@ -30,6 +28,30 @@ loadTimeData.data = {
DOWNLOADS_DIRECTORY_LABEL: ''
};
+function setUp() {
+ ImageEncoder = {
+ encodeMetadata: function() {},
+ getBlob: function() {}
+ };
+}
+
+/**
+ * Returns a mock of metadata model.
+ * @private
+ * @return {!MetadataModel}
+ */
+function getMockMetadataModel() {
+ return {
+ get: function(entries, names) {
+ return Promise.resolve([
+ {size: 200}
+ ]);
+ },
+ notifyEntriesChanged: function() {
+ }
+ };
+}
+
/**
* Tests for GalleryItem#saveToFile.
*/
@@ -50,16 +72,11 @@ function testSaveToFile(callback) {
});
};
var entryChanged = false;
- var metadataModel = {
- get: function(entries, names) {
- return Promise.resolve([
- {size: 200}
- ]);
- },
- notifyEntriesChanged: function() {
- entryChanged = true;
- }
+ var metadataModel = getMockMetadataModel();
+ metadataModel.notifyEntriesChanged = function() {
+ entryChanged = true;
};
+
var item = new Gallery.Item(
entry,
{isReadOnly: false},
@@ -80,3 +97,96 @@ function testSaveToFile(callback) {
assertTrue(entryChanged);
}), callback);
}
+
+/**
+ * Tests for GalleryItem#saveToFile. In this test case, fileWriter.write fails
+ * with an error.
+ */
+function testSaveToFileWriteFailCase(callback) {
+ var fileSystem = new MockFileSystem('volumeId');
+ fileSystem.populate(['/test.jpg']);
+ var entry = fileSystem.entries['/test.jpg'];
+
+ entry.createWriter = function(callback) {
+ callback({
+ write: function() {
+ Promise.resolve().then(function() {
+ this.onerror(new Error());
+ }.bind(this));
+ },
+ truncate: function() {
+ Promise.resolve().then(function() {
+ this.onwriteend();
+ }.bind(this));
+ }
+ });
+ };
+
+ var item = new Gallery.Item(
+ entry,
+ {isReadOnly: false},
+ {size: 100},
+ {},
+ /* original */ true);
+ reportPromise(
+ new Promise(item.saveToFile.bind(
+ item,
+ {getLocationInfo: function() { return {}; }},
+ getMockMetadataModel(),
+ /* fallbackDir */ null,
+ /* overwrite */ true,
+ document.createElement('canvas'))).then(function(result) {
+ assertFalse(result);
+ }), callback);
+}
+
+/**
+ * Tests for GalleryItem#saveToFile. In this test case, ImageEncoder.getBlob
+ * fails with an error. This test case confirms that no write operation runs
+ * when it fails to get a blob of new image.
+ */
+function testSaveToFileGetBlobFailCase(callback) {
+ ImageEncoder.getBlob = function() {
+ throw new Error();
+ };
+
+ var fileSystem = new MockFileSystem('volumeId');
+ fileSystem.populate(['/test.jpg']);
+ var entry = fileSystem.entries['/test.jpg'];
+
+ var writeOperationRun = false;
+ entry.createWriter = function(callback) {
+ callback({
+ write: function() {
+ Promise.resolve().then(function() {
+ writeOperationRun = true;
+ this.onwriteend();
+ }.bind(this));
+ },
+ truncate: function() {
+ Promise.resolve().then(function() {
+ writeOperationRun = true;
+ this.onwriteend();
+ }.bind(this));
+ }
+ });
+ };
+
+ var item = new Gallery.Item(
+ entry,
+ {isReadOnly: false},
+ {size: 100},
+ {},
+ /* original */ true);
+ reportPromise(
+ new Promise(item.saveToFile.bind(
+ item,
+ {getLocationInfo: function() { return {}; }},
+ getMockMetadataModel(),
+ /* fallbackDir */ null,
+ /* overwrite */ true,
+ document.createElement('canvas'))).then(function(result) {
+ assertFalse(result);
+ assertFalse(writeOperationRun);
+ }), callback);
+}
« no previous file with comments | « ui/file_manager/gallery/js/gallery_item.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698