2 * IGameStatisticsMgr tests
4 * Copyright (C) 2010 Mariusz PluciĆski
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
30 /*******************************************************************************
33 static IGameExplorer *ge = NULL;
34 static WCHAR sExeName[MAX_PATH] = {0};
35 static GUID gameInstanceId;
36 static HRESULT WINAPI (*pSHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR);
37 /*******************************************************************************
40 * Helper function, prepares pointers to system procedures which may be not
41 * available on older operating systems.
44 * TRUE procedures were loaded successfully
45 * FALSE procedures were not loaded successfully
47 static BOOL _loadDynamicRoutines(void)
49 HMODULE hModule = LoadLibraryA( "shell32.dll" );
51 pSHGetFolderPathW = (LPVOID)GetProcAddress(hModule, "SHGetFolderPathW");
52 if (!pSHGetFolderPathW) return FALSE;
55 /*******************************************************************************
57 * Registers test suite executable as game in Games Explorer. Required to test
60 static HRESULT _registerGame(void) {
62 WCHAR sExePath[MAX_PATH];
63 BSTR bstrExeName, bstrExePath;
66 /* prepare path to binary */
67 dwExeNameLen = GetModuleFileNameW(NULL, sExeName, sizeof (sExeName) / sizeof (sExeName[0]));
68 hr = (dwExeNameLen!= 0 ? S_OK : E_FAIL);
69 lstrcpynW(sExePath, sExeName, StrRChrW(sExeName, NULL, '\\') - sExeName + 1);
71 bstrExeName = SysAllocString(sExeName);
72 if(!bstrExeName) hr = E_OUTOFMEMORY;
74 bstrExePath = SysAllocString(sExePath);
75 if(!bstrExePath) hr = E_OUTOFMEMORY;
79 gameInstanceId = GUID_NULL;
80 hr = CoCreateInstance(&CLSID_GameExplorer, NULL, CLSCTX_INPROC_SERVER,
81 &IID_IGameExplorer, (LPVOID*)&ge);
85 hr = IGameExplorer_AddGame(ge, bstrExeName, bstrExePath,
86 GIS_CURRENT_USER, &gameInstanceId);
90 IGameExplorer_Release(ge);
94 SysFreeString(bstrExeName);
95 SysFreeString(bstrExePath);
98 /*******************************************************************************
100 * Unregisters test suite from Games Explorer.
102 static HRESULT _unregisterGame(void) {
105 if(!ge) return E_FAIL;
107 hr = IGameExplorer_RemoveGame(ge, gameInstanceId);
109 IGameExplorer_Release(ge);
114 /*******************************************************************************
115 * _buildStatisticsFilePath
116 * Creates path to file containing statistics of game with given id.
119 * guidApplicationId [I] application id of game
120 * lpStatisticsFile [O] pointer where address of
121 * string with path will be
122 * stored. Path must be deallocated
123 * using CoTaskMemFree(...)
125 static HRESULT _buildStatisticsFilePath(LPCGUID guidApplicationId, LPWSTR *lpStatisticsFile)
127 static const WCHAR sBackslash[] = {'\\',0};
128 static const WCHAR sStatisticsDir[] = {'\\','M','i','c','r','o','s','o','f','t',
129 '\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p',
130 'l','o','r','e','r','\\','G','a','m','e','S','t','a','t','i','s',
131 't','i','c','s','\\',0};
132 static const WCHAR sDotGamestats[] = {'.','g','a','m','e','s','t','a','t','s',0};
133 static const DWORD dwGuidLength = 49;
136 WCHAR sGuid[dwGuidLength], sPath[MAX_PATH] = {0};
138 hr = pSHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, sPath);
141 hr = (StringFromGUID2(guidApplicationId, sGuid, dwGuidLength)!=0 ? S_OK : E_FAIL);
145 lstrcatW(sPath, sStatisticsDir);
146 lstrcatW(sPath, sGuid);
147 lstrcatW(sPath, sBackslash);
148 lstrcatW(sPath, sGuid);
149 lstrcatW(sPath, sDotGamestats);
151 *lpStatisticsFile = CoTaskMemAlloc((lstrlenW(sPath)+1)*sizeof(WCHAR));
152 if(!*lpStatisticsFile) hr = E_OUTOFMEMORY;
156 lstrcpyW(*lpStatisticsFile, sPath);
160 /*******************************************************************************
162 * Checks if given file exists
165 * lpFile [I] path to file
169 * FALSE file does not exist
171 static BOOL _isFileExists(LPCWSTR lpFile)
173 HANDLE hFile = CreateFileW(lpFile, GENERIC_READ, 0, NULL,
174 OPEN_EXISTING, 0, NULL);
175 if(hFile == INVALID_HANDLE_VALUE) return FALSE;
179 /*******************************************************************************
182 static void test_create(BOOL* gameStatisticsAvailable)
186 IGameStatisticsMgr* gsm = NULL;
187 *gameStatisticsAvailable = FALSE;
189 /* interface available up from Win7 */
190 hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
193 ok( hr == S_OK, "IGameStatisticsMgr creating failed (result: 0x%08x)\n", hr);
194 *gameStatisticsAvailable = TRUE;
195 IGameStatisticsMgr_Release(gsm);
198 win_skip("IGameStatisticsMgr cannot be created\n");
200 static void test_gamestatisticsmgr( void )
202 static const GUID guidApplicationId = { 0x17A6558E, 0x60BE, 0x4078, { 0xB6, 0x6F, 0x9C, 0x3A, 0xDA, 0x2A, 0x32, 0xE6 } };
203 static const WCHAR sCategory0[] = {'C','a','t','e','g','o','r','y','0',0};
204 static const WCHAR sCategory1[] = {'C','a','t','e','g','o','r','y','1',0};
205 static const WCHAR sCategory2[] = {'C','a','t','e','g','o','r','y','2',0};
206 static const WCHAR sCategory0a[] = {'C','a','t','e','g','o','r','y','0','a',0};
207 static const WCHAR sStatistic00[] = {'S','t','a','t','i','s','t','i','c','0','0',0};
208 static const WCHAR sStatistic01[] = {'S','t','a','t','i','s','t','i','c','0','1',0};
209 static const WCHAR sStatistic10[] = {'S','t','a','t','i','s','t','i','c','1','0',0};
210 static const WCHAR sStatistic11[] = {'S','t','a','t','i','s','t','i','c','1','1',0};
211 static const WCHAR sStatistic20[] = {'S','t','a','t','i','s','t','i','c','2','0',0};
212 static const WCHAR sStatistic21[] = {'S','t','a','t','i','s','t','i','c','2','1',0};
213 static const WCHAR sValue00[] = {'V','a','l','u','e','0','0',0};
214 static const WCHAR sValue01[] = {'V','a','l','u','e','0','1',0};
215 static const WCHAR sValue10[] = {'V','a','l','u','e','1','0',0};
216 static const WCHAR sValue11[] = {'V','a','l','u','e','1','1',0};
217 static const WCHAR sValue20[] = {'V','a','l','u','e','2','0',0};
218 static const WCHAR sValue21[] = {'V','a','l','u','e','2','1',0};
222 LPWSTR lpStatisticsFile = NULL;
223 LPWSTR lpName = NULL, lpValue = NULL, sTooLongString = NULL;
224 UINT uMaxCategoryLength = 0, uMaxNameLength = 0, uMaxValueLength = 0;
225 WORD wMaxStatsPerCategory = 0, wMaxCategories = 0;
227 IGameStatisticsMgr* gsm = NULL;
230 hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
231 ok(hr == S_OK, "IGameStatisticsMgr creating failed (result false)\n");
233 /* test trying to create interface IGameStatistics using GetGameStatistics method */
235 /* this should fail, cause statistics doesn't yet exists */
236 gs = (void *)0xdeadbeef;
237 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENONLY, &dwOpenResult, &gs);
238 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "GetGameStatistics returned unexpected value: 0x%08x\n", hr);
239 ok(gs == NULL, "Expected output pointer to be NULL, got %p\n", gs);
241 /* now, allow to create */
242 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
243 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%x\n", hr);
244 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
247 /* test of limit values returned from interface */
248 hr = IGameStatistics_GetMaxCategoryLength(gs, &uMaxCategoryLength);
249 ok(hr==S_OK, "getting maximum length of category failed\n");
250 ok(uMaxCategoryLength==60, "getting maximum length of category returned invalid value: %d\n", uMaxCategoryLength);
252 hr = IGameStatistics_GetMaxNameLength(gs, &uMaxNameLength);
253 ok(hr==S_OK, "getting maximum name length failed\n");
254 ok(uMaxNameLength==30, "getting maximum name length returned invalid value: %d\n", uMaxNameLength);
256 hr = IGameStatistics_GetMaxValueLength(gs, &uMaxValueLength);
257 ok(hr==S_OK, "getting maximum value length failed\n");
258 ok(uMaxValueLength==30, "getting maximum value length returned invalid value: %d\n", uMaxValueLength);
260 hr = IGameStatistics_GetMaxCategories(gs, &wMaxCategories);
261 ok(hr==S_OK, "getting maximum number of categories failed\n");
262 ok(wMaxCategories==10, "getting maximum number of categories returned invalid value: %d\n", wMaxCategories);
264 hr = IGameStatistics_GetMaxStatsPerCategory(gs, &wMaxStatsPerCategory);
265 ok(hr==S_OK, "getting maximum number of statistics per category failed\n");
266 ok(wMaxStatsPerCategory==10, "getting maximum number of statistics per category returned invalid value: %d\n", wMaxStatsPerCategory);
268 /* create name of statistics file */
269 hr = _buildStatisticsFilePath(&guidApplicationId, &lpStatisticsFile);
270 ok(SUCCEEDED(hr), "cannot build path to game statistics (error 0x%x)\n", hr);
271 trace("statistics file path: %s\n", wine_dbgstr_w(lpStatisticsFile));
272 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
274 /* write sample statistics */
275 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, NULL);
276 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
278 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, sCategory0);
279 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
281 /* check what happen if string is too long */
282 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxCategoryLength+2));
283 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxCategoryLength+1));
284 sTooLongString[uMaxCategoryLength+1]=0;
286 /* when string is too long, Windows returns S_FALSE, but saves string (stripped to expected number of characters) */
287 hr = IGameStatistics_SetCategoryTitle(gs, 0, sTooLongString);
288 ok(hr==S_FALSE, "setting category title invalid result: 0x%x\n", hr);
289 CoTaskMemFree(sTooLongString);
291 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0));
292 ok(IGameStatistics_SetCategoryTitle(gs, 1, sCategory1)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
293 ok(IGameStatistics_SetCategoryTitle(gs, 2, sCategory2)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
295 /* check what happen if any string is NULL */
296 hr = IGameStatistics_SetStatistic(gs, 0, 0, NULL, sValue00);
297 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
299 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, NULL);
300 ok(hr == S_OK, "setting statistic returned unexpected value: 0x%x)\n", hr);
302 /* check what happen if any string is too long */
303 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxNameLength+2));
304 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxNameLength+1));
305 sTooLongString[uMaxNameLength+1]=0;
306 hr = IGameStatistics_SetStatistic(gs, 0, 0, sTooLongString, sValue00);
307 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
308 CoTaskMemFree(sTooLongString);
310 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxValueLength+2));
311 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxValueLength+1));
312 sTooLongString[uMaxValueLength+1]=0;
313 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, sTooLongString);
314 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
315 CoTaskMemFree(sTooLongString);
317 /* check what happen on too big index of category or statistic */
318 hr = IGameStatistics_SetStatistic(gs, wMaxCategories, 0, sStatistic00, sValue00);
319 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
321 hr = IGameStatistics_SetStatistic(gs, 0, wMaxStatsPerCategory, sStatistic00, sValue00);
322 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
324 ok(IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, sValue00)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic00), wine_dbgstr_w(sValue00));
325 ok(IGameStatistics_SetStatistic(gs, 0, 1, sStatistic01, sValue01)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic01), wine_dbgstr_w(sValue01));
326 ok(IGameStatistics_SetStatistic(gs, 1, 0, sStatistic10, sValue10)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic10), wine_dbgstr_w(sValue10));
327 ok(IGameStatistics_SetStatistic(gs, 1, 1, sStatistic11, sValue11)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic11), wine_dbgstr_w(sValue11));
328 ok(IGameStatistics_SetStatistic(gs, 2, 0, sStatistic20, sValue20)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic20), wine_dbgstr_w(sValue20));
329 ok(IGameStatistics_SetStatistic(gs, 2, 1, sStatistic21, sValue21)==S_OK, "setting statistic failed: name=%s, value=%s\n", wine_dbgstr_w(sStatistic21), wine_dbgstr_w(sValue21));
331 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
333 ok(IGameStatistics_Save(gs, FALSE)==S_OK, "statistic saving failed\n");
335 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
337 /* this value should not be stored in storage, we need it only to test is it not saved */
338 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0a)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0a));
340 hr = IGameStatistics_Release(gs);
341 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%08x\n", hr);
343 /* try to read written statistics */
344 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
345 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%08x\n", hr);
346 ok(dwOpenResult == GAMESTATS_OPEN_OPENED, "GetGameStatistics returned invalid open result: 0x%x\n", dwOpenResult);
347 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
349 /* verify values with these which we stored before*/
350 hr = IGameStatistics_GetCategoryTitle(gs, 0, &lpName);
351 ok(hr == S_OK, "getting category title failed\n");
352 ok(lstrcmpW(lpName, sCategory0)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
353 CoTaskMemFree(lpName);
355 hr = IGameStatistics_GetCategoryTitle(gs, 1, &lpName);
356 ok(hr == S_OK, "getting category title failed\n");
357 ok(lstrcmpW(lpName, sCategory1)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
358 CoTaskMemFree(lpName);
360 hr = IGameStatistics_GetCategoryTitle(gs, 2, &lpName);
361 ok(hr == S_OK, "getting category title failed\n");
362 ok(lstrcmpW(lpName, sCategory2)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
363 CoTaskMemFree(lpName);
365 /* check result if category doesn't exists */
366 hr = IGameStatistics_GetCategoryTitle(gs, 3, &lpName);
367 ok(hr == S_OK, "getting category title failed\n");
368 ok(lpName == NULL, "getting category title failed\n");
369 CoTaskMemFree(lpName);
371 hr = IGameStatistics_GetStatistic(gs, 0, 0, &lpName, &lpValue);
372 ok(hr == S_OK, "getting statistic failed\n");
373 ok(lstrcmpW(lpName, sStatistic00)==0, "getting statistic returned invalid name\n");
374 ok(lstrcmpW(lpValue, sValue00)==0, "getting statistic returned invalid value\n");
375 CoTaskMemFree(lpName);
376 CoTaskMemFree(lpValue);
378 hr = IGameStatistics_GetStatistic(gs, 0, 1, &lpName, &lpValue);
379 ok(hr == S_OK, "getting statistic failed\n");
380 ok(lstrcmpW(lpName, sStatistic01)==0, "getting statistic returned invalid name\n");
381 ok(lstrcmpW(lpValue, sValue01)==0, "getting statistic returned invalid value\n");
382 CoTaskMemFree(lpName);
383 CoTaskMemFree(lpValue);
385 hr = IGameStatistics_GetStatistic(gs, 1, 0, &lpName, &lpValue);
386 ok(hr == S_OK, "getting statistic failed\n");
387 ok(lstrcmpW(lpName, sStatistic10)==0, "getting statistic returned invalid name\n");
388 ok(lstrcmpW(lpValue, sValue10)==0, "getting statistic returned invalid value\n");
389 CoTaskMemFree(lpName);
390 CoTaskMemFree(lpValue);
392 hr = IGameStatistics_GetStatistic(gs, 1, 1, &lpName, &lpValue);
393 ok(hr == S_OK, "getting statistic failed\n");
394 ok(lstrcmpW(lpName, sStatistic11)==0, "getting statistic returned invalid name\n");
395 ok(lstrcmpW(lpValue, sValue11)==0, "getting statistic returned invalid value\n");
396 CoTaskMemFree(lpName);
397 CoTaskMemFree(lpValue);
399 hr = IGameStatistics_GetStatistic(gs, 2, 0, &lpName, &lpValue);
400 ok(hr == S_OK, "getting statistic failed\n");
401 ok(lstrcmpW(lpName, sStatistic20)==0, "getting statistic returned invalid name\n");
402 ok(lstrcmpW(lpValue, sValue20)==0, "getting statistic returned invalid value\n");
403 CoTaskMemFree(lpName);
404 CoTaskMemFree(lpValue);
406 hr = IGameStatistics_GetStatistic(gs, 2, 1, &lpName, &lpValue);
407 ok(hr == S_OK, "getting statistic failed\n");
408 ok(lstrcmpW(lpName, sStatistic21)==0, "getting statistic returned invalid name\n");
409 ok(lstrcmpW(lpValue, sValue21)==0, "getting statistic returned invalid value\n");
410 CoTaskMemFree(lpName);
411 CoTaskMemFree(lpValue);
413 hr = IGameStatistics_Release(gs);
414 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%x\n", hr);
416 /* test of removing game statistics from underlying storage */
417 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
418 hr = IGameStatisticsMgr_RemoveGameStatistics(gsm, sExeName);
419 ok(SUCCEEDED(hr), "cannot remove game statistics, error: 0x%x\n", hr);
420 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s still exists\n", wine_dbgstr_w(lpStatisticsFile));
423 hr = IGameStatisticsMgr_Release(gsm);
424 ok(SUCCEEDED(hr), "releasing IGameStatisticsMgr returned error: 0x%x\n", hr);
426 CoTaskMemFree(lpStatisticsFile);
429 START_TEST(gamestatistics)
432 BOOL gameStatisticsAvailable;
434 if(_loadDynamicRoutines())
436 hr = CoInitialize( NULL );
437 ok( hr == S_OK, "failed to init COM\n");
439 test_create(&gameStatisticsAvailable);
441 if(gameStatisticsAvailable)
443 hr = _registerGame();
444 ok( hr == S_OK, "cannot register game in Game Explorer (error: 0x%x)\n", hr);
446 test_gamestatisticsmgr();
448 hr = _unregisterGame();
449 ok( hr == S_OK, "cannot unregister game from Game Explorer (error: 0x%x)\n", hr);
455 /* this is not a failure, because a procedure loaded by address
456 * is always available on systems which has gameux.dll */
457 win_skip("too old system, cannot load required dynamic procedures\n");