Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / QT / installed.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Look if qt is installed, and try out all builders.
29 """
30
31 import os
32 import sys
33
34 import TestSCons
35
36 test = TestSCons.TestSCons()
37
38 if not os.environ.get('QTDIR', None):
39     x ="External environment variable $QTDIR not set; skipping test(s).\n"
40     test.skip_test(x)
41
42 test.Qt_dummy_installation()
43
44 QTDIR=os.environ['QTDIR']
45     
46
47 test.write('SConstruct', """\
48 import os
49 dummy_env = Environment()
50 ENV = dummy_env['ENV']
51 try:
52     PATH=ARGUMENTS['PATH']
53     if 'PATH' in ENV:
54         ENV_PATH = PATH + os.pathsep + ENV['PATH']
55     else:
56         Exit(0) # this is certainly a weird system :-)
57 except KeyError:
58     ENV_PATH=ENV.get('PATH', '')
59
60 env = Environment(tools=['default','qt'],
61                   ENV={'PATH':ENV_PATH,
62                        'PATHEXT':os.environ.get('PATHEXT'),
63                        'HOME':os.getcwd(),
64                        'SystemRoot':ENV.get('SystemRoot')},
65                        # moc / uic want to write stuff in ~/.qt
66                   CXXFILESUFFIX=".cpp")
67
68 conf = env.Configure()
69 if not conf.CheckLib(env.subst("$QT_LIB"), autoadd=0):
70     conf.env['QT_LIB'] = 'qt-mt'
71     if not conf.CheckLib(env.subst("$QT_LIB"), autoadd=0):
72          Exit(0)
73 env = conf.Finish()
74 VariantDir('bld', '.')
75 env.Program('bld/test_realqt', ['bld/mocFromCpp.cpp',
76                                 'bld/mocFromH.cpp',
77                                 'bld/anUiFile.ui',
78                                 'bld/main.cpp'])
79 """)
80
81 test.write('mocFromCpp.h', """\
82 void mocFromCpp();
83 """)
84
85 test.write('mocFromCpp.cpp', """\
86 #include <qobject.h>
87 #include "mocFromCpp.h"
88 class MyClass1 : public QObject {
89   Q_OBJECT
90   public:
91   MyClass1() : QObject() {};
92   public slots:
93   void myslot() {};
94 };
95 void mocFromCpp() {
96   MyClass1 myclass;
97 }
98 #include "mocFromCpp.moc"
99 """)
100
101 test.write('mocFromH.h', """\
102 #include <qobject.h>
103 class MyClass2 : public QObject {
104   Q_OBJECT;
105   public:
106   MyClass2();
107   public slots:
108   void myslot();
109 };
110 void mocFromH();
111 """)
112     
113 test.write('mocFromH.cpp', """\
114 #include "mocFromH.h"
115     
116 MyClass2::MyClass2() : QObject() {}
117 void MyClass2::myslot() {}
118 void mocFromH() {
119   MyClass2 myclass;
120 }
121 """)
122     
123 test.write('anUiFile.ui', """\
124 <!DOCTYPE UI><UI>
125 <class>MyWidget</class>
126 <widget>
127     <class>QWidget</class>
128     <property name="name">
129         <cstring>MyWidget</cstring>
130     </property>
131     <property name="caption">
132         <string>MyWidget</string>
133     </property>
134 </widget>
135 <includes>
136     <include location="local" impldecl="in implementation">anUiFile.ui.h</include>
137 </includes>
138 <slots>
139     <slot>testSlot()</slot>
140 </slots>
141 <layoutdefaults spacing="6" margin="11"/>
142 </UI>
143 """)
144
145 test.write('anUiFile.ui.h', r"""
146 #include <stdio.h>
147 #if QT_VERSION >= 0x030100
148 void MyWidget::testSlot()
149 {
150     printf("Hello World\n");
151 }
152 #endif
153 """)
154
155 test.write('main.cpp', r"""
156 #include <qapp.h>
157 #include "mocFromCpp.h"
158 #include "mocFromH.h"
159 #include "anUiFile.h"
160 #include <stdio.h>
161     
162 int main(int argc, char **argv) {
163   QApplication app(argc, argv);
164   mocFromCpp();
165   mocFromH();
166   MyWidget mywidget;
167 #if QT_VERSION >= 0x030100
168   mywidget.testSlot();
169 #else
170   printf("Hello World\n");
171 #endif
172   return 0;
173 }
174 """)
175
176 test.run(arguments="bld/test_realqt" + TestSCons._exe)
177
178
179 test.run(program=test.workpath("bld", "test_realqt"),
180          stdout=None,
181          status=None,
182          stderr=None)
183
184 if test.stdout() != "Hello World\n" or test.stderr() != '' or test.status:
185     sys.stdout.write(test.stdout())
186     sys.stderr.write(test.stderr())
187     # The test might be run on a system that doesn't have an X server
188     # running, or may be run by an ID that can't connect to the server.
189     # If so, then print whatever it showed us (which is in and of itself
190     # an indication that it built correctly) but don't fail the test.
191     expect = 'cannot connect to X server'
192     test.fail_test(test.stdout())
193     test.fail_test(test.stderr().find(expect) == -1)
194     if test.status != 1 and (test.status>>8) != 1:
195         sys.stdout.write('test_realqt returned status %s\n' % test.status)
196         test.fail_test()
197
198 QTDIR = os.environ['QTDIR']
199 PATH = os.environ['PATH']
200 os.environ['QTDIR']=''
201 os.environ['PATH']='.'
202
203 test.run(stderr=None, arguments="-c bld/test_realqt" + TestSCons._exe)
204
205 expect1 = "scons: warning: Could not detect qt, using empty QTDIR"
206 expect2 = "scons: warning: Could not detect qt, using moc executable as a hint"
207
208 test.fail_test(test.stderr().find(expect1) == -1 and
209                test.stderr().find(expect2) == -1)
210
211
212 test.pass_test()
213
214 # Local Variables:
215 # tab-width:4
216 # indent-tabs-mode:nil
217 # End:
218 # vim: set expandtab tabstop=4 shiftwidth=4: