Skip to content

Conversation

@zhongqiliang
Copy link
Contributor

@zhongqiliang zhongqiliang commented Oct 31, 2025

This change introduces unit tests for the CobaltService.sendToClient method
on Android. The tests verify that JavaScript code is consistently formatted
using Locale.US, preventing issues with locale-dependent number formatting.
Additionally, it ensures that the method handles a null CobaltActivity
gracefully without causing crashes.

Test log: https://paste.googleplex.com/6270902119563264

Bug: 455895170

Change-Id: I8811cad1da6d8d9016b3bf70e3ab09a7bd7a9899

@zhongqiliang
Copy link
Contributor Author

/generate-commit-message

@github-actions
Copy link

🤖 Gemini Suggested Commit Message


android: Add unit tests for CobaltService

This change introduces unit tests for the CobaltService.sendToClient method
on Android. The tests verify that JavaScript code is consistently formatted
using Locale.US, preventing issues with locale-dependent number formatting.
Additionally, it ensures that the method handles a null CobaltActivity
gracefully without causing crashes.

Bug: 455895170

@zhongqiliang zhongqiliang changed the title Unit test android: Add unit tests for CobaltService Oct 31, 2025
@zhongqiliang zhongqiliang marked this pull request as ready for review October 31, 2025 19:09
@zhongqiliang zhongqiliang requested a review from a team as a code owner October 31, 2025 19:09
@zhongqiliang zhongqiliang enabled auto-merge (squash) October 31, 2025 19:09
Change-Id: I8811cad1da6d8d9016b3bf70e3ab09a7bd7a9899
@zhongqiliang
Copy link
Contributor Author

Remove Locale.US in CobaltService.java, the test will fail.

Copy link
Contributor

@yell0wd0g yell0wd0g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -0,0 +1,167 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Remove empty line?

Comment on lines +92 to +100
String jsCodeTemplate =
"((w) => {\n"
+ " let targetWindow = w;\n"
+ " const appIframe = document.getElementById('anchor');\n"
+ " if (appIframe?.contentWindow) {\n"
+ " targetWindow = appIframe.contentWindow;\n"
+ " }\n"
+ " targetWindow.H5vccPlatformService.callbackFromAndroid(%d, '%s');\n"
+ "})(window)";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually one question - does it matter this content, as long as it's
valid JS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point. This block has to exactly match the Java Bridge script. Any changes to the original script would result in this test failing.

Copy link
Contributor

@johnxwork johnxwork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good step forward


} finally {
// Restore the original default locale to avoid affecting other tests.
Locale.setDefault(originalLocale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you would want to fail the test if any exception is thrown and the assertEquals doesn't get executed, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants