xref: /haiku/src/tests/kits/app/bapplication/AppQuitTester.cpp (revision 76a5f3484abd9a293483b45f40455c0b58e6f3f0)
1 //------------------------------------------------------------------------------
2 //	AppQuitTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <Message.h>
11 #include <OS.h>
12 #include <Application.h>
13 #include <Handler.h>
14 #include <Looper.h>
15 #include <String.h>
16 
17 // Project Includes ------------------------------------------------------------
18 #include <TestShell.h>
19 #include <TestUtils.h>
20 #include <cppunit/TestAssert.h>
21 
22 // Local Includes --------------------------------------------------------------
23 #include "AppRunner.h"
24 #include "AppQuitTester.h"
25 #include "PipedAppRunner.h"
26 
27 // Local Defines ---------------------------------------------------------------
28 
29 // Globals ---------------------------------------------------------------------
30 
31 //------------------------------------------------------------------------------
32 
33 // check_output
34 template<typename AppRunnerC>
35 static
36 void
check_output(AppRunnerC & runner,const char * expectedOutput)37 check_output(AppRunnerC &runner, const char *expectedOutput)
38 {
39 	BString buffer;
40 	CHK(runner.GetOutput(&buffer) == B_OK);
41 if (buffer != expectedOutput)
42 printf("result is `%s', but should be `%s'\n", buffer.String(),
43 expectedOutput);
44 	CHK(buffer == expectedOutput);
45 }
46 
47 /*
48 	void Quit()
49 	@case 1			not running application
50 	@results		Should delete the application object.
51 */
QuitTest1()52 void AppQuitTester::QuitTest1()
53 {
54 	BApplication app("application/x-vnd.obos-app-quit-test");
55 	const char *output =
56 		"error: 0\n"
57 		"InitCheck(): 0\n"
58 		"BApplication::~BApplication()\n";
59 	// run the apps
60 	AppRunner runner;
61 	CHK(runner.Run("AppQuitTestApp1") == B_OK);
62 	runner.WaitFor(false);
63 	// get the output and compare the result
64 	check_output(runner, output);
65 }
66 
67 /*
68 	void Quit()
69 	@case 2			running application, call from looper thread
70 	@results		Run() should return. Should not delete the application
71 					object.
72 */
QuitTest2()73 void AppQuitTester::QuitTest2()
74 {
75 	BApplication app("application/x-vnd.obos-app-quit-test");
76 	const char *output =
77 		"error: 0\n"
78 		"InitCheck(): 0\n"
79 		"BApplication::ReadyToRun()\n"
80 		"BApplication::Run() done: 1\n"
81 		"BApplication::~BApplication()\n";
82 	// run the apps
83 	AppRunner runner;
84 	CHK(runner.Run("AppQuitTestApp2") == B_OK);
85 	runner.WaitFor(false);
86 	// get the output and compare the result
87 	check_output(runner, output);
88 }
89 
90 /*
91 	void Quit()
92 	@case 3			running application, call from other thread
93 	@results		Run() should return. Should not delete the application
94 					object.
95 */
QuitTest3()96 void AppQuitTester::QuitTest3()
97 {
98 	BApplication app("application/x-vnd.obos-app-quit-test");
99 	const char *output =
100 		"error: 0\n"
101 		"InitCheck(): 0\n"
102 		"BApplication::ReadyToRun()\n"
103 		"BApplication::Run() done: 1\n"
104 		"BApplication::~BApplication()\n";
105 	// run the apps
106 	AppRunner runner;
107 	CHK(runner.Run("AppQuitTestApp3") == B_OK);
108 	runner.WaitFor(false);
109 	// get the output and compare the result
110 	check_output(runner, output);
111 }
112 
113 /*
114 	void Quit()
115 	@case 4			running application, call from other thread, but don't
116 					lock before
117 	@results		Should print error message, but proceed anyway.
118 					Run() should return.
119 					Should not delete the application object.
120 */
QuitTest4()121 void AppQuitTester::QuitTest4()
122 {
123 	BApplication app("application/x-vnd.obos-app-quit-test");
124 	const char *output =
125 		"error: 0\n"
126 		"InitCheck(): 0\n"
127 		"BApplication::ReadyToRun()\n"
128 		"BApplication::Run() done: 1\n"
129 		"BApplication::~BApplication()\n";
130 	// run the apps
131 	PipedAppRunner runner;
132 	CHK(runner.Run("AppQuitTestApp4") == B_OK);
133 	runner.WaitFor();
134 	// get the output and compare the result
135 	BString buffer;
136 	CHK(runner.GetOutput(&buffer) == B_OK);
137 	// Remove the error message, as it contains the team ID, which we don't
138 	// know.
139 	int32 errorIndex = buffer.FindFirst(
140 		"ERROR - you must Lock the application object before calling Quit()");
141 	CHK(errorIndex >= 0);
142 	int32 errorEnd = buffer.FindFirst('\n', errorIndex);
143 	CHK(errorEnd >= 0);
144 	buffer.Remove(errorIndex, errorEnd - errorIndex + 1);
145 if (buffer != output)
146 printf("result is `%s', but should be `%s'\n", buffer.String(),
147 output);
148 	CHK(buffer == output);
149 }
150 
151 
Suite()152 Test* AppQuitTester::Suite()
153 {
154 	TestSuite* SuiteOfTests = new TestSuite;
155 
156 	ADD_TEST4(BApplication, SuiteOfTests, AppQuitTester, QuitTest1);
157 	ADD_TEST4(BApplication, SuiteOfTests, AppQuitTester, QuitTest2);
158 	ADD_TEST4(BApplication, SuiteOfTests, AppQuitTester, QuitTest3);
159 	ADD_TEST4(BApplication, SuiteOfTests, AppQuitTester, QuitTest4);
160 
161 	return SuiteOfTests;
162 }
163 
164 
165 
166