sourcehook/test/test4.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 07 22:10:07 2007 +0000 (2007-10-07)
changeset 380 f9e21a2ac558
parent 324 257f0837a885
child 387 093c72464b35
permissions -rw-r--r--
New auto-hookfunc-gen friendly interface
! breaks binary compatibility !
     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 	class Test
    23 	{
    24 	public:
    25 		virtual void Func1()
    26 		{
    27 			ADD_STATE(State_Func1_Called);
    28 		}
    29 
    30 		virtual void Func2()
    31 		{
    32 			ADD_STATE(State_Func2_Called);
    33 		}
    34 
    35 		virtual void Func3()
    36 		{
    37 			ADD_STATE(State_Func3_Called);
    38 		}
    39 	};
    40 
    41 	SH_DECL_HOOK0_void(Test, Func1, SH_NOATTRIB, 1);
    42 	SH_DECL_HOOK0_void(Test, Func2, SH_NOATTRIB, 1);
    43 	SH_DECL_HOOK0_void(Test, Func3, SH_NOATTRIB, 1);
    44 
    45 	void Handler_Func1()
    46 	{
    47 		ADD_STATE(State_Func1H_Called);
    48 	}
    49 	void Handler_Func2()
    50 	{
    51 		ADD_STATE(State_Func2H_Called);
    52 	}
    53 	void Handler_Func3()
    54 	{
    55 		ADD_STATE(State_Func3H_Called);
    56 	}
    57 }
    58 
    59 bool TestPlugSys(std::string &error)
    60 {
    61 	GET_SHPTR(g_SHPtr);
    62 	g_PLID = 1;
    63 
    64 	Test inst;
    65 	Test *pInst = &inst;
    66 
    67 	// 1)
    68 	//   Add hooks, then issue a complete shutdown
    69 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
    70 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
    71 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
    72 
    73 	pInst->Func1();
    74 	pInst->Func2();
    75 	pInst->Func3();
    76 
    77 	CHECK_STATES((&g_States,
    78 		new State_Func1H_Called,
    79 		new State_Func1_Called,
    80 		new State_Func2_Called,
    81 		new State_Func2H_Called,
    82 		new State_Func3H_Called,
    83 		new State_Func3_Called,
    84 		NULL), "Part 1.1");
    85 
    86 	Test_CompleteShutdown(g_SHPtr);
    87 
    88 	pInst->Func1();
    89 	pInst->Func2();
    90 	pInst->Func3();
    91 
    92 	CHECK_STATES((&g_States,
    93 		new State_Func1_Called,
    94 		new State_Func2_Called,
    95 		new State_Func3_Called,
    96 		NULL), "Part 1.2");
    97 
    98 	// 2)
    99 	//   Add hooks from "different plugins", then shutdown the plugins
   100 
   101 	g_PLID = 1;
   102 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   103 	g_PLID = 2;
   104 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   105 	g_PLID = 3;
   106 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   107 
   108 	g_PLID = 1;
   109 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   110 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   111 
   112 	g_PLID = 2;
   113 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   114 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   115 
   116 	g_PLID = 3;
   117 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   118 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   119 
   120 	pInst->Func1();
   121 	pInst->Func2();
   122 	pInst->Func3();
   123 
   124 	CHECK_STATES((&g_States,
   125 		new State_Func1H_Called,
   126 		new State_Func1H_Called,
   127 		new State_Func1H_Called,
   128 		new State_Func1_Called,
   129 		new State_Func2_Called,
   130 		new State_Func2H_Called,
   131 		new State_Func2H_Called,
   132 		new State_Func2H_Called,
   133 		new State_Func3H_Called,
   134 		new State_Func3H_Called,
   135 		new State_Func3H_Called,
   136 		new State_Func3_Called,
   137 		NULL), "Part 2.1");
   138 
   139 	// Unload plugins one by one
   140 	Test_UnloadPlugin(g_SHPtr, 3);
   141 
   142 	pInst->Func1();
   143 	pInst->Func2();
   144 	pInst->Func3();
   145 
   146 	CHECK_STATES((&g_States,
   147 		new State_Func1H_Called,
   148 		new State_Func1H_Called,
   149 		new State_Func1_Called,
   150 		new State_Func2_Called,
   151 		new State_Func2H_Called,
   152 		new State_Func2H_Called,
   153 		new State_Func3H_Called,
   154 		new State_Func3H_Called,
   155 		new State_Func3_Called,
   156 		NULL), "Part 2.3.1");
   157 
   158 	Test_UnloadPlugin(g_SHPtr, 2);
   159 
   160 	pInst->Func1();
   161 	pInst->Func2();
   162 	pInst->Func3();
   163 
   164 	CHECK_STATES((&g_States,
   165 		new State_Func1H_Called,
   166 		new State_Func1_Called,
   167 		new State_Func2_Called,
   168 		new State_Func2H_Called,
   169 		new State_Func3H_Called,
   170 		new State_Func3_Called,
   171 		NULL), "Part 2.4.1");
   172 
   173 	Test_UnloadPlugin(g_SHPtr, 1);
   174 
   175 	pInst->Func1();
   176 	pInst->Func2();
   177 	pInst->Func3();
   178 
   179 	CHECK_STATES((&g_States,
   180 		new State_Func1_Called,
   181 		new State_Func2_Called,
   182 		new State_Func3_Called,
   183 		NULL), "Part 2.5.1");
   184 
   185 
   186 	// 3)
   187 	//   Add hooks from "different plugins", then pause the plugins
   188 
   189 	g_PLID = 1;
   190 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   191 	g_PLID = 2;
   192 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   193 	g_PLID = 3;
   194 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   195 
   196 	g_PLID = 1;
   197 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   198 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   199 
   200 	g_PLID = 2;
   201 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   202 	SH_ADD_HOOK(Test, Func3, pInst, SH_STATIC(Handler_Func3), false);
   203 
   204 	g_PLID = 3;
   205 	SH_ADD_HOOK(Test, Func1, pInst, SH_STATIC(Handler_Func1), false);
   206 	SH_ADD_HOOK(Test, Func2, pInst, SH_STATIC(Handler_Func2), true);
   207 
   208 	pInst->Func1();
   209 	pInst->Func2();
   210 	pInst->Func3();
   211 
   212 	CHECK_STATES((&g_States,
   213 		new State_Func1H_Called,
   214 		new State_Func1H_Called,
   215 		new State_Func1H_Called,
   216 		new State_Func1_Called,
   217 		new State_Func2_Called,
   218 		new State_Func2H_Called,
   219 		new State_Func2H_Called,
   220 		new State_Func2H_Called,
   221 		new State_Func3H_Called,
   222 		new State_Func3H_Called,
   223 		new State_Func3H_Called,
   224 		new State_Func3_Called,
   225 		NULL), "Part 3.1");
   226 
   227 	// Unload plugins one by one
   228 	Test_PausePlugin(g_SHPtr, 3);
   229 
   230 	pInst->Func1();
   231 	pInst->Func2();
   232 	pInst->Func3();
   233 
   234 	CHECK_STATES((&g_States,
   235 		new State_Func1H_Called,
   236 		new State_Func1H_Called,
   237 		new State_Func1_Called,
   238 		new State_Func2_Called,
   239 		new State_Func2H_Called,
   240 		new State_Func2H_Called,
   241 		new State_Func3H_Called,
   242 		new State_Func3H_Called,
   243 		new State_Func3_Called,
   244 		NULL), "Part 3.3.1");
   245 
   246 	Test_PausePlugin(g_SHPtr, 2);
   247 
   248 	pInst->Func1();
   249 	pInst->Func2();
   250 	pInst->Func3();
   251 
   252 	CHECK_STATES((&g_States,
   253 		new State_Func1H_Called,
   254 		new State_Func1_Called,
   255 		new State_Func2_Called,
   256 		new State_Func2H_Called,
   257 		new State_Func3H_Called,
   258 		new State_Func3_Called,
   259 		NULL), "Part 3.4.1");
   260 
   261 	Test_PausePlugin(g_SHPtr, 1);
   262 
   263 	pInst->Func1();
   264 	pInst->Func2();
   265 	pInst->Func3();
   266 
   267 	CHECK_STATES((&g_States,
   268 		new State_Func1_Called,
   269 		new State_Func2_Called,
   270 		new State_Func3_Called,
   271 		NULL), "Part 3.5.1");
   272 
   273 	Test_UnpausePlugin(g_SHPtr, 1);
   274 	Test_UnpausePlugin(g_SHPtr, 2);
   275 	Test_UnpausePlugin(g_SHPtr, 3);
   276 
   277 	pInst->Func1();
   278 	pInst->Func2();
   279 	pInst->Func3();
   280 
   281 	CHECK_STATES((&g_States,
   282 		new State_Func1H_Called,
   283 		new State_Func1H_Called,
   284 		new State_Func1H_Called,
   285 		new State_Func1_Called,
   286 		new State_Func2_Called,
   287 		new State_Func2H_Called,
   288 		new State_Func2H_Called,
   289 		new State_Func2H_Called,
   290 		new State_Func3H_Called,
   291 		new State_Func3H_Called,
   292 		new State_Func3H_Called,
   293 		new State_Func3_Called,
   294 		NULL), "Part 3.7");
   295 
   296 	// 4) Shutdown :)
   297 	Test_CompleteShutdown(g_SHPtr);
   298 
   299 	pInst->Func1();
   300 	pInst->Func2();
   301 	pInst->Func3();
   302 
   303 	CHECK_STATES((&g_States,
   304 		new State_Func1_Called,
   305 		new State_Func2_Called,
   306 		new State_Func3_Called,
   307 		NULL), "Part 4");
   308 
   309 	return true;
   310 }