Skip to content

Commit 69497b0

Browse files
test(mobile): cover the Android orientation plugin's launch-time output
Run the plugin's manifest and MainActivity mods behind Expo's built-in orientation plugin, in prebuild's order. The tests check that the portrait lock is removed from the manifest, configChanges still covers rotation and fold/unfold, applyTabletOrientation() runs before super.onCreate, the imports and methods are added once, and a second run changes nothing. Two of the tests fail against the previous plugin. Also soften a comment that claimed phones never draw a landscape frame. The code requests portrait before the content is created; the system splash window on a phone has not been checked.
1 parent 4e76925 commit 69497b0

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎apps/mobile/plugins/withAndroidTabletOrientation.cjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const { AndroidConfig, withAndroidManifest, withMainActivity } = require("expo/c
99
// The manifest itself must stay unlocked. A tablet launched in landscape against a
1010
// portrait manifest starts letterboxed in a portrait-shaped window, and lifting the
1111
// lock in onCreate does not resize it until the user rotates the device. The lock is
12-
// applied before super.onCreate so phones never draw a landscape frame.
12+
// applied before super.onCreate so phones request portrait before the activity's
13+
// content is created.
1314
// FULL_USER allows all four orientations while still respecting the user's
1415
// auto-rotate lock, matching iPad behavior. Foldables change
1516
// smallestScreenWidthDp on fold/unfold without recreating the activity
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { AndroidConfig, XML, type ExportedConfig } from "expo/config-plugins";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import withAndroidTabletOrientation from "./withAndroidTabletOrientation.cjs";
5+
6+
const MANIFEST = `<manifest xmlns:android="http://schemas.android.com/apk/res/android">
7+
<application android:name=".MainApplication">
8+
<activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode|smallestScreenSize" android:exported="true">
9+
<intent-filter>
10+
<action android:name="android.intent.action.MAIN"/>
11+
<category android:name="android.intent.category.LAUNCHER"/>
12+
</intent-filter>
13+
</activity>
14+
</application>
15+
</manifest>`;
16+
17+
// Trimmed from the MainActivity.kt that `expo prebuild` generates for this app.
18+
const MAIN_ACTIVITY = `package com.t3tools.t3code
19+
20+
import android.os.Build
21+
import android.os.Bundle
22+
23+
import com.facebook.react.ReactActivity
24+
25+
class MainActivity : ReactActivity() {
26+
override fun onCreate(savedInstanceState: Bundle?) {
27+
// setTheme(R.style.AppTheme);
28+
super.onCreate(null)
29+
}
30+
31+
override fun getMainComponentName(): String = "main"
32+
}
33+
`;
34+
35+
function modRequest(modName: string) {
36+
return {
37+
platform: "android" as const,
38+
modName,
39+
projectRoot: "/project",
40+
platformProjectRoot: "/project/android",
41+
introspect: false,
42+
};
43+
}
44+
45+
// Registers Expo's built-in orientation plugin after this one, as prebuild does
46+
// for every app plugin. Same-type mods run last-registered-first, so the
47+
// manifest mod sees the lock that `orientation: "portrait"` writes.
48+
function prebuildConfig(): ExportedConfig {
49+
const config = withAndroidTabletOrientation({
50+
name: "T3 Code",
51+
slug: "t3code",
52+
orientation: "portrait",
53+
});
54+
return AndroidConfig.Orientation.withOrientation(config);
55+
}
56+
57+
async function runManifestMod(config: ExportedConfig) {
58+
const result = await config.mods!.android!.manifest!({
59+
...config,
60+
modResults: (await XML.parseXMLAsync(MANIFEST)) as AndroidConfig.Manifest.AndroidManifest,
61+
modRequest: modRequest("manifest"),
62+
modRawConfig: config,
63+
});
64+
return AndroidConfig.Manifest.getMainActivityOrThrow(result.modResults);
65+
}
66+
67+
async function runMainActivityMod(config: ExportedConfig, contents: string) {
68+
const result = await config.mods!.android!.mainActivity!({
69+
...config,
70+
modResults: { contents, language: "kt", path: "/project/MainActivity.kt" },
71+
modRequest: modRequest("mainActivity"),
72+
modRawConfig: config,
73+
});
74+
return result.modResults.contents;
75+
}
76+
77+
function count(haystack: string, needle: string) {
78+
return haystack.split(needle).length - 1;
79+
}
80+
81+
describe("withAndroidTabletOrientation", () => {
82+
it("leaves MainActivity without a manifest orientation lock", async () => {
83+
const mainActivity = await runManifestMod(prebuildConfig());
84+
85+
expect(mainActivity.$["android:screenOrientation"]).toBeUndefined();
86+
// Rotation and fold/unfold must keep reaching onConfigurationChanged
87+
// instead of recreating the activity.
88+
expect(mainActivity.$["android:configChanges"]).toContain(
89+
"orientation|screenSize|screenLayout|uiMode|smallestScreenSize",
90+
);
91+
});
92+
93+
it("requests the orientation before super.onCreate", async () => {
94+
const contents = await runMainActivityMod(prebuildConfig(), MAIN_ACTIVITY);
95+
96+
const onCreate = contents.slice(contents.indexOf("override fun onCreate"));
97+
expect(onCreate.indexOf("applyTabletOrientation()")).toBeLessThan(
98+
onCreate.indexOf("super.onCreate(null)"),
99+
);
100+
expect(contents).toContain(" applyTabletOrientation()\n super.onCreate(null)");
101+
expect(count(contents, "import android.content.pm.ActivityInfo")).toBe(1);
102+
expect(count(contents, "import android.content.res.Configuration")).toBe(1);
103+
expect(count(contents, "override fun onConfigurationChanged")).toBe(1);
104+
expect(count(contents, "private fun applyTabletOrientation")).toBe(1);
105+
});
106+
107+
it("leaves an already patched MainActivity unchanged", async () => {
108+
const once = await runMainActivityMod(prebuildConfig(), MAIN_ACTIVITY);
109+
const twice = await runMainActivityMod(prebuildConfig(), once);
110+
111+
expect(twice).toBe(once);
112+
});
113+
114+
it("fails loudly when the template no longer calls super.onCreate(null)", async () => {
115+
await expect(
116+
runMainActivityMod(
117+
prebuildConfig(),
118+
MAIN_ACTIVITY.replace("super.onCreate(null)", "super.onCreate(savedInstanceState)"),
119+
),
120+
).rejects.toThrow("could not find the super.onCreate call");
121+
});
122+
});

0 commit comments

Comments
 (0)