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