Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9f95d4eb2201dc407a02ac708e09704e195bcae (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2014, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.cdt.qt.tests;

import java.util.Collection;

import org.eclipse.cdt.internal.qt.core.index.IQObject;
import org.eclipse.cdt.internal.qt.core.index.IQmlRegistration;
import org.eclipse.cdt.internal.qt.core.index.QtIndex;

public class QmlRegistrationTests extends BaseQtTestCase {

	// #include "junit-QObject.hh"
    // class B : public QObject
    // {
    // Q_OBJECT
    // };
	//
    // class D : public B
    // {
    // Q_OBJECT
    // };
	//
	// static void func()
	// {
	//     qmlRegisterType<B>( "b-uri",    1, 2, "B" );
	//     qmlRegisterType<B>( "b-uri.34", 3, 4, "B34" );
	//
	//     const char * uri = "d-uri";
	//     int maj = 2, min = 3;
	//     const char * qmlName = "D1";
	//     qmlRegisterType<D, 1>( uri, maj, min, qmlName );
	// }
	public void testQmlRegisterType() throws Exception {
		loadComment("qmlregistertype.hh");

		QtIndex qtIndex = QtIndex.getIndex(fProject);
		assertNotNull(qtIndex);

		IQObject b_qobj = qtIndex.findQObject(new String[]{ "B" });
		if (!isIndexOk("B", b_qobj))
			return;
		assertNotNull(b_qobj);

		Collection<IQmlRegistration> qmlRegistrations = qtIndex.getQmlRegistrations();
		assertNotNull(qmlRegistrations);
		assertEquals(3, qmlRegistrations.size());

		for(IQmlRegistration qmlRegistration : qmlRegistrations) {
			IQObject qobj = qmlRegistration.getQObject();
			assertNotNull(qobj);

			// all values of B should be fully resolved, except for Revision, which was not provided
			if (qobj.getName().equals("B")) {
				assertNull(qmlRegistration.getVersion());
				String qmlName = qmlRegistration.getQmlName();
				assertNotNull(qmlName);
				if ("B".equals(qmlName)) {
					assertEquals(IQmlRegistration.Kind.Type, qmlRegistration.getKind());
					assertEquals("b-uri",         qmlRegistration.getURI());
					assertEquals(Long.valueOf(1), qmlRegistration.getMajor());
					assertEquals(Long.valueOf(2), qmlRegistration.getMinor());
					assertNull(qmlRegistration.getReason());
				} else if ("B34".equals(qmlName)) {
					assertEquals(IQmlRegistration.Kind.Type, qmlRegistration.getKind());
					assertEquals("b-uri.34",      qmlRegistration.getURI());
					assertEquals(Long.valueOf(3), qmlRegistration.getMajor());
					assertEquals(Long.valueOf(4), qmlRegistration.getMinor());
					assertNull(qmlRegistration.getReason());
				} else {
					fail("unexpected uri for B " + qmlName);
				}

			// the values for D are not expected to be resolved (yet), but it does have a Revision
			} else if (qobj.getName().equals("D")) {
				assertEquals(IQmlRegistration.Kind.Type, qmlRegistration.getKind());
				assertEquals(Long.valueOf(1), qmlRegistration.getVersion());
				assertNull(qmlRegistration.getURI());
				assertNull(qmlRegistration.getMajor());
				assertNull(qmlRegistration.getMinor());
				assertNull(qmlRegistration.getQmlName());
				assertNull(qmlRegistration.getReason());

			} else {
				fail("unexpected qmlRegistration " + qobj.getName());
			}
		}
	}

	// #include "junit-QObject.hh"
	// class T;
	//
	// static void func()
	// {
	//     qmlRegisterType<T>( "t-uri", 3, 4, "qml-T" );
	// }
	public void testQmlRegisterFwdDecl() throws Exception {
		loadComment("qmlregistertype.hh");

		QtIndex qtIndex = QtIndex.getIndex(fProject);
		assertNotNull(qtIndex);

		Collection<IQmlRegistration> qmlRegistrations = qtIndex.getQmlRegistrations();
		assertNotNull(qmlRegistrations);
		assertEquals(1, qmlRegistrations.size());

		IQmlRegistration qmlRegistration = qmlRegistrations.iterator().next();
		assertNotNull(qmlRegistration);
		assertEquals(IQmlRegistration.Kind.Type, qmlRegistration.getKind());
		assertEquals("t-uri", qmlRegistration.getURI());
		assertEquals(Long.valueOf(3), qmlRegistration.getMajor());
		assertEquals(Long.valueOf(4), qmlRegistration.getMinor());
		assertEquals("qml-T", qmlRegistration.getQmlName());
		assertNull(qmlRegistration.getReason());

		// The QObject has not been defined, so it cannot be found.
		assertNull(qmlRegistration.getQObject());
	}

	// #include "junit-QObject.hh"
    // class B : public QObject
    // {
    // Q_OBJECT
    // };
	//
    // class D : public B
    // {
    // Q_OBJECT
    // };
	//
	// static void func()
	// {
	//     qmlRegisterUncreatableType<B>( "b-uri",    1, 2, "B",   QString( "msg1" ) );
	//     qmlRegisterUncreatableType<B>( "b-uri.34", 3, 4, "B34", QString( "msg2" ) );
	//
	//     const char * uri = "d-uri";
	//     int maj = 2, min = 3;
	//     const char * qmlName = "D1";
	//     const QString msg( "msg3" );
	//     qmlRegisterUncreatableType<D>( uri, maj, min, qmlName, msg );
	// }
	public void testQmlRegisterUncreatableType() throws Exception {
		loadComment("qmlregisteruncreatabletype.hh");

		QtIndex qtIndex = QtIndex.getIndex(fProject);
		assertNotNull(qtIndex);

		IQObject b_qobj = qtIndex.findQObject(new String[]{ "B" });
		if (!isIndexOk("B", b_qobj))
			return;
		assertNotNull(b_qobj);

		Collection<IQmlRegistration> qmlRegistrations = qtIndex.getQmlRegistrations();
		assertNotNull(qmlRegistrations);
		assertEquals(3, qmlRegistrations.size());

		for(IQmlRegistration qmlRegistration : qmlRegistrations) {
			IQObject qobj = qmlRegistration.getQObject();
			assertNotNull(qobj);

			// all values of B should be fully resolved, except for Revision, which was not provided
			if (qobj.getName().equals("B")) {
				assertNull(qmlRegistration.getVersion());
				String qmlName = qmlRegistration.getQmlName();
				assertNotNull(qmlName);
				if ("B".equals(qmlName)) {
					assertEquals(IQmlRegistration.Kind.Uncreatable, qmlRegistration.getKind());
					assertEquals("b-uri",         qmlRegistration.getURI());
					assertEquals(Long.valueOf(1), qmlRegistration.getMajor());
					assertEquals(Long.valueOf(2), qmlRegistration.getMinor());
					assertEquals(null/*"msg1"*/,          qmlRegistration.getReason());
				} else if ("B34".equals(qmlName)) {
					assertEquals(IQmlRegistration.Kind.Uncreatable, qmlRegistration.getKind());
					assertEquals("b-uri.34",      qmlRegistration.getURI());
					assertEquals(Long.valueOf(3), qmlRegistration.getMajor());
					assertEquals(Long.valueOf(4), qmlRegistration.getMinor());
					assertEquals(null/*"msg2"*/,          qmlRegistration.getReason());
				} else {
					fail("unexpected uri for B " + qmlName);
				}

			// the values for D are not expected to be resolved (yet), but it does have a Revision
			} else if (qobj.getName().equals("D")) {
				assertEquals(IQmlRegistration.Kind.Uncreatable, qmlRegistration.getKind());
				assertNull(qmlRegistration.getVersion());
				assertNull(qmlRegistration.getURI());
				assertNull(qmlRegistration.getMajor());
				assertNull(qmlRegistration.getMinor());
				assertNull(qmlRegistration.getQmlName());
				assertNull(qmlRegistration.getReason());
			} else {
				fail("unexpected qmlRegistration " + qobj.getName());
			}
		}
	}

	// #include "junit-QObject.hh"
    // class B : public QObject
    // {
    // Q_OBJECT
    // };
	//
    // class D : public B
    // {
    // Q_OBJECT
    // };
	//
	// static void func()
	// {
	//     qmlRegisterType<B>( "b-uri",    1, 2, "B" );
	//     qmlRegisterType<B>( "b-uri.34", 3, 4, "B34" );
	//     qmlRegisterType<D, 1>( "d-uri", 2, 3, "D" );
	// }
	public void testAccessFromQObject() throws Exception {
		loadComment("qmlregistertype_fromqobject.hh");

		QtIndex qtIndex = QtIndex.getIndex(fProject);
		assertNotNull(qtIndex);

		IQObject b_qobj = qtIndex.findQObject(new String[]{ "B" });
		if (!isIndexOk("B", b_qobj))
			return;
		assertNotNull(b_qobj);

		Collection<IQmlRegistration> b_qmlRegistrations = b_qobj.getQmlRegistrations();
		assertNotNull(b_qmlRegistrations);
		assertEquals(2, b_qmlRegistrations.size());
		for(IQmlRegistration qmlRegistration : b_qmlRegistrations)
			if ("B".equals(qmlRegistration.getQmlName()))
				assert_checkQmlRegistration(qmlRegistration, IQmlRegistration.Kind.Type, null, "b-uri", 1L, 2L, null);
			else if("B34".equals(qmlRegistration.getQmlName()))
				assert_checkQmlRegistration(qmlRegistration, IQmlRegistration.Kind.Type, null, "b-uri.34", 3L, 4L, null);
			else
				fail("unexpected QmlRegistration with qmlName '" + qmlRegistration.getQmlName() + '\'');

		IQObject d_qobj = qtIndex.findQObject(new String[]{ "D" });
		assertNotNull(d_qobj);

		Collection<IQmlRegistration> d_qmlRegistrations = d_qobj.getQmlRegistrations();
		assertNotNull(d_qmlRegistrations);
		assertEquals(1, d_qmlRegistrations.size());
		for(IQmlRegistration qmlRegistration : d_qmlRegistrations)
			if("D".equals(qmlRegistration.getQmlName()))
				assert_checkQmlRegistration(qmlRegistration, IQmlRegistration.Kind.Type, 1L, "d-uri", 2L, 3L, null);
			else
				fail("unexpected QmlRegistration with qmlName '" + qmlRegistration.getQmlName() + '\'');
	}

	private static void assert_checkQmlRegistration(IQmlRegistration qmlRegistration, IQmlRegistration.Kind eKind, Long eVersion, String eUri, Long eMaj, Long eMin, String reason) throws Exception {
		assertNotNull(qmlRegistration);
		assertEquals(eKind, qmlRegistration.getKind());
		assertEquals(eVersion, qmlRegistration.getVersion());
		assertEquals(eUri, qmlRegistration.getURI());
		assertEquals(eMaj, qmlRegistration.getMajor());
		assertEquals(eMin, qmlRegistration.getMinor());
		assertEquals(reason, qmlRegistration.getReason());
	}
}

Back to the top