mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Tests moved into separate folders (#3794)
Fix android crash on application exit
This commit is contained in:
154
tests/app/image-source/image-source-tests.ts
Normal file
154
tests/app/image-source/image-source-tests.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
// >> imagesource-require
|
||||
// var imageSource = require("image-source");
|
||||
// << imagesource-require
|
||||
|
||||
// >> imagesource-require-alt
|
||||
// var fs = require("file-system");
|
||||
// << imagesource-require-alt
|
||||
|
||||
import * as imageSource from "tns-core-modules/image-source";
|
||||
import * as fs from "tns-core-modules/file-system";
|
||||
import * as app from "tns-core-modules/application";
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as platform from "tns-core-modules/platform";
|
||||
|
||||
var imagePath = "~/logo.png";
|
||||
var smallImagePath = "~/small-image.png";
|
||||
|
||||
export function testFromResource() {
|
||||
var img = imageSource.fromResource("icon");
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
}
|
||||
|
||||
export function testFromUrl(done) {
|
||||
//var completed;
|
||||
var result: imageSource.ImageSource;
|
||||
|
||||
// >> imagesource-load-url
|
||||
imageSource.fromUrl("https://www.google.com/images/errors/logo_sm_2.png")
|
||||
.then(function (res: imageSource.ImageSource) {
|
||||
//console.log("Image successfully loaded");
|
||||
// >> (hide)
|
||||
//completed = true;
|
||||
result = res;
|
||||
try {
|
||||
TKUnit.assertNotEqual(result, undefined, "Image not downloaded");
|
||||
TKUnit.assert(result.height > 0, "Image not downloaded");
|
||||
done(null);
|
||||
}
|
||||
catch (e) {
|
||||
done(e);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (error) {
|
||||
//console.log("Error loading image: " + error);
|
||||
// >> (hide)
|
||||
//completed = true;
|
||||
done(error);
|
||||
// << (hide)
|
||||
});
|
||||
// << imagesource-load-url
|
||||
}
|
||||
|
||||
export function testSaveToFile() {
|
||||
// >> imagesource-save-to
|
||||
var img = imageSource.fromFile(imagePath);
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
var saved = img.saveToFile(path, "png");
|
||||
// << imagesource-save-to
|
||||
TKUnit.assert(saved, "Image not saved to file");
|
||||
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
|
||||
}
|
||||
|
||||
export function testFromFile() {
|
||||
// >> imagesource-load-local
|
||||
var folder = fs.knownFolders.documents();
|
||||
var path = fs.path.join(folder.path, "Test.png");
|
||||
var img = imageSource.fromFile(path);
|
||||
// << imagesource-load-local
|
||||
|
||||
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 function testNativeFields() {
|
||||
var img = imageSource.fromFile(imagePath);
|
||||
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 expectedAndroidPng = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAA3NCSVQICAjb4U/gAAAAFUlEQVQImWP8z4AAjAz/kTnIPGQAAG86AwGcuMlCAAAAAElFTkSuQmCC";
|
||||
var expectedIos7Png = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAHGlET1QAAAACAAAAAAAAAAIAAAAoAAAAAgAAAAIAAABGJLi4kQAAABJJREFUCB1i+M/AAEdIzP8MAAAAAP//HD3fegAAAA5JREFUY2Bg+I+EGBAAAIS9C/UPnL+kAAAAAElFTkSuQmCC";
|
||||
var expectedIos8Png = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAACAAAAKAAAAAIAAAACAAAARiS4uJEAAAASSURBVBgZYvjP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9j/4AAQSkZJRgAB";
|
||||
|
||||
export function testBase64Encode_PNG() {
|
||||
var img = imageSource.fromFile(smallImagePath);
|
||||
|
||||
var expected: string;
|
||||
if (app.android) {
|
||||
expected = expectedAndroidPng;
|
||||
} else if (app.ios) {
|
||||
if (platform.device.osVersion[0] === '7') {
|
||||
expected = expectedIos7Png;
|
||||
}
|
||||
else {
|
||||
expected = expectedIos8Png;
|
||||
}
|
||||
}
|
||||
|
||||
var result = img.toBase64String("png");
|
||||
TKUnit.assertEqual(
|
||||
result,
|
||||
expected,
|
||||
"Base 64 encoded PNG");
|
||||
}
|
||||
|
||||
export function testBase64Encode_JPEG() {
|
||||
var img = imageSource.fromFile(smallImagePath);
|
||||
|
||||
var result = img.toBase64String("jpeg");
|
||||
result = result.substr(0, expectedJpegStart.length);
|
||||
|
||||
TKUnit.assertEqual(
|
||||
result,
|
||||
expectedJpegStart,
|
||||
"Base 64 encoded JPEG");
|
||||
}
|
||||
|
||||
export function testLoadFromBase64Encode_JPEG() {
|
||||
var img: imageSource.ImageSource;
|
||||
img = imageSource.fromBase64(fullJpegImage);
|
||||
|
||||
TKUnit.assert(img !== null, "Actual: " + img);
|
||||
TKUnit.assertEqual(img.width, 4, "img.width");
|
||||
TKUnit.assertEqual(img.height, 4, "img.height");
|
||||
}
|
||||
|
||||
export function testLoadFromBase64Encode_PNG() {
|
||||
var img: imageSource.ImageSource;
|
||||
if (app.android) {
|
||||
img = imageSource.fromBase64(expectedAndroidPng);
|
||||
} else if (app.ios) {
|
||||
if (platform.device.osVersion[0] === '7') {
|
||||
img = imageSource.fromBase64(expectedIos7Png);
|
||||
}
|
||||
else {
|
||||
img = imageSource.fromBase64(expectedIos8Png);
|
||||
}
|
||||
}
|
||||
|
||||
TKUnit.assert(img !== null, "Actual: " + img);
|
||||
TKUnit.assertEqual(img.width, 4, "img.width");
|
||||
TKUnit.assertEqual(img.height, 4, "img.height");
|
||||
}
|
||||
27
tests/app/image-source/image-source.md
Normal file
27
tests/app/image-source/image-source.md
Normal file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
nav-title: "image-source How-To"
|
||||
title: "image-source"
|
||||
environment: nativescript
|
||||
description: "Examples for using image-source"
|
||||
previous_url: /ApiReference/image-source/HOW-TO
|
||||
---
|
||||
# Image source
|
||||
Using the image source requires the image-source module.
|
||||
{%snippet imagesource-require%}
|
||||
The pre-required `imageSource` module is used throughout the following code snippets.
|
||||
We also use fs module defined as follows:
|
||||
{%snippet imagesource-require-alt%}
|
||||
|
||||
## Loading and saving images
|
||||
### Load image using resource name
|
||||
This is similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS
|
||||
{%snippet imagesource-resname%}
|
||||
|
||||
### Load image from URL
|
||||
{%snippet imagesource-load-url%}
|
||||
|
||||
### Save image source to PNG or JPG file
|
||||
{%snippet imagesource-save-to%}
|
||||
|
||||
### Load image from a local file
|
||||
{%snippet imagesource-load-local%}
|
||||
Reference in New Issue
Block a user