mstask: Use wide-char string literals.
[wine.git] / dlls / gameux / tests / gamestatistics.c
blob192a245e096701a7a29dd14ec163a6511c7f7c91
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 %s\n",
185 (gs == (void *)0xdeadbeef ? "deadbeef" : "neither NULL nor deadbeef"));
187 /* now, allow them to be created */
188 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
189 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%x\n", hr);
190 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
191 if(gs)
193 /* test of limit values returned from interface */
194 hr = IGameStatistics_GetMaxCategoryLength(gs, &uMaxCategoryLength);
195 ok(hr==S_OK, "getting maximum length of category failed\n");
196 ok(uMaxCategoryLength==60, "getting maximum length of category returned invalid value: %d\n", uMaxCategoryLength);
198 hr = IGameStatistics_GetMaxNameLength(gs, &uMaxNameLength);
199 ok(hr==S_OK, "getting maximum name length failed\n");
200 ok(uMaxNameLength==30, "getting maximum name length returned invalid value: %d\n", uMaxNameLength);
202 hr = IGameStatistics_GetMaxValueLength(gs, &uMaxValueLength);
203 ok(hr==S_OK, "getting maximum value length failed\n");
204 ok(uMaxValueLength==30, "getting maximum value length returned invalid value: %d\n", uMaxValueLength);
206 hr = IGameStatistics_GetMaxCategories(gs, &wMaxCategories);
207 ok(hr==S_OK, "getting maximum number of categories failed\n");
208 ok(wMaxCategories==10, "getting maximum number of categories returned invalid value: %d\n", wMaxCategories);
210 hr = IGameStatistics_GetMaxStatsPerCategory(gs, &wMaxStatsPerCategory);
211 ok(hr==S_OK, "getting maximum number of statistics per category failed\n");
212 ok(wMaxStatsPerCategory==10, "getting maximum number of statistics per category returned invalid value: %d\n", wMaxStatsPerCategory);
214 /* create name of statistics file */
215 hr = _buildStatisticsFilePath(&guidApplicationId, &lpStatisticsFile);
216 ok(SUCCEEDED(hr), "cannot build path to game statistics (error 0x%x)\n", hr);
217 trace("statistics file path: %s\n", wine_dbgstr_w(lpStatisticsFile));
218 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
220 /* write sample statistics */
221 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, NULL);
222 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
224 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, sCategory0);
225 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
227 /* check what happen if string is too long */
228 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxCategoryLength+2));
229 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxCategoryLength+1));
230 sTooLongString[uMaxCategoryLength+1]=0;
232 /* when string is too long, Windows returns S_FALSE, but saves string (stripped to expected number of characters) */
233 hr = IGameStatistics_SetCategoryTitle(gs, 0, sTooLongString);
234 ok(hr==S_FALSE, "setting category title invalid result: 0x%x\n", hr);
235 CoTaskMemFree(sTooLongString);
237 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0));
238 ok(IGameStatistics_SetCategoryTitle(gs, 1, sCategory1)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
239 ok(IGameStatistics_SetCategoryTitle(gs, 2, sCategory2)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory1));
241 /* check what happen if any string is NULL */
242 hr = IGameStatistics_SetStatistic(gs, 0, 0, NULL, sValue00);
243 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
245 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, NULL);
246 ok(hr == S_OK, "setting statistic returned unexpected value: 0x%x)\n", hr);
248 /* check what happen if any string is too long */
249 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxNameLength+2));
250 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxNameLength+1));
251 sTooLongString[uMaxNameLength+1]=0;
252 hr = IGameStatistics_SetStatistic(gs, 0, 0, sTooLongString, sValue00);
253 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
254 CoTaskMemFree(sTooLongString);
256 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxValueLength+2));
257 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxValueLength+1));
258 sTooLongString[uMaxValueLength+1]=0;
259 hr = IGameStatistics_SetStatistic(gs, 0, 0, sStatistic00, sTooLongString);
260 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
261 CoTaskMemFree(sTooLongString);
263 /* check what happen on too big index of category or statistic */
264 hr = IGameStatistics_SetStatistic(gs, wMaxCategories, 0, sStatistic00, sValue00);
265 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
267 hr = IGameStatistics_SetStatistic(gs, 0, wMaxStatsPerCategory, sStatistic00, sValue00);
268 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
270 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));
271 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));
272 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));
273 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));
274 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));
275 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));
277 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
279 ok(IGameStatistics_Save(gs, FALSE)==S_OK, "statistic saving failed\n");
281 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
283 /* this value should not be stored in storage, we need it only to test is it not saved */
284 ok(IGameStatistics_SetCategoryTitle(gs, 0, sCategory0a)==S_OK, "setting category title failed: %s\n", wine_dbgstr_w(sCategory0a));
286 hr = IGameStatistics_Release(gs);
287 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%08x\n", hr);
289 /* try to read written statistics */
290 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
291 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%08x\n", hr);
292 ok(dwOpenResult == GAMESTATS_OPEN_OPENED, "GetGameStatistics returned invalid open result: 0x%x\n", dwOpenResult);
293 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
295 /* verify values with these which we stored before*/
296 hr = IGameStatistics_GetCategoryTitle(gs, 0, &lpName);
297 ok(hr == S_OK, "getting category title failed\n");
298 ok(lstrcmpW(lpName, sCategory0)==0, "getting category title returned invalid string %s\n", wine_dbgstr_w(lpName));
299 CoTaskMemFree(lpName);
301 hr = IGameStatistics_GetCategoryTitle(gs, 1, &lpName);
302 ok(hr == S_OK, "getting category title failed\n");
303 ok(lstrcmpW(lpName, sCategory1)==0, "getting category title returned invalid string %s\n", wine_dbgstr_w(lpName));
304 CoTaskMemFree(lpName);
306 hr = IGameStatistics_GetCategoryTitle(gs, 2, &lpName);
307 ok(hr == S_OK, "getting category title failed\n");
308 ok(lstrcmpW(lpName, sCategory2)==0, "getting category title returned invalid string %s\n", wine_dbgstr_w(lpName));
309 CoTaskMemFree(lpName);
311 /* check result if category doesn't exists */
312 hr = IGameStatistics_GetCategoryTitle(gs, 3, &lpName);
313 ok(hr == S_OK, "getting category title failed\n");
314 ok(lpName == NULL, "getting category title failed\n");
315 CoTaskMemFree(lpName);
317 hr = IGameStatistics_GetStatistic(gs, 0, 0, &lpName, &lpValue);
318 ok(hr == S_OK, "getting statistic failed\n");
319 ok(lstrcmpW(lpName, sStatistic00)==0, "getting statistic returned invalid name\n");
320 ok(lstrcmpW(lpValue, sValue00)==0, "getting statistic returned invalid value\n");
321 CoTaskMemFree(lpName);
322 CoTaskMemFree(lpValue);
324 hr = IGameStatistics_GetStatistic(gs, 0, 1, &lpName, &lpValue);
325 ok(hr == S_OK, "getting statistic failed\n");
326 ok(lstrcmpW(lpName, sStatistic01)==0, "getting statistic returned invalid name\n");
327 ok(lstrcmpW(lpValue, sValue01)==0, "getting statistic returned invalid value\n");
328 CoTaskMemFree(lpName);
329 CoTaskMemFree(lpValue);
331 hr = IGameStatistics_GetStatistic(gs, 1, 0, &lpName, &lpValue);
332 ok(hr == S_OK, "getting statistic failed\n");
333 ok(lstrcmpW(lpName, sStatistic10)==0, "getting statistic returned invalid name\n");
334 ok(lstrcmpW(lpValue, sValue10)==0, "getting statistic returned invalid value\n");
335 CoTaskMemFree(lpName);
336 CoTaskMemFree(lpValue);
338 hr = IGameStatistics_GetStatistic(gs, 1, 1, &lpName, &lpValue);
339 ok(hr == S_OK, "getting statistic failed\n");
340 ok(lstrcmpW(lpName, sStatistic11)==0, "getting statistic returned invalid name\n");
341 ok(lstrcmpW(lpValue, sValue11)==0, "getting statistic returned invalid value\n");
342 CoTaskMemFree(lpName);
343 CoTaskMemFree(lpValue);
345 hr = IGameStatistics_GetStatistic(gs, 2, 0, &lpName, &lpValue);
346 ok(hr == S_OK, "getting statistic failed\n");
347 ok(lstrcmpW(lpName, sStatistic20)==0, "getting statistic returned invalid name\n");
348 ok(lstrcmpW(lpValue, sValue20)==0, "getting statistic returned invalid value\n");
349 CoTaskMemFree(lpName);
350 CoTaskMemFree(lpValue);
352 hr = IGameStatistics_GetStatistic(gs, 2, 1, &lpName, &lpValue);
353 ok(hr == S_OK, "getting statistic failed\n");
354 ok(lstrcmpW(lpName, sStatistic21)==0, "getting statistic returned invalid name\n");
355 ok(lstrcmpW(lpValue, sValue21)==0, "getting statistic returned invalid value\n");
356 CoTaskMemFree(lpName);
357 CoTaskMemFree(lpValue);
359 hr = IGameStatistics_Release(gs);
360 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%x\n", hr);
362 /* test of removing game statistics from underlying storage */
363 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
364 hr = IGameStatisticsMgr_RemoveGameStatistics(gsm, sExeName);
365 ok(SUCCEEDED(hr), "cannot remove game statistics, error: 0x%x\n", hr);
366 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s still exists\n", wine_dbgstr_w(lpStatisticsFile));
369 hr = IGameStatisticsMgr_Release(gsm);
370 ok(SUCCEEDED(hr), "releasing IGameStatisticsMgr returned error: 0x%x\n", hr);
372 CoTaskMemFree(lpStatisticsFile);
375 START_TEST(gamestatistics)
377 HRESULT hr;
378 IGameStatisticsMgr* gsm;
379 IGameExplorer *ge;
381 hr = CoInitialize( NULL );
382 ok(hr == S_OK, "failed to init COM\n");
384 /* interface available up from Win7 */
385 hr = CoCreateInstance(&CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (void**)&gsm);
386 if (FAILED(hr))
388 win_skip("IGameStatisticsMgr is not supported.\n");
389 CoUninitialize();
390 return;
392 IGameStatisticsMgr_Release(gsm);
394 test_register_game(&ge);
395 test_gamestatisticsmgr();
396 test_unregister_game(ge);
398 CoUninitialize();