sourcehook/test/test4.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri Dec 23 11:58:11 2005 +0000 (2005-12-23)
changeset 162 a95db73792e5
parent 60 d44f93f0677a
child 294 0d87bc8606f8
permissions -rw-r--r--
One bugfix, addition of META_RETURN_(VALUE_)NEWPARAMS (implemented using recalls), tests now support compiling against an older version of sourcehook.h (through the sourcehook_test.h file) while main.cpp which now also acts as a CSourceHookImpl factory compiles against the new version.
     1 #include <string>
     2 #include "sourcehook_test.h"
     3 #include "testevents.h"
     4 
     5 // TEST4
     6 // Tests of plugin management system
     7 
     8 namespace
     9 {
    10 	StateList g_States;
    11 	SourceHook::ISourceHook *g_SHPtr;
    12 	SourceHook::Plugin g_PLID;
    13 
    14 	MAKE_STATE(State_Func1_Called);
    15 	MAKE_STATE(State_Func2_Called);
    16 	MAKE_STATE(State_Func3_Called);
    17 
    18 	MAKE_STATE(State_Func1H_Called);
    19 	MAKE_STATE(State_Func2H_Called);
    20 	MAKE_STATE(State_Func3H_Called);
    21 
    22 	MAKE_STATE_2(State_PluginInUse, int, bool);
    23 
    24 	class Test
    25 	{
    26 	public:
    27 		virtual void Func1()
    28 		{
    29 			ADD_STATE(State_Func1_Called);
    30 		}
    31 
    32 		virtual void Func2()
    33 		{
    34 			ADD_STATE(State_Func2_Called);
    35 		}
    36 
    37 		virtual void Func3()
    38 		{
    39 			ADD_STATE(State_Func3_Called);
    40 		}
    41 	};
    42 
    43 	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 0);
    44 	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 0);
    45 	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 0);
    46 
    47 	void Handler_Func1()
    48 	{
    49 		ADD_STATE(State_Func1H_Called);
    50 	}
    51 	void Handler_Func2()
    52 	{
    53 		ADD_STATE(State_Func2H_Called);
    54 	}
    55 	void Handler_Func3()
    56 	{
    57 		ADD_STATE(State_Func3H_Called);
    58 	}
    59 }
    60 
    61 bool TestPlugSys(std::string &error)
    62 {
    63 	GET_SHPTR(g_SHPtr);
    64 	g_PLID = 1;
    65 
    66 	Test inst;
    67 	Test *pInst = &inst;
    68 
    69 	// 1)
    70 	//   Add hooks, then issue a complete shutdown
    71 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
    72 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
    73 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
    74 
    75 	pInst->Func1();
    76 	pInst->Func2();
    77 	pInst->Func3();
    78 
    79 	CHECK_STATES((&g_States,
    80 		new State_Func1H_Called,
    81 		new State_Func1_Called,
    82 		new State_Func2_Called,
    83 		new State_Func2H_Called,
    84 		new State_Func3H_Called,
    85 		new State_Func3_Called,
    86 		NULL), "Part 1.1");
    87 
    88 	Test_CompleteShutdown(g_SHPtr);
    89 
    90 	pInst->Func1();
    91 	pInst->Func2();
    92 	pInst->Func3();
    93 
    94 	CHECK_STATES((&g_States,
    95 		new State_Func1_Called,
    96 		new State_Func2_Called,
    97 		new State_Func3_Called,
    98 		NULL), "Part 1.2");
    99 
   100 	// 2)
   101 	//   Add hooks from "different plugins", then shutdown the plugins
   102 
   103 	g_PLID = 1;
   104 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   105 	g_PLID = 2;
   106 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   107 	g_PLID = 3;
   108 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   109 
   110 	g_PLID = 1;
   111 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   112 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   113 
   114 	g_PLID = 2;
   115 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   116 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   117 
   118 	g_PLID = 3;
   119 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   120 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   121 
   122 	pInst->Func1();
   123 	pInst->Func2();
   124 	pInst->Func3();
   125 
   126 	CHECK_STATES((&g_States,
   127 		new State_Func1H_Called,
   128 		new State_Func1H_Called,
   129 		new State_Func1H_Called,
   130 		new State_Func1_Called,
   131 		new State_Func2_Called,
   132 		new State_Func2H_Called,
   133 		new State_Func2H_Called,
   134 		new State_Func2H_Called,
   135 		new State_Func3H_Called,
   136 		new State_Func3H_Called,
   137 		new State_Func3H_Called,
   138 		new State_Func3_Called,
   139 		NULL), "Part 2.1");
   140 
   141 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   142 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   143 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   144 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   145 
   146 	CHECK_STATES((&g_States,
   147 		new State_PluginInUse(1, true),
   148 		new State_PluginInUse(2, true),
   149 		new State_PluginInUse(3, true),
   150 		new State_PluginInUse(4, false),
   151 		NULL), "Part 2.2");
   152 
   153 	// Unload plugins one by one
   154 	Test_UnloadPlugin(g_SHPtr, 3);
   155 
   156 	pInst->Func1();
   157 	pInst->Func2();
   158 	pInst->Func3();
   159 
   160 	CHECK_STATES((&g_States,
   161 		new State_Func1H_Called,
   162 		new State_Func1H_Called,
   163 		new State_Func1_Called,
   164 		new State_Func2_Called,
   165 		new State_Func2H_Called,
   166 		new State_Func2H_Called,
   167 		new State_Func3H_Called,
   168 		new State_Func3H_Called,
   169 		new State_Func3_Called,
   170 		NULL), "Part 2.3.1");
   171 
   172 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   173 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   174 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   175 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   176 
   177 	CHECK_STATES((&g_States,
   178 		new State_PluginInUse(1, true),
   179 		new State_PluginInUse(2, true),
   180 		new State_PluginInUse(3, false),
   181 		new State_PluginInUse(4, false),
   182 		NULL), "Part 2.3.2");
   183 
   184 	Test_UnloadPlugin(g_SHPtr, 2);
   185 
   186 	pInst->Func1();
   187 	pInst->Func2();
   188 	pInst->Func3();
   189 
   190 	CHECK_STATES((&g_States,
   191 		new State_Func1H_Called,
   192 		new State_Func1_Called,
   193 		new State_Func2_Called,
   194 		new State_Func2H_Called,
   195 		new State_Func3H_Called,
   196 		new State_Func3_Called,
   197 		NULL), "Part 2.4.1");
   198 
   199 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   200 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   201 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   202 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   203 
   204 	CHECK_STATES((&g_States,
   205 		new State_PluginInUse(1, true),
   206 		new State_PluginInUse(2, false),
   207 		new State_PluginInUse(3, false),
   208 		new State_PluginInUse(4, false),
   209 		NULL), "Part 2.4.2");
   210 
   211 	Test_UnloadPlugin(g_SHPtr, 1);
   212 
   213 	pInst->Func1();
   214 	pInst->Func2();
   215 	pInst->Func3();
   216 
   217 	CHECK_STATES((&g_States,
   218 		new State_Func1_Called,
   219 		new State_Func2_Called,
   220 		new State_Func3_Called,
   221 		NULL), "Part 2.5.1");
   222 
   223 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   224 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   225 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   226 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   227 
   228 	CHECK_STATES((&g_States,
   229 		new State_PluginInUse(1, false),
   230 		new State_PluginInUse(2, false),
   231 		new State_PluginInUse(3, false),
   232 		new State_PluginInUse(4, false),
   233 		NULL), "Part 2.5.2");
   234 
   235 
   236 	// 3)
   237 	//   Add hooks from "different plugins", then pause the plugins
   238 
   239 	g_PLID = 1;
   240 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   241 	g_PLID = 2;
   242 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   243 	g_PLID = 3;
   244 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   245 
   246 	g_PLID = 1;
   247 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   248 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   249 
   250 	g_PLID = 2;
   251 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   252 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   253 
   254 	g_PLID = 3;
   255 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   256 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   257 
   258 	pInst->Func1();
   259 	pInst->Func2();
   260 	pInst->Func3();
   261 
   262 	CHECK_STATES((&g_States,
   263 		new State_Func1H_Called,
   264 		new State_Func1H_Called,
   265 		new State_Func1H_Called,
   266 		new State_Func1_Called,
   267 		new State_Func2_Called,
   268 		new State_Func2H_Called,
   269 		new State_Func2H_Called,
   270 		new State_Func2H_Called,
   271 		new State_Func3H_Called,
   272 		new State_Func3H_Called,
   273 		new State_Func3H_Called,
   274 		new State_Func3_Called,
   275 		NULL), "Part 3.1");
   276 
   277 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   278 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   279 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   280 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   281 
   282 	CHECK_STATES((&g_States,
   283 		new State_PluginInUse(1, true),
   284 		new State_PluginInUse(2, true),
   285 		new State_PluginInUse(3, true),
   286 		new State_PluginInUse(4, false),
   287 		NULL), "Part 3.2");
   288 
   289 	// Unload plugins one by one
   290 	Test_PausePlugin(g_SHPtr, 3);
   291 
   292 	pInst->Func1();
   293 	pInst->Func2();
   294 	pInst->Func3();
   295 
   296 	CHECK_STATES((&g_States,
   297 		new State_Func1H_Called,
   298 		new State_Func1H_Called,
   299 		new State_Func1_Called,
   300 		new State_Func2_Called,
   301 		new State_Func2H_Called,
   302 		new State_Func2H_Called,
   303 		new State_Func3H_Called,
   304 		new State_Func3H_Called,
   305 		new State_Func3_Called,
   306 		NULL), "Part 3.3.1");
   307 
   308 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   309 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   310 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   311 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   312 
   313 	CHECK_STATES((&g_States,
   314 		new State_PluginInUse(1, true),
   315 		new State_PluginInUse(2, true),
   316 		new State_PluginInUse(3, true),
   317 		new State_PluginInUse(4, false),
   318 		NULL), "Part 3.3.2");
   319 
   320 	Test_PausePlugin(g_SHPtr, 2);
   321 
   322 	pInst->Func1();
   323 	pInst->Func2();
   324 	pInst->Func3();
   325 
   326 	CHECK_STATES((&g_States,
   327 		new State_Func1H_Called,
   328 		new State_Func1_Called,
   329 		new State_Func2_Called,
   330 		new State_Func2H_Called,
   331 		new State_Func3H_Called,
   332 		new State_Func3_Called,
   333 		NULL), "Part 3.4.1");
   334 
   335 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   336 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   337 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   338 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   339 
   340 	CHECK_STATES((&g_States,
   341 		new State_PluginInUse(1, true),
   342 		new State_PluginInUse(2, true),
   343 		new State_PluginInUse(3, true),
   344 		new State_PluginInUse(4, false),
   345 		NULL), "Part 3.4.2");
   346 
   347 	Test_PausePlugin(g_SHPtr, 1);
   348 
   349 	pInst->Func1();
   350 	pInst->Func2();
   351 	pInst->Func3();
   352 
   353 	CHECK_STATES((&g_States,
   354 		new State_Func1_Called,
   355 		new State_Func2_Called,
   356 		new State_Func3_Called,
   357 		NULL), "Part 3.5.1");
   358 
   359 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   360 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   361 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   362 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   363 
   364 	CHECK_STATES((&g_States,
   365 		new State_PluginInUse(1, true),
   366 		new State_PluginInUse(2, true),
   367 		new State_PluginInUse(3, true),
   368 		new State_PluginInUse(4, false),
   369 		NULL), "Part 3.5.2");
   370 
   371 	Test_UnpausePlugin(g_SHPtr, 1);
   372 	Test_UnpausePlugin(g_SHPtr, 2);
   373 	Test_UnpausePlugin(g_SHPtr, 3);
   374 
   375 	ADD_STATE(State_PluginInUse(1, Test_IsPluginInUse(g_SHPtr, 1)));
   376 	ADD_STATE(State_PluginInUse(2, Test_IsPluginInUse(g_SHPtr, 2)));
   377 	ADD_STATE(State_PluginInUse(3, Test_IsPluginInUse(g_SHPtr, 3)));
   378 	ADD_STATE(State_PluginInUse(4, Test_IsPluginInUse(g_SHPtr, 4)));
   379 
   380 	CHECK_STATES((&g_States,
   381 		new State_PluginInUse(1, true),
   382 		new State_PluginInUse(2, true),
   383 		new State_PluginInUse(3, true),
   384 		new State_PluginInUse(4, false),
   385 		NULL), "Part 3.6");
   386 
   387 	pInst->Func1();
   388 	pInst->Func2();
   389 	pInst->Func3();
   390 
   391 	CHECK_STATES((&g_States,
   392 		new State_Func1H_Called,
   393 		new State_Func1H_Called,
   394 		new State_Func1H_Called,
   395 		new State_Func1_Called,
   396 		new State_Func2_Called,
   397 		new State_Func2H_Called,
   398 		new State_Func2H_Called,
   399 		new State_Func2H_Called,
   400 		new State_Func3H_Called,
   401 		new State_Func3H_Called,
   402 		new State_Func3H_Called,
   403 		new State_Func3_Called,
   404 		NULL), "Part 3.7");
   405 
   406 	// 4) Shutdown :)
   407 	Test_CompleteShutdown(g_SHPtr);
   408 
   409 	pInst->Func1();
   410 	pInst->Func2();
   411 	pInst->Func3();
   412 
   413 	CHECK_STATES((&g_States,
   414 		new State_Func1_Called,
   415 		new State_Func2_Called,
   416 		new State_Func3_Called,
   417 		NULL), "Part 4");
   418 
   419 	return true;
   420 }
   421