Fixed several unit-tests

This commit is contained in:
hshristov
2015-07-27 19:20:59 +03:00
parent 41168d3782
commit bd50fb5550
12 changed files with 262 additions and 143 deletions

View File

@@ -217,13 +217,19 @@
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
<TypeScriptCompile Include="apps\tests\gestures-tests.ts" />
<TypeScriptCompile Include="apps\tests\fetch-tests.ts" />
<TypeScriptCompile Include="apps\tests\layouts\layout-helper.d.ts" />
<TypeScriptCompile Include="apps\tests\layouts\layout-helper.ios.ts">
<DependentUpon>layout-helper.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="apps\tests\xhr-tests.ts" />
<TypeScriptCompile Include="apps\tests\layouts\dock-layout-tests.ts" />
<TypeScriptCompile Include="apps\tests\pages\app.ts" />
<TypeScriptCompile Include="apps\tests\pages\file-load-test.ts" />
<TypeScriptCompile Include="apps\tests\pages\page12.ts" />
<TypeScriptCompile Include="apps\tests\layouts\absolute-layout-tests.ts" />
<TypeScriptCompile Include="apps\tests\layouts\layout-helper.ts" />
<TypeScriptCompile Include="apps\tests\layouts\layout-helper.android.ts">
<DependentUpon>layout-helper.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="apps\tests\layouts\wrap-layout-tests.ts" />
<TypeScriptCompile Include="apps\tests\pages\page12.ts" />
<TypeScriptCompile Include="apps\tests\pages\page16.ts" />

5
android17.d.ts vendored
View File

@@ -117007,6 +117007,11 @@ declare module android {
MinimumHeight: number;
DrawingCacheQuality: number;
toString(): string;
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void;
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void;
/**
* Returns the size of the vertical faded edges used to indicate that more content in this view is visible.
*/

View File

@@ -0,0 +1,120 @@
import button = require("ui/button");
import utils = require("utils/utils");
import TKUnit = require("../TKUnit");
var DELTA = 0.1;
class NativeButton extends android.widget.Button {
private owner: MyButton;
constructor(context: android.content.Context, owner: MyButton) {
super(context);
this.owner = owner;
return global.__native(this);
}
protected onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.owner._measureWidth = utils.layout.getMeasureSpecSize(widthMeasureSpec);
this.owner._measureHeight = utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.owner.measureCount++;
}
protected onLayout(changed: boolean, left: number, top: number, right: number, bottom: number): void {
this.owner._layoutLeft = left;
this.owner._layoutTop = top;
this.owner._layoutWidth = right - left;
this.owner._layoutHeight = bottom - top;
super.onLayout(changed, left, top, right, bottom);
this.owner.arrangeCount++;
}
}
export class MyButton extends button.Button {
public measureCount: number = 0;
public arrangeCount: number = 0;
_layoutLeft;
_layoutTop;
_layoutWidth;
_layoutHeight;
_measureWidth;
_measureHeight;
public get measured(): boolean {
return this.measureCount > 0;
}
public get arranged(): boolean {
return this.arrangeCount > 0;
}
private _layout: NativeButton;
get android(): NativeButton {
return this._layout;
}
get _nativeView(): NativeButton {
return this._layout;
}
public _createUI() {
this._layout = new NativeButton(this._context, this);
}
get measureHeight(): number {
return this._measureHeight;
}
get measureWidth(): number {
return this._measureWidth;
}
get layoutWidth(): number {
return this._layoutWidth;
}
get layoutHeight(): number {
return this._layoutHeight;
}
get layoutLeft(): number {
return this._layoutLeft;
}
get layoutTop(): number {
return this._layoutTop;
}
}
export function assertMeasure(btn: MyButton, width: number, height: number, name?: string) {
var density = utils.layout.getDisplayDensity();
var delta = Math.floor(density) !== density ? 1.1 : DELTA;
name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(Math.floor(btn.measureWidth / density), width, delta, name + "width");
TKUnit.assertAreClose(Math.floor(btn.measureHeight / density), height, delta, name + "height");
}
export function assertLayout(btn: MyButton, left: number, top: number, width: number, height: number, name?: string): void {
var density = utils.layout.getDisplayDensity();
var delta = Math.floor(density) !== density ? 1.1 : DELTA;
name = name ? "[" + name + "]" : "";
TKUnit.assertAreClose(Math.floor(btn.layoutLeft / density), left, delta, name + "left");
TKUnit.assertAreClose(Math.floor(btn.layoutTop / density), top, delta, name + "top");
TKUnit.assertAreClose(Math.floor(btn.layoutWidth / density), width, delta, name + "width");
TKUnit.assertAreClose(Math.floor(btn.layoutHeight / density), height, delta, name + "height");
}
export function dip(value: number): number {
var density = utils.layout.getDisplayDensity();
return Math.floor(value * density);
}

