Commit a4eaff2a authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

Gallery: Closure compile gallery_item_unittest.js

This is the last Gallery unittest not closure compiled.

Bug: 867700
Change-Id: I6e4c2b2cf23b80967e060d838dd3872b55306daf
Reviewed-on: https://chromium-review.googlesource.com/1184644Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585677}
parent 8ccbe3e4
...@@ -38,7 +38,7 @@ IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryUtilTest) { ...@@ -38,7 +38,7 @@ IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryUtilTest) {
} }
IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryItemTest) { IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryItemTest) {
RunTest(base::FilePath(FILE_PATH_LITERAL("gallery_item_unittest.html"))); RunGeneratedTest("/gallery_item_unittest.html");
} }
IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryDataModelTest) { IN_PROC_BROWSER_TEST_F(GalleryJsTest, GalleryDataModelTest) {
......
...@@ -126,6 +126,15 @@ js_library("gallery_item") { ...@@ -126,6 +126,15 @@ js_library("gallery_item") {
externs_list = [ "../../externs/entry_location.js" ] externs_list = [ "../../externs/entry_location.js" ]
} }
js_library("gallery_item_unittest") {
deps = [
":gallery_item",
":mock_gallery_item",
"../../file_manager/common/js:unittest_util",
"//ui/webui/resources/js:webui_resource_test",
]
}
js_library("gallery_util") { js_library("gallery_util") {
deps = [ deps = [
"../../file_manager/common/js:file_type", "../../file_manager/common/js:file_type",
...@@ -221,6 +230,7 @@ js_unit_tests("unit_tests") { ...@@ -221,6 +230,7 @@ js_unit_tests("unit_tests") {
":dimmable_ui_controller_unittest", ":dimmable_ui_controller_unittest",
":entry_list_watcher_unittest", ":entry_list_watcher_unittest",
":gallery_data_model_unittest", ":gallery_data_model_unittest",
":gallery_item_unittest",
":gallery_util_unittest", ":gallery_util_unittest",
":ribbon_unittest", ":ribbon_unittest",
":slide_mode_unittest", ":slide_mode_unittest",
......
<!DOCTYPE html>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->
<script>
// Define mock Gallery class to define GalleryItem class.
var Gallery = function() {};
</script>
<!-- Should be loaded before volume_manager.js -->
<script src="../../file_manager/common/js/volume_manager_common.js"></script>
<!-- Others -->
<script src="../../../webui/resources/js/assert.js"></script>
<script src="../../../webui/resources/js/cr.js"></script>
<script src="../../../webui/resources/js/cr/event_target.js"></script>
<script src="../../../webui/resources/js/cr/ui/array_data_model.js"></script>
<script src="../../../webui/resources/js/load_time_data.js"></script>
<script src="../../file_manager/common/js/file_type.js"></script>
<script src="../../file_manager/common/js/mock_entry.js"></script>
<script src="../../file_manager/common/js/unittest_util.js"></script>
<script src="../../file_manager/common/js/util.js"></script>
<script src="../../file_manager/foreground/js/metadata/thumbnail_model.js"></script>
<script src="../../file_manager/foreground/js/metadata/mock_metadata.js"></script>
<script src="gallery_item.js"></script>
<script src="gallery_util.js"></script>
<script src="gallery_item_unittest.js"></script>
...@@ -14,12 +14,6 @@ var metrics = { ...@@ -14,12 +14,6 @@ var metrics = {
startInterval: function() {} startInterval: function() {}
}; };
/**
* Mock of ImageEncoder. Since some test changes the behavior of ImageEncoder,
* this is initialized in setUp().
*/
var ImageEncoder;
/** /**
* Load time data. * Load time data.
*/ */
...@@ -29,34 +23,59 @@ loadTimeData.data = { ...@@ -29,34 +23,59 @@ loadTimeData.data = {
}; };
function setUp() { function setUp() {
ImageEncoder = { // Replace the real ImageEncoder with a mock.
encodeMetadata: function() {}, ImageEncoder = /** @lends{ImageEncoder} */ (
getBlob: function() {} {encodeMetadata: function() {}, getBlob: function() {}});
};
} }
function getMockMetadataModel() { function getMockMetadataModel() {
return new MockMetadataModel({size: 200}); return new MockMetadataModel({size: 200});
} }
/**
* Creates a mock result for GalleryItem#saveToFile.
*
* @param{!GalleryItem} item
* @param{!MetadataModel} metadataModel
* @param{boolean} overwrite
* @return {Promise}
*/
function makeMockSavePromise(item, metadataModel, overwrite) {
let canvas =
assertInstanceof(document.createElement('canvas'), HTMLCanvasElement);
let mockVolumeManager = /**@type{!VolumeManagerWrapper} */ ({
getLocationInfo: function() {
return {};
},
getVolumeInfo: function() {
return {};
}
});
return new Promise(item.saveToFile.bind(
item, mockVolumeManager, metadataModel,
/** @type{!DirectoryEntry} */ ({}), // fallbackDir.
canvas, overwrite /* overwrite */));
}
/** /**
* Tests for GalleryItem#saveToFile. * Tests for GalleryItem#saveToFile.
*/ */
function testSaveToFile(callback) { function testSaveToFile(callback) {
var fileSystem = new MockFileSystem('volumeId'); var fileSystem = new MockFileSystem('volumeId');
fileSystem.populate(['/test.jpg']); fileSystem.populate(['/test.jpg']);
var entry = fileSystem.entries['/test.jpg']; var entry = assertInstanceof(fileSystem.entries['/test.jpg'], MockFileEntry);
entry.createWriter = function(callback) { entry.createWriter = function(callback) {
callback({ let mockWriter = /**@lends {FileWriter} */ ({
write: function() { write: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
this.onwriteend(); mockWriter.onwriteend();
}.bind(this)); });
}, },
truncate: function() { truncate: function() {
this.write(); mockWriter.write();
} }
}); });
callback(mockWriter);
}; };
var entryChanged = false; var entryChanged = false;
var metadataModel = getMockMetadataModel(); var metadataModel = getMockMetadataModel();
...@@ -64,28 +83,16 @@ function testSaveToFile(callback) { ...@@ -64,28 +83,16 @@ function testSaveToFile(callback) {
entryChanged = true; entryChanged = true;
}; };
var item = new GalleryItem( var item =
entry, new MockGalleryItem(entry, null, {size: 100}, null, true /*original */);
{isReadOnly: false},
{size: 100},
{},
/* original */ true);
assertEquals(100, item.getMetadataItem().size); assertEquals(100, item.getMetadataItem().size);
assertFalse(entryChanged); assertFalse(entryChanged);
reportPromise( reportPromise(
new Promise(item.saveToFile.bind( makeMockSavePromise(item, metadataModel, true).then(function() {
item,
{
getLocationInfo: function() { return {}; },
getVolumeInfo: function() { return {}; }
},
metadataModel,
/* fallbackDir */ null,
document.createElement('canvas'),
true /* overwrite */)).then(function() {
assertEquals(200, item.getMetadataItem().size); assertEquals(200, item.getMetadataItem().size);
assertTrue(entryChanged); assertTrue(entryChanged);
}), callback); }),
callback);
} }
/** /**
...@@ -95,42 +102,32 @@ function testSaveToFile(callback) { ...@@ -95,42 +102,32 @@ function testSaveToFile(callback) {
function testSaveToFileWriteFailCase(callback) { function testSaveToFileWriteFailCase(callback) {
var fileSystem = new MockFileSystem('volumeId'); var fileSystem = new MockFileSystem('volumeId');
fileSystem.populate(['/test.jpg']); fileSystem.populate(['/test.jpg']);
var entry = fileSystem.entries['/test.jpg']; var entry = assertInstanceof(fileSystem.entries['/test.jpg'], MockFileEntry);
entry.createWriter = function(callback) { entry.createWriter = function(callback) {
callback({ let mockWriter = /**@lends {FileWriter} */ ({
write: function() { write: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
this.onerror(new Error()); mockWriter.onerror(new Error());
}.bind(this)); });
}, },
truncate: function() { truncate: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
this.onwriteend(); mockWriter.onwriteend();
}.bind(this)); });
} }
}); });
callback(mockWriter);
}; };
var item = new GalleryItem( var item =
entry, new MockGalleryItem(entry, null, {size: 100}, null, true /*original */);
{isReadOnly: false},
{size: 100},
{},
/* original */ true);
reportPromise( reportPromise(
new Promise(item.saveToFile.bind( makeMockSavePromise(item, getMockMetadataModel(), true)
item, .then(function(result) {
{
getLocationInfo: function() { return {}; },
getVolumeInfo: function() { return {}; }
},
getMockMetadataModel(),
/* fallbackDir */ null,
document.createElement('canvas'),
true /* overwrite */)).then(function(result) {
assertFalse(result); assertFalse(result);
}), callback); }),
callback);
} }
/** /**
...@@ -145,46 +142,36 @@ function testSaveToFileGetBlobFailCase(callback) { ...@@ -145,46 +142,36 @@ function testSaveToFileGetBlobFailCase(callback) {
var fileSystem = new MockFileSystem('volumeId'); var fileSystem = new MockFileSystem('volumeId');
fileSystem.populate(['/test.jpg']); fileSystem.populate(['/test.jpg']);
var entry = fileSystem.entries['/test.jpg']; var entry = assertInstanceof(fileSystem.entries['/test.jpg'], MockFileEntry);
var writeOperationRun = false; var writeOperationRun = false;
entry.createWriter = function(callback) { entry.createWriter = function(callback) {
callback({ let mockWriter = /**@lends {FileWriter} */ ({
write: function() { write: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
writeOperationRun = true; writeOperationRun = true;
this.onwriteend(); mockWriter.onwriteend();
}.bind(this)); });
}, },
truncate: function() { truncate: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
writeOperationRun = true; writeOperationRun = true;
this.onwriteend(); mockWriter.onwriteend();
}.bind(this)); });
} }
}); });
callback(mockWriter);
}; };
var item = new GalleryItem( var item =
entry, new MockGalleryItem(entry, null, {size: 100}, null, true /*original */);
{isReadOnly: false},
{size: 100},
{},
/* original */ true);
reportPromise( reportPromise(
new Promise(item.saveToFile.bind( makeMockSavePromise(item, getMockMetadataModel(), true)
item, .then(function(result) {
{
getLocationInfo: function() { return {}; },
getVolumeInfo: function() { return {}; }
},
getMockMetadataModel(),
/* fallbackDir */ null,
document.createElement('canvas'),
true /* overwrite*/)).then(function(result) {
assertFalse(result); assertFalse(result);
assertFalse(writeOperationRun); assertFalse(writeOperationRun);
}), callback); }),
callback);
} }
function testSaveToFileRaw(callback) { function testSaveToFileRaw(callback) {
...@@ -196,16 +183,17 @@ function testSaveToFileRaw(callback) { ...@@ -196,16 +183,17 @@ function testSaveToFileRaw(callback) {
fileSystem.populate(['/test - Edited.jpg']); fileSystem.populate(['/test - Edited.jpg']);
var entry = fileSystem.entries['/test - Edited.jpg']; var entry = fileSystem.entries['/test - Edited.jpg'];
entry.createWriter = function(callback) { entry.createWriter = function(callback) {
callback({ let mockWriter = /**@lends {FileWriter} */ ({
write: function() { write: function() {
Promise.resolve().then(function() { Promise.resolve().then(function() {
this.onwriteend(); mockWriter.onwriteend();
}.bind(this)); });
}, },
truncate: function() { truncate: function() {
this.write(); mockWriter.write();
} }
}); });
callback(mockWriter);
}; };
} }
MockDirectoryEntry.prototype.getFile.apply(this, arguments); MockDirectoryEntry.prototype.getFile.apply(this, arguments);
...@@ -216,30 +204,19 @@ function testSaveToFileRaw(callback) { ...@@ -216,30 +204,19 @@ function testSaveToFileRaw(callback) {
entryChanged = true; entryChanged = true;
}; };
var item = new GalleryItem( var item = new MockGalleryItem(
fileSystem.entries['/test.arw'], assertInstanceof(fileSystem.entries['/test.arw'], MockFileEntry), null,
{isReadOnly: false}, {size: 100}, null, true /*original */);
{size: 100},
{},
/* original */ true);
assertEquals(100, item.getMetadataItem().size); assertEquals(100, item.getMetadataItem().size);
assertFalse(entryChanged); assertFalse(entryChanged);
reportPromise( reportPromise(
new Promise(item.saveToFile.bind( makeMockSavePromise(item, metadataModel, false).then(function(success) {
item,
{
getLocationInfo: function() { return {}; },
getVolumeInfo: function() { return {}; }
},
metadataModel,
/* fallbackDir */ null,
document.createElement('canvas'),
false /* not overwrite */)).then(function(success) {
assertTrue(success); assertTrue(success);
assertEquals(200, item.getMetadataItem().size); assertEquals(200, item.getMetadataItem().size);
assertTrue(entryChanged); assertTrue(entryChanged);
assertFalse(item.isOriginal()); assertFalse(item.isOriginal());
}), callback); }),
callback);
} }
function testIsWritableFile() { function testIsWritableFile() {
...@@ -262,59 +239,52 @@ function testIsWritableFile() { ...@@ -262,59 +239,52 @@ function testIsWritableFile() {
} }
}; };
var getGalleryItem = function(path, fileSystem, isReadOnly) {
return new GalleryItem(new MockEntry(fileSystem, path),
{isReadOnly: isReadOnly},
{size: 100},
{},
true /* original */);
};
// Jpeg file on downloads. // Jpeg file on downloads.
assertTrue(getGalleryItem( assertTrue(
'/test.jpg', downloads, false /* not read only */). MockGalleryItem
isWritableFile(volumeManager)); .makeWithPath('/test.jpg', downloads, false /* not read only */)
.isWritableFile(volumeManager));
// Png file on downloads. // Png file on downloads.
assertTrue(getGalleryItem( assertTrue(
'/test.png', downloads, false /* not read only */). MockGalleryItem
isWritableFile(volumeManager)); .makeWithPath('/test.png', downloads, false /* not read only */)
.isWritableFile(volumeManager));
// Webp file on downloads. // Webp file on downloads.
assertFalse(getGalleryItem( assertFalse(
'/test.webp', downloads, false /* not read only */). MockGalleryItem
isWritableFile(volumeManager)); .makeWithPath('/test.webp', downloads, false /* not read only */)
.isWritableFile(volumeManager));
// Jpeg file on non-writable volume. // Jpeg file on non-writable volume.
assertFalse(getGalleryItem( assertFalse(
'/test.jpg', removable, true /* read only */). MockGalleryItem.makeWithPath('/test.jpg', removable, true /* read only */)
isWritableFile(volumeManager)); .isWritableFile(volumeManager));
// Jpeg file on mtp volume. // Jpeg file on mtp volume.
assertFalse(getGalleryItem( assertFalse(
'/test.jpg', mtp, false /* not read only */). MockGalleryItem.makeWithPath('/test.jpg', mtp, false /* not read only */)
isWritableFile(volumeManager)); .isWritableFile(volumeManager));
} }
function testIsEditableFile() { function testIsEditableFile() {
var downloads = new MockFileSystem('downloads'); var downloads = new MockFileSystem('downloads');
var getGalleryItem = function(path, fileSystem, isReadOnly) { var getGalleryItem = function(path, isReadOnly) {
return new GalleryItem( return MockGalleryItem.makeWithPath(path, downloads, isReadOnly);
new MockEntry(fileSystem, path), {isReadOnly: isReadOnly}, {size: 100},
{}, true /* original */);
}; };
// Images and raw files are editable, even if read-only (a copy is made). // Images and raw files are editable, even if read-only (a copy is made).
assertTrue(getGalleryItem('/test.jpg', downloads, false).isEditable()); assertTrue(getGalleryItem('/test.jpg', false).isEditable());
assertTrue(getGalleryItem('/test.png', downloads, false).isEditable()); assertTrue(getGalleryItem('/test.png', false).isEditable());
assertTrue(getGalleryItem('/test.webp', downloads, false).isEditable()); assertTrue(getGalleryItem('/test.webp', false).isEditable());
assertTrue(getGalleryItem('/test.arw', downloads, false).isEditable()); assertTrue(getGalleryItem('/test.arw', false).isEditable());
assertTrue(getGalleryItem('/test.jpg', downloads, true).isEditable()); assertTrue(getGalleryItem('/test.jpg', true).isEditable());
// Video files are not editable. // Video files are not editable.
assertFalse(getGalleryItem('/test.avi', downloads, false).isEditable()); assertFalse(getGalleryItem('/test.avi', false).isEditable());
assertFalse(getGalleryItem('/test.mkv', downloads, false).isEditable()); assertFalse(getGalleryItem('/test.mkv', false).isEditable());
assertFalse(getGalleryItem('/test.mp4', downloads, false).isEditable()); assertFalse(getGalleryItem('/test.mp4', false).isEditable());
assertFalse(getGalleryItem('/test.mov', downloads, false).isEditable()); assertFalse(getGalleryItem('/test.mov', false).isEditable());
assertFalse(getGalleryItem('/test.webm', downloads, false).isEditable()); assertFalse(getGalleryItem('/test.webm', false).isEditable());
} }
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
/** /**
* A namespace class for image encoding functions. All methods are static. * A namespace class for image encoding functions. All methods are static.
* @constructor
*/ */
function ImageEncoder() {} function ImageEncoder() {}
......
...@@ -22,6 +22,21 @@ function MockGalleryItem( ...@@ -22,6 +22,21 @@ function MockGalleryItem(
opt_thumbnailMetadataItem || null, opt_original || false); opt_thumbnailMetadataItem || null, opt_original || false);
} }
/**
* Helper to construct a MockGalleryItem with a given |path| and dummy metadata.
*
* @param {!string} path
* @param {!FileSystem} fileSystem
* @param {boolean} isReadOnly
* @return MockGalleryItem
*/
MockGalleryItem.makeWithPath = function(path, fileSystem, isReadOnly) {
return new MockGalleryItem(
new MockFileEntry(fileSystem, path),
/** @type {EntryLocation} */ ({isReadOnly: isReadOnly}), {size: 100},
null, true /* original */);
};
MockGalleryItem.prototype = { MockGalleryItem.prototype = {
__proto__: GalleryItem.prototype __proto__: GalleryItem.prototype
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment