-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathdebug-service.ts
More file actions
231 lines (184 loc) · 8.1 KB
/
debug-service.ts
File metadata and controls
231 lines (184 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { DebugService } from "../../lib/services/debug-service";
import { Yok } from "../../lib/common/yok";
import * as stubs from "../stubs";
import { assert } from "chai";
import { EventEmitter } from "events";
import * as constants from "../../lib/common/constants";
import { CONNECTION_ERROR_EVENT_NAME, DebugCommandErrors } from "../../lib/constants";
const fakeChromeDebugPort = 123;
const fakeChromeDebugUrl = `fakeChromeDebugUrl?experiments=true&ws=localhost:${fakeChromeDebugPort}`;
const defaultDeviceIdentifier = "Nexus5";
class PlatformDebugService extends EventEmitter /* implements IPlatformDebugService */ {
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
return fakeChromeDebugUrl;
}
}
interface IDebugTestDeviceInfo {
deviceInfo: {
status: string;
platform: string;
identifier: string;
};
isEmulator: boolean;
}
interface IDebugTestData {
isDeviceFound: boolean;
deviceInformation: IDebugTestDeviceInfo;
isApplicationInstalledOnDevice: boolean;
hostInfo: {
isWindows: boolean;
isDarwin: boolean;
};
}
const getDefaultDeviceInformation = (platform?: string): IDebugTestDeviceInfo => ({
deviceInfo: {
status: constants.CONNECTED_STATUS,
platform: platform || "Android",
identifier: defaultDeviceIdentifier
},
isEmulator: false
});
const getDefaultTestData = (platform?: string): IDebugTestData => ({
isDeviceFound: true,
deviceInformation: getDefaultDeviceInformation(platform),
isApplicationInstalledOnDevice: true,
hostInfo: {
isWindows: false,
isDarwin: true
}
});
describe("debugService", () => {
const getTestInjectorForTestConfiguration = (testData: IDebugTestData): IInjector => {
const testInjector = new Yok();
testInjector.register("devicesService", {
getDeviceByIdentifier: (identifier: string): Mobile.IDevice => {
return testData.isDeviceFound ?
<Mobile.IDevice>{
deviceInfo: testData.deviceInformation.deviceInfo,
applicationManager: {
isApplicationInstalled: async (appIdentifier: string): Promise<boolean> => testData.isApplicationInstalledOnDevice
},
isEmulator: testData.deviceInformation.isEmulator
} : null;
}
});
testInjector.register("androidDebugService", PlatformDebugService);
testInjector.register("iOSDebugService", PlatformDebugService);
testInjector.register("mobileHelper", {
isAndroidPlatform: (platform: string) => {
return platform.toLowerCase() === "android";
},
isiOSPlatform: (platform: string) => {
return platform.toLowerCase() === "ios";
}
});
testInjector.register("errors", stubs.ErrorsStub);
testInjector.register("hostInfo", testData.hostInfo);
testInjector.register("logger", stubs.LoggerStub);
return testInjector;
};
describe("debug", () => {
const getDebugData = (deviceIdentifier?: string): IDebugData => ({
applicationIdentifier: "org.nativescript.app1",
deviceIdentifier: deviceIdentifier || defaultDeviceIdentifier,
projectDir: "/Users/user/app1",
projectName: "app1"
});
describe("rejects the result promise when", () => {
const assertIsRejected = async (testData: IDebugTestData, expectedError: string, userSpecifiedOptions?: IDebugOptions): Promise<void> => {
const testInjector = getTestInjectorForTestConfiguration(testData);
const debugService = testInjector.resolve<IDebugServiceBase>(DebugService);
const debugData = getDebugData();
await assert.isRejected(debugService.debug(debugData, userSpecifiedOptions), expectedError);
};
it("there's no attached device as the specified identifier", async () => {
const testData = getDefaultTestData();
testData.isDeviceFound = false;
await assertIsRejected(testData, "Cannot find device with identifier");
});
it("the device is not trusted", async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.status = constants.UNREACHABLE_STATUS;
await assertIsRejected(testData, "is unreachable. Make sure it is Trusted ");
});
it("the application is not installed on device", async () => {
const testData = getDefaultTestData();
testData.isApplicationInstalledOnDevice = false;
await assertIsRejected(testData, "is not installed on device with identifier");
});
it("the OS is neither Windows or macOS and device is iOS", async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = "iOS";
testData.hostInfo.isDarwin = testData.hostInfo.isWindows = false;
await assertIsRejected(testData, "Debugging on iOS devices is not supported for");
});
it("device is neither iOS or Android", async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = "WP8";
await assertIsRejected(testData, DebugCommandErrors.UNSUPPORTED_DEVICE_OS_FOR_DEBUGGING);
});
it("when trying to debug on iOS Simulator on macOS, debug-brk is passed, but pathToAppPackage is not", async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = "iOS";
testData.deviceInformation.isEmulator = true;
await assertIsRejected(testData, "To debug on iOS simulator you need to provide path to the app package.", { debugBrk: true });
});
const assertIsRejectedWhenPlatformDebugServiceFails = async (platform: string): Promise<void> => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = platform;
const testInjector = getTestInjectorForTestConfiguration(testData);
const expectedErrorMessage = "Platform specific error";
const platformDebugService = testInjector.resolve<IPlatformDebugService>(`${platform}DebugService`);
platformDebugService.debug = async (debugData: IDebugData, debugOptions: IDebugOptions): Promise<any> => {
throw new Error(expectedErrorMessage);
};
const debugService = testInjector.resolve<IDebugServiceBase>(DebugService);
const debugData = getDebugData();
await assert.isRejected(debugService.debug(debugData, null), expectedErrorMessage);
};
it("androidDebugService's debug method fails", async () => {
await assertIsRejectedWhenPlatformDebugServiceFails("android");
});
it("iOSDebugService's debug method fails", async () => {
await assertIsRejectedWhenPlatformDebugServiceFails("iOS");
});
});
describe(`raises ${CONNECTION_ERROR_EVENT_NAME} event`, () => {
_.each(["android", "iOS"], platform => {
it(`when ${platform}DebugService raises ${CONNECTION_ERROR_EVENT_NAME} event`, async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = platform;
const testInjector = getTestInjectorForTestConfiguration(testData);
const debugService = testInjector.resolve<IDebugServiceBase>(DebugService);
let dataRaisedForConnectionError: any = null;
debugService.on(CONNECTION_ERROR_EVENT_NAME, (data: any) => {
dataRaisedForConnectionError = data;
});
const debugData = getDebugData();
await assert.isFulfilled(debugService.debug(debugData, null));
const expectedErrorData = { deviceId: "deviceId", message: "my message", code: 2048 };
const platformDebugService = testInjector.resolve<IPlatformDebugService>(`${platform}DebugService`);
platformDebugService.emit(CONNECTION_ERROR_EVENT_NAME, expectedErrorData);
assert.deepEqual(dataRaisedForConnectionError, expectedErrorData);
});
});
});
describe("returns chrome url along with port returned by platform specific debug service", () => {
_.each(["android", "iOS"], platform => {
it(`for ${platform} device`, async () => {
const testData = getDefaultTestData();
testData.deviceInformation.deviceInfo.platform = platform;
const testInjector = getTestInjectorForTestConfiguration(testData);
const debugService = testInjector.resolve<IDebugServiceBase>(DebugService);
const debugData = getDebugData();
const debugInfo = await debugService.debug(debugData, null);
assert.deepEqual(debugInfo, {
url: fakeChromeDebugUrl,
port: fakeChromeDebugPort,
deviceIdentifier: debugData.deviceIdentifier
});
});
});
});
});
});