Release 4.0.4.
[wine.git] / dlls / gameux / tests / gamestatistics.c
blobd823af1aab21997367efa364752daf1557b08bf8
1 /*
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
21 #define COBJMACROS
23 #include "shlwapi.h"
24 #include "oleauto.h"
25 #include "shlobj.h"
27 #include "gameux.h"
28 #include "wine/test.h"
30 /*******************************************************************************
31 * utilities
33 static WCHAR sExeName[MAX_PATH] = {0};
34 static GUID gameInstanceId;
36 /*******************************************************************************
37 * Registers test suite executable as game in Games Explorer. Required to test
38 * game statistics.
40 static void test_register_game(IGameExplorer **explorer)
42 HRESULT hr;
43 WCHAR pathW[MAX_PATH];
44 BSTR bstrExeName, bstrExePath;
46 /* prepare path to binary */
47 GetModuleFileNameW(NULL, sExeName, ARRAY_SIZE(sExeName));
49 lstrcpyW(pathW, sExeName);
50 PathRemoveFileSpecW(pathW);
52 hr = CoCreateInstance(&CLSID_GameExplorer, NULL, CLSCTX_INPROC_SERVER, &IID_IGameExplorer, (void**)explorer);
53 ok(hr == S_OK, "got 0x%08x\n", hr);
55 gameInstanceId = GUID_NULL;
56 bstrExeName = SysAllocString(sExeName);
57 bstrExePath = SysAllocString(pathW);
58 hr = IGameExplorer_AddGame(*explorer, bstrExeName, bstrExePath, GIS_CURRENT_USER, &gameInstanceId);
59 ok(hr == S_OK, "got 0x%08x\n", hr);
61 SysFreeString(bstrExeName);
62 SysFreeString(bstrExePath);
65 /*******************************************************************************
66 * Unregisters test suite from Games Explorer.
68 static void test_unregister_game(IGameExplorer *ge)
70 HRESULT hr;
72 if (!ge) return;
74 hr = IGameExplorer_RemoveGame(ge, gameInstanceId);
75 ok(hr == S_OK, "got 0x%08x\n", hr);
76 IGameExplorer_Release(ge);
79 /*******************************************************************************
80 * _buildStatisticsFilePath
81 * Creates path to file containing statistics of game with given id.
83 * Parameters:
84 * guidApplicationId [I] application id of game
85 * lpStatisticsFile [O] pointer where address of
86 * string with path will be
87 * stored. Path must be deallocated
88 * using CoTaskMemFree(...)
90 static HRESULT _buildStatisticsFilePath(LPCGUID guidApplicationId, LPWSTR *lpStatisticsFile)
92 static const WCHAR sBackslash[] = {'\\',0};
93 static const WCHAR sStatisticsDir[] = {'\\','M','i','c','r','o','s','o','f','t',
94 '\\','W','i','n','d','o','w','s','\\','G','a','m','e','E','x','p',
95 'l','o','r','e','r','\\','G','a','m','e','S','t','a','t','i','s',
96 't','i','c','s','\\',0};
97 static const WCHAR sDotGamestats[] = {'.','g','a','m','e','s','t','a','t','s',0};
98 HRESULT hr;
99 WCHAR sGuid[49], sPath[MAX_PATH];
101 hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, sPath);
103 if(SUCCEEDED(hr))
104 hr = (StringFromGUID2(guidApplicationId, sGuid, ARRAY_SIZE(sGuid)) != 0 ? S_OK : E_FAIL);
106 if(SUCCEEDED(hr))
108 lstrcatW(sPath, sStatisticsDir);
109 lstrcatW(sPath, sGuid);
110 lstrcatW(sPath, sBackslash);
111 lstrcatW(sPath, sGuid);
112 lstrcatW(sPath, sDotGamestats);
114 *lpStatisticsFile = CoTaskMemAlloc((lstrlenW(sPath)+1)*sizeof(WCHAR));
115 if(!*lpStatisticsFile) hr = E_OUTOFMEMORY;
118 if(SUCCEEDED(hr))
119 lstrcpyW(*lpStatisticsFile, sPath);
121 return hr;
123 /*******************************************************************************
124 * _isFileExist
125 * Checks if given file exists
127 * Parameters:
128 * lpFile [I] path to file
130 * Result:
131 * TRUE file exists
132 * FALSE file does not exist
134 static BOOL _isFileExists(LPCWSTR lpFile)
136 HANDLE hFile = CreateFileW(lpFile, GENERIC_READ, 0, NULL,
137 OPEN_EXISTING, 0, NULL);
138 if(hFile == INVALID_HANDLE_VALUE) return FALSE;
139 CloseHandle(hFile);
140 return TRUE;
142 /*******************************************************************************
143 * test routines
145 static void test_gamestatisticsmgr( void )
147 static const GUID guidApplicationId = { 0x17A6558E, 0x60BE, 0x4078, { 0xB6, 0x6F, 0x9C, 0x3A, 0xDA, 0x2A, 0x32, 0xE6 } };
148 static const WCHAR sCategory0[] = {'C','a','t','e','g','o','r','y','0',0};
149 static const WCHAR sCategory1[] = {'C','a','t','e','g','o','r','y','1',0};
150 static const WCHAR sCategory2[] = {'C','a','t','e','g','o','r','y','2',0};
151 static const WCHAR sCategory0a[] = {'C','a','t','e','g','o','r','y','0','a',0};
152 static const WCHAR sStatistic00[] = {'S','t','a','t','i','s','t','i','c','0','0',0};
153 static const WCHAR sStatistic01[] = {'S','t','a','t','i','s','t','i','c','0','1',0};
154 static const WCHAR sStatistic10[] = {'S','t','a','t','i','s','t','i','c','1','0',0};
155 static const WCHAR sStatistic11[] = {'S','t','a','t','i','s','t','i','c','1','1',0};
156 static const WCHAR sStatistic20[] = {'S','t','a','t','i','s','t','i','c','2','0',0};
157 static const WCHAR sStatistic21[] = {'S','t','a','t','i','s','t','i','c','2','1',0};
158 static const WCHAR sValue00[] = {'V','a','l','u','e','0','0',0};
159 static const WCHAR sValue01[] = {'V','a','l','u','e','0','1',0};
160 static const WCHAR sValue10[] = {'V','a','l','u','e','1','0',0};
161 static const WCHAR sValue11[] = {'V','a','l','u','e','1','1',0};
162 static const WCHAR sValue20[] = {'V','a','l','u','e','2','0',0};
163 static const WCHAR sValue21[] = {'V','a','l','u','e','2','1',0};
165 HRESULT hr;
166 DWORD dwOpenResult;
167 LPWSTR lpStatisticsFile = NULL;
168 LPWSTR lpName = NULL, lpValue = NULL, sTooLongString = NULL;
169 UINT uMaxCategoryLength = 0, uMaxNameLength = 0, uMaxValueLength = 0;
170 WORD wMaxStatsPerCategory = 0, wMaxCategories = 0;
172 IGameStatisticsMgr* gsm = NULL;
173 IGameStatistics* gs;
175 hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
176 ok(hr == S_OK, "IGameStatisticsMgr creating failed (result false)\n");
178 /* test trying to create interface IGameStatistics using GetGameStatistics method */
180 /* this should fail, because statistics don't exist yet */
181 gs = (void *)0xdeadbeef;
182 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENONLY, &dwOpenResult, &gs);
183 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "GetGameStatistics returned unexpected value: 0x%08x\n", hr);
184 ok(gs == NULL, "Expected output pointer to be NULL, got %p\n", gs);
186 /* now, allow them to be created */
187 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
188 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%x\n", hr);
189 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
190 if(gs)
192 /* test of limit values returned from interface */
193 hr = IGameStatistics_GetMaxCategoryLength(gs, &uMaxCategoryLength);
194 ok(hr==S_OK, "getting maximum length of category failed\n");
195 ok(uMaxCategoryLength==60, "getting maximum length of category returned invalid value: %d\n", uMaxCategoryLength);
197 hr = IGameStatistics_GetMaxNameLength(gs, &uMaxNameLength);
198 ok(hr==S_OK, "getting maximum name length failed\n");
199 ok(uMaxNameLength==30, "getting maximum name length returned invalid value: %d\n", uMaxNameLength);
201 hr = IGameStatistics_GetMaxValueLength(gs, &uMaxValueLength);
202 ok(hr==S_OK, "getting maximum value length failed\n");
203 ok(uMaxValueLength==30, "getting maximum value length returned invalid value: %d\n", uMaxValueLength);
205 hr = IGameStatistics_GetMaxCategories(gs, &wMaxCategories);
206 ok(hr==S_OK, "getting maximum number of categories failed\n");
207 ok(wMaxCategories==10, "getting maximum number of categories returned invalid value: %d\n", wMaxCategories);
209 hr = IGameStatistics_GetMaxStatsPerCategory(gs, &wMaxStatsPerCategory);
210 ok(hr==S_OK, "getting maximum number of statistics per category failed\n");
211 ok(wMaxStatsPerCategory==10, "getting maximum number of statistics per category returned invalid value: %d\n", wMaxStatsPerCategory);
213 /* create name of statistics file */
214 hr = _buildStatisticsFilePath(&guidApplicationId, &lpStatisticsFile);
215 ok(SUCCEEDED(hr), "cannot build path to game statistics (error 0x%x)\n", hr);
216 trace("statistics file path: %s\n", wine_dbgstr_w(lpStatisticsFile));
217 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
219 /* write sample statistics */
220 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, NULL);
221 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
223 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, sCategory0);
224 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
226 /* check what happen if string is too long */
227 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxCategoryLength+2));
228 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxCategoryLength+1));
229 sTooLongString[uMaxCategoryLength+1]=0;
231 /* when string is too long, Windows returns S_FALSE, but saves string (stripped to expected number of characters) */
232 hr = IGameStatistics_SetCategoryTitle(gs, 0, sTooLongString);
233 ok(hr==S_FALSE, "setting category title invalid result: 0x%x\n", hr);
234 CoTaskMemFree(sTooLongString);
236 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0));
237 ok(IGameStatistics_SetCategoryTitle(gs, 1, sCategory1)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
238 ok(IGameStatistics_SetCategoryTitle(gs, 2, sCategory2)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
240 /* check what happen if any string is NULL */
241 hr = IGameStatistics_SetStatistic(gs, 0, 0, NULL, sValue00);
242 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
244 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, NULL);
245 ok(hr == S_OK, "setting statistic returned unexpected value: 0x%x)\n", hr);
247 /* check what happen if any string is too long */
248 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxNameLength+2));
249 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxNameLength+1));
250 sTooLongString[uMaxNameLength+1]=0;
251 hr = IGameStatistics_SetStatistic(gs, 0, 0, sTooLongString, sValue00);
252 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
253 CoTaskMemFree(sTooLongString);
255 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxValueLength+2));
256 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxValueLength+1));
257 sTooLongString[uMaxValueLength+1]=0;
258 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, sTooLongString);
259 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
260 CoTaskMemFree(sTooLongString);
262 /* check what happen on too big index of category or statistic */
263 hr = IGameStatistics_SetStatistic(gs, wMaxCategories, 0, sStatistic00, sValue00);
264 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
266 hr = IGameStatistics_SetStatistic(gs, 0, wMaxStatsPerCategory, sStatistic00, sValue00);
267 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
269 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));
270 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));
271 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));
272 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));
273 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));
274 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));
276 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
278 ok(IGameStatistics_Save(gs, FALSE)==S_OK, "statistic saving failed\n");
280 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
282 /* this value should not be stored in storage, we need it only to test is it not saved */
283 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0a)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0a));
285 hr = IGameStatistics_Release(gs);
286 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%08x\n", hr);
288 /* try to read written statistics */
289 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
290 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%08x\n", hr);
291 ok(dwOpenResult == GAMESTATS_OPEN_OPENED, "GetGameStatistics returned invalid open result: 0x%x\n", dwOpenResult);
292 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
294 /* verify values with these which we stored before*/
295 hr = IGameStatistics_GetCategoryTitle(gs, 0, &lpName);
296 ok(hr == S_OK, "getting category title failed\n");
297 ok(lstrcmpW(lpName, sCategory0)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
298 CoTaskMemFree(lpName);
300 hr = IGameStatistics_GetCategoryTitle(gs, 1, &lpName);
301 ok(hr == S_OK, "getting category title failed\n");
302 ok(lstrcmpW(lpName, sCategory1)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
303 CoTaskMemFree(lpName);
305 hr = IGameStatistics_GetCategoryTitle(gs, 2, &lpName);
306 ok(hr == S_OK, "getting category title failed\n");
307 ok(lstrcmpW(lpName, sCategory2)==0, "getting category title returned invalid string (%s)\n", wine_dbgstr_w(lpName));
308 CoTaskMemFree(lpName);
310 /* check result if category doesn't exists */
311 hr = IGameStatistics_GetCategoryTitle(gs, 3, &lpName);
312 ok(hr == S_OK, "getting category title failed\n");
313 ok(lpName == NULL, "getting category title failed\n");
314 CoTaskMemFree(lpName);
316 hr = IGameStatistics_GetStatistic(gs, 0, 0, &lpName, &lpValue);
317 ok(hr == S_OK, "getting statistic failed\n");
318 ok(lstrcmpW(lpName, sStatistic00)==0, "getting statistic returned invalid name\n");
319 ok(lstrcmpW(lpValue, sValue00)==0, "getting statistic returned invalid value\n");
320 CoTaskMemFree(lpName);
321 CoTaskMemFree(lpValue);
323 hr = IGameStatistics_GetStatistic(gs, 0, 1, &lpName, &lpValue);
324 ok(hr == S_OK, "getting statistic failed\n");
325 ok(lstrcmpW(lpName, sStatistic01)==0, "getting statistic returned invalid name\n");
326 ok(lstrcmpW(lpValue, sValue01)==0, "getting statistic returned invalid value\n");
327 CoTaskMemFree(lpName);
328 CoTaskMemFree(lpValue);
330 hr = IGameStatistics_GetStatistic(gs, 1, 0, &lpName, &lpValue);
331 ok(hr == S_OK, "getting statistic failed\n");
332 ok(lstrcmpW(lpName, sStatistic10)==0, "getting statistic returned invalid name\n");
333 ok(lstrcmpW(lpValue, sValue10)==0, "getting statistic returned invalid value\n");
334 CoTaskMemFree(lpName);
335 CoTaskMemFree(lpValue);
337 hr = IGameStatistics_GetStatistic(gs, 1, 1, &lpName, &lpValue);
338 ok(hr == S_OK, "getting statistic failed\n");
339 ok(lstrcmpW(lpName, sStatistic11)==0, "getting statistic returned invalid name\n");
340 ok(lstrcmpW(lpValue, sValue11)==0, "getting statistic returned invalid value\n");
341 CoTaskMemFree(lpName);
342 CoTaskMemFree(lpValue);
344 hr = IGameStatistics_GetStatistic(gs, 2, 0, &lpName, &lpValue);
345 ok(hr == S_OK, "getting statistic failed\n");
346 ok(lstrcmpW(lpName, sStatistic20)==0, "getting statistic returned invalid name\n");
347 ok(lstrcmpW(lpValue, sValue20)==0, "getting statistic returned invalid value\n");
348 CoTaskMemFree(lpName);
349 CoTaskMemFree(lpValue);
351 hr = IGameStatistics_GetStatistic(gs, 2, 1, &lpName, &lpValue);
352 ok(hr == S_OK, "getting statistic failed\n");
353 ok(lstrcmpW(lpName, sStatistic21)==0, "getting statistic returned invalid name\n");
354 ok(lstrcmpW(lpValue, sValue21)==0, "getting statistic returned invalid value\n");
355 CoTaskMemFree(lpName);
356 CoTaskMemFree(lpValue);
358 hr = IGameStatistics_Release(gs);
359 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%x\n", hr);
361 /* test of removing game statistics from underlying storage */
362 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
363 hr = IGameStatisticsMgr_RemoveGameStatistics(gsm, sExeName);
364 ok(SUCCEEDED(hr), "cannot remove game statistics, error: 0x%x\n", hr);
365 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s still exists\n", wine_dbgstr_w(lpStatisticsFile));
368 hr = IGameStatisticsMgr_Release(gsm);
369 ok(SUCCEEDED(hr), "releasing IGameStatisticsMgr returned error: 0x%x\n", hr);
371 CoTaskMemFree(lpStatisticsFile);
374 START_TEST(gamestatistics)
376 HRESULT hr;
377 IGameStatisticsMgr* gsm;
378 IGameExplorer *ge;
380 hr = CoInitialize( NULL );
381 ok(hr == S_OK, "failed to init COM\n");
383 /* interface available up from Win7 */
384 hr = CoCreateInstance(&CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (void**)&gsm);
385 if (FAILED(hr))
387 win_skip("IGameStatisticsMgr is not supported.\n");
388 CoUninitialize();
389 return;
391 IGameStatisticsMgr_Release(gsm);
393 test_register_game(&ge);
394 test_gamestatisticsmgr();
395 test_unregister_game(ge);
397 CoUninitialize();