forked from Dushistov/qt_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.hpp
122 lines (109 loc) · 3.8 KB
/
agent.hpp
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
#pragma once
#include <atomic>
#include <cassert>
#include <map>
#include <QKeySequence>
#include <QtCore/QEvent>
#include <QtCore/QObject>
#include "custom_event_analyzer.hpp"
#include "custom_script_extension.hpp"
#include "semaphore.hpp"
#include "shared_resource.hpp"
class QAction;
class QThread;
namespace qt_monkey_agent
{
class UserEventsAnalyzer;
namespace Private
{
class Script;
class ScriptRunner;
class MacMenuActionWatcher;
} // namespace Private
/**
* This class is used as agent inside user's program
* to catch/apply Qt events
*/
class Agent
#ifndef Q_MOC_RUN
final
#endif
: public QObject
{
Q_OBJECT
public:
/**
* using QApplication::installEventFilter, so it should be after all
* other calls to QApplication::installEventFilter in user app
* @param showObjectShortcut shorutcut key to show object info under mouse
* cursor
* @param customEventAnalyzers custom event analyzers, it is possible
* @param populateScriptContext gives you ability to introduce
* new functions or objects for scripts
*/
explicit Agent(const QKeySequence &showObjectShortcut
= QKeySequence(Qt::Key_F12 | Qt::SHIFT),
std::list<CustomEventAnalyzer> customEventAnalyzers
= std::list<CustomEventAnalyzer>(),
PopulateScriptContext populateScriptContext = {});
~Agent();
Agent(const Agent &) = delete;
Agent &operator=(const Agent &) = delete;
//! send log message to monkey
void sendToLog(QString msg);
//! called from script code for break point purposes
void scriptCheckPoint();
//@{
/**
* Run function in GUI thread, and wait it completition
* @param func function to run inside GUI thread
* @return error message if error appear or empty string if all ok
*/
QString runCodeInGuiThreadSync(std::function<QString()> func);
QString runCodeInGuiThreadSyncWithTimeout(std::function<QString()> func,
int timeoutSecs);
//@}
//! throw exception inside script
void throwScriptError(QString msg);
void setDemonstrationMode(bool val) { demonstrationMode_ = val; }
bool demonstrationMode() const { return demonstrationMode_; }
void setTraceEnabled(bool val) { scriptTracingMode_ = val; }
void saveScreenshots(const QString &path, int nSteps);
static Agent *instance() { return gAgent_; }
private slots:
void onUserEventInScriptForm(const QString &);
void onCommunicationError(const QString &);
void onRunScriptCommand(const qt_monkey_agent::Private::Script &);
void onAppAboutToQuit();
void onScriptLog(const QString &);
private:
friend class Private::MacMenuActionWatcher;
friend class ScriptAPI;
struct CurrentScriptContext final {
CurrentScriptContext(Private::ScriptRunner *cur,
Private::ScriptRunner *&global)
: global_(global)
{
assert(global_ == nullptr);
global_ = cur;
}
~CurrentScriptContext() { global_ = nullptr; }
private:
Private::ScriptRunner *&global_;
};
qt_monkey_agent::UserEventsAnalyzer *eventAnalyzer_ = nullptr;
QThread *thread_ = nullptr;
Private::ScriptRunner *curScriptRunner_ = nullptr;
QEvent::Type eventType_;
qt_monkey_common::Semaphore guiRunSem_{0};
PopulateScriptContext populateScriptContextCallback_;
static Agent *gAgent_;
std::atomic<bool> demonstrationMode_{false};
std::atomic<bool> scriptTracingMode_{false};
qt_monkey_common::SharedResource<std::multimap<QString, QAction *>>
menuItemsOnMac_;
qt_monkey_common::SharedResource<std::pair<QString, int>> screenshots_;
QString scriptBaseName_;
void customEvent(QEvent *event) override;
};
} // namespace qt_monkey_agent