Merge pull request #846 from NativeScript/placeholder-event-fix

placeholder creatingView event fixed + tests
This commit is contained in:
Vladimir Enchev
2015-09-30 13:35:38 +03:00
6 changed files with 100 additions and 16 deletions

View File

@ -80,6 +80,7 @@
<DependentUpon>data-binding.xml</DependentUpon> <DependentUpon>data-binding.xml</DependentUpon>
</TypeScriptCompile> </TypeScriptCompile>
<TypeScriptCompile Include="apps\tests\ui\action-bar\ActionBar_NumberAsText.ts" /> <TypeScriptCompile Include="apps\tests\ui\action-bar\ActionBar_NumberAsText.ts" />
<TypeScriptCompile Include="apps\tests\ui\placeholder\placeholder-tests.ts" />
<TypeScriptCompile Include="apps\tests\xml-declaration\custom-code-file.ts" /> <TypeScriptCompile Include="apps\tests\xml-declaration\custom-code-file.ts" />
<TypeScriptCompile Include="apps\transforms\app.ts" /> <TypeScriptCompile Include="apps\transforms\app.ts" />
<TypeScriptCompile Include="apps\transforms\main-page.ts" /> <TypeScriptCompile Include="apps\transforms\main-page.ts" />
@ -183,8 +184,8 @@
<Content Include="apps\tests\pages\tab-view.xml" /> <Content Include="apps\tests\pages\tab-view.xml" />
<Content Include="apps\ui-tests-app\app.css" /> <Content Include="apps\ui-tests-app\app.css" />
<TypeScriptCompile Include="apps\ui-tests-app\nordic\nordic.ts"> <TypeScriptCompile Include="apps\ui-tests-app\nordic\nordic.ts">
<DependentUpon>nordic.xml</DependentUpon> <DependentUpon>nordic.xml</DependentUpon>
</TypeScriptCompile> </TypeScriptCompile>
<TypeScriptCompile Include="apps\ui-tests-app\pages\handlers.ts" /> <TypeScriptCompile Include="apps\ui-tests-app\pages\handlers.ts" />
<TypeScriptCompile Include="apps\ui-tests-app\pages\htmlview.ts" /> <TypeScriptCompile Include="apps\ui-tests-app\pages\htmlview.ts" />
<TypeScriptCompile Include="ui\animation\animation.d.ts" /> <TypeScriptCompile Include="ui\animation\animation.d.ts" />
@ -1984,7 +1985,7 @@
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile> <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties> </WebProjectProperties>
</FlavorProperties> </FlavorProperties>
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" /> <UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
</Project> </Project>

View File

@ -61,6 +61,7 @@ allTests["IMAGE"] = require("./ui/image/image-tests");
allTests["SLIDER"] = require("./ui/slider/slider-tests"); allTests["SLIDER"] = require("./ui/slider/slider-tests");
allTests["SWITCH"] = require("./ui/switch/switch-tests"); allTests["SWITCH"] = require("./ui/switch/switch-tests");
allTests["PROGRESS"] = require("./ui/progress/progress-tests"); allTests["PROGRESS"] = require("./ui/progress/progress-tests");
allTests["PLACEHOLDER"] = require("./ui/placeholder/placeholder-tests");
allTests["PAGE"] = require("./ui/page/page-tests"); allTests["PAGE"] = require("./ui/page/page-tests");
allTests["LISTVIEW"] = require("./ui/list-view/list-view-tests"); allTests["LISTVIEW"] = require("./ui/list-view/list-view-tests");
allTests["ACTIVITY-INDICATOR"] = require("./ui/activity-indicator/activity-indicator-tests"); allTests["ACTIVITY-INDICATOR"] = require("./ui/activity-indicator/activity-indicator-tests");

View File

