summaryrefslogtreecommitdiff
path: root/js/test/Common/TestRunner.js
blob: e6a51f49c57c9e8db66b58542d33c7099d6f22f9 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************


 /* global
    _test : false,
    _testBidir : false,
    Test : false,
*/

var process = { argv : [] };

function isSafari()
{
    return /^((?!chrome).)*safari/i.test(navigator.userAgent);
}

function isChrome()
{
    //
    // We need to check for Edge browser as it might include Chrome in its user agent.
    //
    return navigator.userAgent.indexOf("Edge/") === -1 &&
           navigator.userAgent.indexOf("Chrome/") !== -1;
}

function isWorker()
{
    return typeof(WorkerGlobalScope) !== 'undefined' && this instanceof WorkerGlobalScope;
}

function isWindows()
{
    return navigator.userAgent.indexOf("Windows") != -1;
}

function runTest(testsuite, language, host, protocol, testcases, out)
{
    //
    // This logger is setup to work with Web Workers and normal scripts using
    // the received out object. With some browser like Safari using console.log
    // method doesn't work when running inside a web worker.
    //
    var Logger =
    {
        print:function(message)
        {
            out.writeLine(message, false);
        },
        trace:function(category, message)
        {
            var s = [];
            var d = new Date();
            s.push("-- ");
            s.push(this.timestamp());
            s.push(' ');
            s.push(this._prefix);
            s.push(category);
            s.push(": ");
            s.push(message);
            out.writeLine(s.join(""), true);
        },
        warning:function(message)
        {
            var s = [];
            var d = new Date();
            s.push("-! ");
            s.push(this.timestamp());
            s.push(' ');
            s.push(this._prefix);
            s.push("warning: ");
            s.push(message);
            out.writeLine(s.join(""), true);
        },
        error:function(message)
        {
            var s = [];
            var d = new Date();
            s.push("!! ");
            s.push(this.timestamp());
            s.push(' ');
            s.push(this._prefix);
            s.push("error: ");
            s.push(message);
            out.writeLine(s.join(""), true);
        },
        getPrefix: function()
        {
            return "";
        },
        cloneWithPrefix: function(prefix)
        {
            return Logger;
        },
        timestamp:function()
        {
            var d = new Date();
            return d.toLocaleString("en-US", this._dateformat) + "." + d.getMilliseconds();
        }
    };

    var communicator;
    var id = new Ice.InitializationData();
    var port = protocol == "ws" ? 15002 : 15003;
    var serverTestCase;
    id.logger = Logger;
    id.properties = Ice.createProperties();
    id.properties.setProperty("Ice.Default.Host", host);
    id.properties.setProperty("Ice.Default.Protocol", protocol);
    //id.properties.setProperty("Ice.Trace.Protocol", "1");
    //id.properties.setProperty("Ice.Trace.Network", "3");

    return Ice.Promise.try(
        function()
        {
            if(typeof(_runServer) === "undefined" && typeof(_testBidir) === "undefined")
            {
                return _test(out, id);
            }

            communicator = Ice.initialize();
            var str = "controller:" + protocol + " -h " + host + " -p " + port;
            var controller = Test.Common.ControllerPrx.uncheckedCast(communicator.stringToProxy(str));
            if(testcases === undefined)
            {
                testcases = [ { name: "client/server" } ];
            }

            run = function(testcase, client)
            {
                if(testcase.langs && testcase.langs.indexOf(language) == -1)
                {
                    return;
                }

                if(typeof(_testBidir) !== "undefined" && client == _testBidir)
                {
                    out.writeLine("[ running bidir " + testcase.name + " test]");
                    runTestCase = function() { return controller.runTestCase("cpp", "Ice/echo", "server", language); };
                }
                else
                {
                    out.writeLine("[ running " + testcase.name + " test]");
                    runTestCase = function() { return controller.runTestCase("js", testsuite, testcase.name, language) };
                }
                out.write("starting server side... ");
                return runTestCase().then(
                    function(proxy)
                    {
                        proxy = controller.ice_getCachedConnection().createProxy(proxy.ice_getIdentity())
                        serverTestCase = Test.Common.TestCasePrx.uncheckedCast(proxy);
                        var config = new Test.Common.Config();
                        config.protocol = protocol;
                        return serverTestCase.startServerSide(config);
                    }
                ).then(
                    function()
                    {
                        out.writeLine("ok")
                        var initData = id.clone();
                        if(testcase.args !== undefined)
                        {
                            initData.properties = Ice.createProperties(testcase.args, id.properties);
                            process.argv=testcase.args
                        }
                        return client(out, initData);
                    }
                ).then(
                    function()
                    {
                        return serverTestCase.stopServerSide(true);
                    }
                ).catch(
                    function(ex)
                    {
                        out.writeLine("failed! (" + ex + ")");
                        throw ex
                    }
                ).finally(
                    function()
                    {
                        if(serverTestCase)
                        {
                            return serverTestCase.destroy();
                        }
                    }
                );
            }

            var p = Ice.Promise.resolve();
            if(typeof(_runServer) !== "undefined")
            {
                testcases.forEach(function(testcase) {
                    p = p.then(function() { return run(testcase, _test); })
                });
            }
            if(typeof(_testBidir) !== "undefined" && language === "cpp")
            {
                testcases.forEach(function(testcase) {
                    options = typeof(_runEchoServerOptions) !== "undefined" ? _runEchoServerOptions : []
                    p = p.then(function() { return run(testcase, _testBidir); })
                });
            }
            return p;
        }
    ).finally(
        function()
        {
            if(communicator)
            {
                return communicator.destroy();
            }
        }
    ).then(
        function()
        {
            return true;
        },
        function(ex)
        {
            out.writeLine("");
            if(ex instanceof Test.Common.TestCaseFailedException)
            {
                out.writeLine("Server test case failed to start:\n");
                out.writeLine(ex.output);
            }
            else
            {
                out.writeLine(ex.toString());
                if(ex.stack)
                {
                    out.writeLine(ex.stack);
                }
            }
            return false;
        });
}