]> git.saurik.com Git - wxWidgets.git/blob - tests/exec/exec.cpp
d9e61fdfff7a1dc8148a7cfc302a4c2fb9763347
[wxWidgets.git] / tests / exec / exec.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/exec/exec.cpp
3 // Purpose: test wxExecute()
4 // Author: Francesco Montorsi
5 // (based on console sample TestExecute() function)
6 // Created: 2009-01-10
7 // RCS-ID: $Id$
8 // Copyright: (c) 2009 Francesco Montorsi
9 // (c) 2013 Rob Bresalier, Vadim Zeitlin
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #include "testprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/utils.h"
24 #include "wx/process.h"
25 #include "wx/sstream.h"
26
27 #ifdef __UNIX__
28 #define COMMAND "echo hi"
29 #define ASYNC_COMMAND "xclock"
30 #define SHELL_COMMAND "echo hi from shell>/dev/null"
31 #define COMMAND_NO_OUTPUT "echo -n"
32 #elif defined(__WINDOWS__)
33 #define COMMAND "cmd.exe /c \"echo hi\""
34 #define ASYNC_COMMAND "notepad"
35 #define SHELL_COMMAND "echo hi > nul:"
36 #define COMMAND_NO_OUTPUT COMMAND " > nul:"
37 #else
38 #error "no command to exec"
39 #endif // OS
40
41 // ----------------------------------------------------------------------------
42 // test class
43 // ----------------------------------------------------------------------------
44
45 class ExecTestCase : public CppUnit::TestCase
46 {
47 public:
48 ExecTestCase() { }
49
50 private:
51 CPPUNIT_TEST_SUITE( ExecTestCase );
52 CPPUNIT_TEST( TestShell );
53 CPPUNIT_TEST( TestExecute );
54 CPPUNIT_TEST( TestProcess );
55 CPPUNIT_TEST_SUITE_END();
56
57 void TestShell();
58 void TestExecute();
59 void TestProcess();
60
61 DECLARE_NO_COPY_CLASS(ExecTestCase)
62 };
63
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( ExecTestCase );
66
67 // also include in its own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExecTestCase, "ExecTestCase" );
69
70
71 void ExecTestCase::TestShell()
72 {
73 CPPUNIT_ASSERT( wxShell(SHELL_COMMAND) );
74 }
75
76 void ExecTestCase::TestExecute()
77 {
78 // test sync exec (with a command not producing any output to avoid
79 // interfering with the test):
80 CPPUNIT_ASSERT( wxExecute(COMMAND_NO_OUTPUT, wxEXEC_SYNC) == 0 );
81
82 // test asynch exec
83 long pid = wxExecute(ASYNC_COMMAND, wxEXEC_ASYNC);
84 CPPUNIT_ASSERT( pid != 0 );
85
86 // NOTE: under Windows the first wxKill() invocation with wxSIGTERM
87 // may fail if the system is fast and the ASYNC_COMMAND app
88 // doesn't manage to create its HWND before our wxKill() is
89 // executed; in that case we "fall back" to the second invocation
90 // with wxSIGKILL (which should always succeed)
91 CPPUNIT_ASSERT( wxKill(pid, wxSIGTERM) == 0 ||
92 wxKill(pid, wxSIGKILL) == 0 );
93
94 // test running COMMAND again, but this time with redirection:
95 wxArrayString stdout_arr;
96 CPPUNIT_ASSERT_EQUAL( 0, wxExecute(COMMAND, stdout_arr, wxEXEC_SYNC) );
97 CPPUNIT_ASSERT_EQUAL( "hi", stdout_arr[0] );
98 }
99
100 void ExecTestCase::TestProcess()
101 {
102 // test wxExecute with wxProcess
103 wxProcess *proc = new wxProcess;
104 long pid = wxExecute(ASYNC_COMMAND, wxEXEC_ASYNC, proc);
105 CPPUNIT_ASSERT( proc->GetPid() == pid && pid != 0 );
106
107 // we're not going to process the wxEVT_END_PROCESS event,
108 // so the proc instance will auto-delete itself after we kill
109 // the asynch process:
110 CPPUNIT_ASSERT( wxKill(pid, wxSIGTERM) == 0 ||
111 wxKill(pid, wxSIGKILL) == 0 );
112
113
114 // test wxExecute with wxProcess and REDIRECTION
115 wxProcess *proc2 = new wxProcess;
116 proc2->Redirect();
117 CPPUNIT_ASSERT( wxExecute(COMMAND, wxEXEC_SYNC, proc2) == 0 );
118
119 wxStringOutputStream stdout_stream;
120 CPPUNIT_ASSERT( proc2->GetInputStream() );
121 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF,
122 proc2->GetInputStream()->Read(stdout_stream).GetLastError() );
123
124 wxString str(stdout_stream.GetString());
125 CPPUNIT_ASSERT_EQUAL( "hi", str.Trim() );
126 }
127