Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 6af0f1d

Browse files
committed
SystemDispatcher : Add test case for data conversion between C++ and Java
1 parent 4e11f0c commit 6af0f1d

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "qasystemdispatcher.h"
2+
#include "automator.h"
3+
4+
Automator::Automator(QObject *parent) : QObject(parent)
5+
{
6+
7+
}
8+
9+
Automator::~Automator()
10+
{
11+
12+
}
13+
14+
void Automator::start()
15+
{
16+
connect(QASystemDispatcher::instance(),SIGNAL(dispatched(QString,QVariantMap)),
17+
this,SLOT(onDispatched(QString,QVariantMap)));
18+
}
19+
20+
void Automator::onDispatched(QString name, QVariantMap message)
21+
{
22+
if (name == "Automater::echo") {
23+
QASystemDispatcher::instance()->dispatch("Automater::response",message);
24+
}
25+
}
26+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef AUTOMATOR_H
2+
#define AUTOMATOR_H
3+
4+
#include <QObject>
5+
#include <QVariantMap>
6+
7+
/// For unit test purpose. Not needed for regular project.
8+
9+
class Automator : public QObject
10+
{
11+
Q_OBJECT
12+
public:
13+
explicit Automator(QObject *parent = 0);
14+
~Automator();
15+
16+
void start();
17+
18+
signals:
19+
20+
public slots:
21+
22+
private slots:
23+
void onDispatched(QString name,QVariantMap message);
24+
25+
};
26+
27+
#endif // AUTOMATOR_H

examples/quickandroidexample/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "quickandroid.h"
66
#include "qadrawableprovider.h"
77
#include "qasystemdispatcher.h"
8+
#include "automator.h"
89

910
#ifdef Q_OS_ANDROID
1011
#include <QtAndroidExtras/QAndroidJniObject>
@@ -39,6 +40,11 @@ int main(int argc, char *argv[])
3940
view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
4041
view.show();
4142

43+
44+
/* Testing Code. Not needed for regular project */
45+
Automator* automator = new Automator();
46+
automator->start();
47+
4248
QVariantMap message;
4349
message["field1"] = "value1";
4450
message["field2"] = 10;

examples/quickandroidexample/quickandroidexample.pro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ TEMPLATE = app
22

33
QT += qml quick
44

5-
SOURCES += main.cpp
5+
SOURCES += main.cpp \
6+
automator.cpp
67

78
RESOURCES += qml.qrc
89

@@ -22,3 +23,6 @@ include(deployment.pri)
2223
DISTFILES += \
2324
android-sources/src/quickandroid/example/ExampleActivity.java \
2425
android-sources/AndroidManifest.xml
26+
27+
HEADERS += \
28+
automator.h

tests/quickandroidactivitytests/tests/src/quickandroid/example/ExampleActivityTest.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.LinkedList;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.HashMap;
1516

1617
/**
1718
* This is a simple framework for a test of an Application. See
@@ -49,9 +50,8 @@ private void startActivity() {
4950

5051
private int counter = 0;
5152

52-
public void testSendMessage() {
53+
public void testDispatch() {
5354
startActivity();
54-
Log.v(TAG,"testSendMessage");
5555

5656
SystemDispatcher.Listener listener = new SystemDispatcher.Listener() {
5757

@@ -80,11 +80,10 @@ public void onDispatched(String name , Map data) {
8080

8181
private List<String> messages ;
8282

83-
public void testReentrant() {
83+
public void testDispatchReentrant() {
8484
startActivity();
8585
messages = new ArrayList();
8686

87-
Log.v(TAG,"testReentrant");
8887
final String messageName = "testReentrant";
8988

9089
SystemDispatcher.Listener listener = new SystemDispatcher.Listener() {
@@ -120,6 +119,58 @@ public void onDispatched(String name , Map data) {
120119
SystemDispatcher.removeListener(listener);
121120
}
122121

122+
private static class Payload {
123+
public String name;
124+
public Map message;
125+
}
126+
127+
128+
private static Payload lastPayload;
129+
130+
/** Verify the data convension function between C++ and Java */
131+
public void testDispatchTypes() {
132+
133+
SystemDispatcher.Listener listener = new SystemDispatcher.Listener() {
134+
135+
public void onDispatched(String name , Map message) {
136+
Payload payload = new Payload();
137+
payload.name = name;
138+
payload.message = message;
139+
140+
lastPayload = payload;
141+
}
142+
};
143+
144+
SystemDispatcher.addListener(listener);
145+
146+
Map message = new HashMap();
147+
message.put("field1","value1");
148+
message.put("field2",10);
149+
message.put("field3",true);
150+
message.put("field4",false);
151+
152+
SystemDispatcher.dispatch("Automater::echo",message);
153+
154+
assertTrue(lastPayload != null);
155+
assertTrue(lastPayload.message.containsKey("field1"));
156+
assertTrue(lastPayload.message.containsKey("field2"));
157+
assertTrue(lastPayload.message.containsKey("field3"));
158+
159+
String field1 = (String) lastPayload.message.get("field1");
160+
assertTrue(field1.equals("value1"));
161+
162+
int field2 = (int) (Integer) lastPayload.message.get("field2");
163+
assertEquals(field2,10);
164+
165+
boolean field3 = (boolean)(Boolean) lastPayload.message.get("field3");
166+
assertEquals(field3,true);
167+
168+
boolean field4 = (boolean)(Boolean) lastPayload.message.get("field4");
169+
assertEquals(field4,false);
170+
171+
SystemDispatcher.removeListener(listener);
172+
}
173+
123174

124175

125176
}

0 commit comments

Comments
 (0)