xref: /haiku/src/tests/kits/app/broster/LaunchTesterHelper.h (revision 9473794637e6f91a31ff05f49dbeb824a6d5e7ac)
1 //	LaunchTesterHelper.h
2 
3 #ifndef LAUNCH_TESTER_HELPER_H
4 #define LAUNCH_TESTER_HELPER_H
5 
6 #include <Application.h>
7 #include <List.h>
8 #include <Locker.h>
9 #include <MessageQueue.h>
10 #include <OS.h>
11 
12 enum {
13 	MSG_1	= 'msg1',
14 	MSG_2	= 'msg2',
15 	MSG_3	= 'msg3',
16 };
17 
18 // LaunchCaller
19 class LaunchCaller {
20 public:
21 	LaunchCaller() : fNext(NULL) {}
22 	virtual ~LaunchCaller() { if (fNext) delete fNext;}
23 
24 	virtual status_t operator()(const char *type, BList *messages, int32 argc,
25 								const char **argv, team_id *team) = 0;
26 	virtual int32 SupportsMessages() const { return 1; }
27 	virtual bool SupportsArgv() const { return SupportsMessages() == 0; }
28 	virtual bool SupportsRefs() const { return false; }
29 	virtual const entry_ref *Ref() const { return NULL; }
30 
31 	virtual LaunchCaller &Clone()
32 	{
33 		LaunchCaller *newCaller = CloneInternal();
34 		newCaller->fNext = fNext;
35 		fNext = newCaller;
36 		return *newCaller;
37 	}
38 
39 	virtual LaunchCaller *CloneInternal() = 0;
40 
41 private:
42 	LaunchCaller	*fNext;
43 };
44 
45 // LaunchContext
46 class LaunchContext {
47 public:
48 	LaunchContext();
49 	~LaunchContext();
50 
51 	status_t operator()(LaunchCaller &caller, const char *type, team_id *team);
52 	status_t operator()(LaunchCaller &caller, const char *type,
53 						BList *messages, int32 argc, const char **argv,
54 						team_id *team);
55 
56 	void HandleMessage(BMessage *message);
57 
58 	void Terminate();
59 
60 	team_id TeamAt(int32 index) const;
61 
62 	BMessage *NextMessageFrom(team_id team, int32 &cookie,
63 							  bigtime_t *time = NULL);
64 	bool CheckNextMessage(LaunchCaller &caller, team_id team, int32 &cookie,
65 						  uint32 what);
66 	bool CheckMainArgsMessage(LaunchCaller &caller, team_id team,
67 							  int32 &cookie, const entry_ref *appRef,
68 							  bool useRef = true);
69 	bool CheckMainArgsMessage(LaunchCaller &caller, team_id team,
70 							  int32 &cookie, const entry_ref *appRef,
71 							  int32 argc, const char **argv,
72 							  bool useRef = true);
73 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
74 						  const entry_ref *appRef, bool useRef = true);
75 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
76 						  const entry_ref *appRef,
77 						  int32 argc, const char **argv, bool useRef = true);
78 	bool CheckArgvMessage(LaunchCaller &caller, team_id team, int32 &cookie,
79 						  const entry_ref *appRef, const entry_ref *ref,
80 						  int32 argc, const char **argv);
81 	bool CheckArgsMessage(LaunchCaller &caller, team_id team, int32 &cookie,
82 						  const entry_ref *appRef, const entry_ref *ref,
83 						  int32 argc, const char **argv, uint32 messageCode);
84 	bool CheckMessageMessages(LaunchCaller &caller, team_id team,
85 							  int32 &cookie);
86 	bool CheckMessageMessage(LaunchCaller &caller, team_id team, int32 &cookie,
87 							 int32 index);
88 	bool CheckMessageMessage(LaunchCaller &caller, team_id team, int32 &cookie,
89 							 const BMessage *message);
90 	bool CheckRefsMessage(LaunchCaller &caller, team_id team, int32 &cookie);
91 	bool CheckRefsMessage(LaunchCaller &caller, team_id team, int32 &cookie,
92 						  const entry_ref *refs, int32 count = 1);
93 
94 	bool WaitForMessage(uint32 messageCode, bool fromNow = false,
95 						bigtime_t timeout = B_INFINITE_TIMEOUT);
96 	bool WaitForMessage(team_id team, uint32 messageCode, bool fromNow = false,
97 						bigtime_t timeout = B_INFINITE_TIMEOUT);
98 
99 	BList *StandardMessages();
100 
101 public:
102 	static const char *kStandardArgv[];
103 	static const int32 kStandardArgc;
104 
105 private:
106 	class Message;
107 	class Sleeper;
108 	class AppInfo;
109 
110 private:
111 	AppInfo *AppInfoAt(int32 index) const;
112 	AppInfo *AppInfoFor(team_id team) const;
113 	AppInfo *CreateAppInfo(team_id team, const BMessenger *messenger = NULL);
114 	AppInfo *CreateAppInfo(BMessenger messenger);
115 	void TerminateApp(AppInfo *info);
116 
117 	Message *FindMessage(uint32 messageCode);
118 	void AddSleeper(Sleeper *sleeper);
119 	void RemoveSleeper(Sleeper *sleeper);
120 	void NotifySleepers(uint32 messageCode);
121 
122 	int32 Terminator();
123 
124 	static int32 AppThreadEntry(void *data);
125 	static int32 TerminatorEntry(void *data);
126 
127 private:
128 	BList			fAppInfos;
129 	BList			fSleepers;
130 	mutable BLocker	fLock;
131 	thread_id		fAppThread;
132 	thread_id		fTerminator;
133 	bool			fTerminating;
134 	BList			fStandardMessages;
135 };
136 
137 // RosterLaunchApp
138 class RosterLaunchApp : public BApplication {
139 public:
140 	RosterLaunchApp(const char *signature);
141 
142 	virtual void MessageReceived(BMessage *message);
143 
144 	BMessageQueue &MessageQueue();
145 
146 	void SetLaunchContext(LaunchContext *context);
147 	LaunchContext *GetLaunchContext() const;
148 
149 private:
150 	BMessageQueue	fMessageQueue;
151 	LaunchContext	*fLaunchContext;
152 };
153 
154 #endif	// LAUNCH_TESTER_HELPER_H
155