taskkill: Implement graceful termination by process name.
[wine] / dlls / gameux / tests / gamestatistics.c
1 /*
2  * IGameStatisticsMgr tests
3  *
4  * Copyright (C) 2010 Mariusz PluciƄski
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define COBJMACROS
22
23 #include "shlwapi.h"
24 #include "oleauto.h"
25
26 #include "gameux.h"
27 #include "wine/test.h"
28
29 /*******************************************************************************
30  * utilities
31  */
32 static IGameExplorer *ge = NULL;
33 static WCHAR sExeName[MAX_PATH] = {0};
34 static GUID gameInstanceId;
35 /*******************************************************************************
36  * _registerGame
37  * Registers test suite executable as game in Games Explorer. Required to test
38  * game statistics.
39  */
40 static HRESULT _registerGame(void) {
41     HRESULT hr;
42     WCHAR sExePath[MAX_PATH];
43     BSTR bstrExeName, bstrExePath;
44     DWORD dwExeNameLen;
45
46     /* prepare path to binary */
47     dwExeNameLen = GetModuleFileNameW(NULL, sExeName, sizeof (sExeName) / sizeof (sExeName[0]));
48     hr = (dwExeNameLen!= 0 ? S_OK : E_FAIL);
49     lstrcpynW(sExePath, sExeName, StrRChrW(sExeName, NULL, '\\') - sExeName + 1);
50
51     bstrExeName = SysAllocString(sExeName);
52     if(!bstrExeName) hr = E_OUTOFMEMORY;
53
54     bstrExePath = SysAllocString(sExePath);
55     if(!bstrExePath) hr = E_OUTOFMEMORY;
56
57     if(SUCCEEDED(hr))
58     {
59         gameInstanceId = GUID_NULL;
60         hr = CoCreateInstance(&CLSID_GameExplorer, NULL, CLSCTX_INPROC_SERVER,
61                               &IID_IGameExplorer, (LPVOID*)&ge);
62     }
63
64     if(SUCCEEDED(hr))
65         hr = IGameExplorer_AddGame(ge, bstrExeName, bstrExePath,
66                                    GIS_CURRENT_USER, &gameInstanceId);
67
68     if(FAILED(hr) && ge)
69     {
70         IGameExplorer_Release(ge);
71         ge = NULL;
72     }
73
74     SysFreeString(bstrExeName);
75     SysFreeString(bstrExePath);
76     return hr;
77 }
78 /*******************************************************************************
79  * _unregisterGame
80  * Unregisters test suite from Games Explorer.
81  */
82 static HRESULT _unregisterGame(void) {
83     HRESULT hr;
84
85     if(!ge) return E_FAIL;
86
87     hr = IGameExplorer_RemoveGame(ge, gameInstanceId);
88
89     IGameExplorer_Release(ge);
90     ge = NULL;
91
92     return hr;
93 }
94 /*******************************************************************************
95  * test routines
96  */
97 static void test_create(BOOL* gameStatisticsAvailable)
98 {
99     HRESULT hr;
100
101     IGameStatisticsMgr* gsm = NULL;
102     *gameStatisticsAvailable = FALSE;
103
104     /* interface available up from Win7 */
105     hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
106     if(gsm)
107     {
108         ok( hr == S_OK, "IGameStatisticsMgr creating failed (result: 0x%08x)\n", hr);
109         *gameStatisticsAvailable = TRUE;
110         IGameStatisticsMgr_Release(gsm);
111     }
112     else
113         win_skip("IGameStatisticsMgr cannot be created\n");
114 }
115 static void test_gamestatisticsmgr( void )
116 {
117     HRESULT hr;
118     DWORD dwOpenResult;
119
120     IGameStatisticsMgr* gsm = NULL;
121     IGameStatistics* gs = NULL;
122
123     hr = CoCreateInstance( &CLSID_GameStatistics, NULL, CLSCTX_INPROC_SERVER, &IID_IGameStatisticsMgr, (LPVOID*)&gsm);
124     ok(hr == S_OK, "IGameStatisticsMgr creating failed (result false)\n");
125
126     /* test trying to create interface IGameStatistics using GetGameStatistics method */
127     hr = IGameStatisticsMgr_GetGameStatistics(gsm, sExeName, GAMESTATS_OPEN_OPENORCREATE, &dwOpenResult, &gs);
128     todo_wine ok(SUCCEEDED(hr), "GetGameStatistics returned error: 0x%x\n", hr);
129     todo_wine ok(gs!=NULL, "GetGameStatistics did not return valid interface pointer\n");
130     if(gs)
131     {
132         hr = IGameStatistics_Release(gs);
133         ok(SUCCEEDED(hr), "releasing IGameStatistics returned error: 0x%x\n", hr);
134
135         /* test of removing game statistics from underlying storage */
136         hr = IGameStatisticsMgr_RemoveGameStatistics(gsm, sExeName);
137         todo_wine ok(SUCCEEDED(hr), "cannot remove game statistics, error: 0x%x\n", hr);
138     }
139
140     hr = IGameStatisticsMgr_Release(gsm);
141     ok(SUCCEEDED(hr), "releasing IGameStatisticsMgr returned error: 0x%x\n", hr);
142 }
143
144 START_TEST(gamestatistics)
145 {
146     HRESULT hr;
147     BOOL gameStatisticsAvailable;
148
149     hr = CoInitialize( NULL );
150     ok( hr == S_OK, "failed to init COM\n");
151
152     test_create(&gameStatisticsAvailable);
153
154     if(gameStatisticsAvailable)
155     {
156         hr = _registerGame();
157         ok( hr == S_OK, "cannot register game in Game Explorer (error: 0x%x)\n", hr);
158
159         test_gamestatisticsmgr();
160
161         hr = _unregisterGame();
162         ok( hr == S_OK, "cannot unregister game from Game Explorer (error: 0x%x)\n", hr);
163     }
164
165     CoUninitialize();
166 }