-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathchannels-support-2.0.ts
145 lines (123 loc) · 5.24 KB
/
channels-support-2.0.ts
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
import { assert, expect } from "chai";
import { Channel, Context, Listener, DesktopAgent } from "fdc3_2_0";
import constants from "../../../constants";
import { ChannelControl, ChannelsAppConfig, ChannelsAppContext } from "../../common/control/channel-control";
import { AppControlContext } from "../../../context-types";
import { closeMockAppWindow, waitForContext } from "../fdc3-2_0-utils";
declare let fdc3: DesktopAgent;
export class ChannelControl2_0 implements ChannelControl<Channel, Context, Listener> {
private readonly testAppChannelName = "test-channel";
getNonGlobalUserChannels = async () => {
const channels = await fdc3.getUserChannels();
if(channels.find((channel) => channel.id.indexOf('global') >=0 )) {
assert.fail("Global channel recieved ");
}
return channels.filter(channel => channel.id.indexOf('global') === -1);
}
getNonGlobalUserChannel = async (): Promise<Channel> => {
const channels = await this.getNonGlobalUserChannels();
if (channels.length > 0) {
return channels[0];
} else {
assert.fail("No system channels available for app A");
}
}
leaveChannel = async () => {
return await fdc3.leaveCurrentChannel();
};
joinChannel = async (channel: Channel) => {
return fdc3.joinUserChannel(channel.id);
};
createRandomTestChannel = async (name: string = "test-channel") => {
const channelName = `${this.testAppChannelName}.${this.getRandomId()}`;
return await fdc3.getOrCreateChannel(channelName);
};
getCurrentChannel = async (): Promise<Channel> => {
return await fdc3.getCurrentChannel();
};
unsubscribeListeners = async (listeners: Listener[]) => {
listeners.map((listener) => {
listener.unsubscribe();
listener = undefined;
});
};
initCompleteListener = async (testId: string) => {
return await waitForContext("executionComplete", testId, await fdc3.getOrCreateChannel(constants.ControlChannel));
};
openChannelApp = async (testId: string, channelId: string | undefined, commands: string[], historyItems: number = undefined, notify: boolean = true, contextId?: string) => {
const channelsAppConfig: ChannelsAppConfig = {
fdc3ApiVersion: "2.0",
testId: testId,
channelId: channelId,
notifyAppAOnCompletion: notify,
contextId: contextId,
};
if (historyItems) {
channelsAppConfig.historyItems = historyItems;
}
//Open ChannelsApp then execute commands in order
await fdc3.open({ appId: "ChannelsAppId" }, buildChannelsAppContext(commands, channelsAppConfig));
};
async closeMockApp(testId: string) {
await closeMockAppWindow(testId);
}
setupAndValidateListener = async (channel: Channel, listenContextType: string | null, expectedContextType: string | null, errorMessage: string, onComplete: (ctx: Context) => void): Promise<Listener> => {
let listener;
if (channel) {
listener = await channel.addContextListener(listenContextType, (context) => {
if (expectedContextType != null) {
expect(context.type).to.be.equals(expectedContextType, errorMessage);
}
onComplete(context);
});
} else {
listener = await fdc3.addContextListener(expectedContextType, (context) => {
if (expectedContextType != null) {
expect(context.type).to.be.equals(expectedContextType, errorMessage);
}
onComplete(context);
});
}
validateListenerObject(listener);
return listener;
};
setupContextChecker = async (channel: Channel, requestedContextType: string | null, expectedContextType: string, errorMessage: string, onComplete: (ctx: Context) => void): Promise<void> => {
//Retrieve current context from channel
const context = requestedContextType === undefined ? await channel.getCurrentContext() : await channel.getCurrentContext(requestedContextType);
expect(context, "await channel.getCurrentContext() returned null").to.not.be.null;
expect(context.type, "retrieved context was not of the expected type").to.be.equals(expectedContextType, errorMessage);
onComplete(context);
};
getRandomId(): string {
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
return uint32.toString(16);
}
}
function validateListenerObject(listenerObject) {
assert.isTrue(typeof listenerObject === "object", "No listener object found");
expect(typeof listenerObject.unsubscribe).to.be.equals("function", "Listener does not contain an unsubscribe method");
}
const broadcastAppChannelCloseWindow = async (testId: string) => {
const appControlChannel = await fdc3.getOrCreateChannel(constants.ControlChannel);
/* tslint:disable-next-line */
const closeContext: AppControlContext = {
type: "closeWindow",
testId: testId,
};
await appControlChannel.broadcast(closeContext);
return appControlChannel;
};
function buildChannelsAppContext(mockAppCommands: string[], config: ChannelsAppConfig): ChannelsAppContext {
return {
type: "channelsAppContext",
commands: mockAppCommands,
config: {
fdc3ApiVersion: config.fdc3ApiVersion,
testId: config.testId,
notifyAppAOnCompletion: config.notifyAppAOnCompletion ?? false,
historyItems: config.historyItems ?? 1,
channelId: config.channelId,
contextId: config.contextId,
},
};
}