xref: /haiku/src/tests/kits/app/broster/LaunchTesterHelper.h (revision 5993f44c275e36959d7728bdd11fd8ac018633d7)
1be074227SIngo Weinhold //	LaunchTesterHelper.h
2be074227SIngo Weinhold 
3be074227SIngo Weinhold #ifndef LAUNCH_TESTER_HELPER_H
4be074227SIngo Weinhold #define LAUNCH_TESTER_HELPER_H
5be074227SIngo Weinhold 
6be074227SIngo Weinhold #include <Application.h>
701cf44b7SIngo Weinhold #include <List.h>
8be074227SIngo Weinhold #include <Locker.h>
9be074227SIngo Weinhold #include <MessageQueue.h>
10be074227SIngo Weinhold #include <OS.h>
11be074227SIngo Weinhold 
12be074227SIngo Weinhold // LaunchCaller
13be074227SIngo Weinhold class LaunchCaller {
14be074227SIngo Weinhold public:
LaunchCaller()1548bf189fSIngo Weinhold 	LaunchCaller() : fNext(NULL) {}
~LaunchCaller()1648bf189fSIngo Weinhold 	virtual ~LaunchCaller() { if (fNext) delete fNext;}
1748bf189fSIngo Weinhold 
1801cf44b7SIngo Weinhold 	virtual status_t operator()(const char *type, BList *messages, int32 argc,
1901cf44b7SIngo Weinhold 								const char **argv, team_id *team) = 0;
SupportsMessages()2001cf44b7SIngo Weinhold 	virtual int32 SupportsMessages() const { return 1; }
SupportsArgv()2101cf44b7SIngo Weinhold 	virtual bool SupportsArgv() const { return SupportsMessages() == 0; }
SupportsRefs()2201cf44b7SIngo Weinhold 	virtual bool SupportsRefs() const { return false; }
Ref()2301cf44b7SIngo Weinhold 	virtual const entry_ref *Ref() const { return NULL; }
2448bf189fSIngo Weinhold 
Clone()2548bf189fSIngo Weinhold 	virtual LaunchCaller &Clone()
2648bf189fSIngo Weinhold 	{
2748bf189fSIngo Weinhold 		LaunchCaller *newCaller = CloneInternal();
2848bf189fSIngo Weinhold 		newCaller->fNext = fNext;
2948bf189fSIngo Weinhold 		fNext = newCaller;
3048bf189fSIngo Weinhold 		return *newCaller;
3148bf189fSIngo Weinhold 	}
3248bf189fSIngo Weinhold 
3348bf189fSIngo Weinhold 	virtual LaunchCaller *CloneInternal() = 0;
3448bf189fSIngo Weinhold 
3548bf189fSIngo Weinhold private:
3648bf189fSIngo Weinhold 	LaunchCaller	*fNext;
37be074227SIngo Weinhold };
38be074227SIngo Weinhold 
39be074227SIngo Weinhold // LaunchContext
40be074227SIngo Weinhold class LaunchContext {
41be074227SIngo Weinhold public:
4248bf189fSIngo Weinhold 	LaunchContext();
43be074227SIngo Weinhold 	~LaunchContext();
44be074227SIngo Weinhold 
4548bf189fSIngo Weinhold 	status_t operator()(LaunchCaller &caller, const char *type, team_id *team);
4648bf189fSIngo Weinhold 	status_t operator()(LaunchCaller &caller, const char *type,
4748bf189fSIngo Weinhold 						BList *messages, int32 argc, const char **argv,
4848bf189fSIngo Weinhold 						team_id *team);
49be074227SIngo Weinhold 
50be074227SIngo Weinhold 	void HandleMessage(BMessage *message);
51be074227SIngo Weinhold 
52be074227SIngo Weinhold 	void Terminate();
53*5993f44cSIngo Weinhold 	void TerminateApp(team_id team, bool wait = true);
54be074227SIngo Weinhold 
55be074227SIngo Weinhold 	team_id TeamAt(int32 index) const;
56be074227SIngo Weinhold 
57*5993f44cSIngo Weinhold 	BMessenger AppMessengerFor(team_id team) const;
58*5993f44cSIngo Weinhold 
59be074227SIngo Weinhold 	BMessage *NextMessageFrom(team_id team, int32 &cookie,
60be074227SIngo Weinhold 							  bigtime_t *time = NULL);
6148bf189fSIngo Weinhold 	bool CheckNextMessage(LaunchCaller &caller, team_id team, int32 &cookie,
6248bf189fSIngo Weinhold 						  uint32 what);
6394737946SIngo Weinhold 	bool CheckMainArgsMessage(LaunchCaller &caller, team_id team,
6494737946SIngo Weinhold 							  int32 &cookie, const entry_ref *appRef,
6594737946SIngo Weinhold 							  bool useRef = true);
6694737946SIngo Weinhold 	bool CheckMainArgsMessage(LaunchCaller &caller, team_id team,
6794737946SIngo Weinhold 							  int32 &cookie, const entry_ref *appRef,
6894737946SIngo Weinhold 							  int32 argc, const char **argv,
6994737946SIngo Weinhold 							  bool useRef = true);
7048bf189fSIngo Weinhold 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
7101cf44b7SIngo Weinhold 						  const entry_ref *appRef, bool useRef = true);
7248bf189fSIngo Weinhold 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
7348bf189fSIngo Weinhold 						  const entry_ref *appRef,
7401cf44b7SIngo Weinhold 						  int32 argc, const char **argv, bool useRef = true);
7548bf189fSIngo Weinhold 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
7648bf189fSIngo Weinhold 						  const entry_ref *appRef, const entry_ref *ref,
7748bf189fSIngo Weinhold 						  int32 argc, const char **argv);
7894737946SIngo Weinhold 	bool CheckArgsMessage(LaunchCaller &caller, team_id team, int32 &cookie,
7994737946SIngo Weinhold 						  const entry_ref *appRef, const entry_ref *ref,
8094737946SIngo Weinhold 						  int32 argc, const char **argv, uint32 messageCode);
8148bf189fSIngo Weinhold 	bool CheckMessageMessages(LaunchCaller &caller, team_id team,
8248bf189fSIngo Weinhold 							  int32 &cookie);
8348bf189fSIngo Weinhold 	bool CheckMessageMessage(LaunchCaller &caller, team_id team, int32 &cookie,
8448bf189fSIngo Weinhold 							 int32 index);
8548bf189fSIngo Weinhold 	bool CheckMessageMessage(LaunchCaller &caller, team_id team, int32 &cookie,
8601cf44b7SIngo Weinhold 							 const BMessage *message);
8748bf189fSIngo Weinhold 	bool CheckRefsMessage(LaunchCaller &caller, team_id team, int32 &cookie);
8848bf189fSIngo Weinhold 	bool CheckRefsMessage(LaunchCaller &caller, team_id team, int32 &cookie,
8948bf189fSIngo Weinhold 						  const entry_ref *refs, int32 count = 1);
9048bf189fSIngo Weinhold 
9148bf189fSIngo Weinhold 	bool WaitForMessage(uint32 messageCode, bool fromNow = false,
9248bf189fSIngo Weinhold 						bigtime_t timeout = B_INFINITE_TIMEOUT);
9348bf189fSIngo Weinhold 	bool WaitForMessage(team_id team, uint32 messageCode, bool fromNow = false,
94*5993f44cSIngo Weinhold 						bigtime_t timeout = B_INFINITE_TIMEOUT,
95*5993f44cSIngo Weinhold 						int32 startIndex = 0);
9601cf44b7SIngo Weinhold 
9701cf44b7SIngo Weinhold 	BList *StandardMessages();
9801cf44b7SIngo Weinhold 
9901cf44b7SIngo Weinhold public:
10001cf44b7SIngo Weinhold 	static const char *kStandardArgv[];
10101cf44b7SIngo Weinhold 	static const int32 kStandardArgc;
102be074227SIngo Weinhold 
103be074227SIngo Weinhold private:
104be074227SIngo Weinhold 	class Message;
10548bf189fSIngo Weinhold 	class Sleeper;
106be074227SIngo Weinhold 	class AppInfo;
107be074227SIngo Weinhold 
108be074227SIngo Weinhold private:
109be074227SIngo Weinhold 	AppInfo *AppInfoAt(int32 index) const;
110be074227SIngo Weinhold 	AppInfo *AppInfoFor(team_id team) const;
111be074227SIngo Weinhold 	AppInfo *CreateAppInfo(team_id team, const BMessenger *messenger = NULL);
112be074227SIngo Weinhold 	AppInfo *CreateAppInfo(BMessenger messenger);
113be074227SIngo Weinhold 	void TerminateApp(AppInfo *info);
114be074227SIngo Weinhold 
11548bf189fSIngo Weinhold 	Message *FindMessage(uint32 messageCode);
11648bf189fSIngo Weinhold 	void AddSleeper(Sleeper *sleeper);
11748bf189fSIngo Weinhold 	void RemoveSleeper(Sleeper *sleeper);
11848bf189fSIngo Weinhold 	void NotifySleepers(uint32 messageCode);
11948bf189fSIngo Weinhold 
120be074227SIngo Weinhold 	int32 Terminator();
121be074227SIngo Weinhold 
122be074227SIngo Weinhold 	static int32 AppThreadEntry(void *data);
123be074227SIngo Weinhold 	static int32 TerminatorEntry(void *data);
124be074227SIngo Weinhold 
125be074227SIngo Weinhold private:
126be074227SIngo Weinhold 	BList			fAppInfos;
12748bf189fSIngo Weinhold 	BList			fSleepers;
128be074227SIngo Weinhold 	mutable BLocker	fLock;
129be074227SIngo Weinhold 	thread_id		fAppThread;
130be074227SIngo Weinhold 	thread_id		fTerminator;
131be074227SIngo Weinhold 	bool			fTerminating;
13201cf44b7SIngo Weinhold 	BList			fStandardMessages;
133be074227SIngo Weinhold };
134be074227SIngo Weinhold 
135be074227SIngo Weinhold // RosterLaunchApp
136be074227SIngo Weinhold class RosterLaunchApp : public BApplication {
137be074227SIngo Weinhold public:
138be074227SIngo Weinhold 	RosterLaunchApp(const char *signature);
139c8dbd6eaSIngo Weinhold 	~RosterLaunchApp();
140be074227SIngo Weinhold 
141be074227SIngo Weinhold 	virtual void MessageReceived(BMessage *message);
142be074227SIngo Weinhold 
143be074227SIngo Weinhold 	void SetLaunchContext(LaunchContext *context);
144be074227SIngo Weinhold 	LaunchContext *GetLaunchContext() const;
145be074227SIngo Weinhold 
Handler()146c8dbd6eaSIngo Weinhold 	BHandler *Handler() { return fHandler; }
147c8dbd6eaSIngo Weinhold 	void SetHandler(BHandler *handler);
148c8dbd6eaSIngo Weinhold 
149be074227SIngo Weinhold private:
150be074227SIngo Weinhold 	LaunchContext	*fLaunchContext;
151c8dbd6eaSIngo Weinhold 	BHandler		*fHandler;
152c8dbd6eaSIngo Weinhold };
153c8dbd6eaSIngo Weinhold 
154c8dbd6eaSIngo Weinhold // RosterBroadcastHandler
155c8dbd6eaSIngo Weinhold class RosterBroadcastHandler : public BHandler {
156c8dbd6eaSIngo Weinhold public:
157c8dbd6eaSIngo Weinhold 	RosterBroadcastHandler();
158c8dbd6eaSIngo Weinhold 
159c8dbd6eaSIngo Weinhold 	virtual void MessageReceived(BMessage *message);
160be074227SIngo Weinhold };
161be074227SIngo Weinhold 
162be074227SIngo Weinhold #endif	// LAUNCH_TESTER_HELPER_H
163