Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion apps/automated/src/ui/segmented-bar/segmented-bar-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as TKUnit from '../../tk-unit';
import * as segmentedBarTestsNative from './segmented-bar-tests-native';
import { buildUIAndRunTest } from '../../ui-helper';
import { Color, Observable, BindingOptions, View } from '@nativescript/core';
import { Color, Observable, BindingOptions, View, Utils } from '@nativescript/core';
import * as helper from '../../ui-helper';

// >> article-require-segmentedbar-module
Expand Down Expand Up @@ -34,6 +34,31 @@ function _createItems(count: number): Array<segmentedBarModule.SegmentedBarItem>
return items;
}

function collectJsAndJavaGarbage() {
for (let i = 0; i < 3; i++) {
Utils.GC();
if (__ANDROID__) {
java.lang.System.gc();
java.lang.System.runFinalization();
}
TKUnit.wait(0.05);
}
}

export function test_SelectingTabsAfterGarbageCollection_CreatesTheirContent() {
const segmentedBar = _createSegmentedBar();
segmentedBar.items = _createItems(3);
buildUIAndRunTest(segmentedBar, function (views: Array<View>) {
const bar = <segmentedBarModule.SegmentedBar>views[0];
collectJsAndJavaGarbage();
bar.selectedIndex = 2;
collectJsAndJavaGarbage();
bar.selectedIndex = 1;
TKUnit.assertEqual(bar.selectedIndex, 1, 'selectedIndex');
TKUnit.assertEqual(segmentedBarTestsNative.getNativeItemsCount(bar), 3, 'native items');
});
}

export var testWhenSegmentedBarIsCreatedItemsAreUndefined = function () {
buildUIAndRunTest(_createSegmentedBar(), function (views: Array<View>) {
var segmentedBar = <segmentedBarModule.SegmentedBar>views[0];
Expand Down
14 changes: 10 additions & 4 deletions packages/core/ui/segmented-bar/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SegmentedBarItemBase, SegmentedBarBase, selectedIndexProperty, itemsPro
import { AndroidHelper, isEnabledProperty } from '../core/view';
import { colorProperty, fontInternalProperty, fontSizeProperty } from '../styling/style-properties';
import { Color } from '../../color';
import { layout } from '../../utils';
import { android as androidUtils, layout } from '../../utils';
import { SDK_VERSION } from '../../utils/constants';
import { Trace } from '../../trace';
import { CoreTypes } from '../../core-types';
Expand Down Expand Up @@ -65,13 +65,19 @@ function initializeNativeClasses(): void {
@NativeClass
@Interfaces([android.widget.TabHost.TabContentFactory])
class TabContentFactoryImpl extends java.lang.Object implements android.widget.TabHost.TabContentFactory {
constructor(public owner: SegmentedBar) {
// Weak: the bar holds this factory, so a strong owner would be a cycle
// through the factory's native counterpart.
private owner: WeakRef<SegmentedBar>;

constructor(owner: SegmentedBar) {
super();
this.owner = new WeakRef(owner);
}

createTabContent(tag: string): android.view.View {
const tv = new android.widget.TextView(this.owner._context);
const isRtl = this.owner.direction === CoreTypes.LayoutDirection.rtl;
const owner = this.owner.deref();
const tv = new android.widget.TextView(owner ? owner._context : androidUtils.getApplicationContext());
const isRtl = owner?.direction === CoreTypes.LayoutDirection.rtl;

// This is collapsed by default and made visible
// by android when TabItem becomes visible/selected.
Expand Down
Loading