5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
28 Testing the 'qt' tool, i.e. support for .ui files and automatic
29 generation of qt's moc files.
35 python = TestSCons.python
40 test = TestSCons.TestSCons()
42 test.subdir( 'qt', ['qt', 'bin'], ['qt', 'include'], ['qt', 'lib'] )
44 # create a dummy qt installation
46 test.write(['qt', 'bin', 'mymoc.py'], """
51 cmd_opts, args = getopt.getopt(sys.argv[1:], 'io:', [])
55 for opt, arg in cmd_opts:
56 if opt == '-o': output = open(arg, 'wb')
57 elif opt == '-i': impl = 1
58 else: opt_string = opt_string + ' ' + opt
60 contents = open(a, 'rb').read()
61 subst = r'{ my_qt_symbol( "' + a + '\\\\n" ); }'
63 contents = re.sub( r'#include.*', '', contents )
64 output.write(string.replace(contents, 'Q_OBJECT', subst))
69 test.write(['qt', 'bin', 'myuic.py'], """
76 for arg in sys.argv[1:]:
78 output = open(arg, 'wb')
90 source = open(arg, 'rb')
92 output.write( '#include "' + impl + '"\\n' )
94 output.write( '#include "my_qobject.h"\\n' + source.read() + " Q_OBJECT \\n" )
99 test.write(['qt', 'include', 'my_qobject.h'], r"""
101 void my_qt_symbol(const char *arg);
104 test.write(['qt', 'lib', 'my_qobject.cpp'], r"""
105 #include "../include/my_qobject.h"
107 void my_qt_symbol(const char *arg) {
112 test.write(['qt', 'lib', 'SConstruct'], r"""
114 env.StaticLibrary( 'myqt', 'my_qobject.cpp' )
117 test.run(chdir=test.workpath('qt','lib'), arguments = '.')
119 QT = test.workpath('qt')
121 QT_MOC = '%s %s' % (python, test.workpath('qt','bin','mymoc.py'))
122 QT_UIC = '%s %s' % (python, test.workpath('qt','bin','myuic.py'))
124 # 3 test cases with 3 different operation modes
126 def createSConstruct(test,place):
127 test.write(place, """
128 env = Environment(QTDIR = r'%s',
132 tools=['default','qt'])
133 if ARGUMENTS.get('build_dir', 0):
134 if ARGUMENTS.get('chdir', 0):
138 BuildDir('build', '.', duplicate=1)
139 sconscript = Dir('build').File('SConscript')
141 sconscript = File('SConscript')
143 SConscript( sconscript )
144 """ % (QT, QT_LIB, QT_MOC, QT_UIC))
146 test.subdir( 'work1', 'work2', 'work3', 'work4' )
148 # 1. create a moc file from a header file.
150 aaa_exe = 'aaa' + _exe
153 createSConstruct(test, ['work1', 'SConstruct'])
154 test.write( ['work1', 'SConscript'], """
156 env.Program(target = 'aaa', source = 'aaa.cpp')
159 test.write(['work1', 'aaa.cpp'], r"""
161 int main() { aaa(); return 0; }
164 test.write(['work1', 'aaa.h'], r"""
165 #include "my_qobject.h"
166 void aaa(void) Q_OBJECT;
169 test.run(chdir='work1', arguments = aaa_exe)
170 test.up_to_date(chdir='work1', options = '-n', arguments=aaa_exe)
172 test.up_to_date(chdir='work1', options = '-n', arguments = aaa_exe)
173 test.write(['work1', 'aaa.h'], r"""
175 #include "my_qobject.h"
176 void aaa(void) Q_OBJECT;
178 test.not_up_to_date(chdir='work1', options='-n', arguments = moc)
179 test.run(program = test.workpath('work1', aaa_exe), stdout = 'aaa.h\n')
181 test.run(chdir='work1',
182 arguments = "build_dir=1 " +
183 test.workpath('work1', 'build', aaa_exe) )
184 test.run(chdir='work1',
185 arguments = "build_dir=1 chdir=1 " +
186 test.workpath('work1', 'build', aaa_exe) )
188 test.fail_test( not os.path.exists(test.workpath('work1', 'build', moc)) )
190 # 2. create .cpp, .h, moc_....cpp from .ui file
192 aaa_dll = lib_ + 'aaa' + _dll
197 createSConstruct(test, ['work2', 'SConstruct'])
198 test.write(['work2', 'SConscript'], """
200 env.SharedLibrary(target = 'aaa', source = ['aaa.ui', 'useit.cpp'])
203 test.write(['work2', 'aaa.ui'], r"""
207 test.write(['work2', 'useit.cpp'], r"""
214 test.run(chdir='work2', arguments = aaa_dll)
215 test.up_to_date(chdir='work2', options='-n',arguments = aaa_dll)
216 test.write(['work2', 'aaa.ui'], r"""
220 test.not_up_to_date(chdir='work2', options = '-n', arguments = moc)
221 test.not_up_to_date(chdir='work2', options = '-n', arguments = cpp)
222 test.not_up_to_date(chdir='work2', options = '-n', arguments = h)
223 test.run(chdir='work2', arguments = aaa_dll)
224 test.write(['work2', 'aaa.ui.h'], r"""
225 /* test dependency to .ui.h */
227 test.not_up_to_date(chdir='work2', options = '-n', arguments = cpp)
228 test.up_to_date(chdir='work2', options = '-n', arguments = h)
229 test.up_to_date(chdir='work2', options = '-n', arguments = moc)
231 test.run(chdir='work2',
232 arguments = "build_dir=1 " +
233 test.workpath('work2', 'build', aaa_dll) )
234 test.run(chdir='work2',
235 arguments = "build_dir=1 chdir=1 " +
236 test.workpath('work2', 'build', aaa_dll) )
238 test.fail_test(not os.path.exists(test.workpath('work2','build',moc)) or
239 not os.path.exists(test.workpath('work2','build',cpp)) or
240 not os.path.exists(test.workpath('work2','build',h)))
242 # 3. create a moc file from a cpp file
244 lib_aaa = lib_ + 'aaa.a'
247 createSConstruct(test, ['work3', 'SConstruct'])
248 test.write(['work3', 'SConscript'], """
250 env.StaticLibrary(target = '%s', source = ['aaa.cpp','useit.cpp'])
253 test.write(['work3', 'aaa.h'], r"""
257 test.write(['work3', 'aaa.cpp'], r"""
258 #include "my_qobject.h"
259 void aaa(void) Q_OBJECT
263 test.write(['work3', 'useit.cpp'], r"""
270 test.run(chdir='work3', arguments = lib_aaa)
271 test.up_to_date(chdir='work3', options = '-n', arguments = lib_aaa)
272 test.write(['work3', 'aaa.cpp'], r"""
273 #include "my_qobject.h"
275 void aaa(void) Q_OBJECT
278 test.not_up_to_date(chdir='work3', options = '-n', arguments = moc)
280 test.run(chdir='work3',
281 arguments = "build_dir=1 " +
282 test.workpath('work3', 'build', lib_aaa) )
283 test.run(chdir='work3',
284 arguments = "build_dir=1 chdir=1 " +
285 test.workpath('work3', 'build', lib_aaa) )
287 test.fail_test(not os.path.exists(test.workpath('work3', 'build', moc)))
290 # look if qt is installed, and try out all builders
292 if os.environ.get('QTDIR', None):
294 test.write( ['work4', 'SConstruct'],"""
296 env = Environment(tools=['default','qt'], CXXFILESUFFIX=".cpp")
297 env.Program('test_realqt', ['mocFromCpp.cpp',
303 test.write( ['work4', 'mocFromCpp.h'],"""
307 test.write( ['work4', 'mocFromCpp.cpp'],"""
309 #include "mocFromCpp.h"
310 class MyClass1 : public QObject {
313 MyClass1() : QObject() {};
320 #include "moc_mocFromCpp.cpp"
323 test.write( ['work4', 'mocFromH.h'],"""
325 class MyClass2 : public QObject {
335 test.write( ['work4', 'mocFromH.cpp'],"""
336 #include "mocFromH.h"
338 MyClass2::MyClass2() : QObject() {}
339 void MyClass2::myslot() {}
345 test.write( ['work4', 'anUiFile.ui'],"""
347 <class>MyWidget</class>
349 <class>QWidget</class>
350 <property name="name">
351 <cstring>MyWidget</cstring>
353 <property name="caption">
354 <string>MyWidget</string>
357 <layoutdefaults spacing="6" margin="11"/>
361 test.write( ['work4', 'main.cpp'], """
362 #include "mocFromCpp.h"
363 #include "mocFromH.h"
364 #include "anUiFile.h"
372 test.run(chdir='work4', arguments="test_realqt" + _exe)
374 print "Could not find QT, skipping test(s)."