|
| 1 | +import { LogcatHelper } from "../../../../mobile/android/logcat-helper"; |
| 2 | +import { Yok } from "../../../../yok"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import * as path from "path"; |
| 5 | +import * as childProcess from "child_process"; |
| 6 | + |
| 7 | +class ChildProcessStub { |
| 8 | + public static methodCallCount = 0; |
| 9 | + private isWin = /^win/.test(process.platform); |
| 10 | + |
| 11 | + public spawn(command: string, args?: string[], options?: any): childProcess.ChildProcess { |
| 12 | + ChildProcessStub.methodCallCount++; |
| 13 | + let pathToExecutable = ""; |
| 14 | + if (this.isWin) { |
| 15 | + pathToExecutable = "type"; |
| 16 | + } else { |
| 17 | + pathToExecutable = "cat"; |
| 18 | + } |
| 19 | + pathToExecutable = path.join(pathToExecutable); |
| 20 | + const pathToSample = path.join(__dirname, "valid-sample.txt"); |
| 21 | + return childProcess.spawn(pathToExecutable, [pathToSample]); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function createTestInjector(): IInjector { |
| 26 | + const injector = new Yok(); |
| 27 | + injector.register("injector", injector); |
| 28 | + injector.register("logcatHelper", LogcatHelper); |
| 29 | + injector.register("logger", { |
| 30 | + debug(formatStr?: any, ...args: any[]): void { |
| 31 | + //left blank intentionally because of lint |
| 32 | + }, |
| 33 | + trace(formatStr?: any, ...args: any[]): void { |
| 34 | + if (formatStr && formatStr.indexOf("ADB") !== -1) { |
| 35 | + //loghelper failed or socket closed" |
| 36 | + assert.isTrue(false); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + injector.register("errors", {}); |
| 41 | + injector.register("devicePlatformsConstants", { Android: "Android" }); |
| 42 | + injector.register("processService", { |
| 43 | + attachToProcessExitSignals(context: any, callback: () => void): void { |
| 44 | + //left blank intentionally because of lint |
| 45 | + }, |
| 46 | + }); |
| 47 | + injector.register("deviceLogProvider", { |
| 48 | + logData(line: string, platform: string, deviceIdentifier: string): void { |
| 49 | + //left blank intentionally because of lint |
| 50 | + }, |
| 51 | + }); |
| 52 | + injector.register("childProcess", ChildProcessStub); |
| 53 | + injector.register("staticConfig", { |
| 54 | + async getAdbFilePath(): Promise<string> { |
| 55 | + return ""; |
| 56 | + } |
| 57 | + }); |
| 58 | + injector.register("androidDebugBridgeResultHandler", {}); |
| 59 | + |
| 60 | + return injector; |
| 61 | +} |
| 62 | + |
| 63 | +describe("logcat-helper", () => { |
| 64 | + let injector: IInjector; |
| 65 | + let adbLogcatCrashedOrClosed: boolean; |
| 66 | + let loggedData: string[]; |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + adbLogcatCrashedOrClosed = false; |
| 70 | + injector = createTestInjector(); |
| 71 | + loggedData = []; |
| 72 | + ChildProcessStub.methodCallCount = 0; |
| 73 | + }); |
| 74 | + |
| 75 | + describe("start", () => { |
| 76 | + it("whole logcat is read correctly", (done: mocha.Done) => { |
| 77 | + injector.register("deviceLogProvider", { |
| 78 | + logData(line: string, platform: string, deviceIdentifier: string): void { |
| 79 | + loggedData.push(line); |
| 80 | + if (line === "end") { |
| 81 | + assert.isAbove(loggedData.length, 0); |
| 82 | + done(); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper"); |
| 88 | + logcatHelper.start("valid-identifier"); |
| 89 | + }); |
| 90 | + it("if loghelper start is called multiple times with the same identifier it's a noop the second time", async () => { |
| 91 | + const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper"); |
| 92 | + await logcatHelper.start("valid-identifier"); |
| 93 | + assert.equal(ChildProcessStub.methodCallCount, 1); |
| 94 | + await logcatHelper.start("valid-identifier"); |
| 95 | + assert.equal(ChildProcessStub.methodCallCount, 1); |
| 96 | + await logcatHelper.start("valid-identifier"); |
| 97 | + assert.equal(ChildProcessStub.methodCallCount, 1); |
| 98 | + }); |
| 99 | + it("if loghelper start works when it's called multiple times with different identifiers", async () => { |
| 100 | + const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper"); |
| 101 | + await logcatHelper.start("valid-identifier1"); |
| 102 | + assert.equal(ChildProcessStub.methodCallCount, 1); |
| 103 | + await logcatHelper.start("valid-identifier2"); |
| 104 | + assert.equal(ChildProcessStub.methodCallCount, 2); |
| 105 | + await logcatHelper.start("valid-identifier3"); |
| 106 | + assert.equal(ChildProcessStub.methodCallCount, 3); |
| 107 | + }); |
| 108 | + }); |
| 109 | + describe("stop", () => { |
| 110 | + it("device identifier is cleared on stop", async () => { |
| 111 | + const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper"); |
| 112 | + await logcatHelper.start("valid-identifier"); |
| 113 | + assert.equal(ChildProcessStub.methodCallCount, 1); |
| 114 | + await logcatHelper.stop("valid-identifier"); |
| 115 | + await logcatHelper.start("valid-identifier"); |
| 116 | + assert.equal(ChildProcessStub.methodCallCount, 2); |
| 117 | + }); |
| 118 | + it("stop doesn't blow up if called multiple times", async () => { |
| 119 | + const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper"); |
| 120 | + await logcatHelper.stop("valid-identifier"); |
| 121 | + await logcatHelper.stop("valid-identifier"); |
| 122 | + assert.isTrue(true); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments