78c1979bffb4dd705325cd1a17b455656e12ae9c
[reactos.git] / modules / rostests / winetests / propsys / propstore.c
1 /*
2 * Unit tests for IPropertyStore and related interfaces
3 *
4 * Copyright 2012 Vincent Povirk
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 WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #define COBJMACROS
26
27 #include <stdarg.h>
28 //#include <stdio.h>
29
30 #define NONAMELESSUNION
31
32 #include <windef.h>
33 #include <winbase.h>
34 #include <objbase.h>
35 #include <propsys.h>
36 #include <wine/test.h>
37
38 #include <initguid.h>
39
40 DEFINE_GUID(PKEY_WineTest, 0x7b317433, 0xdfa3, 0x4c44, 0xad, 0x3e, 0x2f, 0x80, 0x4b, 0x90, 0xdb, 0xf4);
41
42 static void test_inmemorystore(void)
43 {
44 IPropertyStoreCache *propcache;
45 HRESULT hr;
46 PROPERTYKEY pkey;
47 PROPVARIANT propvar;
48 DWORD count;
49 PSC_STATE state;
50
51 hr = CoCreateInstance(&CLSID_InMemoryPropertyStore, NULL, CLSCTX_INPROC_SERVER,
52 &IID_IPropertyStoreCache, (void**)&propcache);
53 ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
54
55 if (FAILED(hr))
56 {
57 skip("CLSID_InMemoryPropertyStore not supported\n");
58 return;
59 }
60
61 hr = IPropertyStoreCache_GetCount(propcache, NULL);
62 ok(hr == E_POINTER, "GetCount failed, hr=%x\n", hr);
63
64 hr = IPropertyStoreCache_GetCount(propcache, &count);
65 ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
66 ok(count == 0, "GetCount returned %i, expected 0\n", count);
67
68 hr = IPropertyStoreCache_Commit(propcache);
69 ok(hr == S_OK, "Commit failed, hr=%x\n", hr);
70
71 hr = IPropertyStoreCache_Commit(propcache);
72 ok(hr == S_OK, "Commit failed, hr=%x\n", hr);
73
74 hr = IPropertyStoreCache_GetAt(propcache, 0, &pkey);
75 ok(hr == E_INVALIDARG, "GetAt failed, hr=%x\n", hr);
76
77 pkey.fmtid = PKEY_WineTest;
78 pkey.pid = 4;
79
80 memset(&propvar, 0, sizeof(propvar));
81 propvar.vt = VT_I4;
82 propvar.u.lVal = 12345;
83
84 if (0)
85 {
86 /* Crashes on Windows 7 */
87 hr = IPropertyStoreCache_SetValue(propcache, NULL, &propvar);
88 ok(hr == E_POINTER, "SetValue failed, hr=%x\n", hr);
89
90 hr = IPropertyStoreCache_SetValue(propcache, &pkey, NULL);
91 ok(hr == E_POINTER, "SetValue failed, hr=%x\n", hr);
92 }
93
94 hr = IPropertyStoreCache_SetValue(propcache, &pkey, &propvar);
95 ok(hr == S_OK, "SetValue failed, hr=%x\n", hr);
96
97 hr = IPropertyStoreCache_GetCount(propcache, &count);
98 ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
99 ok(count == 1, "GetCount returned %i, expected 0\n", count);
100
101 memset(&pkey, 0, sizeof(pkey));
102
103 hr = IPropertyStoreCache_GetAt(propcache, 0, &pkey);
104 ok(hr == S_OK, "GetAt failed, hr=%x\n", hr);
105 ok(IsEqualGUID(&pkey.fmtid, &PKEY_WineTest), "got wrong pkey\n");
106 ok(pkey.pid == 4, "got pid of %i, expected 4\n", pkey.pid);
107
108 pkey.fmtid = PKEY_WineTest;
109 pkey.pid = 4;
110
111 memset(&propvar, 0, sizeof(propvar));
112
113 if (0)
114 {
115 /* Crashes on Windows 7 */
116 hr = IPropertyStoreCache_GetValue(propcache, NULL, &propvar);
117 ok(hr == E_POINTER, "GetValue failed, hr=%x\n", hr);
118 }
119
120 hr = IPropertyStoreCache_GetValue(propcache, &pkey, NULL);
121 ok(hr == E_POINTER, "GetValue failed, hr=%x\n", hr);
122
123 hr = IPropertyStoreCache_GetValue(propcache, &pkey, &propvar);
124 ok(hr == S_OK, "GetValue failed, hr=%x\n", hr);
125 ok(propvar.vt == VT_I4, "expected VT_I4, got %d\n", propvar.vt);
126 ok(propvar.u.lVal == 12345, "expected 12345, got %d\n", propvar.u.lVal);
127
128 pkey.fmtid = PKEY_WineTest;
129 pkey.pid = 10;
130
131 /* Get information for field that isn't set yet */
132 propvar.vt = VT_I2;
133 hr = IPropertyStoreCache_GetValue(propcache, &pkey, &propvar);
134 ok(hr == S_OK, "GetValue failed, hr=%x\n", hr);
135 ok(propvar.vt == VT_EMPTY, "expected VT_EMPTY, got %d\n", propvar.vt);
136
137 state = 0xdeadbeef;
138 hr = IPropertyStoreCache_GetState(propcache, &pkey, &state);
139 ok(hr == TYPE_E_ELEMENTNOTFOUND, "GetState failed, hr=%x\n", hr);
140 ok(state == PSC_NORMAL, "expected PSC_NORMAL, got %d\n", state);
141
142 propvar.vt = VT_I2;
143 state = 0xdeadbeef;
144 hr = IPropertyStoreCache_GetValueAndState(propcache, &pkey, &propvar, &state);
145 ok(hr == TYPE_E_ELEMENTNOTFOUND, "GetValueAndState failed, hr=%x\n", hr);
146 ok(propvar.vt == VT_EMPTY, "expected VT_EMPTY, got %d\n", propvar.vt);
147 ok(state == PSC_NORMAL, "expected PSC_NORMAL, got %d\n", state);
148
149 /* Set state on an unset field */
150 hr = IPropertyStoreCache_SetState(propcache, &pkey, PSC_NORMAL);
151 ok(hr == TYPE_E_ELEMENTNOTFOUND, "SetState failed, hr=%x\n", hr);
152
153 /* Manipulate state on already set field */
154 pkey.fmtid = PKEY_WineTest;
155 pkey.pid = 4;
156
157 state = 0xdeadbeef;
158 hr = IPropertyStoreCache_GetState(propcache, &pkey, &state);
159 ok(hr == S_OK, "GetState failed, hr=%x\n", hr);
160 ok(state == PSC_NORMAL, "expected PSC_NORMAL, got %d\n", state);
161
162 hr = IPropertyStoreCache_SetState(propcache, &pkey, 10);
163 ok(hr == S_OK, "SetState failed, hr=%x\n", hr);
164
165 state = 0xdeadbeef;
166 hr = IPropertyStoreCache_GetState(propcache, &pkey, &state);
167 ok(hr == S_OK, "GetState failed, hr=%x\n", hr);
168 ok(state == 10, "expected 10, got %d\n", state);
169
170 propvar.vt = VT_I4;
171 propvar.u.lVal = 12346;
172 hr = IPropertyStoreCache_SetValueAndState(propcache, &pkey, &propvar, 5);
173 ok(hr == S_OK, "SetValueAndState failed, hr=%x\n", hr);
174
175 memset(&propvar, 0, sizeof(propvar));
176 state = 0xdeadbeef;
177 hr = IPropertyStoreCache_GetValueAndState(propcache, &pkey, &propvar, &state);
178 ok(hr == S_OK, "GetValueAndState failed, hr=%x\n", hr);
179 ok(propvar.vt == VT_I4, "expected VT_I4, got %d\n", propvar.vt);
180 ok(propvar.u.lVal == 12346, "expected 12346, got %d\n", propvar.vt);
181 ok(state == 5, "expected 5, got %d\n", state);
182
183 /* Set new field with state */
184 pkey.fmtid = PKEY_WineTest;
185 pkey.pid = 8;
186
187 propvar.vt = VT_I4;
188 propvar.u.lVal = 12347;
189 hr = IPropertyStoreCache_SetValueAndState(propcache, &pkey, &propvar, PSC_DIRTY);
190 ok(hr == S_OK, "SetValueAndState failed, hr=%x\n", hr);
191
192 memset(&propvar, 0, sizeof(propvar));
193 state = 0xdeadbeef;
194 hr = IPropertyStoreCache_GetValueAndState(propcache, &pkey, &propvar, &state);
195 ok(hr == S_OK, "GetValueAndState failed, hr=%x\n", hr);
196 ok(propvar.vt == VT_I4, "expected VT_I4, got %d\n", propvar.vt);
197 ok(propvar.u.lVal == 12347, "expected 12347, got %d\n", propvar.vt);
198 ok(state == PSC_DIRTY, "expected PSC_DIRTY, got %d\n", state);
199
200 IPropertyStoreCache_Release(propcache);
201 }
202
203 static void test_persistserialized(void)
204 {
205 IPropertyStore *propstore;
206 IPersistSerializedPropStorage *serialized;
207 HRESULT hr;
208 SERIALIZEDPROPSTORAGE *result;
209 DWORD result_size;
210
211 hr = CoCreateInstance(&CLSID_InMemoryPropertyStore, NULL, CLSCTX_INPROC_SERVER,
212 &IID_IPropertyStore, (void**)&propstore);
213 ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
214
215 hr = IPropertyStore_QueryInterface(propstore, &IID_IPersistSerializedPropStorage,
216 (void**)&serialized);
217 todo_wine ok(hr == S_OK, "QueryInterface failed, hr=%x\n", hr);
218
219 if (FAILED(hr))
220 {
221 skip("IPersistSerializedPropStorage not supported\n");
222 return;
223 }
224
225 hr = IPersistSerializedPropStorage_GetPropertyStorage(serialized, NULL, &result_size);
226 ok(hr == E_POINTER, "GetPropertyStorage failed, hr=%x\n", hr);
227
228 hr = IPersistSerializedPropStorage_GetPropertyStorage(serialized, &result, NULL);
229 ok(hr == E_POINTER, "GetPropertyStorage failed, hr=%x\n", hr);
230
231 hr = IPersistSerializedPropStorage_GetPropertyStorage(serialized, &result, &result_size);
232 ok(hr == S_OK, "GetPropertyStorage failed, hr=%x\n", hr);
233
234 if (SUCCEEDED(hr))
235 {
236 ok(result_size == 0, "expected 0 bytes, got %i\n", result_size);
237
238 CoTaskMemFree(result);
239 }
240
241 hr = IPersistSerializedPropStorage_SetPropertyStorage(serialized, NULL, 4);
242 ok(hr == E_POINTER, "SetPropertyStorage failed, hr=%x\n", hr);
243
244 hr = IPersistSerializedPropStorage_SetPropertyStorage(serialized, NULL, 0);
245 ok(hr == S_OK, "SetPropertyStorage failed, hr=%x\n", hr);
246
247 hr = IPropertyStore_GetCount(propstore, &result_size);
248 ok(hr == S_OK, "GetCount failed, hr=%x\n", hr);
249 ok(result_size == 0, "expecting 0, got %d\n", result_size);
250
251 IPropertyStore_Release(propstore);
252 IPersistSerializedPropStorage_Release(serialized);
253 }
254
255 START_TEST(propstore)
256 {
257 CoInitialize(NULL);
258
259 test_inmemorystore();
260 test_persistserialized();
261
262 CoUninitialize();
263 }