Skip to content

Commit 06ad345

Browse files
author
Hristo Hristov
authored
Fix issue 4302 (NativeScript#4723)
Fix tests for that issue
1 parent 9bcf4ef commit 06ad345

5 files changed

Lines changed: 58 additions & 73 deletions

File tree

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1-
import * as tabViewModule from "tns-core-modules/ui/tab-view";
1+
import { TabView } from "tns-core-modules/ui/tab-view";
22

3-
export function getNativeTabCount(tabView: tabViewModule.TabView): number {
4-
var pagerAdapter: android.support.v4.view.PagerAdapter = (<any>tabView)._pagerAdapter;
3+
export function getNativeTabCount(tabView: TabView): number {
4+
const pagerAdapter: android.support.v4.view.PagerAdapter = (<any>tabView)._pagerAdapter;
55
return pagerAdapter ? pagerAdapter.getCount() : 0;
66
}
77

8-
export function selectNativeTab(tabView: tabViewModule.TabView, index: number): void {
9-
var viewPager: android.support.v4.view.ViewPager = (<any>tabView)._viewPager;
8+
export function selectNativeTab(tabView: TabView, index: number): void {
9+
const viewPager: android.support.v4.view.ViewPager = (<any>tabView)._viewPager;
1010
if (viewPager) {
1111
viewPager.setCurrentItem(index);
1212
}
1313
}
1414

15-
export function getNativeSelectedIndex(tabView: tabViewModule.TabView): number {
16-
var viewPager: android.support.v4.view.ViewPager = (<any>tabView)._viewPager;
15+
export function getNativeSelectedIndex(tabView: TabView): number {
16+
const viewPager: android.support.v4.view.ViewPager = (<any>tabView)._viewPager;
1717
return viewPager ? viewPager.getCurrentItem() : -1;
1818
}
1919

20-
export function getNativeFont(tabView: tabViewModule.TabView): any {
21-
var tv: android.widget.TextView = (<org.nativescript.widgets.TabLayout>(<any>tabView)._tabLayout).getTextViewForItemAt(0);
22-
if (tv) {
23-
return {
24-
typeface: tv.getTypeface(),
25-
size: tv.getTextSize()
26-
}
27-
}
28-
else {
29-
return null;
20+
export function getNativeFont(tabView: TabView): any {
21+
const tv: android.widget.TextView = (<org.nativescript.widgets.TabLayout>(<any>tabView)._tabLayout).getTextViewForItemAt(0);
22+
if (tv) {
23+
return {
24+
typeface: tv.getTypeface(),
25+
size: tv.getTextSize()
3026
}
27+
}
28+
29+
return null;
3130
}
31+
32+
export function getOriginalFont(tabView: TabView): any {
33+
const tv: android.widget.TextView = (<org.nativescript.widgets.TabLayout>(<any>tabView)._tabLayout).getTextViewForItemAt(0);
34+
return {
35+
typeface: tv.getTypeface(),
36+
size: tv.getTextSize()
37+
}
38+
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//@private
2-
import * as tabViewModule from "tns-core-modules/ui/tab-view";
2+
import { TabView } from "tns-core-modules/ui/tab-view";
33

4-
export declare function getNativeTabCount(tabView: tabViewModule.TabView): number;
5-
export declare function selectNativeTab(tabView: tabViewModule.TabView, index: number): void;
6-
export declare function getNativeSelectedIndex(tabView: tabViewModule.TabView): number;
7-
export declare function getNativeFont(tabView: tabViewModule.TabView): any;
4+
export function getNativeTabCount(tabView: TabView): number;
5+
export function selectNativeTab(tabView: TabView, index: number): void;
6+
export function getNativeSelectedIndex(tabView: TabView): number;
7+
export function getNativeFont(tabView: TabView): any;
8+
export function getOriginalFont(tabView: TabView): any;

tests/app/ui/tab-view/tab-view-tests-native.ios.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@ export function getNativeSelectedIndex(tabView: tabViewModule.TabView): number {
1919
return tabView.ios.selectedIndex;
2020
}
2121

22-
export function getNativeFont(tabView: tabViewModule.TabView): any {
23-
let tabBar = <UITabBar>tabView.ios.tabBar;
24-
let currentFont;
25-
22+
export function getNativeFont(tabView: tabViewModule.TabView): UIFont {
23+
const tabBar = <UITabBar>tabView.ios.tabBar;
2624
if (tabBar.items.count > 0) {
27-
let currentAttrs = tabBar.items[0].titleTextAttributesForState(UIControlState.Normal);
25+
const currentAttrs = tabBar.items[0].titleTextAttributesForState(UIControlState.Normal);
2826
if (currentAttrs) {
29-
currentFont = currentAttrs.objectForKey(NSFontAttributeName);
27+
return currentAttrs.objectForKey(NSFontAttributeName);
3028
}
3129
}
3230

33-
if (!currentFont) {
34-
currentFont = UIFont.systemFontOfSize(getter(UIFont, UIFont.labelFontSize));
35-
}
36-
37-
return currentFont;
31+
return null;
3832
}
33+
34+
export function getOriginalFont(tabView: tabViewModule.TabView): UIFont {
35+
return tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(10));
36+
}

tests/app/ui/tab-view/tab-view-tests.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -290,54 +290,35 @@ export class TabViewTest extends testModule.UITest<tabViewModule.TabView> {
290290
}
291291

292292
public test_FontIsReappliedWhenTabItemsChange = function () {
293-
// let fontToString = (font: any): string => {
294-
// if (this.testView.ios){
295-
// return font.toString();
296-
// }
297-
// else {
298-
// return `${font.typeface} ${font.size}`;
299-
// }
300-
// }
301-
302-
let assertFontsAreEqual = (actual: any, expected: any, message?: string) => {
293+
const assertFontsAreEqual = (actual: any, expected: any, message?: string) => {
303294
if (this.testView.ios) {
304295
TKUnit.assertEqual(actual, expected, message);
305-
}
306-
else {
296+
} else {
307297
TKUnit.assertEqual(actual.typeface, expected.typeface, `${message} [typeface]`);
308298
TKUnit.assertEqual(actual.size, expected.size, `${message} [size]`);
309299
}
310300
}
311301

312-
//console.log(`>>>>>>>>>>>>> CREATE 3 ITEMS`);
313302
this.testView.items = this._createItems(1);
314303
this.waitUntilTestElementIsLoaded();
315304

316-
let originalFont = tabViewTestsNative.getNativeFont(this.testView);
317-
//console.log(`>>>>>>>>>>>>> originalFont: ${fontToString(originalFont)}`);
318-
let nativeFont: any;
305+
const originalFont = tabViewTestsNative.getOriginalFont(this.testView);
306+
TKUnit.assertNotNull(originalFont, "Original Font should be applied");
319307

320-
//console.log(`>>>>>>>>>>>>> PACIFICO`);
321308
this.testView.style.font = "20 Pacifico";
322-
nativeFont = tabViewTestsNative.getNativeFont(this.testView);
323-
//console.log(`>>>>>>>>>>>>> nativeFont: ${fontToString(nativeFont)}`);
309+
let nativeFont = tabViewTestsNative.getNativeFont(this.testView);
310+
TKUnit.assertNotNull(nativeFont, "Native Font should not be null");
311+
TKUnit.assertNotEqual(originalFont, nativeFont, "Font should be changed");
324312

325-
//console.log(`>>>>>>>>>>>>> CREATE 3 ITEMS`);
326313
this.testView.items = this._createItems(2);
327314
assertFontsAreEqual(tabViewTestsNative.getNativeFont(this.testView), nativeFont, "Font must be 20 Pacifico after rebinding items.");
328-
//console.log(`>>>>>>>>>>>>> nativeFont: ${fontToString(nativeFont)}`);
329315

330-
//console.log(`>>>>>>>>>>>>> MONOSPACE;`);
331316
this.testView.style.font = "bold 12 monospace";
332317
nativeFont = tabViewTestsNative.getNativeFont(this.testView);
333-
//console.log(`>>>>>>>>>>>>> nativeFont: ${fontToString(nativeFont)}`);
334318

335-
//console.log(`>>>>>>>>>>>>> CREATE 3 ITEMS`);
336319
this.testView.items = this._createItems(3);
337320
assertFontsAreEqual(tabViewTestsNative.getNativeFont(this.testView), nativeFont, "Font must be bold 12 monospace after rebinding items.");
338-
//console.log(`>>>>>>>>>>>>> nativeFont: ${fontToString(nativeFont)}`);
339321

340-
//console.log(`>>>>>>>>>>>>> RESET`);
341322
this.testView.style.font = unsetValue;
342323
assertFontsAreEqual(tabViewTestsNative.getNativeFont(this.testView), originalFont, "Font must be the original one after resetting the style.");
343324
}

tns-core-modules/ui/tab-view/tab-view.ios.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function updateItemTitlePosition(tabBarItem: UITabBarItem): void {
120120
}
121121

122122
function updateItemIconPosition(tabBarItem: UITabBarItem): void {
123-
tabBarItem.imageInsets = new UIEdgeInsets({top: 6, left: 0, bottom: -6, right: 0});
123+
tabBarItem.imageInsets = new UIEdgeInsets({ top: 6, left: 0, bottom: -6, right: 0 });
124124
}
125125

126126
export class TabViewItem extends TabViewItemBase {
@@ -130,7 +130,7 @@ export class TabViewItem extends TabViewItemBase {
130130
this._iosViewController = controller;
131131
this.setNativeView((<any>this)._nativeView = controller.view);
132132
}
133-
133+
134134
public disposeNativeView() {
135135
this._iosViewController = undefined;
136136
this.setNativeView(undefined);
@@ -462,20 +462,18 @@ function getTitleAttributesForStates(tabView: TabView): TabStates {
462462
const result: TabStates = {};
463463

464464
const font = tabView.style.fontInternal.getUIFont(UIFont.systemFontOfSize(10));
465-
let tabItemTextColor = tabView.style.tabTextColor;
466-
if (tabItemTextColor instanceof Color) {
467-
result.normalState = {
468-
[UITextAttributeTextColor]: tabItemTextColor.ios,
469-
[NSFontAttributeName]: font
470-
}
465+
const tabItemTextColor = tabView.style.tabTextColor;
466+
const textColor = tabItemTextColor instanceof Color ? tabItemTextColor.ios : null;
467+
result.normalState = { [NSFontAttributeName]: font }
468+
if (textColor) {
469+
result.normalState[UITextAttributeTextColor] = textColor
471470
}
472471

473472
const tabSelectedItemTextColor = tabView.style.selectedTabTextColor;
474-
if (tabSelectedItemTextColor instanceof Color) {
475-
result.selectedState = {
476-
[UITextAttributeTextColor]: tabSelectedItemTextColor.ios,
477-
[NSFontAttributeName]: font
478-
}
473+
const selectedTextColor = tabItemTextColor instanceof Color ? tabSelectedItemTextColor.ios : null;
474+
result.selectedState = { [NSFontAttributeName]: font }
475+
if (selectedTextColor) {
476+
result.selectedState[UITextAttributeTextColor] = selectedTextColor
479477
}
480478

481479
return result;
@@ -484,4 +482,4 @@ function getTitleAttributesForStates(tabView: TabView): TabStates {
484482
function applyStatesToItem(item: UITabBarItem, states: TabStates) {
485483
item.setTitleTextAttributesForState(states.normalState, UIControlState.Normal);
486484
item.setTitleTextAttributesForState(states.selectedState, UIControlState.Selected);
487-
}
485+
}

0 commit comments

Comments
 (0)