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:
23
tests/app/application/application-tests-common.ts
Normal file
23
tests/app/application/application-tests-common.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// >> application-require
|
||||
import * as app from "tns-core-modules/application";
|
||||
import * as platform from "tns-core-modules/platform";
|
||||
// << application-require
|
||||
|
||||
// >> application-app-check
|
||||
if (app.android) {
|
||||
console.log("We are running on Android device!");
|
||||
} else if (app.ios) {
|
||||
console.log("We are running on iOS device");
|
||||
}
|
||||
// << application-app-check
|
||||
|
||||
import * as TKUnit from "../TKUnit";
|
||||
|
||||
export var testInitialized = function () {
|
||||
if (platform.device.os === platform.platformNames.android) {
|
||||
// we have the android defined
|
||||
TKUnit.assert(app.android, "Application module not properly intialized");
|
||||
} else if (platform.device.os === platform.platformNames.ios) {
|
||||
TKUnit.assert(app.ios, "Application module not properly intialized");
|
||||
}
|
||||
}
|
||||
50
tests/app/application/application-tests.android.ts
Normal file
50
tests/app/application/application-tests.android.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/* tslint:disable:no-unused-variable */
|
||||
import * as app from "tns-core-modules/application";
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as commonTests from "./application-tests-common";
|
||||
|
||||
global.moduleMerge(commonTests, exports);
|
||||
|
||||
// >> application-app-android
|
||||
var androidApp = app.android;
|
||||
// << application-app-android
|
||||
|
||||
// >> application-app-android-context
|
||||
var context = app.android.context;
|
||||
//// get the Files (Documents) folder (directory)
|
||||
var dir = context.getFilesDir();
|
||||
// << application-app-android-context
|
||||
|
||||
// >> application-app-android-current
|
||||
if (androidApp.foregroundActivity === androidApp.startActivity) {
|
||||
////console.log("We are currently in the main (start) activity of the application");
|
||||
}
|
||||
// << application-app-android-current
|
||||
|
||||
// >> application-app-android-broadcast
|
||||
//// Register the broadcast receiver
|
||||
if (app.android) {
|
||||
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
|
||||
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
|
||||
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
|
||||
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
|
||||
var percent = (level / scale) * 100.0;
|
||||
////console.log("Battery: " + percent + "%");
|
||||
});
|
||||
}
|
||||
//// When no longer needed, unregister the broadcast receiver
|
||||
if (app.android) {
|
||||
app.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
|
||||
}
|
||||
// << application-app-android-broadcast
|
||||
|
||||
export var testAndroidApplicationInitialized = function () {
|
||||
TKUnit.assert(app.android, "Android application not initialized.");
|
||||
TKUnit.assert(app.android.context, "Android context not initialized.");
|
||||
TKUnit.assert(app.android.currentContext, "Android currentContext not initialized.");
|
||||
TKUnit.assert(app.android.foregroundActivity, "Android foregroundActivity not initialized.");
|
||||
TKUnit.assert(app.android.foregroundActivity.isNativeScriptActivity, "Andorid foregroundActivity.isNativeScriptActivity is true");
|
||||
TKUnit.assert(app.android.startActivity, "Android startActivity not initialized.");
|
||||
TKUnit.assert(app.android.nativeApp, "Android nativeApp not initialized.");
|
||||
TKUnit.assert(app.android.packageName, "Android packageName not initialized.");
|
||||
}
|
||||
3
tests/app/application/application-tests.d.ts
vendored
Normal file
3
tests/app/application/application-tests.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
/* tslint:disable */
|
||||
import * as android from "./application-tests.android";
|
||||
import * as iOS from "./application-tests.ios";
|
||||
46
tests/app/application/application-tests.ios.ts
Normal file
46
tests/app/application/application-tests.ios.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/* tslint:disable:no-unused-variable */
|
||||
import * as app from "tns-core-modules/application";
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as commonTests from "./application-tests-common";
|
||||
|
||||
import * as utils from "tns-core-modules/utils/utils";
|
||||
import getter = utils.ios.getter;
|
||||
|
||||
global.moduleMerge(commonTests, exports);
|
||||
|
||||
// >> application-ios-observer
|
||||
//// Add the notification observer
|
||||
if (app.ios) {
|
||||
var observer = app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
|
||||
function onReceiveCallback(notification: NSNotification) {
|
||||
var percent = getter(UIDevice, UIDevice.currentDevice).batteryLevel * 100;
|
||||
var message = "Battery: " + percent + "%";
|
||||
////console.log(message);
|
||||
});
|
||||
}
|
||||
//// When no longer needed, remove the notification observer
|
||||
if (app.ios) {
|
||||
app.ios.removeNotificationObserver(observer, UIDeviceBatteryLevelDidChangeNotification);
|
||||
}
|
||||
// << application-ios-observer
|
||||
|
||||
// >> application-ios-delegate
|
||||
//// Add custom application delegate
|
||||
if (app.ios) {
|
||||
class MyDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
public static ObjCProtocols = [UIApplicationDelegate];
|
||||
|
||||
applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<any, any>): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
applicationDidBecomeActive(application: UIApplication): void {
|
||||
// Get reference to the application window.
|
||||
//console.log("keyWindow: " + application.keyWindow);
|
||||
}
|
||||
}
|
||||
|
||||
app.ios.delegate = MyDelegate;
|
||||
}
|
||||
|
||||
// << application-ios-delegate
|
||||
34
tests/app/application/application.md
Normal file
34
tests/app/application/application.md
Normal file
@@ -0,0 +1,34 @@
|
||||
---
|
||||
nav-title: "application How-To"
|
||||
title: "application"
|
||||
environment: nativescript
|
||||
description: "Examples for using application"
|
||||
previous_url: /ApiReference/application/HOW-TO
|
||||
---
|
||||
# Application
|
||||
The Application module provides abstraction over the platform-specific Application implementations.
|
||||
It is the main BCL module and is required for other BCL modules to work properly.
|
||||
The default bootstrap.js implementation for each platform loads and initializes this module.
|
||||
{%snippet application-require%}
|
||||
|
||||
The pre-required `app` module is used throughout the following code snippets.
|
||||
### Checking the target platform
|
||||
Use the following code in case you need to check somewhere in your code the platform you are running against:
|
||||
{%snippet application-app-check%}
|
||||
|
||||
### Using the Android-specific implementation
|
||||
Accessing the Android-specific object instance (will be undefined if running on iOS)
|
||||
|
||||
{%snippet application-app-android%}
|
||||
|
||||
### Using the Android Application context
|
||||
{%snippet application-app-android-context%}
|
||||
|
||||
### Tracking the current Activity
|
||||
{%snippet application-app-android-current%}
|
||||
|
||||
### Registering a Broadcast Receiver (Android)
|
||||
{%snippet application-app-android-broadcast%}
|
||||
|
||||
### Adding a Notification Observer (iOS)
|
||||
{%snippet application-ios-observer%}
|
||||
Reference in New Issue
Block a user