diff options
Diffstat (limited to 'framework/actions/tests/actiontest.cpp')
-rw-r--r-- | framework/actions/tests/actiontest.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/framework/actions/tests/actiontest.cpp b/framework/actions/tests/actiontest.cpp new file mode 100644 index 00000000..a4ec4432 --- /dev/null +++ b/framework/actions/tests/actiontest.cpp | |||
@@ -0,0 +1,102 @@ | |||
1 | #include <QTest> | ||
2 | #include <QDebug> | ||
3 | #include <QSignalSpy> | ||
4 | |||
5 | #include <actions/action.h> | ||
6 | #include <actions/context.h> | ||
7 | #include <actions/actionhandler.h> | ||
8 | |||
9 | #include <sink/log.h> | ||
10 | |||
11 | SINK_DEBUG_AREA("actiontest") | ||
12 | |||
13 | class HandlerContext : public Kube::Context { | ||
14 | Q_OBJECT | ||
15 | KUBE_CONTEXT_PROPERTY(QString, Property1, property1) | ||
16 | KUBE_CONTEXT_PROPERTY(QString, Property2, property2) | ||
17 | }; | ||
18 | |||
19 | class HandlerContextWrapper : public Kube::ContextWrapper { | ||
20 | using Kube::ContextWrapper::ContextWrapper; | ||
21 | KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property1, property1) | ||
22 | KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property2, property2) | ||
23 | }; | ||
24 | |||
25 | |||
26 | |||
27 | class Handler : public Kube::ActionHandlerBase<HandlerContextWrapper> | ||
28 | { | ||
29 | public: | ||
30 | Handler() : Kube::ActionHandlerBase<HandlerContextWrapper>{"org.kde.kube.test.action1"} | ||
31 | {} | ||
32 | |||
33 | //TODO default implementation checks that all defined properties are available in the context | ||
34 | // bool isReady() override { | ||
35 | // auto accountId = context->property("accountId").value<QByteArray>(); | ||
36 | // return !accountId.isEmpty(); | ||
37 | // } | ||
38 | |||
39 | KAsync::Job<void> execute(HandlerContextWrapper &context) | ||
40 | { | ||
41 | SinkLog() << "Executing action1"; | ||
42 | SinkLog() << context; | ||
43 | executions.append(context.context); | ||
44 | return KAsync::null<void>(); | ||
45 | } | ||
46 | mutable QList<Kube::Context> executions; | ||
47 | }; | ||
48 | |||
49 | class Context1 : public Kube::ContextWrapper { | ||
50 | using Kube::ContextWrapper::ContextWrapper; | ||
51 | KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property1, property1) | ||
52 | KUBE_CONTEXTWRAPPER_PROPERTY(QByteArray, Property2, property2) | ||
53 | }; | ||
54 | |||
55 | class Context2 : public Kube::ContextWrapper { | ||
56 | using Kube::ContextWrapper::ContextWrapper; | ||
57 | KUBE_CONTEXTWRAPPER_PROPERTY(QByteArray, Property2, property2) | ||
58 | }; | ||
59 | |||
60 | |||
61 | class ActionTest : public QObject | ||
62 | { | ||
63 | Q_OBJECT | ||
64 | private slots: | ||
65 | |||
66 | void initTestCase() | ||
67 | { | ||
68 | } | ||
69 | |||
70 | void testActionExecution() | ||
71 | { | ||
72 | Handler actionHandler; | ||
73 | |||
74 | HandlerContext context; | ||
75 | //Kube::Context context; | ||
76 | HandlerContextWrapper{context}.setProperty1(QString("property1")); | ||
77 | context.setProperty("property2", QVariant::fromValue(QString("property2"))); | ||
78 | auto future = Kube::Action("org.kde.kube.test.action1", context).executeWithResult(); | ||
79 | |||
80 | QTRY_VERIFY(future.isDone()); | ||
81 | QVERIFY(!future.error()); | ||
82 | |||
83 | QCOMPARE(actionHandler.executions.size(), 1); | ||
84 | QCOMPARE(actionHandler.executions.first().availableProperties().size(), 2); | ||
85 | } | ||
86 | |||
87 | void testContextCasting() | ||
88 | { | ||
89 | Kube::Context c; | ||
90 | |||
91 | Context1 context1{c}; | ||
92 | context1.setProperty1("property1"); | ||
93 | context1.setProperty2("property2"); | ||
94 | |||
95 | auto context2 = Context2{c}; | ||
96 | QCOMPARE(context2.getProperty2(), QByteArray("property2")); | ||
97 | } | ||
98 | |||
99 | }; | ||
100 | |||
101 | QTEST_GUILESS_MAIN(ActionTest) | ||
102 | #include "actiontest.moc" | ||