Skip to content

Commit fe646be

Browse files
committed
fix(android): hold the TextBase from its text transformation weakly
A TextView holds the TextTransformationImpl that text-transform and a Button's formatted text install (TextView.mTransformation), and the transformation held its TextBase in a strong textBase property, which reaches the TextView again through the view's native view. Only tearing the view down broke that cycle, so a view that is never torn down, such as the previous root after Application.resetRootView(), stayed alive once wrapper resurrection no longer collected it. The transformation now holds the TextBase through a WeakRef and returns the text unchanged once it is gone.
1 parent 7668c2b commit fe646be

3 files changed

Lines changed: 90 additions & 7 deletions

File tree

‎apps/automated/src/ui/root-view/reset-root-view-tests.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as TKUnit from '../../tk-unit';
22
import * as helper from '../../ui-helper';
3-
import { Page, isAndroid, Frame, NavigationEntry, Application, TabView, TabViewItem } from '@nativescript/core';
3+
import { Page, isAndroid, Frame, NavigationEntry, Application, TabView, TabViewItem, Label, Button, StackLayout, FormattedString, Span } from '@nativescript/core';
44

55
function waitUntilTabViewReady(page: Page, action: Function) {
66
helper.waitUntilNavigatedTo(page, action);
@@ -144,6 +144,31 @@ export function test_reset_during_tab_index_change() {
144144
TKUnit.assertTrue(true);
145145
}
146146

147+
export function test_reset_layout_with_transformed_text_to_frame() {
148+
const label = new Label();
149+
label.text = 'label';
150+
label.textTransform = 'uppercase';
151+
152+
const formattedText = new FormattedString();
153+
const span = new Span();
154+
span.text = 'button';
155+
formattedText.spans.push(span);
156+
const button = new Button();
157+
button.formattedText = formattedText;
158+
159+
const layoutRoot = new StackLayout();
160+
layoutRoot.addChild(label);
161+
layoutRoot.addChild(button);
162+
163+
Application.resetRootView({ create: () => layoutRoot });
164+
TKUnit.waitUntilReady(() => layoutRoot.isLoaded);
165+
TKUnit.assertEqual(Application.getRootView(), layoutRoot);
166+
167+
const testFrameRoot = createTestFrameRootEntry();
168+
helper.waitUntilNavigatedTo(testFrameRoot.page, () => Application.resetRootView(testFrameRoot.entry));
169+
TKUnit.assertEqual(Application.getRootView(), testFrameRoot.root);
170+
}
171+
147172
export function tearDownModule() {
148173
// reset the root to frame for other tests
149174
const resetFrameRoot = createTestFrameRootEntry();

‎apps/automated/src/ui/text-base/text-base-tests.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as TKUnit from '../../tk-unit';
22
import * as helper from '../../ui-helper';
3-
import { FormattedString, Label, Span, StackLayout } from '@nativescript/core';
3+
import { Button, FormattedString, Label, Span, StackLayout, Utils } from '@nativescript/core';
44

55
export function test_native_font_size_with_a11y_font_scale() {
66
if (__APPLE__) {
@@ -21,3 +21,51 @@ export function test_native_font_size_with_a11y_font_scale() {
2121
TKUnit.assertEqual(nativeFontSize, expectedNativeFontSize, 'View font size does not respect a11y font scaling');
2222
}
2323
}
24+
25+
function collectJsAndJavaGarbage() {
26+
for (let i = 0; i < 3; i++) {
27+
Utils.GC();
28+
java.lang.System.gc();
29+
java.lang.System.runFinalization();
30+
TKUnit.wait(0.05);
31+
}
32+
}
33+
34+
function displayedText(view: Label | Button): string {
35+
const nativeView = <android.widget.TextView>view.nativeTextViewProtected;
36+
37+
return nativeView.getTransformationMethod().getTransformation(nativeView.getText(), nativeView).toString();
38+
}
39+
40+
export function test_android_text_transform_survives_garbage_collection() {
41+
if (!__ANDROID__) {
42+
return;
43+
}
44+
45+
const label = new Label();
46+
label.text = 'label';
47+
label.textTransform = 'uppercase';
48+
49+
const span = new Span();
50+
span.text = 'button';
51+
const formattedText = new FormattedString();
52+
formattedText.spans.push(span);
53+
const button = new Button();
54+
button.formattedText = formattedText;
55+
56+
const layout = new StackLayout();
57+
layout.addChild(label);
58+
layout.addChild(button);
59+
helper.getCurrentPage().content = layout;
60+
TKUnit.waitUntilReady(() => layout.isLoaded);
61+
62+
collectJsAndJavaGarbage();
63+
label.text = 'changed';
64+
span.text = 'span changed';
65+
label.nativeViewProtected.requestLayout();
66+
button.nativeViewProtected.requestLayout();
67+
TKUnit.wait(0.05);
68+
69+
TKUnit.assertEqual(displayedText(label), 'CHANGED', 'label text-transform after garbage collection');
70+
TKUnit.assertEqual(displayedText(button), 'span changed', 'button formatted text after garbage collection');
71+
}

‎packages/core/ui/text-base/index.android.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,30 @@ function initializeTextTransformation(): void {
3232
@NativeClass
3333
@Interfaces([android.text.method.TransformationMethod])
3434
class TextTransformationImpl extends java.lang.Object implements android.text.method.TransformationMethod {
35-
constructor(public textBase: TextBase) {
35+
// Weak: the native text view holds this transformation, so a strong owner would be
36+
// a cycle that only tearing down the view breaks.
37+
public textBase: WeakRef<TextBase>;
38+
39+
constructor(textBase: TextBase) {
3640
super();
41+
this.textBase = new WeakRef(textBase);
3742
}
3843

3944
public getTransformation(charSeq: any, view: android.view.View): any {
45+
const textBase = this.textBase.deref();
46+
if (!textBase) {
47+
return charSeq;
48+
}
49+
4050
// NOTE: Do we need to transform the new text here?
41-
const formattedText = this.textBase.formattedText;
51+
const formattedText = textBase.formattedText;
4252
if (formattedText) {
43-
return this.textBase.createFormattedTextNative(formattedText);
53+
return textBase.createFormattedTextNative(formattedText);
4454
} else {
45-
const text = this.textBase.text;
55+
const text = textBase.text;
4656
const stringValue = isNullOrUndefined(text) ? '' : text.toString();
4757

48-
return getTransformedText(stringValue, this.textBase.textTransform);
58+
return getTransformedText(stringValue, textBase.textTransform);
4959
}
5060
}
5161

0 commit comments

Comments
 (0)