123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #include "main.h"
- #include "./testSuite.h"
- #include <strsafe.h>
- TestSuite::TestSuite()
- {
- }
- TestSuite::~TestSuite()
- {
- size_t index;
- index = deviceList.size();
- while(index--)
- {
- deviceList[index]->Release();
- }
- index = typeList.size();
- while(index--)
- {
- typeList[index]->Release();
- }
- index = connectionList.size();
- while(index--)
- {
- connectionList[index]->Release();
- }
- index = commandList.size();
- while(index--)
- {
- commandList[index]->Release();
- }
- index = insertList.size();
- while(index--)
- {
- AnsiString_Free(insertList[index]);
- }
- }
- BOOL TestSuite::AddDevice(Device *device)
- {
- if (NULL == device)
- return FALSE;
- deviceList.push_back(device);
- device->AddRef();
- return TRUE;
- }
- size_t TestSuite::GetDeviceCount()
- {
- return deviceList.size();
- }
- Device *TestSuite::GetDevice(size_t index)
- {
- return deviceList[index];
- }
- static int
- String_RemoveCounter(wchar_t *string)
- {
- int len;
- len = lstrlenW(string);
- if (len > 3)
- {
- int cutoff = 0;
- if (string[len-1] == L')')
- {
- WORD charType;
- cutoff = 1;
- while(len > cutoff &&
- FALSE != GetStringTypeW(CT_CTYPE1, string + (len - 1) - cutoff, 1, &charType) &&
- 0 != (C1_DIGIT & charType))
- {
- cutoff++;
- }
- if (len > cutoff &&
- cutoff > 1 &&
- L'(' == string[len - 1 - cutoff])
- {
- cutoff++;
- if (len > cutoff &&
- L' ' == string[len - 1 - cutoff])
- {
- string[len - 1 - cutoff] = L'\0';
- len -= (cutoff + 1);
- }
- }
- }
- }
- return len;
- }
- static int
- AnsiString_RemoveCounter(char *string)
- {
- int len;
- len = lstrlenA(string);
- if (len > 3)
- {
- int cutoff = 0;
- if (string[len-1] == ')')
- {
- WORD charType;
- cutoff = 1;
- while(len > cutoff &&
- FALSE != GetStringTypeA(LOCALE_SYSTEM_DEFAULT, CT_CTYPE1, string + (len - 1) - cutoff, 1, &charType) &&
- 0 != (C1_DIGIT & charType))
- {
- cutoff++;
- }
- if (len > cutoff &&
- cutoff > 1 &&
- '(' == string[len - 1 - cutoff])
- {
- cutoff++;
- if (len > cutoff &&
- ' ' == string[len - 1 - cutoff])
- {
- string[len - 1 - cutoff] = '\0';
- len -= (cutoff + 1);
- }
- }
- }
- }
- return len;
- }
- Device *TestSuite::CreateDeviceCopy(Device *source)
- {
- const char *name;
- size_t index, counter;
- char buffer[1024];
- wchar_t *displayName;
- BOOL found;
- Device *destination;
- int length;
- if (NULL == source)
- return NULL;
-
- found = FALSE;
- counter = 0;
- name = source->GetName();
- StringCbCopyA(buffer, sizeof(buffer), name);
- length = AnsiString_RemoveCounter(buffer);
- for (;;)
- {
-
- if (0 != counter)
- StringCbPrintfA(buffer + length, sizeof(buffer) - length, " (%d)", counter);
- found = TRUE;
- index = deviceList.size();
- while(index--)
- {
- if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, deviceList[index]->GetName(), -1, buffer, -1))
- {
- found = FALSE;
- break;
- }
- }
- if (FALSE != found)
- break;
- else
- counter++;
- }
- if (FAILED(Device::CreateInstance(buffer, source->GetType(), source->GetConnection(), &destination)))
- return NULL;
- source->CopyTo(destination);
-
- source->GetDisplayName((wchar_t*)buffer, sizeof(buffer)/sizeof(wchar_t));
- displayName = (wchar_t*)buffer;
- length = String_RemoveCounter(displayName);
-
- if (0 != counter)
- StringCchPrintf(displayName + length, sizeof(buffer)/sizeof(wchar_t) - length, L" (%d)", counter);
-
- destination->SetDisplayName(displayName);
-
- return destination;
- }
- Device *TestSuite::GetRandomDevice()
- {
- size_t index;
- Device *device;
- LARGE_INTEGER perfCounter;
- if (0 == deviceList.size())
- return NULL;
- if (FALSE != QueryPerformanceCounter(&perfCounter))
- srand(perfCounter.LowPart);
- else
- srand(GetTickCount());
- index = (size_t)((double)rand()/(RAND_MAX + 1) * deviceList.size());
- device = deviceList[index];
- if (S_OK == device->IsConnected())
- {
- size_t search;
- for(search = index + 1; search < deviceList.size(); search++)
- {
- device = deviceList[search];
- if (S_FALSE == device->IsConnected())
- return device;
- }
- search = index;
- while(search--)
- {
- device = deviceList[search];
- if (S_FALSE == device->IsConnected())
- return device;
- }
- device = CreateDeviceCopy(deviceList[index]);
- if (NULL != device)
- {
- size_t totalSpace, usedSpace;
- totalSpace = (size_t)((double)rand()/(RAND_MAX + 1) * 1000);
- usedSpace = (size_t)((double)rand()/(RAND_MAX + 1) * totalSpace);
- device->SetTotalSpace(totalSpace);
- device->SetUsedSpace(usedSpace);
- device->Disconnect();
- device->Detach(NULL);
- AddDevice(device);
- }
- }
- return device;
- }
-
- BOOL TestSuite::AddType(ifc_devicetype *type)
- {
- if (NULL == type)
- return FALSE;
- typeList.push_back(type);
- type->AddRef();
- return TRUE;
- }
- size_t TestSuite::GetTypeCount()
- {
- return typeList.size();
- }
- ifc_devicetype *TestSuite::GetType(size_t index)
- {
- return typeList[index];
- }
- BOOL TestSuite::RegisterTypes(api_devicemanager *manager)
- {
- if (NULL == manager)
- return FALSE;
- if (0 != typeList.size())
- manager->TypeRegister((ifc_devicetype**)typeList.begin(), typeList.size());
- return TRUE;
- }
- BOOL TestSuite::UnregisterTypes(api_devicemanager *manager)
- {
- size_t index;
- if (NULL == manager)
- return FALSE;
- index = typeList.size();
- while(index--)
- {
- manager->TypeUnregister(typeList[index]->GetName());
- }
- return TRUE;
- }
- BOOL TestSuite::SetIconBase(const wchar_t *path)
- {
- size_t index;
- Device *device;
- ifc_devicetype *type;
- ifc_devicetypeeditor *typeEditor;
- ifc_deviceiconstore *iconStore;
- ifc_deviceconnection *connection;
- ifc_deviceconnectioneditor *connectionEditor;
- ifc_devicecommand *command;
- ifc_devicecommandeditor *commandEditor;
- index = deviceList.size();
- while(index--)
- {
- device = deviceList[index];
- device->SetIconBase(path);
- }
- index = typeList.size();
- while(index--)
- {
- type = typeList[index];
- if (SUCCEEDED(type->QueryInterface(IFC_DeviceTypeEditor, (void**)&typeEditor)))
- {
- if(SUCCEEDED(typeEditor->GetIconStore(&iconStore)))
- {
- iconStore->SetBasePath(path);
- iconStore->Release();
- }
- typeEditor->Release();
- }
- }
- index = commandList.size();
- while(index--)
- {
- command = commandList[index];
- if (SUCCEEDED(command->QueryInterface(IFC_DeviceCommandEditor, (void**)&commandEditor)))
- {
- if(SUCCEEDED(commandEditor->GetIconStore(&iconStore)))
- {
- iconStore->SetBasePath(path);
- iconStore->Release();
- }
- commandEditor->Release();
- }
- }
- index = connectionList.size();
- while(index--)
- {
- connection = connectionList[index];
- if (SUCCEEDED(connection->QueryInterface(IFC_DeviceConnectionEditor, (void**)&connectionEditor)))
- {
- if(SUCCEEDED(connectionEditor->GetIconStore(&iconStore)))
- {
- iconStore->SetBasePath(path);
- iconStore->Release();
- }
- connectionEditor->Release();
- }
- }
- return S_OK;
- }
- BOOL TestSuite::SetConnectList(char **devices, size_t count)
- {
- size_t index;
- char *name;
- index = insertList.size();
- if (index > 0)
- {
- while(index--)
- {
- name = insertList[index];
- AnsiString_Free(name);
- }
- insertList.clear();
- }
- for(index = 0; index < count; index++)
- {
- name = AnsiString_Duplicate(devices[index]);
- if (NULL != name)
- insertList.push_back(name);
- }
- return TRUE;
- }
- Device *TestSuite::GetDeviceByName(const char *name)
- {
- size_t index;
- Device *device;
- for (index = 0; index < deviceList.size(); index++)
- {
- device = deviceList[index];
- if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, name, -1, device->GetName(), -1))
- return device;
- }
- return NULL;
- }
- BOOL TestSuite::RegisterDevices(api_devicemanager *manager)
- {
- size_t index;
- const char *name;
- Device *device;
- if (NULL == manager)
- return FALSE;
-
- for (index = 0; index < insertList.size(); index++)
- {
- name = insertList[index];
- device = GetDeviceByName(name);
- if (NULL != device)
- {
- manager->DeviceRegister((ifc_device**)&device, 1);
- device->Connect();
- }
- }
-
- return TRUE;
- }
- BOOL TestSuite::UnregisterDevices(api_devicemanager *manager)
- {
- size_t index;
- Device *device;
- if (NULL == manager)
- return FALSE;
-
- index = deviceList.size();
- while(index--)
- {
- device = deviceList[index];
- if (S_OK == device->IsConnected())
- {
- device->Disconnect();
- manager->DeviceUnregister(device->GetName());
- }
- }
-
- return TRUE;
- }
- BOOL TestSuite::AddConnection(ifc_deviceconnection *connection)
- {
- if (NULL == connection)
- return FALSE;
- connectionList.push_back(connection);
- connection->AddRef();
- return TRUE;
- }
- size_t TestSuite::GetConnectionCount()
- {
- return connectionList.size();
- }
- ifc_deviceconnection *TestSuite::GetConnection(size_t index)
- {
- return connectionList[index];
- }
- BOOL TestSuite::RegisterConnections(api_devicemanager *manager)
- {
- if (NULL == manager)
- return FALSE;
- if (0 != connectionList.size())
- manager->ConnectionRegister((ifc_deviceconnection**)connectionList.begin(), connectionList.size());
- return TRUE;
- }
- BOOL TestSuite::UnregisterConnections(api_devicemanager *manager)
- {
- size_t index;
- if (NULL == manager)
- return FALSE;
- index = connectionList.size();
- while(index--)
- {
- manager->ConnectionUnregister(connectionList[index]->GetName());
- }
- return TRUE;
- }
- BOOL TestSuite::AddCommand(ifc_devicecommand *command)
- {
- if (NULL == command)
- return FALSE;
- commandList.push_back(command);
- command->AddRef();
- return TRUE;
- }
- size_t TestSuite::GetCommandCount()
- {
- return commandList.size();
- }
- ifc_devicecommand *TestSuite::GetCommand(size_t index)
- {
- return commandList[index];
- }
- BOOL TestSuite::RegisterCommands(api_devicemanager *manager)
- {
- if (NULL == manager)
- return FALSE;
- if (0 != commandList.size())
- manager->CommandRegister((ifc_devicecommand**)commandList.begin(), commandList.size());
- return TRUE;
- }
- BOOL TestSuite::UnregisterCommands(api_devicemanager *manager)
- {
- size_t index;
- if (NULL == manager)
- return FALSE;
- index = commandList.size();
- while(index--)
- {
- manager->CommandUnregister(commandList[index]->GetName());
- }
- return TRUE;
- }
|