Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
325 andreas 1
/*
2
 * Copyright (C) 2023 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#include <cstring>
326 andreas 19
#include <iomanip>
20
#include <fstream>
325 andreas 21
 
22
#include "testmode.h"
23
#include "tpagemanager.h"
326 andreas 24
#include "tdirectory.h"
325 andreas 25
#include "tconfig.h"
326 andreas 26
#include "tresources.h"
325 andreas 27
#include "terror.h"
328 andreas 28
#ifdef __ANDROID__
29
#include <android/log.h>
30
#endif
325 andreas 31
 
334 andreas 32
#define TIMEOUT     5000000         // Microseconds (5 seconds)
33
#define DELAY       100             // Wait this amount of microseconds
326 andreas 34
 
35
using std::string;
325 andreas 36
using std::strncpy;
37
using std::min;
326 andreas 38
using std::atomic;
39
using std::vector;
40
using std::cout;
41
using std::endl;
42
using std::ifstream;
43
using namespace dir;
325 andreas 44
 
334 andreas 45
bool __success{false};              // TRUE indicates a successful test
46
bool __done{false};                 // TRUE marks a test case as done
47
bool _test_screen{false};           // TRUE: The graphical part of a test case is done.
48
bool _testmode{false};              // TRUE: Test mode is active. Activated by command line.
49
bool _run_test_ready{false};        // TRUE: The test cases are allowd to start.
50
string gLastCommand;                // The last command to be tested.
51
_TestMode *_gTestMode{nullptr};     // Pointer to class _TestMode. This class must exist only once!
326 andreas 52
 
53
extern amx::TAmxNet *gAmxNet;
54
 
55
_TestMode::_TestMode(const string& path)
56
    : mPath(path)
57
{
325 andreas 58
#if TESTMODE == 1
326 andreas 59
    DECL_TRACER("_TestMode::_TestMode(const string& path)");
325 andreas 60
 
326 andreas 61
    TDirectory dir(mPath);
62
    dir.scanFiles(".tst");
63
    size_t num = dir.getNumEntries();
64
    size_t pos = 0;
65
 
66
    while (pos < num)
67
    {
68
        DFILES_T f = dir.getEntry(pos);
69
 
70
        if (dir.testText(f.attr))
71
            mCmdFiles.push_back(f.name);
72
 
73
        pos++;
74
    }
339 andreas 75
 
76
    sort(mCmdFiles.begin(), mCmdFiles.end());
325 andreas 77
#endif
78
}
79
 
326 andreas 80
_TestMode::~_TestMode()
325 andreas 81
{
326 andreas 82
    DECL_TRACER("_TestMode::~_TestMode()");
83
 
84
    _gTestMode = nullptr;
85
    gLastCommand.clear();
86
    prg_stopped = true;
87
    killed = true;
88
 
89
    if (gAmxNet)
90
        gAmxNet->stop();
91
 
92
    if (gPageManager)
93
        gPageManager->callShutdown();
94
}
95
 
96
void _TestMode::run()
97
{
98
#if TESTMODE == 1
99
    DECL_TRACER("_TestMode::run()");
100
 
101
    if (isRunning)
102
        return;
103
 
104
    try
105
    {
106
        mThread = std::thread([=] { this->start(); });
107
        mThread.detach();
108
    }
109
    catch (std::exception& e)
110
    {
111
        MSG_ERROR("Error spawning thread: " << e.what());
112
    }
113
#endif
114
}
115
 
116
void _TestMode::start()
117
{
118
    isRunning = true;
119
    DECL_TRACER("_TestMode::start()");
120
 
330 andreas 121
    while (!_run_test_ready)
122
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
123
 
326 andreas 124
    vector<string>::iterator iter;
125
 
126
    for (iter = mCmdFiles.begin(); iter != mCmdFiles.end(); ++iter)
127
    {
128
        _TESTCMD tcmd;
339 andreas 129
        inform("------ File: " + *iter + " ------");
326 andreas 130
 
131
        try
132
        {
133
            ifstream is(*iter);
134
            char buffer[4096];
327 andreas 135
            int line = 1;
330 andreas 136
            int port = 1;
137
            string command, content;
326 andreas 138
            tcmd.command.clear();
139
            tcmd.result.clear();
140
            tcmd.compare = false;
141
 
142
            while (is.getline(buffer, sizeof(buffer)))
143
            {
334 andreas 144
                if (buffer[0] == '#' || buffer[0] == '\0')
145
                {
146
                    line++;
326 andreas 147
                    continue;
334 andreas 148
                }
326 andreas 149
 
330 andreas 150
                string scmd(buffer);
151
                command.clear();
152
                content.clear();
153
                size_t pos = scmd.find("=");
154
 
155
                // Parse the command line and extract the command and the content
156
                if (scmd == "exec")
157
                    command = scmd;
158
                else if (pos == string::npos)
326 andreas 159
                {
334 andreas 160
                    MSG_WARNING("(" << *iter << ") Line " << line << ": Invalid or malformed command!");
161
                    line++;
330 andreas 162
                    continue;
163
                }
164
                else
165
                {
166
                    command = scmd.substr(0, pos);
167
                    content = scmd.substr(pos + 1);
168
                }
326 andreas 169
 
330 andreas 170
                // Test for the command and take the desired action.
171
                if (command == "command")
172
                    tcmd.command = content;
173
                else if (command == "port")
326 andreas 174
                {
330 andreas 175
                    int p = atoi(content.c_str());
326 andreas 176
 
330 andreas 177
                    if (p > 0)
178
                        port = p;
326 andreas 179
                }
330 andreas 180
                else if (command == "result")
181
                    tcmd.result = content;
182
                else if (command == "compare")
183
                    tcmd.compare = isTrue(content);
334 andreas 184
                else if (command == "reverse")
185
                    tcmd.reverse = isTrue(content);
330 andreas 186
                else if (command == "wait")
187
                {
188
                    unsigned long ms = atol(content.c_str());
326 andreas 189
 
330 andreas 190
                    if (ms != 0)
191
                        std::this_thread::sleep_for(std::chrono::milliseconds(ms));
326 andreas 192
                }
331 andreas 193
                else if (command == "nowait")
194
                    tcmd.nowait = isTrue(content);
335 andreas 195
                else if (command == "screenwait")
196
                    tcmd.waitscreen = isTrue(content);
341 andreas 197
                else if (command == "saveresult")
198
                {
199
                    mVarName = content;
200
                    tcmd.saveresult = true;
201
                }
202
                else if (command == "compresult")
203
                {
204
                    mVarName = content;
205
                    tcmd.compresult = true;
206
                }
330 andreas 207
                else if (command == "exec")
327 andreas 208
                {
209
                    if (tcmd.command.empty())
210
                    {
334 andreas 211
                       MSG_WARNING("(" << *iter << ") Nothing to execute on line " << line << "!");
327 andreas 212
                       line++;
213
                       continue;
214
                    }
215
 
334 andreas 216
                    mCaseNumber++;
335 andreas 217
                    inform("\x1b[1;37mCase " + intToString(mCaseNumber) + "\x1b[0m: \x1b[0;33m" + tcmd.command + "\x1b[0m");
332 andreas 218
                    __done = false;
219
                    __success = false;
334 andreas 220
                    _test_screen = false;
330 andreas 221
                    inject(port, tcmd.command);
327 andreas 222
 
331 andreas 223
                    if (!tcmd.nowait)
224
                        testSuccess(tcmd);
225
                    else
226
                    {
227
                        MSG_WARNING("Skipped result check!");
228
                        inform("   Result check skipped!");
229
                    }
230
 
327 andreas 231
                    tcmd.command.clear();
232
                    tcmd.result.clear();
233
                    tcmd.compare = false;
331 andreas 234
                    tcmd.nowait = false;
334 andreas 235
                    tcmd.reverse = false;
335 andreas 236
                    tcmd.waitscreen = false;
341 andreas 237
                    tcmd.saveresult = false;
238
                    tcmd.compresult = false;
334 andreas 239
                    mVerify.clear();
330 andreas 240
                    port = 1;
327 andreas 241
                }
242
 
243
                line++;
326 andreas 244
            }
245
 
246
            is.close();
247
        }
248
        catch (std::exception& e)
249
        {
250
            MSG_ERROR("Error on file " << *iter << ": " << e.what());
251
            continue;
252
        }
253
    }
254
 
255
    isRunning = false;
256
    delete this;
257
}
258
 
259
void _TestMode::inject(int port, const string& c)
260
{
325 andreas 261
    if (!gPageManager)
262
        return;
263
 
326 andreas 264
    gLastCommand = c;
325 andreas 265
    int channel = TConfig::getChannel();
266
    int system = TConfig::getSystem();
267
 
334 andreas 268
    string bef = c;
269
 
270
    if (startsWith(bef, "PUSH-"))
271
    {
272
        amx::ANET_SEND scmd;
273
        size_t pos = bef.find("-");
274
        string bt = bef.substr(pos + 1);
275
        vector<string> parts = StrSplit(bt, ",");
276
 
277
        if (parts.size() < 2)
278
        {
279
            MSG_ERROR("Command PUSH has syntax: PUSH-<x>,<y>!");
280
            return;
281
        }
282
 
283
        mX = atoi(parts[0].c_str());
284
        mY = atoi(parts[1].c_str());
285
        mPressed = true;
286
 
287
        if (gPageManager)
288
            gPageManager->mouseEvent(mX, mY, mPressed);
289
    }
290
    else if (startsWith(bef, "RELEASE-"))
291
    {
292
        amx::ANET_SEND scmd;
293
        size_t pos = bef.find("-");
294
        string bt = bef.substr(pos + 1);
295
        vector<string> parts = StrSplit(bt, ",");
296
 
297
        if (parts.size() < 2)
298
        {
299
            MSG_ERROR("Command RELEASE has syntax: RELEASE-<x>,<y>!");
300
            return;
301
        }
302
 
303
        mX = atoi(parts[0].c_str());
304
        mY = atoi(parts[1].c_str());
305
        mPressed = false;
306
 
307
        if (gPageManager)
308
            gPageManager->mouseEvent(mX, mY, mPressed);
309
    }
335 andreas 310
    else if (startsWith(bef, "CLICK-"))
311
    {
312
        amx::ANET_SEND scmd;
313
        size_t pos = bef.find("-");
314
        string bt = bef.substr(pos + 1);
315
        vector<string> parts = StrSplit(bt, ",");
316
 
317
        if (parts.size() < 2)
318
        {
319
            MSG_ERROR("Command CLICK has syntax: CLICK-<x>,<y>!");
320
            return;
321
        }
322
 
323
        mX = atoi(parts[0].c_str());
324
        mY = atoi(parts[1].c_str());
325
 
326
        if (gPageManager)
327
        {
328
            mPressed = true;
329
            gPageManager->mouseEvent(mX, mY, true);
330
            std::this_thread::sleep_for(std::chrono::milliseconds(200));
331
            mPressed = false;
332
            gPageManager->mouseEvent(mX, mY, false);
333
        }
334
    }
334 andreas 335
    else
336
    {
337
        amx::ANET_COMMAND cmd;
338
        cmd.MC = 0x000c;
339
        cmd.device1 = channel;
340
        cmd.port1 = port;
341
        cmd.system = system;
342
        cmd.data.message_string.system = system;
343
        cmd.data.message_string.device = channel;
344
        cmd.data.message_string.port = port;    // Must be the address port of button
345
        cmd.data.message_string.type = DTSZ_STRING;    // Char string
346
        cmd.data.message_string.length = min(c.length(), sizeof(cmd.data.message_string.content));
347
        memset(cmd.data.message_string.content, 0, sizeof(cmd.data.message_string.content));
348
        memcpy ((char *)cmd.data.message_string.content, c.c_str(), cmd.data.message_string.length);
349
        gPageManager->doCommand(cmd);
350
    }
325 andreas 351
}
326 andreas 352
 
334 andreas 353
void _TestMode::setMouseClick(int x, int y, bool pressed)
354
{
355
    DECL_TRACER("_TestMode::setMouseClick(int x, int y, bool pressed)");
356
 
357
    if (mX == 0 && mY == 0 && mPressed == false)
358
        return;
359
 
360
    if (mX == x && mY == y && mPressed == pressed)
361
    {
362
        __success = true;
363
        _test_screen = true;
364
        __done = true;
365
        mX = mY = 0;
366
        mPressed = false;
367
    }
368
}
369
 
327 andreas 370
void _TestMode::testSuccess(_TESTCMD& tc)
326 andreas 371
{
372
    ulong total = 0;
335 andreas 373
    bool ready = ((tc.compare || tc.waitscreen) ? (__done && _test_screen) : __done);
326 andreas 374
 
334 andreas 375
    while (!ready && total < TIMEOUT)
326 andreas 376
    {
377
        usleep(DELAY);
378
        total += DELAY;
335 andreas 379
        ready = ((tc.compare || tc.waitscreen) ? (__done && _test_screen) : __done);
326 andreas 380
    }
381
 
341 andreas 382
    if (!mVarName.empty())
383
    {
384
        if (tc.saveresult)
342 andreas 385
            saveVariable(mVarName, mVerify);
341 andreas 386
 
387
        if (tc.compresult)
388
            mVerify = getVariable(mVarName);
389
 
390
        mVarName.clear();
391
    }
392
 
326 andreas 393
    if (total >= TIMEOUT)
394
    {
395
        MSG_WARNING("Command \"" << gLastCommand << "\" timed out!");
396
#ifdef __ANDROID__
328 andreas 397
        __android_log_print(ANDROID_LOG_INFO, "tpanel", "Command \"%s\" timed out!", gLastCommand.c_str());
326 andreas 398
#else
399
        cout << "Command \"" << gLastCommand << "\" timed out!" << endl;
400
#endif
334 andreas 401
        __success = false;
402
        tc.compare = false;
326 andreas 403
    }
404
 
335 andreas 405
    if (tc.compare && tc.result.compare(mVerify) == 0)
327 andreas 406
    {
334 andreas 407
        MSG_WARNING("The result \"" << mVerify << "\" is equal to \"" << tc.result << "\"!");
408
        __success = tc.reverse ? false : true;
327 andreas 409
    }
410
    else if (tc.compare)
411
    {
334 andreas 412
        MSG_WARNING("The result \"" << mVerify << "\" does not match the expected result of \"" << tc.result << "\"!");
413
        __success = tc.reverse ? true : false;
327 andreas 414
    }
415
 
326 andreas 416
    if (__success)
417
    {
418
        MSG_INFO("SUCCESS (" << gLastCommand << ")");
419
#ifdef __ANDROID__
334 andreas 420
        __android_log_print(ANDROID_LOG_INFO, "tpanel", "SUCCESS");
326 andreas 421
#else
335 andreas 422
        cout << "   \x1b[0;32mSUCCESS\x1b[0m" << endl;
326 andreas 423
#endif
424
    }
425
    else
426
    {
427
        MSG_ERROR("FAILED (" << gLastCommand << ")");
428
#ifdef __ANDROID__
335 andreas 429
        __android_log_print(ANDROID_LOG_ERROR, "tpanel", "FAILED");
326 andreas 430
#else
335 andreas 431
        cout << "   \x1b[0;31mFAILED\x1b[0m" << endl;
326 andreas 432
#endif
433
    }
434
 
435
    __success = false;
334 andreas 436
    _test_screen = false;
326 andreas 437
    __done = false;
438
}
439
 
440
void _TestMode::inform(const string& msg)
441
{
442
    MSG_INFO(msg);
443
#ifdef __ANDROID__
444
    __android_log_print(ANDROID_LOG_ERROR, "tpanel", "%s", msg.c_str());
445
#else
446
    cout << msg << endl;
447
#endif
448
}
341 andreas 449
 
450
void _TestMode::saveVariable(const string &name, const string &content)
451
{
452
    DECL_TRACER("_TestMode::saveVariable(const string &name, const string &content)");
453
 
454
    if (name.empty())
455
        return;
456
 
457
    _VARS v;
458
 
459
    if (mVariables.empty())
460
    {
461
        v.name = name;
462
        v.content = content;
463
        mVariables.push_back(v);
464
        return;
465
    }
466
 
467
    vector<_VARS>::iterator iter;
468
 
469
    for (iter = mVariables.begin(); iter != mVariables.end(); ++iter)
470
    {
471
        if (iter->name == name)
472
        {
473
            iter->content = content;
474
            return;
475
        }
476
    }
477
 
478
    v.name = name;
479
    v.content = content;
480
    mVariables.push_back(v);
481
}
482
 
483
string _TestMode::getVariable(const string &name)
484
{
485
    DECL_TRACER("_TestMode::getVariable(const string &name)");
486
 
487
    if (mVariables.empty())
488
        return string();
489
 
490
    vector<_VARS>::iterator iter;
491
 
492
    for (iter = mVariables.begin(); iter != mVariables.end(); ++iter)
493
    {
494
        if (iter->name == name)
495
            return iter->content;
496
    }
497
 
498
    return string();
499
}
500
 
501
void _TestMode::deleteVariable(const string &name)
502
{
503
    DECL_TRACER("_TestMode::deleteVariable(const string &name)");
504
 
505
    if (mVariables.empty())
506
        return;
507
 
508
    vector<_VARS>::iterator iter;
509
 
510
    for (iter = mVariables.begin(); iter != mVariables.end(); ++iter)
511
    {
512
        if (iter->name == name)
513
        {
514
            mVariables.erase(iter);
515
            return;
516
        }
517
    }
518
}