|
| 1 | +import { AndroidApplicationManager } from "../../mobile/android/android-application-manager"; |
| 2 | +import { Yok } from "../../yok"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import { CommonLoggerStub } from "./stubs"; |
| 5 | +const invalidIdentifier: string = "invalid.identifier"; |
| 6 | + |
| 7 | +class AndroidDebugBridgeStub { |
| 8 | + public startedWithActivityManager: Boolean = false; |
| 9 | + public validIdentifierPassed: Boolean = false; |
| 10 | + public static methodCallCount: number = 0; |
| 11 | + private expectedValidTestInput: string[] = [ |
| 12 | + "org.nativescript.testApp/com.tns.TestClass", |
| 13 | + "org.nativescript.testApp/com.tns.$TestClass", |
| 14 | + "org.nativescript.testApp/com.tns._TestClass", |
| 15 | + "org.nativescript.testApp/com.tns.$_TestClass", |
| 16 | + "org.nativescript.testApp/com.tns._$TestClass", |
| 17 | + "org.nativescript.testApp/com.tns.NativeScriptActivity" |
| 18 | + ]; |
| 19 | + private validTestInput: string[] = [ |
| 20 | + "other.stuff/ org.nativescript.testApp/com.tns.TestClass asdaas.dasdh2", |
| 21 | + "other.stuff.the.regex.might.fail.on org.nativescript.testApp/com.tns.$TestClass other.stuff.the.regex.might.fail.on", |
| 22 | + "/might.fail.on org.nativescript.testApp/com.tns._TestClass /might.fail.on", |
| 23 | + "might.fail.on/ org.nativescript.testApp/com.tns.$_TestClass might.fail.on//", |
| 24 | + "/might.fail org.nativescript.testApp/com.tns._$TestClass something/might.fail.on/", |
| 25 | + "android.intent.action.MAIN: \ |
| 26 | + 3b2df03 org.nativescript.testApp/com.tns.NativeScriptActivity filter 50dd82e \ |
| 27 | + Action: \"android.intent.action.MAIN\" \ |
| 28 | + Category: \"android.intent.category.LAUNCHER\" \ |
| 29 | + -- \ |
| 30 | + intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.nativescript.testApp/com.tns.NativeScriptActivity} \ |
| 31 | + realActivity=org.nativescript.testApp/com.tns.NativeScriptActivity \ |
| 32 | + -- \ |
| 33 | + Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.nativescript.testApp/com.tns.NativeScriptActivity } \ |
| 34 | + frontOfTask=true task=TaskRecord{fe592ac #449 A=org.nativescript.testApp U=0 StackId=1 sz=1}" |
| 35 | + ]; |
| 36 | + |
| 37 | + public async executeShellCommand(args: string[]): Promise<any> { |
| 38 | + if (args && args.length > 0) { |
| 39 | + if (args[0] === "pm") { |
| 40 | + const passedIdentifier = args[2]; |
| 41 | + if (passedIdentifier === invalidIdentifier) { |
| 42 | + return "invalid output string"; |
| 43 | + } else { |
| 44 | + const testString = this.validTestInput[AndroidDebugBridgeStub.methodCallCount]; |
| 45 | + return testString; |
| 46 | + } |
| 47 | + } else { |
| 48 | + this.startedWithActivityManager = this.checkIfStartedWithActivityManager(args); |
| 49 | + if (this.startedWithActivityManager) { |
| 50 | + this.validIdentifierPassed = this.checkIfValidIdentifierPassed(args); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + AndroidDebugBridgeStub.methodCallCount++; |
| 55 | + } |
| 56 | + |
| 57 | + public getInputLength(): number { |
| 58 | + return this.validTestInput.length; |
| 59 | + } |
| 60 | + |
| 61 | + private checkIfStartedWithActivityManager(args: string[]): Boolean { |
| 62 | + const firstArgument = args[0].trim(); |
| 63 | + switch (firstArgument) { |
| 64 | + case "am": return true; |
| 65 | + case "monkey": return false; |
| 66 | + default: return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private checkIfValidIdentifierPassed(args: string[]): Boolean { |
| 71 | + if (args && args.length) { |
| 72 | + const possibleIdentifier = args[args.length - 1]; |
| 73 | + const validTestString = this.expectedValidTestInput[AndroidDebugBridgeStub.methodCallCount]; |
| 74 | + |
| 75 | + return possibleIdentifier === validTestString; |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function createTestInjector(): IInjector { |
| 82 | + let testInjector = new Yok(); |
| 83 | + testInjector.register("androidApplicationManager", AndroidApplicationManager); |
| 84 | + testInjector.register("adb", AndroidDebugBridgeStub); |
| 85 | + testInjector.register('childProcess', {}); |
| 86 | + testInjector.register("logger", CommonLoggerStub); |
| 87 | + testInjector.register("config", {}); |
| 88 | + testInjector.register("staticConfig", {}); |
| 89 | + testInjector.register("androidDebugBridgeResultHandler", {}); |
| 90 | + testInjector.register("options", {justlaunch: true}); |
| 91 | + testInjector.register("errors", {}); |
| 92 | + testInjector.register("identifier", {}); |
| 93 | + testInjector.register("logcatHelper", {}); |
| 94 | + testInjector.register("androidProcessService", {}); |
| 95 | + testInjector.register("httpClient", {}); |
| 96 | + testInjector.register("deviceLogProvider", {}); |
| 97 | + testInjector.register("hooksService", {}); |
| 98 | + return testInjector; |
| 99 | +} |
| 100 | + |
| 101 | +describe("android-application-manager", () => { |
| 102 | + |
| 103 | + let testInjector: IInjector, |
| 104 | + androidApplicationManager:AndroidApplicationManager, |
| 105 | + androidDebugBridge:AndroidDebugBridgeStub; |
| 106 | + |
| 107 | + beforeEach(() => { |
| 108 | + testInjector = createTestInjector(); |
| 109 | + androidApplicationManager = testInjector.resolve("androidApplicationManager"); |
| 110 | + androidDebugBridge = testInjector.resolve("adb"); |
| 111 | + }); |
| 112 | + describe("startApplication", () => { |
| 113 | + it("fires up the right application", async () => { |
| 114 | + for (let i = 0; i < androidDebugBridge.getInputLength(); i++) { |
| 115 | + androidDebugBridge.validIdentifierPassed = false; |
| 116 | + |
| 117 | + await androidApplicationManager.startApplication("valid.identifier"); |
| 118 | + assert.isTrue(androidDebugBridge.validIdentifierPassed); |
| 119 | + assert.isTrue(androidDebugBridge.startedWithActivityManager); |
| 120 | + } |
| 121 | + }); |
| 122 | + it("if regex fails monkey is called to start application", async () => { |
| 123 | + await androidApplicationManager.startApplication(invalidIdentifier); |
| 124 | + assert.isFalse(androidDebugBridge.startedWithActivityManager); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments