mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
updated image source tests, unit test projects and snippets
This commit is contained in:
@@ -138,7 +138,7 @@
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="Tests\file-system-tests.ts" />
|
||||
<TypeScriptCompile Include="Tests\http-tests.ts" />
|
||||
<TypeScriptCompile Include="Tests\image-tests.ts" />
|
||||
<TypeScriptCompile Include="Tests\image-source-tests.ts" />
|
||||
<TypeScriptCompile Include="Tests\index.ts" />
|
||||
<TypeScriptCompile Include="Tests\location-tests.ts" />
|
||||
<TypeScriptCompile Include="Tests\testRunner.ts" />
|
||||
|
||||
@@ -1,10 +1,32 @@
|
||||
import imageSource = require("image-source/image-source");
|
||||
// <snippet name="image-source">
|
||||
// # Image source
|
||||
// Using the image source requires the image-source module.
|
||||
// ``` JavaScript
|
||||
//var imageSource = require("image-source");
|
||||
// ```
|
||||
// The pre-required `imageSource` module is used throughout the following code snippets.
|
||||
// we also use fs module defined as follows:
|
||||
// ``` JavaScript
|
||||
//var fs = require("file-system");
|
||||
// ```
|
||||
// ## Loading and saving images
|
||||
// </snippet>
|
||||
|
||||
import imageSource = require("image-source/image-source");
|
||||
import app = require("application/application");
|
||||
import fs = require("file-system/file-system");
|
||||
import TKUnit = require("Tests/TKUnit");
|
||||
|
||||
|
||||
export var testFromResource = function () {
|
||||
var img = imageSource.fromResource(getTestImageName());
|
||||
// <snippet name="image-source">
|
||||
// ### Load image using resource name
|
||||
// this similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS
|
||||
// TODO: replace getTestImageName() with "logo"
|
||||
// ``` JavaScript
|
||||
var img = imageSource.fromResource("logo");
|
||||
// ```
|
||||
// </snippet>
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
}
|
||||
|
||||
@@ -12,14 +34,25 @@ export var testFromUrl = function () {
|
||||
var completed;
|
||||
var result: imageSource.ImageSource;
|
||||
|
||||
// <snippet name="image-source">
|
||||
// ### Load image from URL
|
||||
// ``` JavaScript
|
||||
imageSource.fromUrl("http://www.google.com/images/errors/logo_sm_2.png")
|
||||
.then(function (res: imageSource.ImageSource) {
|
||||
//console.log("Image successfully loaded");
|
||||
// <hide>
|
||||
completed = true;
|
||||
result = res;
|
||||
// </hide>
|
||||
})
|
||||
.fail(function (error) {
|
||||
//console.log("Error loading image: " + error);
|
||||
// <hide>
|
||||
completed = true;
|
||||
});
|
||||
// </hide>
|
||||
});
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
var isReady = function () {
|
||||
return completed;
|
||||
@@ -31,21 +64,29 @@ export var testFromUrl = function () {
|
||||
}
|
||||
|
||||
export var testSaveToFile = function () {
|
||||
var img = imageSource.fromResource(getTestImageName());
|
||||
// <snippet name="image-source">
|
||||
// ### Save image source to PNG or JPG file
|
||||
// ``` JavaScript
|
||||
var img = imageSource.fromResource("logo");
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
|
||||
var saved = img.saveToFile(path, imageSource.ImageFormat.PNG);
|
||||
// ```
|
||||
// </snippet>
|
||||
TKUnit.assert(saved, "Image not saved to file");
|
||||
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
|
||||
}
|
||||
|
||||
export var testFromFile = function () {
|
||||
// <snippet name="image-source">
|
||||
// ### Load image from a local file
|
||||
// ``` JavaScript
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
|
||||
var img = imageSource.fromFile(path);
|
||||
|
||||
// ```
|
||||
// </snippet>
|
||||
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
|
||||
// remove the image from the file system
|
||||
@@ -55,22 +96,10 @@ export var testFromFile = function () {
|
||||
}
|
||||
|
||||
export var testNativeFields = function () {
|
||||
var img = imageSource.fromResource(getTestImageName());
|
||||
var img = imageSource.fromResource("logo");
|
||||
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) {
|
||||
// TODO: This expects
|
||||
return "logo";
|
||||
}
|
||||
if (app.android) {
|
||||
return "ic_launcher";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
@@ -47,7 +47,9 @@ export var testString = function () {
|
||||
|
||||
export var testNumber = function () {
|
||||
// <snippet name="local-settings">
|
||||
// ### Set and get numeric value. We use toFixed() here in order to avoid number based errors
|
||||
// ### Set and get numeric value.
|
||||
// We use `toFixed()` here in order to avoid floating point errors - ex: `54.321` becoming `54.320999999537`.
|
||||
// Beware the result of `toFixed()` is a string not a number therefore you cannot use `===` or `!==` when comparing with a number.
|
||||
// ``` JavaScript
|
||||
LocalSettings.setNumber("numberKey", 54.321);
|
||||
var value = LocalSettings.getNumber("numberKey").toFixed(3);
|
||||
|
||||
@@ -6,7 +6,7 @@ allTests["FILE SYSTEM"] = require("Tests/file-system-tests");
|
||||
allTests["HTTP"] = require("Tests/http-tests");
|
||||
allTests["LOCATION"] = require("Tests/location-tests");
|
||||
allTests["LOCAL SETTINGS"] = require("Tests/local-settings-tests");
|
||||
allTests["IMAGE SOURCE"] = require("Tests/image-tests");
|
||||
allTests["IMAGE SOURCE"] = require("Tests/image-source-tests");
|
||||
allTests["TIMER"] = require("Tests/timer-tests");
|
||||
|
||||
export var runAll = function (moduleName?: string) {
|
||||
|
||||
Reference in New Issue
Block a user