From 38bc482359b2eb8e15b590bb3a2821c0335a1429 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Fri, 3 Apr 2015 12:56:23 +0300 Subject: [PATCH] Moved delegate handling to onLoaded / onUnloaded methods and reworked the WebView tests. --- apps/tests/ui/web-view/web-view-tests.ts | 118 +++++++++++++++-------- ui/list-picker/list-picker.ios.ts | 9 ++ ui/list-view/list-view.ios.ts | 15 ++- ui/search-bar/search-bar.ios.ts | 9 ++ ui/tab-view/tab-view.ios.ts | 17 +++- ui/text-field/text-field.ios.ts | 9 ++ ui/text-view/text-view.ios.ts | 9 ++ ui/web-view/web-view.ios.ts | 9 ++ 8 files changed, 147 insertions(+), 48 deletions(-) diff --git a/apps/tests/ui/web-view/web-view-tests.ts b/apps/tests/ui/web-view/web-view-tests.ts index 976d1c43b..e6e9d2bb0 100644 --- a/apps/tests/ui/web-view/web-view-tests.ts +++ b/apps/tests/ui/web-view/web-view-tests.ts @@ -1,6 +1,7 @@ import TKUnit = require("../../TKUnit"); import helper = require("../helper"); import viewModule = require("ui/core/view"); +import page = require("ui/page"); // // # WebView @@ -28,49 +29,82 @@ var _createWebViewFunc = function (): webViewModule.WebView { return webView; } -export var testLoadExistingUrl = function (done) { - helper.buildUIAndRunTest(_createWebViewFunc(), function (views: Array) { - var webView = views[0]; - // - // ### 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); - // - TKUnit.assert(args.url === "https://httpbin.org/html", "args.url should equal https://httpbin.org/html"); - TKUnit.assert(args.error === undefined, args.error); - done(); - // - }); - webView.url = "https://httpbin.org/html"; - // ``` - // - }); +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; + + // + // ### Using WebView + // ``` JavaScript + webView.on(webViewModule.knownEvents.loadFinished, function (args: webViewModule.LoadEventData) { + // + actualUrl = args.url; + actualError = args.error; + testFinished = true; + // + 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) { - var 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"); + } } \ No newline at end of file diff --git a/ui/list-picker/list-picker.ios.ts b/ui/list-picker/list-picker.ios.ts index b3bab7107..31803628f 100644 --- a/ui/list-picker/list-picker.ios.ts +++ b/ui/list-picker/list-picker.ios.ts @@ -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; } diff --git a/ui/list-view/list-view.ios.ts b/ui/list-view/list-view.ios.ts index 8ea711c89..4bff16324 100644 --- a/ui/list-view/list-view.ios.ts +++ b/ui/list-view/list-view.ios.ts @@ -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; 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(); } + public onLoaded() { + super.onLoaded(); + this._ios.delegate = this._delegate; + } + + public onUnloaded() { + this._ios.delegate = null; + super.onUnloaded(); + } + get ios(): UITableView { return this._ios; } diff --git a/ui/search-bar/search-bar.ios.ts b/ui/search-bar/search-bar.ios.ts index 336f11aff..0960f6052 100644 --- a/ui/search-bar/search-bar.ios.ts +++ b/ui/search-bar/search-bar.ios.ts @@ -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; } diff --git a/ui/tab-view/tab-view.ios.ts b/ui/tab-view/tab-view.ios.ts index bc4454de1..4e0071b36 100644 --- a/ui/tab-view/tab-view.ios.ts +++ b/ui/tab-view/tab-view.ios.ts @@ -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)); } } + } \ No newline at end of file diff --git a/ui/text-field/text-field.ios.ts b/ui/text-field/text-field.ios.ts index 630778290..7ba604185 100644 --- a/ui/text-field/text-field.ios.ts +++ b/ui/text-field/text-field.ios.ts @@ -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; } diff --git a/ui/text-view/text-view.ios.ts b/ui/text-view/text-view.ios.ts index 2393582d2..679977ca1 100644 --- a/ui/text-view/text-view.ios.ts +++ b/ui/text-view/text-view.ios.ts @@ -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; } diff --git a/ui/web-view/web-view.ios.ts b/ui/web-view/web-view.ios.ts index 64b9148db..dd98f5214 100644 --- a/ui/web-view/web-view.ios.ts +++ b/ui/web-view/web-view.ios.ts @@ -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; }