summaryrefslogtreecommitdiffstats
path: root/framework/actions/tests/actiontest.cpp
blob: a4ec443205c610c76e7eeae5c976b618cb7a665f (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
#include <QTest>
#include <QDebug>
#include <QSignalSpy>

#include <actions/action.h>
#include <actions/context.h>
#include <actions/actionhandler.h>

#include <sink/log.h>

SINK_DEBUG_AREA("actiontest")

class HandlerContext : public Kube::Context {
    Q_OBJECT
    KUBE_CONTEXT_PROPERTY(QString, Property1, property1)
    KUBE_CONTEXT_PROPERTY(QString, Property2, property2)
};

class HandlerContextWrapper : public Kube::ContextWrapper {
    using Kube::ContextWrapper::ContextWrapper;
    KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property1, property1)
    KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property2, property2)
};



class Handler : public Kube::ActionHandlerBase<HandlerContextWrapper>
{
public:
    Handler() : Kube::ActionHandlerBase<HandlerContextWrapper>{"org.kde.kube.test.action1"}
    {}

    //TODO default implementation checks that all defined properties are available in the context
    // bool isReady() override {
    //     auto accountId = context->property("accountId").value<QByteArray>();
    //     return !accountId.isEmpty();
    // }

    KAsync::Job<void> execute(HandlerContextWrapper &context)
    {
        SinkLog() << "Executing action1";
        SinkLog() << context;
        executions.append(context.context);
        return KAsync::null<void>();
    }
    mutable QList<Kube::Context> executions;
};

class Context1 : public Kube::ContextWrapper {
    using Kube::ContextWrapper::ContextWrapper;
    KUBE_CONTEXTWRAPPER_PROPERTY(QString, Property1, property1)
    KUBE_CONTEXTWRAPPER_PROPERTY(QByteArray, Property2, property2)
};

class Context2 : public Kube::ContextWrapper {
    using Kube::ContextWrapper::ContextWrapper;
    KUBE_CONTEXTWRAPPER_PROPERTY(QByteArray, Property2, property2)
};


class ActionTest : public QObject
{
    Q_OBJECT
private slots:

    void initTestCase()
    {
    }

    void testActionExecution()
    {
        Handler actionHandler;

        HandlerContext context;
        //Kube::Context context;
        HandlerContextWrapper{context}.setProperty1(QString("property1"));
        context.setProperty("property2", QVariant::fromValue(QString("property2")));
        auto future = Kube::Action("org.kde.kube.test.action1", context).executeWithResult();

        QTRY_VERIFY(future.isDone());
        QVERIFY(!future.error());

        QCOMPARE(actionHandler.executions.size(), 1);
        QCOMPARE(actionHandler.executions.first().availableProperties().size(), 2);
    }

    void testContextCasting()
    {
        Kube::Context c;

        Context1 context1{c};
        context1.setProperty1("property1");
        context1.setProperty2("property2");

        auto context2 = Context2{c};
        QCOMPARE(context2.getProperty2(), QByteArray("property2"));
    }

};

QTEST_GUILESS_MAIN(ActionTest)
#include "actiontest.moc"