|
| 1 | +import { assert } from "chai"; |
| 2 | +import { |
| 3 | + DEV_CLEARTEXT_MARKER, |
| 4 | + devCleartextManifest, |
| 5 | + hasDevCleartext, |
| 6 | + needsDevCleartext, |
| 7 | + withDevCleartext, |
| 8 | +} from "../../../lib/services/android/dev-cleartext-manifest"; |
| 9 | + |
| 10 | +describe("dev-cleartext-manifest", () => { |
| 11 | + describe("needsDevCleartext", () => { |
| 12 | + it("is true only for Vite debug HMR runs", () => { |
| 13 | + assert.isTrue( |
| 14 | + needsDevCleartext({ bundler: "vite" }, { hmr: true, release: false }), |
| 15 | + ); |
| 16 | + assert.isFalse( |
| 17 | + needsDevCleartext({ bundler: "vite" }, { hmr: true, release: true }), |
| 18 | + ); |
| 19 | + assert.isFalse( |
| 20 | + needsDevCleartext({ bundler: "vite" }, { hmr: false, release: false }), |
| 21 | + ); |
| 22 | + assert.isFalse( |
| 23 | + needsDevCleartext( |
| 24 | + { bundler: "webpack" }, |
| 25 | + { hmr: true, release: false }, |
| 26 | + ), |
| 27 | + ); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("devCleartextManifest", () => { |
| 32 | + it("permits cleartext and overrides a main manifest's value", () => { |
| 33 | + const manifest = devCleartextManifest(); |
| 34 | + assert.include(manifest, 'android:usesCleartextTraffic="true"'); |
| 35 | + assert.include(manifest, 'tools:replace="android:usesCleartextTraffic"'); |
| 36 | + assert.include( |
| 37 | + manifest, |
| 38 | + 'xmlns:tools="http://schemas.android.com/tools"', |
| 39 | + ); |
| 40 | + assert.isTrue(hasDevCleartext(manifest)); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("hasDevCleartext", () => { |
| 45 | + it("only recognizes a manifest the CLI marked", () => { |
| 46 | + assert.isFalse(hasDevCleartext(null)); |
| 47 | + assert.isFalse( |
| 48 | + hasDevCleartext( |
| 49 | + '<manifest><application android:usesCleartextTraffic="true"/></manifest>', |
| 50 | + ), |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("withDevCleartext", () => { |
| 56 | + it("adds the attribute, tools:replace and the tools namespace to the app's debug manifest", () => { |
| 57 | + const result = withDevCleartext( |
| 58 | + [ |
| 59 | + '<?xml version="1.0" encoding="utf-8"?>', |
| 60 | + '<manifest xmlns:android="http://schemas.android.com/apk/res/android">', |
| 61 | + '\t<application android:debuggable="true">', |
| 62 | + '\t\t<activity android:name="a.B"/>', |
| 63 | + "\t</application>", |
| 64 | + "</manifest>", |
| 65 | + ].join("\n"), |
| 66 | + ); |
| 67 | + assert.include(result, 'xmlns:tools="http://schemas.android.com/tools"'); |
| 68 | + assert.match( |
| 69 | + result, |
| 70 | + /<application tools:replace="android:usesCleartextTraffic" android:usesCleartextTraffic="true" android:debuggable="true">/, |
| 71 | + ); |
| 72 | + assert.include(result, '<activity android:name="a.B"/>'); |
| 73 | + assert.include(result, DEV_CLEARTEXT_MARKER); |
| 74 | + assert.match(result, /^<\?xml/); |
| 75 | + }); |
| 76 | + |
| 77 | + it("turns an explicit false into true and extends an existing tools:replace", () => { |
| 78 | + const result = withDevCleartext( |
| 79 | + '<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"><application android:usesCleartextTraffic="false" tools:replace="android:label"/></manifest>', |
| 80 | + ); |
| 81 | + assert.include(result, 'android:usesCleartextTraffic="true"'); |
| 82 | + assert.notInclude(result, 'usesCleartextTraffic="false"'); |
| 83 | + assert.include( |
| 84 | + result, |
| 85 | + 'tools:replace="android:label,android:usesCleartextTraffic"', |
| 86 | + ); |
| 87 | + assert.equal(result.match(/xmlns:tools=/g).length, 1); |
| 88 | + }); |
| 89 | + |
| 90 | + it("adds an application element when the manifest has none", () => { |
| 91 | + const result = withDevCleartext( |
| 92 | + '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n\t<uses-permission android:name="x"/>\n</manifest>', |
| 93 | + ); |
| 94 | + assert.match( |
| 95 | + result, |
| 96 | + /<application android:usesCleartextTraffic="true" tools:replace="android:usesCleartextTraffic" \/>\n<\/manifest>/, |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments