From c2f084224add0c07849dc512ccea2ee2671fcace Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Thu, 28 Apr 2016 18:35:55 +0530 Subject: [PATCH 1/7] Expose configurable attributes property when loading components --- ui/builder/builder.d.ts | 1 + ui/builder/builder.ts | 2 +- ui/builder/component-builder.d.ts | 2 +- ui/builder/component-builder.ts | 6 +++--- ui/builder/special-properties.d.ts | 2 +- ui/builder/special-properties.ts | 2 +- utils/utils-common.ts | 12 +++++++----- utils/utils.d.ts | 2 +- 8 files changed, 16 insertions(+), 13 deletions(-) diff --git a/ui/builder/builder.d.ts b/ui/builder/builder.d.ts index e2262d644..57318bb68 100644 --- a/ui/builder/builder.d.ts +++ b/ui/builder/builder.d.ts @@ -9,6 +9,7 @@ export interface LoadOptions { path: string; name: string; + attributes?: any; exports?: any; page?: page.Page; } diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index f1c642310..73d3dfa18 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -115,7 +115,7 @@ export function load(pathOrOptions: string | LoadOptions, context?: any): View { if (!context) { if (!isString(pathOrOptions)) { let options = pathOrOptions; - componentModule = loadCustomComponent(options.path, options.name, undefined, options.exports, options.page); + componentModule = loadCustomComponent(options.path, options.name, options.attributes, options.exports, options.page); } else { let path = pathOrOptions; componentModule = loadInternal(path); diff --git a/ui/builder/component-builder.d.ts b/ui/builder/component-builder.d.ts index 24711fc29..8da3f4278 100644 --- a/ui/builder/component-builder.d.ts +++ b/ui/builder/component-builder.d.ts @@ -2,7 +2,7 @@ declare module "ui/builder/component-builder" { import view = require("ui/core/view"); export function getComponentModule(elementName: string, namespace: string, attributes: Object, exports: Object): ComponentModule; - export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: string) : void; + export function setPropertyValue(instance: view.View, instanceModuleExports: Object, pageExports: Object, propertyName: string, propertyValue: any) : void; export interface ComponentModule { component: view.View; diff --git a/ui/builder/component-builder.ts b/ui/builder/component-builder.ts index 7fbef2f21..b82740b2e 100644 --- a/ui/builder/component-builder.ts +++ b/ui/builder/component-builder.ts @@ -154,7 +154,7 @@ export function getComponentModule(elementName: string, namespace: string, attri return componentModule; } -export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: string) { +export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) { // Note: instanceModule can be null if we are loading custom compnenet with no code-behind. if (isBinding(propertyValue) && instance.bind) { @@ -183,7 +183,7 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports if (!attrHandled && (instance)._applyXmlAttribute) { attrHandled = (instance)._applyXmlAttribute(propertyName, propertyValue); } - if (!attrHandled) { + if (!attrHandled) { instance[propertyName] = convertString(propertyValue); } } @@ -193,7 +193,7 @@ function getBindingExpressionFromAttribute(value: string): string { return value.replace("{{", "").replace("}}", "").trim(); } -function isBinding(value: string): boolean { +function isBinding(value: any): boolean { var isBinding; if (isString(value)) { diff --git a/ui/builder/special-properties.d.ts b/ui/builder/special-properties.d.ts index 66ed2b3be..e233ae523 100644 --- a/ui/builder/special-properties.d.ts +++ b/ui/builder/special-properties.d.ts @@ -1,7 +1,7 @@ declare module "ui/builder/special-properties" { import view = require("ui/core/view"); - export type PropertySetter = (instance: view.View, propertyValue: string) => void; + export type PropertySetter = (instance: view.View, propertyValue: any) => void; export function registerSpecialProperty(name: string, setter: PropertySetter): void; export function getSpecialPropertySetter(name: string): PropertySetter; } diff --git a/ui/builder/special-properties.ts b/ui/builder/special-properties.ts index 83596513d..e8d8b6c74 100644 --- a/ui/builder/special-properties.ts +++ b/ui/builder/special-properties.ts @@ -1,6 +1,6 @@ import view = require("ui/core/view"); -export type PropertySetter = (instance: view.View, propertyValue: string) => void; +export type PropertySetter = (instance: view.View, propertyValue: any) => void; var specialProperties: Map = new Map(); diff --git a/utils/utils-common.ts b/utils/utils-common.ts index d3db424b9..e3e9f30df 100644 --- a/utils/utils-common.ts +++ b/utils/utils-common.ts @@ -34,10 +34,12 @@ export function escapeRegexSymbols(source: string): string { return source.replace(escapeRegex, "\\$&"); } -export function convertString(value: string): any { +export function convertString(value: any): any { var result; - - if (value.trim() === "") { + + if (!types.isString(value)) { + result = value; + } else if (value.trim() === "") { result = value; } else { // Try to convert value to number. @@ -50,7 +52,7 @@ export function convertString(value: string): any { result = value; } } - + return result; } @@ -131,4 +133,4 @@ export function isDataURI(uri: string): boolean { var firstSegment = uri.trim().split(',')[0]; return firstSegment && firstSegment.indexOf("data:") === 0 && firstSegment.indexOf('base64') >= 0; -} \ No newline at end of file +} diff --git a/utils/utils.d.ts b/utils/utils.d.ts index b5381759b..72889d093 100644 --- a/utils/utils.d.ts +++ b/utils/utils.d.ts @@ -240,5 +240,5 @@ * Converts string value to number or boolean. * @param value The original value. */ - export function convertString(value: string): any + export function convertString(value: any): any } From d227c8479b8fddf689a932f4e80feadc450e5b27 Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Tue, 10 May 2016 21:53:32 +0530 Subject: [PATCH 2/7] Add tests for loading attributes in components --- .../xml-declaration/xml-declaration-tests.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 8c316b505..17771211b 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -133,6 +133,25 @@ export function test_loadWithOptionsFromTNSPath() { TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";"); }; +export function test_loadWithAttributes() { + var lText = "Nativescript rocks"; + var lWrap = true; + var lColor = "#FF0000"; // red + var v = builder.load({ + path: "ui/label", + name: "Label", + attributes: { + text: lText, + textWrap: lWrap, + style: "background-color: " + lColor + ";" + } + }); + + TKUnit.assertEqual(v.text, lText, "Expected result: true; Actual result: " + false + ";"); + TKUnit.assertEqual(v.textWrap, lWrap, "Expected result: true; Actual result: " + false + ";"); + TKUnit.assertViewColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); +}; + export function test_parse_ShouldNotCrashWithoutExports() { var file = fs.File.fromPath(fs.path.join(__dirname, "mainPage.xml")); var text = file.readTextSync(); @@ -879,4 +898,4 @@ export function test_hasSourceCodeLocations() { var label = page.getViewById("label"); var labelSource = Source.get(label); TKUnit.assertEqual(labelSource.toString(), "file:///app/" + basePath + "examples/test-page.xml:3:5"); -} \ No newline at end of file +} From 26e73f4e2f93ab736fe301457a231c2413073872 Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Tue, 10 May 2016 22:25:38 +0530 Subject: [PATCH 3/7] Fixed incorrect reference to assertViewColor --- apps/tests/xml-declaration/xml-declaration-tests.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 17771211b..5ac34beb9 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -138,7 +138,7 @@ export function test_loadWithAttributes() { var lWrap = true; var lColor = "#FF0000"; // red var v = builder.load({ - path: "ui/label", + path: "tns_modules/ui/label", name: "Label", attributes: { text: lText, @@ -147,9 +147,10 @@ export function test_loadWithAttributes() { } }); + TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";"); TKUnit.assertEqual(v.text, lText, "Expected result: true; Actual result: " + false + ";"); TKUnit.assertEqual(v.textWrap, lWrap, "Expected result: true; Actual result: " + false + ";"); - TKUnit.assertViewColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); + helper.assertViewColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); }; export function test_parse_ShouldNotCrashWithoutExports() { From 7d5953d57372dc4cce81c5c6eefebbfddc5c5247 Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Tue, 10 May 2016 22:58:28 +0530 Subject: [PATCH 4/7] Using custom component instead of label --- .../xml-declaration/xml-declaration-tests.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 5ac34beb9..eff4369b1 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -135,22 +135,22 @@ export function test_loadWithOptionsFromTNSPath() { export function test_loadWithAttributes() { var lText = "Nativescript rocks"; - var lWrap = true; var lColor = "#FF0000"; // red + var v = builder.load({ - path: "tns_modules/ui/label", - name: "Label", + path: "~/xml-declaration/mymodulewithxml", + name: "MyControl", + page: new Page(), attributes: { - text: lText, - textWrap: lWrap, + customAttribute: { + text: lText + }, style: "background-color: " + lColor + ";" } }); - TKUnit.assert(v instanceof Label, "Expected result: Label; Actual result: " + v + ";"); - TKUnit.assertEqual(v.text, lText, "Expected result: true; Actual result: " + false + ";"); - TKUnit.assertEqual(v.textWrap, lWrap, "Expected result: true; Actual result: " + false + ";"); - helper.assertViewColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); + TKUnit.assertEqual(v.customAttribute.text, lText, "Expected result: true; Actual result: " + v + ";"); + helper.assertViewBackgroundColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); }; export function test_parse_ShouldNotCrashWithoutExports() { From 89701515c6e719dd5af8030600b3f3cd424980a1 Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Tue, 10 May 2016 23:26:16 +0530 Subject: [PATCH 5/7] Trying to work around typescript's error --- apps/tests/xml-declaration/xml-declaration-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index eff4369b1..edb1d3371 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -149,8 +149,8 @@ export function test_loadWithAttributes() { } }); - TKUnit.assertEqual(v.customAttribute.text, lText, "Expected result: true; Actual result: " + v + ";"); - helper.assertViewBackgroundColor(v, lColor, "Expected result: true; Actual result: " + false + ";"); + TKUnit.assertEqual(v["customAttribute"]["text"], lText, "Expected result: true; Actual result: " + v + ";"); + helper.assertViewBackgroundColor(v, lColor); }; export function test_parse_ShouldNotCrashWithoutExports() { From bd48e2da49fd2f9a1d605ed74cc6f317bac7f00c Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Tue, 10 May 2016 23:54:16 +0530 Subject: [PATCH 6/7] Shifting back to label now that I know how to deal with tsc's error --- .../xml-declaration/xml-declaration-tests.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index edb1d3371..bbf7c6a85 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -135,22 +135,22 @@ export function test_loadWithOptionsFromTNSPath() { export function test_loadWithAttributes() { var lText = "Nativescript rocks"; + var lWrap = true; var lColor = "#FF0000"; // red var v = builder.load({ - path: "~/xml-declaration/mymodulewithxml", - name: "MyControl", - page: new Page(), + path: "tns_modules/ui/label", + name: "Label", attributes: { - customAttribute: { - text: lText - }, - style: "background-color: " + lColor + ";" + text: lText, + textWrap: lWrap, + style: "color: " + lColor + ";" } }); - TKUnit.assertEqual(v["customAttribute"]["text"], lText, "Expected result: true; Actual result: " + v + ";"); - helper.assertViewBackgroundColor(v, lColor); + TKUnit.assertEqual(v["text"], lText, "Expected result: true; Actual result: " + v + ";"); + TKUnit.assertEqual(v["textWrap"], true, "Expected result: true; Actual result: " + v + ";"); + helper.assertViewColor(v, lColor); }; export function test_parse_ShouldNotCrashWithoutExports() { From 1e2a991b372b4781c7686dfb92df3bd37475a874 Mon Sep 17 00:00:00 2001 From: Akash Agrawal Date: Wed, 11 May 2016 00:46:34 +0530 Subject: [PATCH 7/7] Reduced adb log level to root out the problem --- build/run-testsapp.grunt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/run-testsapp.grunt.js b/build/run-testsapp.grunt.js index 7ffd381dc..285a3ac7c 100644 --- a/build/run-testsapp.grunt.js +++ b/build/run-testsapp.grunt.js @@ -209,7 +209,7 @@ module.exports = { }, shell: { collectAndroidLog: { - command: "./expect.exp " + "'adb logcat' " + localCfg.outFile, + command: "./expect.exp " + "'adb logcat *:D' " + localCfg.outFile, options: { execOptions: { maxBuffer: Infinity