View File

@@ -217,7 +217,7 @@ export function test_insertChildAtPosition() {
let newChild = new Button();
newChild.text = 'in-between';
rootLayout.insertChild(1, newChild);
rootLayout.insertChild(newChild, 1);
assertChildTexts("btn1|in-between|btn2", rootLayout, "button inserted at correct location");
}

View File

@@ -777,7 +777,7 @@ export class View extends proxy.ProxyObject implements definition.View {
* Core logic for adding a child view to this instance. Used by the framework to handle lifecycle events more centralized. Do not outside the UI Stack implementation.
* // TODO: Think whether we need the base Layout routine.
*/
public _addView(view: View) {
public _addView(view: View, atIndex?: number) {
if (!view) {
throw new Error("Expecting a valid View instance.");
}
@@ -787,7 +787,7 @@ export class View extends proxy.ProxyObject implements definition.View {
}
view._parent = this;
this._addViewCore(view);
this._addViewCore(view, atIndex);
trace.write("called _addView on view " + this._domId + " for a child " + view._domId, trace.categories.ViewHierarchy);
}
@@ -795,13 +795,13 @@ export class View extends proxy.ProxyObject implements definition.View {
/**
* Method is intended to be overridden by inheritors and used as "protected"
*/
public _addViewCore(view: View) {
public _addViewCore(view: View, atIndex?: number) {
this._propagateInheritableProperties(view);
view.style._inheritStyleProperties();
if (!view._isAddedToNativeVisualTree) {
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view);
view._isAddedToNativeVisualTree = this._addViewToNativeVisualTree(view, atIndex);
}
// TODO: Discuss this.
@@ -874,7 +874,7 @@ export class View extends proxy.ProxyObject implements definition.View {
/**
* Method is intended to be overridden by inheritors and used as "protected".
*/
public _addViewToNativeVisualTree(view: View): boolean {
public _addViewToNativeVisualTree(view: View, atIndex?: number): boolean {
if (view._isAddedToNativeVisualTree) {
throw new Error("Child already added to the native visual tree.");
}

View File

@@ -5,6 +5,7 @@ import utils = require("utils/utils");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import gestures = require("ui/gestures");
import types = require("utils/types");
global.moduleMerge(viewCommon, exports);
@@ -336,11 +337,16 @@ export class CustomLayoutView extends View implements viewDefinition.CustomLayou
this._viewGroup = new org.nativescript.widgets.ContentLayout(this._context);
}
public _addViewToNativeVisualTree(child: View): boolean {
public _addViewToNativeVisualTree(child: View, atIndex?: number): boolean {
super._addViewToNativeVisualTree(child);
if (this._nativeView && child._nativeView) {
if (types.isNullOrUndefined(atIndex) || atIndex >= this._nativeView.getChildCount()) {
this._nativeView.addView(child._nativeView);
}
else {
this._nativeView.addView(child._nativeView, atIndex);
}
return true;
}

4
ui/core/view.d.ts vendored
View File

@@ -387,7 +387,7 @@ declare module "ui/core/view" {
onUnloaded(): void;
isLoaded: boolean;
_addView(view: View);
_addView(view: View, atIndex?: number);
_propagateInheritableProperties(view: View)
_inheritProperties(parentView: View)
_removeView(view: View);
@@ -407,7 +407,7 @@ declare module "ui/core/view" {
/**
* Performs the core logic of adding a child view to the native visual tree. Returns true if the view's native representation has been successfully added, false otherwise.
*/
_addViewToNativeVisualTree(view: View): boolean;
_addViewToNativeVisualTree(view: View, atIndex?: number): boolean;
_removeViewFromNativeVisualTree(view: View): void;
_eachChildView(callback: (child: View) => boolean);

View File

@@ -4,6 +4,7 @@ import utils = require("utils/utils");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import background = require("ui/styling/background");
import types = require("utils/types");
global.moduleMerge(viewCommon, exports);
@@ -251,11 +252,17 @@ export class CustomLayoutView extends View {
trace.write(this + " :onMeasure: " + utils.layout.getMode(widthMode) + " " + width + ", " + utils.layout.getMode(heightMode) + " " + height, trace.categories.Layout);
}
public _addViewToNativeVisualTree(child: View): boolean {
public _addViewToNativeVisualTree(child: View, atIndex: number): boolean {
super._addViewToNativeVisualTree(child);
if (this._nativeView && child._nativeView) {
if (types.isNullOrUndefined(atIndex) || atIndex >= this._nativeView.subviews.count) {
this._nativeView.addSubview(child._nativeView);
}
else {
this._nativeView.insertSubviewAtIndex(child._nativeView, atIndex);
}
return true;
}

View File

@@ -20,12 +20,25 @@
*/
getChildAt(index: number): view.View;
/**
* Returns the position of the child view
* @param child The child view that we are looking for.
*/
getChildIndex(child: view.View): number;
/**
* Adds the view to children array.
* @param view The view to be added to the end of the children array.
*/
addChild(view: view.View);
/**
* Inserts the view to children array at the specified index.
* @param view The view to be added to the end of the children array.
* @param atIndex The insertion index.
*/
insertChild(child: view.View, atIndex: number);
/**
* Removes the specified view from the children array.
* @param view The view to remove from the children array.

View File

@@ -43,8 +43,8 @@ export class LayoutBase extends view.CustomLayoutView implements definition.Layo
this._subViews.push(child);
}
public insertChild(atIndex: number, child: view.View) {
this._addView(child);
public insertChild(child: view.View, atIndex: number) {
this._addView(child, atIndex);
this._subViews.splice(atIndex, 0, child);
}

View File

@@ -188,6 +188,38 @@ function onBackgroundImagePropertyChanged(data: observable.PropertyChangeData) {
}
}
function onBackgroundColorPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (!color.Color.equals(currentBackground.color, data.newValue)) {
style._setValue(backgroundInternalProperty, currentBackground.withColor(data.newValue));
}
}
function onBackgroundSizePropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.size) {
style._setValue(backgroundInternalProperty, currentBackground.withSize(data.newValue));
}
}
function onBackgroundRepeatPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.repeat) {
style._setValue(backgroundInternalProperty, currentBackground.withRepeat(data.newValue));
}
}
function onBackgroundPositionPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.position) {
style._setValue(backgroundInternalProperty, currentBackground.withPosition(data.newValue));
}
}
function getHandlerInternal(propertyId: number, classInfo: types.ClassInfo): styling.stylers.StylePropertyChangedHandler {
var className = classInfo ? classInfo.name : "default";
var handlerKey = className + propertyId;
@@ -240,6 +272,61 @@ function isOpacityValid(value: string): boolean {
return !isNaN(parsedValue) && 0 <= parsedValue && parsedValue <= 1;
}
function isFontWeightValid(value: string): boolean {
return value === enums.FontWeight.normal || value === enums.FontWeight.bold;
}
function isFontStyleValid(value: string): boolean {
return value === enums.FontStyle.normal || value === enums.FontStyle.italic;
}
function onFontFamilyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontFamily !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontFamily(data.newValue));
}
}
function onFontStyleChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontStyle !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontStyle(data.newValue));
}
}
function onFontWeightChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontWeight !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontWeight(data.newValue));
}
}
function onFontSizeChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontSize !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontSize(data.newValue));
}
}
function onFontChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var newFont = font.Font.parse(data.newValue);
var valueSource = style._getValueSource(fontProperty);
style._setValue(fontFamilyProperty, newFont.fontFamily, valueSource);
style._setValue(fontStyleProperty, newFont.fontStyle, valueSource);
style._setValue(fontWeightProperty, newFont.fontWeight, valueSource);
style._setValue(fontSizeProperty, newFont.fontSize, valueSource);
}
export class Style extends observable.DependencyObservable implements styling.Style {
private _view: view.View;
private _updateCounter = 0;
@@ -671,77 +758,6 @@ export var borderRadiusProperty = new styleProperty.Property("borderRadius", "bo
export var backgroundInternalProperty = new styleProperty.Property("_backgroundInternal", "_backgroundInternal",
new observable.PropertyMetadata(background.Background.default, observable.PropertyMetadataSettings.None, undefined, undefined, background.Background.equals));
function onBackgroundImagePropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var url: string = data.newValue;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
var isValid = false;
if (types.isString(data.newValue)) {
var pattern: RegExp = /url\(('|")(.*?)\1\)/;
var match = url.match(pattern);
if (match && match[2]) {
url = match[2];
}
if (utils.isDataURI(url)) {
var base64Data = url.split(",")[1];
if (types.isDefined(base64Data)) {
style._setValue(backgroundInternalProperty, currentBackground.withImage(imageSource.fromBase64(base64Data)));
isValid = true;
}
} else if (utils.isFileOrResourcePath(url)) {
style._setValue(backgroundInternalProperty, currentBackground.withImage(imageSource.fromFileOrResource(url)));
isValid = true;
} else if (url.indexOf("http") !== -1) {
style["_url"] = url;
style._setValue(backgroundInternalProperty, currentBackground.withImage(undefined));
imageSource.fromUrl(url).then((r) => {
if (style && style["_url"] === url) {
style._setValue(backgroundInternalProperty, currentBackground.withImage(r));
}
});
isValid = true;
}
}
if (!isValid) {
style._setValue(backgroundInternalProperty, currentBackground.withImage(undefined));
}
}
function onBackgroundColorPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (!color.Color.equals(currentBackground.color, data.newValue)) {
style._setValue(backgroundInternalProperty, currentBackground.withColor(data.newValue));
}
}
function onBackgroundSizePropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.size) {
style._setValue(backgroundInternalProperty, currentBackground.withSize(data.newValue));
}
}
function onBackgroundRepeatPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.repeat) {
style._setValue(backgroundInternalProperty, currentBackground.withRepeat(data.newValue));
}
}
function onBackgroundPositionPropertyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentBackground = <background.Background>style._getValue(backgroundInternalProperty);
if (data.newValue !== currentBackground.position) {
style._setValue(backgroundInternalProperty, currentBackground.withPosition(data.newValue));
}
}
export var fontProperty = new styleProperty.Property("font", "font",
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, onFontChanged));
@@ -760,61 +776,6 @@ export var fontWeightProperty = new styleProperty.Property("fontWeight", "font-w
export var fontInternalProperty = new styleProperty.Property("_fontInternal", "_fontInternal",
new observable.PropertyMetadata(font.Font.default, AffectsLayout, null, null, font.Font.equals), font.Font.parse);
function isFontWeightValid(value: string): boolean {
return value === enums.FontWeight.normal || value === enums.FontWeight.bold;
}
function isFontStyleValid(value: string): boolean {
return value === enums.FontStyle.normal || value === enums.FontStyle.italic;
}
function onFontFamilyChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontFamily !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontFamily(data.newValue));
}
}
function onFontStyleChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontStyle !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontStyle(data.newValue));
}
}
function onFontWeightChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontWeight !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontWeight(data.newValue));
}
}
function onFontSizeChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var currentFont = <font.Font>style._getValue(fontInternalProperty);
if (currentFont.fontSize !== data.newValue) {
style._setValue(fontInternalProperty, currentFont.withFontSize(data.newValue));
}
}
function onFontChanged(data: observable.PropertyChangeData) {
var style = <Style>data.object;
var newFont = font.Font.parse(data.newValue);
var valueSource = style._getValueSource(fontProperty);
style._setValue(fontFamilyProperty, newFont.fontFamily, valueSource);
style._setValue(fontStyleProperty, newFont.fontStyle, valueSource);
style._setValue(fontWeightProperty, newFont.fontWeight, valueSource);
style._setValue(fontSizeProperty, newFont.fontSize, valueSource);
}
export var textAlignmentProperty = new styleProperty.Property("textAlignment", "text-align",
new observable.PropertyMetadata(undefined, AffectsLayout | observable.PropertyMetadataSettings.Inheritable), converters.textAlignConverter);
@@ -868,7 +829,8 @@ export var marginTopProperty = new styleProperty.Property("marginTop", "margin-t
export var marginBottomProperty = new styleProperty.Property("marginBottom", "margin-bottom",
new observable.PropertyMetadata(0, AffectsLayout, onLayoutParamsChanged, isMarginValid), converters.numberConverter);
export var paddingProperty = new styleProperty.Property("padding", "padding", new observable.PropertyMetadata(null, null, onPaddingChanged));
export var paddingProperty = new styleProperty.Property("padding", "padding",
new observable.PropertyMetadata(null, null, onPaddingChanged));
export var paddingLeftProperty = new styleProperty.Property("paddingLeft", "padding-left",
new observable.PropertyMetadata(0, AffectsLayout, onPaddingValueChanged, isPaddingValid), converters.numberConverter);