sourcehook/test/test4.cpp
author Pavol Marko <pavolmarko@gmail.com>
Thu May 05 14:00:08 2005 +0000 (2005-05-05)
changeset 60 d44f93f0677a
child 162 a95db73792e5
permissions -rw-r--r--
Added test4.cpp for plugin managment system testing, added -v parameter for verbose output on test fail
     1 #include <string>
     2 #include "sourcehook_impl.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 	SourceHook::CSourceHookImpl g_SHImpl;
    64 	g_SHPtr = &g_SHImpl;
    65 	g_PLID = 1;
    66 
    67 	Test inst;
    68 	Test *pInst = &inst;
    69 
    70 	// 1)
    71 	//   Add hooks, then issue a complete shutdown
    72 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
    73 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
    74 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
    75 
    76 	pInst->Func1();
    77 	pInst->Func2();
    78 	pInst->Func3();
    79 
    80 	CHECK_STATES((&g_States,
    81 		new State_Func1H_Called,
    82 		new State_Func1_Called,
    83 		new State_Func2_Called,
    84 		new State_Func2H_Called,
    85 		new State_Func3H_Called,
    86 		new State_Func3_Called,
    87 		NULL), "Part 1.1");
    88 
    89 	g_SHImpl.CompleteShutdown();
    90 
    91 	pInst->Func1();
    92 	pInst->Func2();
    93 	pInst->Func3();
    94 
    95 	CHECK_STATES((&g_States,
    96 		new State_Func1_Called,
    97 		new State_Func2_Called,
    98 		new State_Func3_Called,
    99 		NULL), "Part 1.2");
   100 
   101 	// 2)
   102 	//   Add hooks from "different plugins", then shutdown the plugins
   103 
   104 	g_PLID = 1;
   105 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   106 	g_PLID = 2;
   107 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   108 	g_PLID = 3;
   109 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   110 
   111 	g_PLID = 1;
   112 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   113 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   114 
   115 	g_PLID = 2;
   116 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   117 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   118 
   119 	g_PLID = 3;
   120 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   121 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   122 
   123 	pInst->Func1();
   124 	pInst->Func2();
   125 	pInst->Func3();
   126 
   127 	CHECK_STATES((&g_States,
   128 		new State_Func1H_Called,
   129 		new State_Func1H_Called,
   130 		new State_Func1H_Called,
   131 		new State_Func1_Called,
   132 		new State_Func2_Called,
   133 		new State_Func2H_Called,
   134 		new State_Func2H_Called,
   135 		new State_Func2H_Called,
   136 		new State_Func3H_Called,
   137 		new State_Func3H_Called,
   138 		new State_Func3H_Called,
   139 		new State_Func3_Called,
   140 		NULL), "Part 2.1");
   141 
   142 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   143 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   144 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   145 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   146 
   147 	CHECK_STATES((&g_States,
   148 		new State_PluginInUse(1, true),
   149 		new State_PluginInUse(2, true),
   150 		new State_PluginInUse(3, true),
   151 		new State_PluginInUse(4, false),
   152 		NULL), "Part 2.2");
   153 
   154 	// Unload plugins one by one
   155 	g_SHImpl.UnloadPlugin(3);
   156 
   157 	pInst->Func1();
   158 	pInst->Func2();
   159 	pInst->Func3();
   160 
   161 	CHECK_STATES((&g_States,
   162 		new State_Func1H_Called,
   163 		new State_Func1H_Called,
   164 		new State_Func1_Called,
   165 		new State_Func2_Called,
   166 		new State_Func2H_Called,
   167 		new State_Func2H_Called,
   168 		new State_Func3H_Called,
   169 		new State_Func3H_Called,
   170 		new State_Func3_Called,
   171 		NULL), "Part 2.3.1");
   172 
   173 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   174 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   175 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   176 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   177 
   178 	CHECK_STATES((&g_States,
   179 		new State_PluginInUse(1, true),
   180 		new State_PluginInUse(2, true),
   181 		new State_PluginInUse(3, false),
   182 		new State_PluginInUse(4, false),
   183 		NULL), "Part 2.3.2");
   184 
   185 	g_SHImpl.UnloadPlugin(2);
   186 
   187 	pInst->Func1();
   188 	pInst->Func2();
   189 	pInst->Func3();
   190 
   191 	CHECK_STATES((&g_States,
   192 		new State_Func1H_Called,
   193 		new State_Func1_Called,
   194 		new State_Func2_Called,
   195 		new State_Func2H_Called,
   196 		new State_Func3H_Called,
   197 		new State_Func3_Called,
   198 		NULL), "Part 2.4.1");
   199 
   200 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   201 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   202 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   203 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   204 
   205 	CHECK_STATES((&g_States,
   206 		new State_PluginInUse(1, true),
   207 		new State_PluginInUse(2, false),
   208 		new State_PluginInUse(3, false),
   209 		new State_PluginInUse(4, false),
   210 		NULL), "Part 2.4.2");
   211 
   212 	g_SHImpl.UnloadPlugin(1);
   213 
   214 	pInst->Func1();
   215 	pInst->Func2();
   216 	pInst->Func3();
   217 
   218 	CHECK_STATES((&g_States,
   219 		new State_Func1_Called,
   220 		new State_Func2_Called,
   221 		new State_Func3_Called,
   222 		NULL), "Part 2.5.1");
   223 
   224 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   225 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   226 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   227 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   228 
   229 	CHECK_STATES((&g_States,
   230 		new State_PluginInUse(1, false),
   231 		new State_PluginInUse(2, false),
   232 		new State_PluginInUse(3, false),
   233 		new State_PluginInUse(4, false),
   234 		NULL), "Part 2.5.2");
   235 
   236 
   237 	// 3)
   238 	//   Add hooks from "different plugins", then pause the plugins
   239 
   240 	g_PLID = 1;
   241 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   242 	g_PLID = 2;
   243 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   244 	g_PLID = 3;
   245 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   246 
   247 	g_PLID = 1;
   248 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   249 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   250 
   251 	g_PLID = 2;
   252 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   253 	SH_ADD_HOOK_STATICFUNC(Test, Func3, pInst, Handler_Func3, false);
   254 
   255 	g_PLID = 3;
   256 	SH_ADD_HOOK_STATICFUNC(Test, Func1, pInst, Handler_Func1, false);
   257 	SH_ADD_HOOK_STATICFUNC(Test, Func2, pInst, Handler_Func2, true);
   258 
   259 	pInst->Func1();
   260 	pInst->Func2();
   261 	pInst->Func3();
   262 
   263 	CHECK_STATES((&g_States,
   264 		new State_Func1H_Called,
   265 		new State_Func1H_Called,
   266 		new State_Func1H_Called,
   267 		new State_Func1_Called,
   268 		new State_Func2_Called,
   269 		new State_Func2H_Called,
   270 		new State_Func2H_Called,
   271 		new State_Func2H_Called,
   272 		new State_Func3H_Called,
   273 		new State_Func3H_Called,
   274 		new State_Func3H_Called,
   275 		new State_Func3_Called,
   276 		NULL), "Part 3.1");
   277 
   278 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   279 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   280 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   281 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   282 
   283 	CHECK_STATES((&g_States,
   284 		new State_PluginInUse(1, true),
   285 		new State_PluginInUse(2, true),
   286 		new State_PluginInUse(3, true),
   287 		new State_PluginInUse(4, false),
   288 		NULL), "Part 3.2");
   289 
   290 	// Unload plugins one by one
   291 	g_SHImpl.PausePlugin(3);
   292 
   293 	pInst->Func1();
   294 	pInst->Func2();
   295 	pInst->Func3();
   296 
   297 	CHECK_STATES((&g_States,
   298 		new State_Func1H_Called,
   299 		new State_Func1H_Called,
   300 		new State_Func1_Called,
   301 		new State_Func2_Called,
   302 		new State_Func2H_Called,
   303 		new State_Func2H_Called,
   304 		new State_Func3H_Called,
   305 		new State_Func3H_Called,
   306 		new State_Func3_Called,
   307 		NULL), "Part 3.3.1");
   308 
   309 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   310 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   311 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   312 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   313 
   314 	CHECK_STATES((&g_States,
   315 		new State_PluginInUse(1, true),
   316 		new State_PluginInUse(2, true),
   317 		new State_PluginInUse(3, true),
   318 		new State_PluginInUse(4, false),
   319 		NULL), "Part 3.3.2");
   320 
   321 	g_SHImpl.PausePlugin(2);
   322 
   323 	pInst->Func1();
   324 	pInst->Func2();
   325 	pInst->Func3();
   326 
   327 	CHECK_STATES((&g_States,
   328 		new State_Func1H_Called,
   329 		new State_Func1_Called,
   330 		new State_Func2_Called,
   331 		new State_Func2H_Called,
   332 		new State_Func3H_Called,
   333 		new State_Func3_Called,
   334 		NULL), "Part 3.4.1");
   335 
   336 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   337 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   338 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   339 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   340 
   341 	CHECK_STATES((&g_States,
   342 		new State_PluginInUse(1, true),
   343 		new State_PluginInUse(2, true),
   344 		new State_PluginInUse(3, true),
   345 		new State_PluginInUse(4, false),
   346 		NULL), "Part 3.4.2");
   347 
   348 	g_SHImpl.PausePlugin(1);
   349 
   350 	pInst->Func1();
   351 	pInst->Func2();
   352 	pInst->Func3();
   353 
   354 	CHECK_STATES((&g_States,
   355 		new State_Func1_Called,
   356 		new State_Func2_Called,
   357 		new State_Func3_Called,
   358 		NULL), "Part 3.5.1");
   359 
   360 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   361 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   362 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   363 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   364 
   365 	CHECK_STATES((&g_States,
   366 		new State_PluginInUse(1, true),
   367 		new State_PluginInUse(2, true),
   368 		new State_PluginInUse(3, true),
   369 		new State_PluginInUse(4, false),
   370 		NULL), "Part 3.5.2");
   371 
   372 	g_SHImpl.UnpausePlugin(1);
   373 	g_SHImpl.UnpausePlugin(2);
   374 	g_SHImpl.UnpausePlugin(3);
   375 
   376 	ADD_STATE(State_PluginInUse(1, g_SHImpl.IsPluginInUse(1)));
   377 	ADD_STATE(State_PluginInUse(2, g_SHImpl.IsPluginInUse(2)));
   378 	ADD_STATE(State_PluginInUse(3, g_SHImpl.IsPluginInUse(3)));
   379 	ADD_STATE(State_PluginInUse(4, g_SHImpl.IsPluginInUse(4)));
   380 
   381 	CHECK_STATES((&g_States,
   382 		new State_PluginInUse(1, true),
   383 		new State_PluginInUse(2, true),
   384 		new State_PluginInUse(3, true),
   385 		new State_PluginInUse(4, false),
   386 		NULL), "Part 3.6");
   387 
   388 	pInst->Func1();
   389 	pInst->Func2();
   390 	pInst->Func3();
   391 
   392 	CHECK_STATES((&g_States,
   393 		new State_Func1H_Called,
   394 		new State_Func1H_Called,
   395 		new State_Func1H_Called,
   396 		new State_Func1_Called,
   397 		new State_Func2_Called,
   398 		new State_Func2H_Called,
   399 		new State_Func2H_Called,
   400 		new State_Func2H_Called,
   401 		new State_Func3H_Called,
   402 		new State_Func3H_Called,
   403 		new State_Func3H_Called,
   404 		new State_Func3_Called,
   405 		NULL), "Part 3.7");
   406 
   407 	// 4) Shutdown :)
   408 	g_SHImpl.CompleteShutdown();
   409 
   410 	pInst->Func1();
   411 	pInst->Func2();
   412 	pInst->Func3();
   413 
   414 	CHECK_STATES((&g_States,
   415 		new State_Func1_Called,
   416 		new State_Func2_Called,
   417 		new State_Func3_Called,
   418 		NULL), "Part 4");
   419 
   420 	return true;
   421 }
   422