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