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