mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Refactored the Image module. Added image.fromUrl method. Added image-tests.
This commit is contained in:
@@ -70,7 +70,7 @@ export var runTestModule = function (module, moduleName) {
|
||||
console.info("--- " + moduleName + " TESTS COMPLETE --- (" + totalSuccess + " of " + totalTests + ") OK, " + (totalTests - totalSuccess) + " failed");
|
||||
};
|
||||
|
||||
export var assert = function (test: boolean, message?: string) {
|
||||
export var assert = function (test: any, message?: string) {
|
||||
if (!test) {
|
||||
throw new Error(message);
|
||||
}
|
||||
@@ -85,11 +85,16 @@ export var wait = function (ms) {
|
||||
}
|
||||
};
|
||||
|
||||
export var waitUntilReady = function (isReady, timeoutSec) {
|
||||
export var waitUntilReady = function (isReady: () => boolean, timeoutSec?: number) {
|
||||
if (!isReady) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!timeoutSec) {
|
||||
// TODO: How much should be the default timeout in seconds?
|
||||
timeoutSec = 20;
|
||||
}
|
||||
|
||||
if (Application.ios) {
|
||||
var waitTime = 20 / 1000;
|
||||
var totalWaitTime = 0;
|
||||
@@ -109,7 +114,7 @@ export var waitUntilReady = function (isReady, timeoutSec) {
|
||||
}
|
||||
};
|
||||
|
||||
var doModalAndroid = function (quitLoop, timeoutSec) {
|
||||
var doModalAndroid = function (quitLoop: () => boolean, timeoutSec: number) {
|
||||
if (!quitLoop) {
|
||||
return;
|
||||
}
|
||||
@@ -123,31 +128,27 @@ var doModalAndroid = function (quitLoop, timeoutSec) {
|
||||
for (i = 0; i < methods.length; i++) {
|
||||
if (methods[i].getName() === "next") {
|
||||
nextMethod = methods[i];
|
||||
nextMethod.setAccessible(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
nextMethod.setAccessible(true);
|
||||
|
||||
var targetField;
|
||||
var fields = clsMsg.getDeclaredFields();
|
||||
for (i = 0; i < fields.length; i++) {
|
||||
if (fields[i].getName() === "target") {
|
||||
targetField = fields[i];
|
||||
targetField.setAccessible(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
targetField.setAccessible(true);
|
||||
|
||||
var queue = android.os.Looper.myQueue();
|
||||
|
||||
var quit = false;
|
||||
if (timeoutSec) {
|
||||
timer.setTimeout(function () {
|
||||
quit = true;
|
||||
}, timeoutSec * 1000);
|
||||
}
|
||||
timer.setTimeout(function () {
|
||||
quit = true;
|
||||
}, timeoutSec * 1000);
|
||||
|
||||
var msg;
|
||||
|
||||
@@ -163,7 +164,7 @@ var doModalAndroid = function (quitLoop, timeoutSec) {
|
||||
msg.recycle();
|
||||
}
|
||||
|
||||
if (quitLoop()) {
|
||||
if (!quit && quitLoop()) {
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// <snippet name="file-system">
|
||||
// # File System
|
||||
// Using the file system requires the FileSystem module.
|
||||
// TODO: var fs = require("filesystem"); => this will break the intellisense of the tests
|
||||
// ``` JavaScript
|
||||
import fs = require("filesystem/file_system");
|
||||
// ```
|
||||
@@ -347,7 +348,7 @@ export var testGetParent = function () {
|
||||
TKUnit.assert(<any>file, "Failed to create file in the Documents folder.");
|
||||
// </hide>
|
||||
//// The parent folder of the file would be the documents folder.
|
||||
var parent = file.getParent();
|
||||
var parent = file.parent;
|
||||
// <hide>
|
||||
TKUnit.assert(documents == parent, "The parent folder should be the Documents folder.");
|
||||
file.remove();
|
||||
|
||||
75
Tests/image-tests.ts
Normal file
75
Tests/image-tests.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import image = require("image/image");
|
||||
import app = require("application/application");
|
||||
import fs = require("filesystem/file_system");
|
||||
import TKUnit = require("Tests/TKUnit");
|
||||
|
||||
export var testFromResource = function () {
|
||||
var img = image.fromResource(getTestImageName());
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
}
|
||||
|
||||
export var testFromUrl = function () {
|
||||
var completed;
|
||||
var result: image.Image;
|
||||
|
||||
image.fromUrl("http://www.google.com/images/errors/logo_sm_2.png")
|
||||
.then(function (res: image.Image) {
|
||||
completed = true;
|
||||
result = res;
|
||||
})
|
||||
.fail(function (error) {
|
||||
completed = true;
|
||||
});
|
||||
|
||||
var isReady = function () {
|
||||
return completed;
|
||||
}
|
||||
|
||||
TKUnit.waitUntilReady(isReady, 3);
|
||||
TKUnit.assert(typeof result !== "undefined", "Image not downloaded");
|
||||
TKUnit.assert(result.height > 0, "Image not downloaded");
|
||||
}
|
||||
|
||||
export var testSaveToFile = function () {
|
||||
var img = image.fromResource(getTestImageName());
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
|
||||
var saved = img.saveToFile(path, image.ImageFormat.PNG);
|
||||
TKUnit.assert(saved, "Image not saved to file");
|
||||
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
|
||||
}
|
||||
|
||||
export var testFromFile = function () {
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
|
||||
var img = image.fromFile(path);
|
||||
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
|
||||
// remove the image from the file system
|
||||
var file = folder.getFile("Test.png");
|
||||
file.remove();
|
||||
TKUnit.assert(!fs.File.exists(path), "Test.png not removed");
|
||||
}
|
||||
|
||||
export var testNativeFields = function () {
|
||||
var img = image.fromResource(getTestImageName());
|
||||
if (app.android) {
|
||||
TKUnit.assert(img.android != null, "Image.android not updated.");
|
||||
} else if (app.ios) {
|
||||
TKUnit.assert(img.ios != null, "Image.ios not updated.");
|
||||
}
|
||||
}
|
||||
|
||||
var getTestImageName = function (): string {
|
||||
if (app.ios) {
|
||||
return "AppIcon";
|
||||
}
|
||||
if (app.android) {
|
||||
return "ic_launcher";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
@@ -3,8 +3,10 @@ var fsTests = require("Tests/file_system_tests");
|
||||
var httpTests = require("Tests/http_tests");
|
||||
var locationTests = require("Tests/location_tests");
|
||||
var localSettingsTests = require("Tests/local_settings_tests");
|
||||
var imageTests = require("Tests/image-tests");
|
||||
|
||||
export var runAll = function () {
|
||||
TKUnit.runTestModule(imageTests, "IMAGE");
|
||||
TKUnit.runTestModule(fsTests, "FILE SYSTEM");
|
||||
TKUnit.runTestModule(httpTests, "HTTP");
|
||||
TKUnit.runTestModule(locationTests, "LOCATION");
|
||||
|
||||
Reference in New Issue
Block a user