mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / gameux / tests / gamestatistics.c
blobc29c6b444708ed0069f720f7ccb2d6435e1e3271
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 HRESULT hr;
93 WCHAR sGuid[49], sPath[MAX_PATH];
95 hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, sPath);
97 if(SUCCEEDED(hr))
98 hr = (StringFromGUID2(guidApplicationId, sGuid, ARRAY_SIZE(sGuid)) != 0 ? S_OK : E_FAIL);
100 if(SUCCEEDED(hr))
102 lstrcatW(sPath, L"\\Microsoft\\Windows\\GameExplorer\\GameStatistics\\");
103 lstrcatW(sPath, sGuid);
104 lstrcatW(sPath, L"\\");
105 lstrcatW(sPath, sGuid);
106 lstrcatW(sPath, L".gamestats");
108 *lpStatisticsFile = CoTaskMemAlloc((lstrlenW(sPath)+1)*sizeof(WCHAR));
109 if(!*lpStatisticsFile) hr = E_OUTOFMEMORY;
112 if(SUCCEEDED(hr))
113 lstrcpyW(*lpStatisticsFile, sPath);
115 return hr;
117 /*******************************************************************************
118 * _isFileExist
119 * Checks if given file exists
121 * Parameters:
122 * lpFile [I] path to file
124 * Result:
125 * TRUE file exists
126 * FALSE file does not exist
128 static BOOL _isFileExists(LPCWSTR lpFile)
130 HANDLE hFile = CreateFileW(lpFile, GENERIC_READ, 0, NULL,
131 OPEN_EXISTING, 0, NULL);
132 if(hFile == INVALID_HANDLE_VALUE) return FALSE;
133 CloseHandle(hFile);
134 return TRUE;
136 /*******************************************************************************
137 * test routines
139 static void test_gamestatisticsmgr( void )
141 static const GUID guidApplicationId = { 0x17A6558E, 0x60BE, 0x4078, { 0xB6, 0x6F, 0x9C, 0x3A, 0xDA, 0x2A, 0x32, 0xE6 } };
143 HRESULT hr;
144 DWORD dwOpenResult;
145 LPWSTR lpStatisticsFile = NULL;
146 LPWSTR lpName = NULL, lpValue = NULL, sTooLongString = NULL;
147 UINT uMaxCategoryLength = 0, uMaxNameLength = 0, uMaxValueLength = 0;
148 WORD wMaxStatsPerCategory = 0, wMaxCategories = 0;
150 IGameStatisticsMgr* gsm = NULL;
151 IGameStatistics* gs;
153 hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
154 ok(hr == S_OK, "IGameStatisticsMgr creating failed (result false)\n");
156 /* test trying to create interface IGameStatistics using GetGameStatistics method */
158 /* this should fail, because statistics don't exist yet */
159 gs = (void *)0xdeadbeef;
160 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENONLY, &dwOpenResult, &gs);
161 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "GetGameStatistics returned unexpected value: 0x%08x\n", hr);
162 ok(gs == NULL, "Expected output pointer to be NULL, got %s\n",
163 (gs == (void *)0xdeadbeef ? "deadbeef" : "neither NULL nor deadbeef"));
165 /* now, allow them to be created */
166 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
167 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%x\n", hr);
168 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
169 if(gs)
171 /* test of limit values returned from interface */
172 hr = IGameStatistics_GetMaxCategoryLength(gs, &uMaxCategoryLength);
173 ok(hr==S_OK, "getting maximum length of category failed\n");
174 ok(uMaxCategoryLength==60, "getting maximum length of category returned invalid value: %d\n", uMaxCategoryLength);
176 hr = IGameStatistics_GetMaxNameLength(gs, &uMaxNameLength);
177 ok(hr==S_OK, "getting maximum name length failed\n");
178 ok(uMaxNameLength==30, "getting maximum name length returned invalid value: %d\n", uMaxNameLength);
180 hr = IGameStatistics_GetMaxValueLength(gs, &uMaxValueLength);
181 ok(hr==S_OK, "getting maximum value length failed\n");
182 ok(uMaxValueLength==30, "getting maximum value length returned invalid value: %d\n", uMaxValueLength);
184 hr = IGameStatistics_GetMaxCategories(gs, &wMaxCategories);
185 ok(hr==S_OK, "getting maximum number of categories failed\n");
186 ok(wMaxCategories==10, "getting maximum number of categories returned invalid value: %d\n", wMaxCategories);
188 hr = IGameStatistics_GetMaxStatsPerCategory(gs, &wMaxStatsPerCategory);
189 ok(hr==S_OK, "getting maximum number of statistics per category failed\n");
190 ok(wMaxStatsPerCategory==10, "getting maximum number of statistics per category returned invalid value: %d\n", wMaxStatsPerCategory);
192 /* create name of statistics file */
193 hr = _buildStatisticsFilePath(&guidApplicationId, &lpStatisticsFile);
194 ok(SUCCEEDED(hr), "cannot build path to game statistics (error 0x%x)\n", hr);
195 trace("statistics file path: %s\n", wine_dbgstr_w(lpStatisticsFile));
196 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
198 /* write sample statistics */
199 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, NULL);
200 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
202 hr = IGameStatistics_SetCategoryTitle(gs, wMaxCategories, L"Category0");
203 ok(hr==E_INVALIDARG, "setting category title invalid value: 0x%x\n", hr);
205 /* check what happen if string is too long */
206 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxCategoryLength+2));
207 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxCategoryLength+1));
208 sTooLongString[uMaxCategoryLength+1]=0;
210 /* when string is too long, Windows returns S_FALSE, but saves string (stripped to expected number of characters) */
211 hr = IGameStatistics_SetCategoryTitle(gs, 0, sTooLongString);
212 ok(hr==S_FALSE, "setting category title invalid result: 0x%x\n", hr);
213 CoTaskMemFree(sTooLongString);
215 ok(IGameStatistics_SetCategoryTitle(gs, 0, L"Category0")==S_OK, "setting category title failed: Category0\n");
216 ok(IGameStatistics_SetCategoryTitle(gs, 1, L"Category1")==S_OK, "setting category title failed: Category1\n");
217 ok(IGameStatistics_SetCategoryTitle(gs, 2, L"Category2")==S_OK, "setting category title failed: Category2\n");
219 /* check what happen if any string is NULL */
220 hr = IGameStatistics_SetStatistic(gs, 0, 0, NULL, L"Value00");
221 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
223 hr = IGameStatistics_SetStatistic(gs, 0, 0, L"Statistic00", NULL);
224 ok(hr == S_OK, "setting statistic returned unexpected value: 0x%x)\n", hr);
226 /* check what happen if any string is too long */
227 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxNameLength+2));
228 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxNameLength+1));
229 sTooLongString[uMaxNameLength+1]=0;
230 hr = IGameStatistics_SetStatistic(gs, 0, 0, sTooLongString, L"Value00");
231 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
232 CoTaskMemFree(sTooLongString);
234 sTooLongString = CoTaskMemAlloc(sizeof(WCHAR)*(uMaxValueLength+2));
235 memset(sTooLongString, 'a', sizeof(WCHAR)*(uMaxValueLength+1));
236 sTooLongString[uMaxValueLength+1]=0;
237 hr = IGameStatistics_SetStatistic(gs, 0, 0, L"Statistic00", sTooLongString);
238 ok(hr == S_FALSE, "setting statistic returned unexpected value: 0x%x)\n", hr);
239 CoTaskMemFree(sTooLongString);
241 /* check what happen on too big index of category or statistic */
242 hr = IGameStatistics_SetStatistic(gs, wMaxCategories, 0, L"Statistic00", L"Value00");
243 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
245 hr = IGameStatistics_SetStatistic(gs, 0, wMaxStatsPerCategory, L"Statistic00", L"Value00");
246 ok(hr == E_INVALIDARG, "setting statistic returned unexpected value: 0x%x)\n", hr);
248 ok(IGameStatistics_SetStatistic(gs, 0, 0, L"Statistic00", L"Value00")==S_OK,
249 "setting statistic failed: name=Statistic00, value=Value00\n");
250 ok(IGameStatistics_SetStatistic(gs, 0, 1, L"Statistic01", L"Value01")==S_OK,
251 "setting statistic failed: name=Statistic01, value=Value01\n");
252 ok(IGameStatistics_SetStatistic(gs, 1, 0, L"Statistic10", L"Value10")==S_OK,
253 "setting statistic failed: name=Statistic10, value=Value10\n");
254 ok(IGameStatistics_SetStatistic(gs, 1, 1, L"Statistic11", L"Value11")==S_OK,
255 "setting statistic failed: name=Statistic11, value=Value11\n");
256 ok(IGameStatistics_SetStatistic(gs, 2, 0, L"Statistic20", L"Value20")==S_OK,
257 "setting statistic failed: name=Statistic20, value=Value20\n");
258 ok(IGameStatistics_SetStatistic(gs, 2, 1, L"Statistic21", L"Value21")==S_OK,
259 "setting statistic failed: name=Statistic21, value=Value21\n");
261 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s already exists\n", wine_dbgstr_w(lpStatisticsFile));
263 ok(IGameStatistics_Save(gs, FALSE)==S_OK, "statistic saving failed\n");
265 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
267 /* this value should not be stored in storage, we need it only to test is it not saved */
268 ok(IGameStatistics_SetCategoryTitle(gs, 0, L"Category0a")==S_OK, "setting category title failed: Category0a\n");
270 hr = IGameStatistics_Release(gs);
271 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%08x\n", hr);
273 /* try to read written statistics */
274 hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
275 ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%08x\n", hr);
276 ok(dwOpenResult == GAMESTATS_OPEN_OPENED, "GetGameStatistics returned invalid open result: 0x%x\n", dwOpenResult);
277 ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
279 /* verify values with these which we stored before*/
280 hr = IGameStatistics_GetCategoryTitle(gs, 0, &lpName);
281 ok(hr == S_OK, "getting category title failed\n");
282 ok(lstrcmpW(lpName, L"Category0")==0, "getting category title returned invalid string %s\n",
283 wine_dbgstr_w(lpName));
284 CoTaskMemFree(lpName);
286 hr = IGameStatistics_GetCategoryTitle(gs, 1, &lpName);
287 ok(hr == S_OK, "getting category title failed\n");
288 ok(lstrcmpW(lpName, L"Category1")==0, "getting category title returned invalid string %s\n",
289 wine_dbgstr_w(lpName));
290 CoTaskMemFree(lpName);
292 hr = IGameStatistics_GetCategoryTitle(gs, 2, &lpName);
293 ok(hr == S_OK, "getting category title failed\n");
294 ok(lstrcmpW(lpName, L"Category2")==0, "getting category title returned invalid string %s\n",
295 wine_dbgstr_w(lpName));
296 CoTaskMemFree(lpName);
298 /* check result if category doesn't exists */
299 hr = IGameStatistics_GetCategoryTitle(gs, 3, &lpName);
300 ok(hr == S_OK, "getting category title failed\n");
301 ok(lpName == NULL, "getting category title failed\n");
302 CoTaskMemFree(lpName);
304 hr = IGameStatistics_GetStatistic(gs, 0, 0, &lpName, &lpValue);
305 ok(hr == S_OK, "getting statistic failed\n");
306 ok(lstrcmpW(lpName, L"Statistic00")==0, "getting statistic returned invalid name\n");
307 ok(lstrcmpW(lpValue, L"Value00")==0, "getting statistic returned invalid value\n");
308 CoTaskMemFree(lpName);
309 CoTaskMemFree(lpValue);
311 hr = IGameStatistics_GetStatistic(gs, 0, 1, &lpName, &lpValue);
312 ok(hr == S_OK, "getting statistic failed\n");
313 ok(lstrcmpW(lpName, L"Statistic01")==0, "getting statistic returned invalid name\n");
314 ok(lstrcmpW(lpValue, L"Value01")==0, "getting statistic returned invalid value\n");
315 CoTaskMemFree(lpName);
316 CoTaskMemFree(lpValue);
318 hr = IGameStatistics_GetStatistic(gs, 1, 0, &lpName, &lpValue);
319 ok(hr == S_OK, "getting statistic failed\n");
320 ok(lstrcmpW(lpName, L"Statistic10")==0, "getting statistic returned invalid name\n");
321 ok(lstrcmpW(lpValue, L"Value10")==0, "getting statistic returned invalid value\n");
322 CoTaskMemFree(lpName);
323 CoTaskMemFree(lpValue);
325 hr = IGameStatistics_GetStatistic(gs, 1, 1, &lpName, &lpValue);
326 ok(hr == S_OK, "getting statistic failed\n");
327 ok(lstrcmpW(lpName, L"Statistic11")==0, "getting statistic returned invalid name\n");
328 ok(lstrcmpW(lpValue, L"Value11")==0, "getting statistic returned invalid value\n");
329 CoTaskMemFree(lpName);
330 CoTaskMemFree(lpValue);
332 hr = IGameStatistics_GetStatistic(gs, 2, 0, &lpName, &lpValue);
333 ok(hr == S_OK, "getting statistic failed\n");
334 ok(lstrcmpW(lpName, L"Statistic20")==0, "getting statistic returned invalid name\n");
335 ok(lstrcmpW(lpValue, L"Value20")==0, "getting statistic returned invalid value\n");
336 CoTaskMemFree(lpName);
337 CoTaskMemFree(lpValue);
339 hr = IGameStatistics_GetStatistic(gs, 2, 1, &lpName, &lpValue);
340 ok(hr == S_OK, "getting statistic failed\n");
341 ok(lstrcmpW(lpName, L"Statistic21")==0, "getting statistic returned invalid name\n");
342 ok(lstrcmpW(lpValue, L"Value21")==0, "getting statistic returned invalid value\n");
343 CoTaskMemFree(lpName);
344 CoTaskMemFree(lpValue);
346 hr = IGameStatistics_Release(gs);
347 ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%x\n", hr);
349 /* test of removing game statistics from underlying storage */
350 ok(_isFileExists(lpStatisticsFile) == TRUE, "statistics file %s does not exists\n", wine_dbgstr_w(lpStatisticsFile));
351 hr = IGameStatisticsMgr_RemoveGameStatistics(gsm, sExeName);
352 ok(SUCCEEDED(hr), "cannot remove game statistics, error: 0x%x\n", hr);
353 ok(_isFileExists(lpStatisticsFile) == FALSE, "statistics file %s still exists\n", wine_dbgstr_w(lpStatisticsFile));
356 hr = IGameStatisticsMgr_Release(gsm);
357 ok(SUCCEEDED(hr), "releasing IGameStatisticsMgr returned error: 0x%x\n", hr);
359 CoTaskMemFree(lpStatisticsFile);
362 START_TEST(gamestatistics)
364 HRESULT hr;
365 IGameStatisticsMgr* gsm;
366 IGameExplorer *ge;
368 hr = CoInitialize( NULL );
369 ok(hr == S_OK, "failed to init COM\n");
371 /* interface available up from Win7 */
372 hr = CoCreateInstance(&CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (void**)&gsm);
373 if (FAILED(hr))
375 win_skip("IGameStatisticsMgr is not supported.\n");
376 CoUninitialize();
377 return;
379 IGameStatisticsMgr_Release(gsm);
381 test_register_game(&ge);
382 test_gamestatisticsmgr();
383 test_unregister_game(ge);
385 CoUninitialize();