sourcehook/test/test4.cpp
branchsourcemm-1.6.0
changeset 347 e82968c21606
child 361 f34718303c8f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sourcehook/test/test4.cpp	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,420 @@
     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 +	MAKE_STATE_2(State_PluginInUse, int, bool);
    1.26 +
    1.27 +	class Test
    1.28 +	{
    1.29 +	public:
    1.30 +		virtual void Func1()
    1.31 +		{
    1.32 +			ADD_STATE(State_Func1_Called);
    1.33 +		}
    1.34 +
    1.35 +		virtual void Func2()
    1.36 +		{
    1.37 +			ADD_STATE(State_Func2_Called);
    1.38 +		}
    1.39 +
    1.40 +		virtual void Func3()
    1.41 +		{
    1.42 +			ADD_STATE(State_Func3_Called);
    1.43 +		}
    1.44 +	};
    1.45 +
    1.46 +	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 0);
    1.47 +	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 0);
    1.48 +	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 0);
    1.49 +
    1.50 +	void Handler_Func1()
    1.51 +	{
    1.52 +		ADD_STATE(State_Func1H_Called);
    1.53 +	}
    1.54 +	void Handler_Func2()
    1.55 +	{
    1.56 +		ADD_STATE(State_Func2H_Called);
    1.57 +	}
    1.58 +	void Handler_Func3()
    1.59 +	{
    1.60 +		ADD_STATE(State_Func3H_Called);
    1.61 +	}
    1.62 +}
    1.63 +
    1.64 +bool TestPlugSys(std::string &error)
    1.65 +{
    1.66 +	GET_SHPTR(g_SHPtr);
    1.67 +	g_PLID = 1;
    1.68 +
    1.69 +	Test inst;
    1.70 +	Test *pInst = &inst;
    1.71 +
    1.72 +	// 1)
    1.73 +	//   Add hooks, then issue a complete shutdown
    1.74 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
    1.75 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
    1.76 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
    1.77 +
    1.78 +	pInst->Func1();
    1.79 +	pInst->Func2();
    1.80 +	pInst->Func3();
    1.81 +
    1.82 +	CHECK_STATES((&g_States,
    1.83 +		new State_Func1H_Called,
    1.84 +		new State_Func1_Called,
    1.85 +		new State_Func2_Called,
    1.86 +		new State_Func2H_Called,
    1.87 +		new State_Func3H_Called,
    1.88 +		new State_Func3_Called,
    1.89 +		NULL), "Part 1.1");
    1.90 +
    1.91 +	Test_CompleteShutdown(g_SHPtr);
    1.92 +
    1.93 +	pInst->Func1();
    1.94 +	pInst->Func2();
    1.95 +	pInst->Func3();
    1.96 +
    1.97 +	CHECK_STATES((&g_States,
    1.98 +		new State_Func1_Called,
    1.99 +		new State_Func2_Called,
   1.100 +		new State_Func3_Called,
   1.101 +		NULL), "Part 1.2");
   1.102 +
   1.103 +	// 2)
   1.104 +	//   Add hooks from "different plugins", then shutdown the plugins
   1.105 +
   1.106 +	g_PLID = 1;
   1.107 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.108 +	g_PLID = 2;
   1.109 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.110 +	g_PLID = 3;
   1.111 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.112 +
   1.113 +	g_PLID = 1;
   1.114 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.115 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.116 +
   1.117 +	g_PLID = 2;
   1.118 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.119 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.120 +
   1.121 +	g_PLID = 3;
   1.122 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.123 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.124 +
   1.125 +	pInst->Func1();
   1.126 +	pInst->Func2();
   1.127 +	pInst->Func3();
   1.128 +
   1.129 +	CHECK_STATES((&g_States,
   1.130 +		new State_Func1H_Called,
   1.131 +		new State_Func1H_Called,
   1.132 +		new State_Func1H_Called,
   1.133 +		new State_Func1_Called,
   1.134 +		new State_Func2_Called,
   1.135 +		new State_Func2H_Called,
   1.136 +		new State_Func2H_Called,
   1.137 +		new State_Func2H_Called,
   1.138 +		new State_Func3H_Called,
   1.139 +		new State_Func3H_Called,
   1.140 +		new State_Func3H_Called,
   1.141 +		new State_Func3_Called,
   1.142 +		NULL), "Part 2.1");
   1.143 +
   1.144 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.145 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.146 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.147 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.148 +
   1.149 +	CHECK_STATES((&g_States,
   1.150 +		new State_PluginInUse(1, true),
   1.151 +		new State_PluginInUse(2, true),
   1.152 +		new State_PluginInUse(3, true),
   1.153 +		new State_PluginInUse(4, false),
   1.154 +		NULL), "Part 2.2");
   1.155 +
   1.156 +	// Unload plugins one by one
   1.157 +	Test_UnloadPlugin(g_SHPtr, 3);
   1.158 +
   1.159 +	pInst->Func1();
   1.160 +	pInst->Func2();
   1.161 +	pInst->Func3();
   1.162 +
   1.163 +	CHECK_STATES((&g_States,
   1.164 +		new State_Func1H_Called,
   1.165 +		new State_Func1H_Called,
   1.166 +		new State_Func1_Called,
   1.167 +		new State_Func2_Called,
   1.168 +		new State_Func2H_Called,
   1.169 +		new State_Func2H_Called,
   1.170 +		new State_Func3H_Called,
   1.171 +		new State_Func3H_Called,
   1.172 +		new State_Func3_Called,
   1.173 +		NULL), "Part 2.3.1");
   1.174 +
   1.175 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.176 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.177 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.178 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.179 +
   1.180 +	CHECK_STATES((&g_States,
   1.181 +		new State_PluginInUse(1, true),
   1.182 +		new State_PluginInUse(2, true),
   1.183 +		new State_PluginInUse(3, false),
   1.184 +		new State_PluginInUse(4, false),
   1.185 +		NULL), "Part 2.3.2");
   1.186 +
   1.187 +	Test_UnloadPlugin(g_SHPtr, 2);
   1.188 +
   1.189 +	pInst->Func1();
   1.190 +	pInst->Func2();
   1.191 +	pInst->Func3();
   1.192 +
   1.193 +	CHECK_STATES((&g_States,
   1.194 +		new State_Func1H_Called,
   1.195 +		new State_Func1_Called,
   1.196 +		new State_Func2_Called,
   1.197 +		new State_Func2H_Called,
   1.198 +		new State_Func3H_Called,
   1.199 +		new State_Func3_Called,
   1.200 +		NULL), "Part 2.4.1");
   1.201 +
   1.202 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.203 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.204 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.205 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.206 +
   1.207 +	CHECK_STATES((&g_States,
   1.208 +		new State_PluginInUse(1, true),
   1.209 +		new State_PluginInUse(2, false),
   1.210 +		new State_PluginInUse(3, false),
   1.211 +		new State_PluginInUse(4, false),
   1.212 +		NULL), "Part 2.4.2");
   1.213 +
   1.214 +	Test_UnloadPlugin(g_SHPtr, 1);
   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_Func1_Called,
   1.222 +		new State_Func2_Called,
   1.223 +		new State_Func3_Called,
   1.224 +		NULL), "Part 2.5.1");
   1.225 +
   1.226 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.227 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.228 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.229 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.230 +
   1.231 +	CHECK_STATES((&g_States,
   1.232 +		new State_PluginInUse(1, false),
   1.233 +		new State_PluginInUse(2, false),
   1.234 +		new State_PluginInUse(3, false),
   1.235 +		new State_PluginInUse(4, false),
   1.236 +		NULL), "Part 2.5.2");
   1.237 +
   1.238 +
   1.239 +	// 3)
   1.240 +	//   Add hooks from "different plugins", then pause the plugins
   1.241 +
   1.242 +	g_PLID = 1;
   1.243 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.244 +	g_PLID = 2;
   1.245 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.246 +	g_PLID = 3;
   1.247 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.248 +
   1.249 +	g_PLID = 1;
   1.250 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.251 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.252 +
   1.253 +	g_PLID = 2;
   1.254 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.255 +	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   1.256 +
   1.257 +	g_PLID = 3;
   1.258 +	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   1.259 +	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   1.260 +
   1.261 +	pInst->Func1();
   1.262 +	pInst->Func2();
   1.263 +	pInst->Func3();
   1.264 +
   1.265 +	CHECK_STATES((&g_States,
   1.266 +		new State_Func1H_Called,
   1.267 +		new State_Func1H_Called,
   1.268 +		new State_Func1H_Called,
   1.269 +		new State_Func1_Called,
   1.270 +		new State_Func2_Called,
   1.271 +		new State_Func2H_Called,
   1.272 +		new State_Func2H_Called,
   1.273 +		new State_Func2H_Called,
   1.274 +		new State_Func3H_Called,
   1.275 +		new State_Func3H_Called,
   1.276 +		new State_Func3H_Called,
   1.277 +		new State_Func3_Called,
   1.278 +		NULL), "Part 3.1");
   1.279 +
   1.280 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.281 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.282 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.283 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.284 +
   1.285 +	CHECK_STATES((&g_States,
   1.286 +		new State_PluginInUse(1, true),
   1.287 +		new State_PluginInUse(2, true),
   1.288 +		new State_PluginInUse(3, true),
   1.289 +		new State_PluginInUse(4, false),
   1.290 +		NULL), "Part 3.2");
   1.291 +
   1.292 +	// Unload plugins one by one
   1.293 +	Test_PausePlugin(g_SHPtr, 3);
   1.294 +
   1.295 +	pInst->Func1();
   1.296 +	pInst->Func2();
   1.297 +	pInst->Func3();
   1.298 +
   1.299 +	CHECK_STATES((&g_States,
   1.300 +		new State_Func1H_Called,
   1.301 +		new State_Func1H_Called,
   1.302 +		new State_Func1_Called,
   1.303 +		new State_Func2_Called,
   1.304 +		new State_Func2H_Called,
   1.305 +		new State_Func2H_Called,
   1.306 +		new State_Func3H_Called,
   1.307 +		new State_Func3H_Called,
   1.308 +		new State_Func3_Called,
   1.309 +		NULL), "Part 3.3.1");
   1.310 +
   1.311 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.312 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.313 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.314 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.315 +
   1.316 +	CHECK_STATES((&g_States,
   1.317 +		new State_PluginInUse(1, true),
   1.318 +		new State_PluginInUse(2, true),
   1.319 +		new State_PluginInUse(3, true),
   1.320 +		new State_PluginInUse(4, false),
   1.321 +		NULL), "Part 3.3.2");
   1.322 +
   1.323 +	Test_PausePlugin(g_SHPtr, 2);
   1.324 +
   1.325 +	pInst->Func1();
   1.326 +	pInst->Func2();
   1.327 +	pInst->Func3();
   1.328 +
   1.329 +	CHECK_STATES((&g_States,
   1.330 +		new State_Func1H_Called,
   1.331 +		new State_Func1_Called,
   1.332 +		new State_Func2_Called,
   1.333 +		new State_Func2H_Called,
   1.334 +		new State_Func3H_Called,
   1.335 +		new State_Func3_Called,
   1.336 +		NULL), "Part 3.4.1");
   1.337 +
   1.338 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.339 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.340 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.341 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.342 +
   1.343 +	CHECK_STATES((&g_States,
   1.344 +		new State_PluginInUse(1, true),
   1.345 +		new State_PluginInUse(2, true),
   1.346 +		new State_PluginInUse(3, true),
   1.347 +		new State_PluginInUse(4, false),
   1.348 +		NULL), "Part 3.4.2");
   1.349 +
   1.350 +	Test_PausePlugin(g_SHPtr, 1);
   1.351 +
   1.352 +	pInst->Func1();
   1.353 +	pInst->Func2();
   1.354 +	pInst->Func3();
   1.355 +
   1.356 +	CHECK_STATES((&g_States,
   1.357 +		new State_Func1_Called,
   1.358 +		new State_Func2_Called,
   1.359 +		new State_Func3_Called,
   1.360 +		NULL), "Part 3.5.1");
   1.361 +
   1.362 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.363 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.364 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.365 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.366 +
   1.367 +	CHECK_STATES((&g_States,
   1.368 +		new State_PluginInUse(1, true),
   1.369 +		new State_PluginInUse(2, true),
   1.370 +		new State_PluginInUse(3, true),
   1.371 +		new State_PluginInUse(4, false),
   1.372 +		NULL), "Part 3.5.2");
   1.373 +
   1.374 +	Test_UnpausePlugin(g_SHPtr, 1);
   1.375 +	Test_UnpausePlugin(g_SHPtr, 2);
   1.376 +	Test_UnpausePlugin(g_SHPtr, 3);
   1.377 +
   1.378 +	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   1.379 +	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   1.380 +	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   1.381 +	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   1.382 +
   1.383 +	CHECK_STATES((&g_States,
   1.384 +		new State_PluginInUse(1, true),
   1.385 +		new State_PluginInUse(2, true),
   1.386 +		new State_PluginInUse(3, true),
   1.387 +		new State_PluginInUse(4, false),
   1.388 +		NULL), "Part 3.6");
   1.389 +
   1.390 +	pInst->Func1();
   1.391 +	pInst->Func2();
   1.392 +	pInst->Func3();
   1.393 +
   1.394 +	CHECK_STATES((&g_States,
   1.395 +		new State_Func1H_Called,
   1.396 +		new State_Func1H_Called,
   1.397 +		new State_Func1H_Called,
   1.398 +		new State_Func1_Called,
   1.399 +		new State_Func2_Called,
   1.400 +		new State_Func2H_Called,
   1.401 +		new State_Func2H_Called,
   1.402 +		new State_Func2H_Called,
   1.403 +		new State_Func3H_Called,
   1.404 +		new State_Func3H_Called,
   1.405 +		new State_Func3H_Called,
   1.406 +		new State_Func3_Called,
   1.407 +		NULL), "Part 3.7");
   1.408 +
   1.409 +	// 4) Shutdown :)
   1.410 +	Test_CompleteShutdown(g_SHPtr);
   1.411 +
   1.412 +	pInst->Func1();
   1.413 +	pInst->Func2();
   1.414 +	pInst->Func3();
   1.415 +
   1.416 +	CHECK_STATES((&g_States,
   1.417 +		new State_Func1_Called,
   1.418 +		new State_Func2_Called,
   1.419 +		new State_Func3_Called,
   1.420 +		NULL), "Part 4");
   1.421 +
   1.422 +	return true;
   1.423 +}