mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
Moved delegate handling to onLoaded / onUnloaded methods and reworked the WebView tests.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import TKUnit = require("../../TKUnit");
|
||||
import helper = require("../helper");
|
||||
import viewModule = require("ui/core/view");
|
||||
import page = require("ui/page");
|
||||
|
||||
// <snippet module="ui/web-view" title="WebView">
|
||||
// # WebView
|
||||
@ -28,49 +29,82 @@ var _createWebViewFunc = function (): webViewModule.WebView {
|
||||
return webView;
|
||||
}
|
||||
|
||||
export var testLoadExistingUrl = function (done) {
|
||||
helper.buildUIAndRunTest(_createWebViewFunc(), function (views: Array<viewModule.View>) {
|
||||
var webView = <webViewModule.WebView>views[0];
|
||||
// <snippet module="ui/web-view" title="WebView">
|
||||
// ### Using WebView,
|
||||
// ``` JavaScript
|
||||
webView.on(webViewModule.knownEvents.loadFinished, function (args: webViewModule.LoadEventData) {
|
||||
var message;
|
||||
if (!args.error) {
|
||||
message = "WebView finished loading " + args.url;
|
||||
}
|
||||
else {
|
||||
message = "Error loading " + args.url + ": " + args.error;
|
||||
}
|
||||
//console.log(message);
|
||||
// <hide>
|
||||
TKUnit.assert(args.url === "https://httpbin.org/html", "args.url should equal https://httpbin.org/html");
|
||||
TKUnit.assert(args.error === undefined, args.error);
|
||||
done();
|
||||
// </hide>
|
||||
});
|
||||
webView.url = "https://httpbin.org/html";
|
||||
// ```
|
||||
// </snippet>
|
||||
});
|
||||
export var testLoadExistingUrl = function () {
|
||||
var newPage: page.Page;
|
||||
var webView = _createWebViewFunc();
|
||||
var pageFactory = function (): page.Page {
|
||||
newPage = new page.Page();
|
||||
newPage.content = webView;
|
||||
return newPage;
|
||||
};
|
||||
|
||||
helper.navigate(pageFactory);
|
||||
|
||||
var testFinished = false;
|
||||
var actualUrl;
|
||||
var actualError;
|
||||
|
||||
// <snippet module="ui/web-view" title="WebView">
|
||||
// ### Using WebView
|
||||
// ``` JavaScript
|
||||
webView.on(webViewModule.knownEvents.loadFinished, function (args: webViewModule.LoadEventData) {
|
||||
// <hide>
|
||||
actualUrl = args.url;
|
||||
actualError = args.error;
|
||||
testFinished = true;
|
||||
// </hide>
|
||||
var message;
|
||||
if (!args.error) {
|
||||
message = "WebView finished loading " + args.url;
|
||||
}
|
||||
else {
|
||||
message = "Error loading " + args.url + ": " + args.error;
|
||||
}
|
||||
//console.log(message);
|
||||
});
|
||||
webView.url = "https://httpbin.org/html";
|
||||
|
||||
TKUnit.wait(2);
|
||||
|
||||
helper.goBack();
|
||||
|
||||
if (testFinished) {
|
||||
TKUnit.assert(actualUrl === "https://httpbin.org/html", "args.url should equal https://httpbin.org/html");
|
||||
TKUnit.assert(actualError === undefined, actualError);
|
||||
}
|
||||
else {
|
||||
TKUnit.assert(false, "TIMEOUT");
|
||||
}
|
||||
}
|
||||
|
||||
export var testLoadInvalidUrl = function (done) {
|
||||
helper.buildUIAndRunTest(_createWebViewFunc(), function (views: Array<viewModule.View>) {
|
||||
var webView = <webViewModule.WebView>views[0];
|
||||
|
||||
var errorReceived = false;
|
||||
webView.on(webViewModule.knownEvents.loadFinished, function (args: webViewModule.LoadEventData) {
|
||||
if (errorReceived) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.error) {
|
||||
errorReceived = true;
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
webView.url = "kofti://mnogokofti";
|
||||
export var testLoadInvalidUrl = function () {
|
||||
var newPage: page.Page;
|
||||
var webView = _createWebViewFunc();
|
||||
var pageFactory = function (): page.Page {
|
||||
newPage = new page.Page();
|
||||
newPage.content = webView;
|
||||
return newPage;
|
||||
};
|
||||
|
||||
helper.navigate(pageFactory);
|
||||
|
||||
var testFinished = false;
|
||||
var actualError;
|
||||
|
||||
webView.on(webViewModule.knownEvents.loadFinished, function (args: webViewModule.LoadEventData) {
|
||||
testFinished = true;
|
||||
actualError = args.error;
|
||||
});
|
||||
webView.url = "kofti://mnogokofti";
|
||||
|
||||
TKUnit.wait(2);
|
||||
|
||||
helper.goBack();
|
||||
|
||||
if (testFinished) {
|
||||
TKUnit.assert(actualError !== undefined, "There should be an error.");
|
||||
}
|
||||
else {
|
||||
TKUnit.assert(false, "TIMEOUT");
|
||||
}
|
||||
}
|
@ -22,9 +22,18 @@ export class ListPicker extends common.ListPicker {
|
||||
this._ios.dataSource = this._dataSource;
|
||||
|
||||
this._delegate = ListPickerDelegateImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UIPickerView {
|
||||
return this._ios;
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ function onSeparatorColorPropertyChanged(data: dependencyObservable.PropertyChan
|
||||
export class ListView extends common.ListView {
|
||||
private _ios: UITableView;
|
||||
private _dataSource;
|
||||
private _uiTableViewDelegate;
|
||||
private _delegate;
|
||||
private _heights: Array<number>;
|
||||
private _preparingCell: boolean = false;
|
||||
|
||||
@ -165,12 +165,21 @@ export class ListView extends common.ListView {
|
||||
this._dataSource = dataSource;
|
||||
this._ios.dataSource = this._dataSource;
|
||||
|
||||
this._uiTableViewDelegate = UITableViewDelegateImpl.new().initWithOwner(this);
|
||||
this._delegate = UITableViewDelegateImpl.new().initWithOwner(this);
|
||||
|
||||
this._ios.delegate = this._uiTableViewDelegate;
|
||||
this._heights = new Array<number>();
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UITableView {
|
||||
return this._ios;
|
||||
}
|
||||
|
@ -96,9 +96,18 @@ export class SearchBar extends common.SearchBar {
|
||||
this._ios = new UISearchBar();
|
||||
|
||||
this._delegate = UISearchBarDelegateImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UISearchBar {
|
||||
return this._ios;
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ class UINavigationControllerDelegateImpl extends NSObject implements UINavigatio
|
||||
|
||||
export class TabView extends common.TabView {
|
||||
private _ios: UITabBarControllerImpl;
|
||||
private _tabBarControllerDelegate: UITabBarControllerDelegateImpl;
|
||||
private _delegate: UITabBarControllerDelegateImpl;
|
||||
private _moreNavigationControllerDelegate: UINavigationControllerDelegateImpl;
|
||||
private _tabBarHeight: number = 0;
|
||||
private _navBarHeight: number = 0;
|
||||
@ -93,13 +93,23 @@ export class TabView extends common.TabView {
|
||||
|
||||
this._ios = UITabBarControllerImpl.new().initWithOwner(this);
|
||||
|
||||
this._tabBarControllerDelegate = UITabBarControllerDelegateImpl.new().initWithOwner(this);
|
||||
this._ios.delegate = this._tabBarControllerDelegate;
|
||||
this._delegate = UITabBarControllerDelegateImpl.new().initWithOwner(this);
|
||||
|
||||
this._moreNavigationControllerDelegate = UINavigationControllerDelegateImpl.new().initWithOwner(this);
|
||||
//This delegate is set on the last line of _addTabs method.
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
this._ios.moreNavigationController.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UIViewController {
|
||||
return this._ios;
|
||||
}
|
||||
@ -249,4 +259,5 @@ export class TabView extends common.TabView {
|
||||
view.View.layoutChild(this, child, 0, this._navBarHeight, right, (bottom - this._navBarHeight - this._tabBarHeight));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -74,9 +74,18 @@ export class TextField extends common.TextField {
|
||||
this._ios = new UITextField();
|
||||
|
||||
this._delegate = UITextFieldDelegateImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UITextField {
|
||||
return this._ios;
|
||||
}
|
||||
|
@ -50,9 +50,18 @@ export class TextView extends common.TextView {
|
||||
}
|
||||
|
||||
this._delegate = UITextViewDelegateImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UITextView {
|
||||
return this._ios;
|
||||
}
|
||||
|
@ -56,9 +56,18 @@ export class WebView extends common.WebView {
|
||||
|
||||
this._ios = new UIWebView();
|
||||
this._delegate = UIWebViewDelegateImpl.new().initWithOwner(this);
|
||||
}
|
||||
|
||||
public onLoaded() {
|
||||
super.onLoaded();
|
||||
this._ios.delegate = this._delegate;
|
||||
}
|
||||
|
||||
public onUnloaded() {
|
||||
this._ios.delegate = null;
|
||||
super.onUnloaded();
|
||||
}
|
||||
|
||||
get ios(): UIWebView {
|
||||
return this._ios;
|
||||
}
|
||||
|
Reference in New Issue
Block a user