@ -0,0 +1,78 @@
import TKUnit = require("../../TKUnit");
import platform = require("platform");
import utils = require("utils/utils");
import helper = require("../helper");
import viewModule = require("ui/core/view");
// <snippet module="ui/placeholder" title="placeholder">
// # Placeholder
// Using the placeholder requires the Placeholder module.
// ``` JavaScript
import placeholderModule = require("ui/placeholder");
// ```
// Creating native view for the Placeholder using creatingView event.
//```XML
// <Page>
// {%raw%}<Placeholder creatingView="creatingView" />{%endraw%}
// </Page>
//```
//```JS
//var platform = require("platform");
//var utils = require("utils/utils");
//
//function creatingView(args) {
// var nativeView;
// if (platform.device.os === platform.platformNames.ios) {
// nativeView = new UITextView();
// nativeView.text = "Native";
// } else if (platform.device.os === platform.platformNames.android) {
// nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
// nativeView.setText("Native");
// }
//
// args.view = nativeView;
//}
//
//exports.creatingView = creatingView;
//```
// </snippet>
export function test_placeholder_creatingView() {
var nativeView;
var p = new placeholderModule.Placeholder();
p.id = "test";
p.on(placeholderModule.Placeholder.creatingViewEvent, (args: placeholderModule.CreateViewEventData) => {
if (platform.device.os === platform.platformNames.ios) {
nativeView = new UITextView();
nativeView.text = "Native";
} else if (platform.device.os === platform.platformNames.android) {
nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
nativeView.setText("Native");
}
args.view = nativeView;
});
if (platform.device.os === platform.platformNames.ios) {
TKUnit.assert(p.ios instanceof UITextView, "ios property should be UITextView. Current value: " + p.ios);
} else if (platform.device.os === platform.platformNames.android) {
p._emit("creatingView");
TKUnit.assert(nativeView instanceof android.widget.TextView, "Native view should be android.widget.TextView. Current value: " + nativeView);
}
}
export function test_placeholder_will_not_crash_wihout_creatingView() {
var p = new placeholderModule.Placeholder();
function testAction(views: Array<viewModule.View>) {
if (platform.device.os === platform.platformNames.ios) {
TKUnit.assert(p.ios === undefined, "ios property should be undefined. Current value: " + p.ios);
} else if (platform.device.os === platform.platformNames.android) {
TKUnit.assert(p.android === undefined, "android view should be undefined. Current value: " + p.android);
}
};
helper.buildUIAndRunTest(p, testAction);
}

View File

@ -196,8 +196,10 @@ export class View extends viewCommon.View {
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void { public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
var view = this._nativeView; var view = this._nativeView;
if (view) { let nativeWidth = 0;
let nativeHeight = 0;
if (view) {
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec); var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec); var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
@ -213,15 +215,17 @@ export class View extends viewCommon.View {
} }
var nativeSize = view.sizeThatFits(CGSizeMake(width, height)); var nativeSize = view.sizeThatFits(CGSizeMake(width, height));
nativeWidth = nativeSize.width;
var measureWidth = Math.max(nativeSize.width, this.minWidth); nativeHeight = nativeSize.height;
var measureHeight = Math.max(nativeSize.height, this.minHeight);
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
} }
var measureWidth = Math.max(nativeWidth, this.minWidth);
var measureHeight = Math.max(nativeHeight, this.minHeight);
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
} }
public onLayout(left: number, top: number, right: number, bottom: number): void { public onLayout(left: number, top: number, right: number, bottom: number): void {
@ -305,12 +309,12 @@ export class CustomLayoutView extends View {
if (this._nativeView && child._nativeView) { if (this._nativeView && child._nativeView) {
if (types.isNullOrUndefined(atIndex) || atIndex >= this._nativeView.subviews.count) { if (types.isNullOrUndefined(atIndex) || atIndex >= this._nativeView.subviews.count) {
this._nativeView.addSubview(child._nativeView); this._nativeView.addSubview(child._nativeView);
} }
else { else {
this._nativeView.insertSubviewAtIndex(child._nativeView, atIndex); this._nativeView.insertSubviewAtIndex(child._nativeView, atIndex);
} }
return true; return true;
} }

View File

@ -9,7 +9,7 @@ export class Placeholder extends common.Placeholder {
public _createUI() { public _createUI() {
var args = <definition.CreateViewEventData>{ eventName: common.Placeholder.creatingViewEvent, object: this, view: undefined, context: this._context }; var args = <definition.CreateViewEventData>{ eventName: common.Placeholder.creatingViewEvent, object: this, view: undefined, context: this._context };
this.notify(args); this.notify(args);
this._android = <android.view.View>args.view || new android.view.View(this._context); this._android = <android.view.View>args.view;
} }
get android(): android.view.View { get android(): android.view.View {

View File

@ -10,7 +10,7 @@ export class Placeholder extends common.Placeholder {
if (!this._ios) { if (!this._ios) {
var args = <definition.CreateViewEventData>{ eventName: common.Placeholder.creatingViewEvent, object: this, view: undefined, context: undefined }; var args = <definition.CreateViewEventData>{ eventName: common.Placeholder.creatingViewEvent, object: this, view: undefined, context: undefined };
super.notify(args); super.notify(args);
this._ios = args.view || new UIView(); this._ios = args.view;
} }
return this._ios; return this._ios;
} }