testSuite.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #include "main.h"
  2. #include "./testSuite.h"
  3. #include <strsafe.h>
  4. TestSuite::TestSuite()
  5. {
  6. }
  7. TestSuite::~TestSuite()
  8. {
  9. size_t index;
  10. index = deviceList.size();
  11. while(index--)
  12. {
  13. deviceList[index]->Release();
  14. }
  15. index = typeList.size();
  16. while(index--)
  17. {
  18. typeList[index]->Release();
  19. }
  20. index = connectionList.size();
  21. while(index--)
  22. {
  23. connectionList[index]->Release();
  24. }
  25. index = commandList.size();
  26. while(index--)
  27. {
  28. commandList[index]->Release();
  29. }
  30. index = insertList.size();
  31. while(index--)
  32. {
  33. AnsiString_Free(insertList[index]);
  34. }
  35. }
  36. BOOL TestSuite::AddDevice(Device *device)
  37. {
  38. if (NULL == device)
  39. return FALSE;
  40. deviceList.push_back(device);
  41. device->AddRef();
  42. return TRUE;
  43. }
  44. size_t TestSuite::GetDeviceCount()
  45. {
  46. return deviceList.size();
  47. }
  48. Device *TestSuite::GetDevice(size_t index)
  49. {
  50. return deviceList[index];
  51. }
  52. static int
  53. String_RemoveCounter(wchar_t *string)
  54. {
  55. int len;
  56. len = lstrlenW(string);
  57. if (len > 3)
  58. {
  59. int cutoff = 0;
  60. if (string[len-1] == L')')
  61. {
  62. WORD charType;
  63. cutoff = 1;
  64. while(len > cutoff &&
  65. FALSE != GetStringTypeW(CT_CTYPE1, string + (len - 1) - cutoff, 1, &charType) &&
  66. 0 != (C1_DIGIT & charType))
  67. {
  68. cutoff++;
  69. }
  70. if (len > cutoff &&
  71. cutoff > 1 &&
  72. L'(' == string[len - 1 - cutoff])
  73. {
  74. cutoff++;
  75. if (len > cutoff &&
  76. L' ' == string[len - 1 - cutoff])
  77. {
  78. string[len - 1 - cutoff] = L'\0';
  79. len -= (cutoff + 1);
  80. }
  81. }
  82. }
  83. }
  84. return len;
  85. }
  86. static int
  87. AnsiString_RemoveCounter(char *string)
  88. {
  89. int len;
  90. len = lstrlenA(string);
  91. if (len > 3)
  92. {
  93. int cutoff = 0;
  94. if (string[len-1] == ')')
  95. {
  96. WORD charType;
  97. cutoff = 1;
  98. while(len > cutoff &&
  99. FALSE != GetStringTypeA(LOCALE_SYSTEM_DEFAULT, CT_CTYPE1, string + (len - 1) - cutoff, 1, &charType) &&
  100. 0 != (C1_DIGIT & charType))
  101. {
  102. cutoff++;
  103. }
  104. if (len > cutoff &&
  105. cutoff > 1 &&
  106. '(' == string[len - 1 - cutoff])
  107. {
  108. cutoff++;
  109. if (len > cutoff &&
  110. ' ' == string[len - 1 - cutoff])
  111. {
  112. string[len - 1 - cutoff] = '\0';
  113. len -= (cutoff + 1);
  114. }
  115. }
  116. }
  117. }
  118. return len;
  119. }
  120. Device *TestSuite::CreateDeviceCopy(Device *source)
  121. {
  122. const char *name;
  123. size_t index, counter;
  124. char buffer[1024];
  125. wchar_t *displayName;
  126. BOOL found;
  127. Device *destination;
  128. int length;
  129. if (NULL == source)
  130. return NULL;
  131. found = FALSE;
  132. counter = 0;
  133. name = source->GetName();
  134. StringCbCopyA(buffer, sizeof(buffer), name);
  135. length = AnsiString_RemoveCounter(buffer);
  136. for (;;)
  137. {
  138. if (0 != counter)
  139. StringCbPrintfA(buffer + length, sizeof(buffer) - length, " (%d)", counter);
  140. found = TRUE;
  141. index = deviceList.size();
  142. while(index--)
  143. {
  144. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, deviceList[index]->GetName(), -1, buffer, -1))
  145. {
  146. found = FALSE;
  147. break;
  148. }
  149. }
  150. if (FALSE != found)
  151. break;
  152. else
  153. counter++;
  154. }
  155. if (FAILED(Device::CreateInstance(buffer, source->GetType(), source->GetConnection(), &destination)))
  156. return NULL;
  157. source->CopyTo(destination);
  158. source->GetDisplayName((wchar_t*)buffer, sizeof(buffer)/sizeof(wchar_t));
  159. displayName = (wchar_t*)buffer;
  160. length = String_RemoveCounter(displayName);
  161. if (0 != counter)
  162. StringCchPrintf(displayName + length, sizeof(buffer)/sizeof(wchar_t) - length, L" (%d)", counter);
  163. destination->SetDisplayName(displayName);
  164. return destination;
  165. }
  166. Device *TestSuite::GetRandomDevice()
  167. {
  168. size_t index;
  169. Device *device;
  170. LARGE_INTEGER perfCounter;
  171. if (0 == deviceList.size())
  172. return NULL;
  173. if (FALSE != QueryPerformanceCounter(&perfCounter))
  174. srand(perfCounter.LowPart);
  175. else
  176. srand(GetTickCount());
  177. index = (size_t)((double)rand()/(RAND_MAX + 1) * deviceList.size());
  178. device = deviceList[index];
  179. if (S_OK == device->IsConnected())
  180. {
  181. size_t search;
  182. for(search = index + 1; search < deviceList.size(); search++)
  183. {
  184. device = deviceList[search];
  185. if (S_FALSE == device->IsConnected())
  186. return device;
  187. }
  188. search = index;
  189. while(search--)
  190. {
  191. device = deviceList[search];
  192. if (S_FALSE == device->IsConnected())
  193. return device;
  194. }
  195. device = CreateDeviceCopy(deviceList[index]);
  196. if (NULL != device)
  197. {
  198. size_t totalSpace, usedSpace;
  199. totalSpace = (size_t)((double)rand()/(RAND_MAX + 1) * 1000);
  200. usedSpace = (size_t)((double)rand()/(RAND_MAX + 1) * totalSpace);
  201. device->SetTotalSpace(totalSpace);
  202. device->SetUsedSpace(usedSpace);
  203. device->Disconnect();
  204. device->Detach(NULL);
  205. AddDevice(device);
  206. }
  207. }
  208. return device;
  209. }
  210. BOOL TestSuite::AddType(ifc_devicetype *type)
  211. {
  212. if (NULL == type)
  213. return FALSE;
  214. typeList.push_back(type);
  215. type->AddRef();
  216. return TRUE;
  217. }
  218. size_t TestSuite::GetTypeCount()
  219. {
  220. return typeList.size();
  221. }
  222. ifc_devicetype *TestSuite::GetType(size_t index)
  223. {
  224. return typeList[index];
  225. }
  226. BOOL TestSuite::RegisterTypes(api_devicemanager *manager)
  227. {
  228. if (NULL == manager)
  229. return FALSE;
  230. if (0 != typeList.size())
  231. manager->TypeRegister((ifc_devicetype**)typeList.begin(), typeList.size());
  232. return TRUE;
  233. }
  234. BOOL TestSuite::UnregisterTypes(api_devicemanager *manager)
  235. {
  236. size_t index;
  237. if (NULL == manager)
  238. return FALSE;
  239. index = typeList.size();
  240. while(index--)
  241. {
  242. manager->TypeUnregister(typeList[index]->GetName());
  243. }
  244. return TRUE;
  245. }
  246. BOOL TestSuite::SetIconBase(const wchar_t *path)
  247. {
  248. size_t index;
  249. Device *device;
  250. ifc_devicetype *type;
  251. ifc_devicetypeeditor *typeEditor;
  252. ifc_deviceiconstore *iconStore;
  253. ifc_deviceconnection *connection;
  254. ifc_deviceconnectioneditor *connectionEditor;
  255. ifc_devicecommand *command;
  256. ifc_devicecommandeditor *commandEditor;
  257. index = deviceList.size();
  258. while(index--)
  259. {
  260. device = deviceList[index];
  261. device->SetIconBase(path);
  262. }
  263. index = typeList.size();
  264. while(index--)
  265. {
  266. type = typeList[index];
  267. if (SUCCEEDED(type->QueryInterface(IFC_DeviceTypeEditor, (void**)&typeEditor)))
  268. {
  269. if(SUCCEEDED(typeEditor->GetIconStore(&iconStore)))
  270. {
  271. iconStore->SetBasePath(path);
  272. iconStore->Release();
  273. }
  274. typeEditor->Release();
  275. }
  276. }
  277. index = commandList.size();
  278. while(index--)
  279. {
  280. command = commandList[index];
  281. if (SUCCEEDED(command->QueryInterface(IFC_DeviceCommandEditor, (void**)&commandEditor)))
  282. {
  283. if(SUCCEEDED(commandEditor->GetIconStore(&iconStore)))
  284. {
  285. iconStore->SetBasePath(path);
  286. iconStore->Release();
  287. }
  288. commandEditor->Release();
  289. }
  290. }
  291. index = connectionList.size();
  292. while(index--)
  293. {
  294. connection = connectionList[index];
  295. if (SUCCEEDED(connection->QueryInterface(IFC_DeviceConnectionEditor, (void**)&connectionEditor)))
  296. {
  297. if(SUCCEEDED(connectionEditor->GetIconStore(&iconStore)))
  298. {
  299. iconStore->SetBasePath(path);
  300. iconStore->Release();
  301. }
  302. connectionEditor->Release();
  303. }
  304. }
  305. return S_OK;
  306. }
  307. BOOL TestSuite::SetConnectList(char **devices, size_t count)
  308. {
  309. size_t index;
  310. char *name;
  311. index = insertList.size();
  312. if (index > 0)
  313. {
  314. while(index--)
  315. {
  316. name = insertList[index];
  317. AnsiString_Free(name);
  318. }
  319. insertList.clear();
  320. }
  321. for(index = 0; index < count; index++)
  322. {
  323. name = AnsiString_Duplicate(devices[index]);
  324. if (NULL != name)
  325. insertList.push_back(name);
  326. }
  327. return TRUE;
  328. }
  329. Device *TestSuite::GetDeviceByName(const char *name)
  330. {
  331. size_t index;
  332. Device *device;
  333. for (index = 0; index < deviceList.size(); index++)
  334. {
  335. device = deviceList[index];
  336. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, name, -1, device->GetName(), -1))
  337. return device;
  338. }
  339. return NULL;
  340. }
  341. BOOL TestSuite::RegisterDevices(api_devicemanager *manager)
  342. {
  343. size_t index;
  344. const char *name;
  345. Device *device;
  346. if (NULL == manager)
  347. return FALSE;
  348. for (index = 0; index < insertList.size(); index++)
  349. {
  350. name = insertList[index];
  351. device = GetDeviceByName(name);
  352. if (NULL != device)
  353. {
  354. manager->DeviceRegister((ifc_device**)&device, 1);
  355. device->Connect();
  356. }
  357. }
  358. return TRUE;
  359. }
  360. BOOL TestSuite::UnregisterDevices(api_devicemanager *manager)
  361. {
  362. size_t index;
  363. Device *device;
  364. if (NULL == manager)
  365. return FALSE;
  366. index = deviceList.size();
  367. while(index--)
  368. {
  369. device = deviceList[index];
  370. if (S_OK == device->IsConnected())
  371. {
  372. device->Disconnect();
  373. manager->DeviceUnregister(device->GetName());
  374. }
  375. }
  376. return TRUE;
  377. }
  378. BOOL TestSuite::AddConnection(ifc_deviceconnection *connection)
  379. {
  380. if (NULL == connection)
  381. return FALSE;
  382. connectionList.push_back(connection);
  383. connection->AddRef();
  384. return TRUE;
  385. }
  386. size_t TestSuite::GetConnectionCount()
  387. {
  388. return connectionList.size();
  389. }
  390. ifc_deviceconnection *TestSuite::GetConnection(size_t index)
  391. {
  392. return connectionList[index];
  393. }
  394. BOOL TestSuite::RegisterConnections(api_devicemanager *manager)
  395. {
  396. if (NULL == manager)
  397. return FALSE;
  398. if (0 != connectionList.size())
  399. manager->ConnectionRegister((ifc_deviceconnection**)connectionList.begin(), connectionList.size());
  400. return TRUE;
  401. }
  402. BOOL TestSuite::UnregisterConnections(api_devicemanager *manager)
  403. {
  404. size_t index;
  405. if (NULL == manager)
  406. return FALSE;
  407. index = connectionList.size();
  408. while(index--)
  409. {
  410. manager->ConnectionUnregister(connectionList[index]->GetName());
  411. }
  412. return TRUE;
  413. }
  414. BOOL TestSuite::AddCommand(ifc_devicecommand *command)
  415. {
  416. if (NULL == command)
  417. return FALSE;
  418. commandList.push_back(command);
  419. command->AddRef();
  420. return TRUE;
  421. }
  422. size_t TestSuite::GetCommandCount()
  423. {
  424. return commandList.size();
  425. }
  426. ifc_devicecommand *TestSuite::GetCommand(size_t index)
  427. {
  428. return commandList[index];
  429. }
  430. BOOL TestSuite::RegisterCommands(api_devicemanager *manager)
  431. {
  432. if (NULL == manager)
  433. return FALSE;
  434. if (0 != commandList.size())
  435. manager->CommandRegister((ifc_devicecommand**)commandList.begin(), commandList.size());
  436. return TRUE;
  437. }
  438. BOOL TestSuite::UnregisterCommands(api_devicemanager *manager)
  439. {
  440. size_t index;
  441. if (NULL == manager)
  442. return FALSE;
  443. index = commandList.size();
  444. while(index--)
  445. {
  446. manager->CommandUnregister(commandList[index]->GetName());
  447. }
  448. return TRUE;
  449. }