core/sourcehook/test/test4.cpp
changeset 539 db914129ae01
parent 387 093c72464b35
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcehook/test/test4.cpp	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,315 @@
     1.4 +#include <string>
     1.5 +#include "sourcehook_test.h"
     1.6 +#include "testevents.h"
     1.7 +
     1.8 +// TEST4
     1.9 +// Tests of plugin management system
    1.10 +
    1.11 +namespace
    1.12 +{
    1.13 +	StateList g_States;
    1.14 +	SourceHook::ISourceHook *g_SHPtr;
    1.15 +	SourceHook::Plugin g_PLID;
    1.16 +
    1.17 +	MAKE_STATE(State_Func1_Called);
    1.18 +	MAKE_STATE(State_Func2_Called);
    1.19 +	MAKE_STATE(State_Func3_Called);
    1.20 +
    1.21 +	MAKE_STATE(State_Func1H_Called);
    1.22 +	MAKE_STATE(State_Func2H_Called);
    1.23 +	MAKE_STATE(State_Func3H_Called);
    1.24 +
    1.25 +	class Test
    1.26 +	{
    1.27 +	public:
    1.28 +		virtual void Func1()
    1.29 +		{
    1.30 +			ADD_STATE(State_Func1_Called);
    1.31 +		}
    1.32 +
    1.33 +		virtual void Func2()
    1.34 +		{
    1.35 +			ADD_STATE(State_Func2_Called);
    1.36 +		}
    1.37 +
    1.38 +		virtual void Func3()
    1.39 +		{
    1.40 +			ADD_STATE(State_Func3_Called);
    1.41 +		}
    1.42 +	};
    1.43 +
    1.44 +	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 1);
    1.45 +	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 1);
    1.46 +	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 1);
    1.47 +
    1.48 +	void Handler_Func1()
    1.49 +	{
    1.50 +		ADD_STATE(State_Func1H_Called);
    1.51 +	}
    1.52 +	void Handler_Func2()
    1.53 +	{
    1.54 +		ADD_STATE(State_Func2H_Called);
    1.55 +	}
    1.56 +	void Handler_Func3()
    1.57 +	{
    1.58 +		ADD_STATE(State_Func3H_Called);
    1.59 +	}
    1.60 +
    1.61 +	Test *MyTestFactory()
    1.62 +	{
    1.63 +		return new Test;
    1.64 +	}
    1.65 +}
    1.66 +
    1.67 +bool TestPlugSys(std::string &error)
    1.68 +{
    1.69 +	GET_SHPTR(g_SHPtr);
    1.70 +	g_PLID = 1;
    1.71 +
    1.72 +	Test *pInst = MyTestFactory();
    1.73 +	CAutoPtrDestruction<Test> apd(pInst);
    1.74 +
    1.75 +	// 1)
    1.76 +	//   Add hooks, then issue a complete shutdown
    1.77 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
    1.78 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
    1.79 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
    1.80 +
    1.81 +	pInst->Func1();
    1.82 +	pInst->Func2();
    1.83 +	pInst->Func3();
    1.84 +
    1.85 +	CHECK_STATES((&g_States,
    1.86 +		new State_Func1H_Called,
    1.87 +		new State_Func1_Called,
    1.88 +		new State_Func2_Called,
    1.89 +		new State_Func2H_Called,
    1.90 +		new State_Func3H_Called,
    1.91 +		new State_Func3_Called,
    1.92 +		NULL), "Part 1.1");
    1.93 +
    1.94 +	Test_CompleteShutdown(g_SHPtr);
    1.95 +
    1.96 +	pInst->Func1();
    1.97 +	pInst->Func2();
    1.98 +	pInst->Func3();
    1.99 +
   1.100 +	CHECK_STATES((&g_States,
   1.101 +		new State_Func1_Called,
   1.102 +		new State_Func2_Called,
   1.103 +		new State_Func3_Called,
   1.104 +		NULL), "Part 1.2");
   1.105 +
   1.106 +	// 2)
   1.107 +	//   Add hooks from "different plugins", then shutdown the plugins
   1.108 +
   1.109 +	g_PLID = 1;
   1.110 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.111 +	g_PLID = 2;
   1.112 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.113 +	g_PLID = 3;
   1.114 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.115 +
   1.116 +	g_PLID = 1;
   1.117 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.118 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.119 +
   1.120 +	g_PLID = 2;
   1.121 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.122 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.123 +
   1.124 +	g_PLID = 3;
   1.125 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.126 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.127 +
   1.128 +	pInst->Func1();
   1.129 +	pInst->Func2();
   1.130 +	pInst->Func3();
   1.131 +
   1.132 +	CHECK_STATES((&g_States,
   1.133 +		new State_Func1H_Called,
   1.134 +		new State_Func1H_Called,
   1.135 +		new State_Func1H_Called,
   1.136 +		new State_Func1_Called,
   1.137 +		new State_Func2_Called,
   1.138 +		new State_Func2H_Called,
   1.139 +		new State_Func2H_Called,
   1.140 +		new State_Func2H_Called,
   1.141 +		new State_Func3H_Called,
   1.142 +		new State_Func3H_Called,
   1.143 +		new State_Func3H_Called,
   1.144 +		new State_Func3_Called,
   1.145 +		NULL), "Part 2.1");
   1.146 +
   1.147 +	// Unload plugins one by one
   1.148 +	Test_UnloadPlugin(g_SHPtr, 3);
   1.149 +
   1.150 +	pInst->Func1();
   1.151 +	pInst->Func2();
   1.152 +	pInst->Func3();
   1.153 +
   1.154 +	CHECK_STATES((&g_States,
   1.155 +		new State_Func1H_Called,
   1.156 +		new State_Func1H_Called,
   1.157 +		new State_Func1_Called,
   1.158 +		new State_Func2_Called,
   1.159 +		new State_Func2H_Called,
   1.160 +		new State_Func2H_Called,
   1.161 +		new State_Func3H_Called,
   1.162 +		new State_Func3H_Called,
   1.163 +		new State_Func3_Called,
   1.164 +		NULL), "Part 2.3.1");
   1.165 +
   1.166 +	Test_UnloadPlugin(g_SHPtr, 2);
   1.167 +
   1.168 +	pInst->Func1();
   1.169 +	pInst->Func2();
   1.170 +	pInst->Func3();
   1.171 +
   1.172 +	CHECK_STATES((&g_States,
   1.173 +		new State_Func1H_Called,
   1.174 +		new State_Func1_Called,
   1.175 +		new State_Func2_Called,
   1.176 +		new State_Func2H_Called,
   1.177 +		new State_Func3H_Called,
   1.178 +		new State_Func3_Called,
   1.179 +		NULL), "Part 2.4.1");
   1.180 +
   1.181 +	Test_UnloadPlugin(g_SHPtr, 1);
   1.182 +
   1.183 +	pInst->Func1();
   1.184 +	pInst->Func2();
   1.185 +	pInst->Func3();
   1.186 +
   1.187 +	CHECK_STATES((&g_States,
   1.188 +		new State_Func1_Called,
   1.189 +		new State_Func2_Called,
   1.190 +		new State_Func3_Called,
   1.191 +		NULL), "Part 2.5.1");
   1.192 +
   1.193 +
   1.194 +	// 3)
   1.195 +	//   Add hooks from "different plugins", then pause the plugins
   1.196 +
   1.197 +	g_PLID = 1;
   1.198 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.199 +	g_PLID = 2;
   1.200 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.201 +	g_PLID = 3;
   1.202 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.203 +
   1.204 +	g_PLID = 1;
   1.205 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.206 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.207 +
   1.208 +	g_PLID = 2;
   1.209 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.210 +	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   1.211 +
   1.212 +	g_PLID = 3;
   1.213 +	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   1.214 +	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   1.215 +
   1.216 +	pInst->Func1();
   1.217 +	pInst->Func2();
   1.218 +	pInst->Func3();
   1.219 +
   1.220 +	CHECK_STATES((&g_States,
   1.221 +		new State_Func1H_Called,
   1.222 +		new State_Func1H_Called,
   1.223 +		new State_Func1H_Called,
   1.224 +		new State_Func1_Called,
   1.225 +		new State_Func2_Called,
   1.226 +		new State_Func2H_Called,
   1.227 +		new State_Func2H_Called,
   1.228 +		new State_Func2H_Called,
   1.229 +		new State_Func3H_Called,
   1.230 +		new State_Func3H_Called,
   1.231 +		new State_Func3H_Called,
   1.232 +		new State_Func3_Called,
   1.233 +		NULL), "Part 3.1");
   1.234 +
   1.235 +	// Unload plugins one by one
   1.236 +	Test_PausePlugin(g_SHPtr, 3);
   1.237 +
   1.238 +	pInst->Func1();
   1.239 +	pInst->Func2();
   1.240 +	pInst->Func3();
   1.241 +
   1.242 +	CHECK_STATES((&g_States,
   1.243 +		new State_Func1H_Called,
   1.244 +		new State_Func1H_Called,
   1.245 +		new State_Func1_Called,
   1.246 +		new State_Func2_Called,
   1.247 +		new State_Func2H_Called,
   1.248 +		new State_Func2H_Called,
   1.249 +		new State_Func3H_Called,
   1.250 +		new State_Func3H_Called,
   1.251 +		new State_Func3_Called,
   1.252 +		NULL), "Part 3.3.1");
   1.253 +
   1.254 +	Test_PausePlugin(g_SHPtr, 2);
   1.255 +
   1.256 +	pInst->Func1();
   1.257 +	pInst->Func2();
   1.258 +	pInst->Func3();
   1.259 +
   1.260 +	CHECK_STATES((&g_States,
   1.261 +		new State_Func1H_Called,
   1.262 +		new State_Func1_Called,
   1.263 +		new State_Func2_Called,
   1.264 +		new State_Func2H_Called,
   1.265 +		new State_Func3H_Called,
   1.266 +		new State_Func3_Called,
   1.267 +		NULL), "Part 3.4.1");
   1.268 +
   1.269 +	Test_PausePlugin(g_SHPtr, 1);
   1.270 +
   1.271 +	pInst->Func1();
   1.272 +	pInst->Func2();
   1.273 +	pInst->Func3();
   1.274 +
   1.275 +	CHECK_STATES((&g_States,
   1.276 +		new State_Func1_Called,
   1.277 +		new State_Func2_Called,
   1.278 +		new State_Func3_Called,
   1.279 +		NULL), "Part 3.5.1");
   1.280 +
   1.281 +	Test_UnpausePlugin(g_SHPtr, 1);
   1.282 +	Test_UnpausePlugin(g_SHPtr, 2);
   1.283 +	Test_UnpausePlugin(g_SHPtr, 3);
   1.284 +
   1.285 +	pInst->Func1();
   1.286 +	pInst->Func2();
   1.287 +	pInst->Func3();
   1.288 +
   1.289 +	CHECK_STATES((&g_States,
   1.290 +		new State_Func1H_Called,
   1.291 +		new State_Func1H_Called,
   1.292 +		new State_Func1H_Called,
   1.293 +		new State_Func1_Called,
   1.294 +		new State_Func2_Called,
   1.295 +		new State_Func2H_Called,
   1.296 +		new State_Func2H_Called,
   1.297 +		new State_Func2H_Called,
   1.298 +		new State_Func3H_Called,
   1.299 +		new State_Func3H_Called,
   1.300 +		new State_Func3H_Called,
   1.301 +		new State_Func3_Called,
   1.302 +		NULL), "Part 3.7");
   1.303 +
   1.304 +	// 4) Shutdown :)
   1.305 +	Test_CompleteShutdown(g_SHPtr);
   1.306 +
   1.307 +	pInst->Func1();
   1.308 +	pInst->Func2();
   1.309 +	pInst->Func3();
   1.310 +
   1.311 +	CHECK_STATES((&g_States,
   1.312 +		new State_Func1_Called,
   1.313 +		new State_Func2_Called,
   1.314 +		new State_Func3_Called,
   1.315 +		NULL), "Part 4");
   1.316 +
   1.317 +	return true;
   1.318 +}