urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / msxml3 / tests / schema.c
1 /*
2  * Schema test
3  *
4  * Copyright 2007 Huw Davies
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 "windows.h"
24 #include "ole2.h"
25 #include "xmldom.h"
26 #include "msxml2.h"
27 #include <stdio.h>
28
29 #include "wine/test.h"
30
31 const CLSID CLSID_XMLSchemaCache      = {0x373984c9, 0xb845, 0x449b, {0x91, 0xe7, 0x45, 0xac, 0x83, 0x03, 0x6a, 0xde}};
32 const IID IID_IXMLDOMSchemaCollection = {0x373984c8, 0xb845, 0x449b, {0x91, 0xe7, 0x45, 0xac, 0x83, 0x03, 0x6a, 0xde}};
33
34 static const WCHAR schema_uri[] = {'x','-','s','c','h','e','m','a',':','t','e','s','t','.','x','m','l',0};
35
36 static const WCHAR schema_xml[] = {
37     '<','S','c','h','e','m','a',' ','x','m','l','n','s','=','\"','u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','x','m','l','-','d','a','t','a','\"','\n',
38     'x','m','l','n','s',':','d','t','=','\"','u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','d','a','t','a','t','y','p','e','s','\"','>','\n',
39     '<','/','S','c','h','e','m','a','>','\n',0
40 };
41
42 static void test_schema_refs(void)
43 {
44     IXMLDOMDocument2 *doc;
45     IXMLDOMSchemaCollection *schema;
46     HRESULT r;
47     LONG ref;
48     VARIANT v;
49     VARIANT_BOOL b;
50     BSTR str;
51
52     r = CoCreateInstance( &CLSID_DOMDocument, NULL,
53         CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument2, (LPVOID*)&doc );
54     if( r != S_OK )
55         return;
56
57     r = CoCreateInstance( &CLSID_XMLSchemaCache, NULL,
58         CLSCTX_INPROC_SERVER, &IID_IXMLDOMSchemaCollection, (LPVOID*)&schema );
59     if( r != S_OK )
60     {
61         IXMLDOMDocument2_Release(doc);
62         return;
63     }
64
65     str = SysAllocString(schema_xml);
66     r = IXMLDOMDocument2_loadXML(doc, str, &b);
67     ok(r == S_OK, "ret %08x\n", r);
68     ok(b == VARIANT_TRUE, "b %04x\n", b);
69     SysFreeString(str);
70
71     ref = IXMLDOMDocument2_AddRef(doc);
72     ok(ref == 2, "ref %d\n", ref);
73     VariantInit(&v);
74     V_VT(&v) = VT_DISPATCH;
75     V_DISPATCH(&v) = (IDispatch*)doc;
76
77     str = SysAllocString(schema_uri);
78     r = IXMLDOMSchemaCollection_add(schema, str, v);
79     ok(r == S_OK, "ret %08x\n", r);
80
81     /* IXMLDOMSchemaCollection_add doesn't add a ref on doc */
82     ref = IXMLDOMDocument2_AddRef(doc);
83     ok(ref == 3, "ref %d\n", ref);
84     IXMLDOMDocument2_Release(doc);
85
86     SysFreeString(str);
87     VariantClear(&v);
88
89     V_VT(&v) = VT_INT;
90     r = IXMLDOMDocument2_get_schemas(doc, &v);
91     ok(r == S_FALSE, "ret %08x\n", r);
92     ok(V_VT(&v) == VT_NULL, "vt %x\n", V_VT(&v));
93
94     ref = IXMLDOMSchemaCollection_AddRef(schema);
95     ok(ref == 2, "ref %d\n", ref);
96     V_VT(&v) = VT_DISPATCH;
97     V_DISPATCH(&v) = (IDispatch*)schema;
98
99     /* check that putref_schemas takes a ref */
100     r = IXMLDOMDocument2_putref_schemas(doc, v);
101     ok(r == S_OK, "ret %08x\n", r);
102     ref = IXMLDOMSchemaCollection_AddRef(schema);
103     ok(ref == 4, "ref %d\n", ref);
104     IXMLDOMSchemaCollection_Release(schema);
105     VariantClear(&v);
106
107     /* refs now 2 */
108     V_VT(&v) = VT_INT;
109     /* check that get_schemas adds a ref */
110     r = IXMLDOMDocument2_get_schemas(doc, &v);
111     ok(r == S_OK, "ret %08x\n", r);
112     ok(V_VT(&v) == VT_DISPATCH, "vt %x\n", V_VT(&v));
113     ref = IXMLDOMSchemaCollection_AddRef(schema);
114     ok(ref == 4, "ref %d\n", ref);
115     IXMLDOMSchemaCollection_Release(schema);
116
117     /* refs now 3 */
118     /* get_schemas doesn't release a ref if passed VT_DISPATCH - ie it doesn't call VariantClear() */
119     r = IXMLDOMDocument2_get_schemas(doc, &v);
120     ok(r == S_OK, "ret %08x\n", r);
121     ok(V_VT(&v) == VT_DISPATCH, "vt %x\n", V_VT(&v));
122     ref = IXMLDOMSchemaCollection_AddRef(schema);
123     ok(ref == 5, "ref %d\n", ref);
124     IXMLDOMSchemaCollection_Release(schema);
125
126     /* refs now 4 */
127     /* release the two refs returned by get_schemas */
128     IXMLDOMSchemaCollection_Release(schema);
129     IXMLDOMSchemaCollection_Release(schema);
130
131     /* refs now 2 */
132
133     /* check that taking another ref on the document doesn't change the schema's ref count */
134     IXMLDOMDocument2_AddRef(doc);
135     ref = IXMLDOMSchemaCollection_AddRef(schema);
136     ok(ref == 3, "ref %d\n", ref);
137     IXMLDOMSchemaCollection_Release(schema);
138     IXMLDOMDocument2_Release(doc);
139
140
141     /* refs now 2 */
142     /* call putref_schema with some odd variants */
143     V_VT(&v) = VT_INT;
144     r = IXMLDOMDocument2_putref_schemas(doc, v);
145     ok(r == E_FAIL, "ret %08x\n", r);
146     ref = IXMLDOMSchemaCollection_AddRef(schema);
147     ok(ref == 3, "ref %d\n", ref);
148     IXMLDOMSchemaCollection_Release(schema);
149
150     /* refs now 2 */
151     /* calling with VT_EMPTY releases the schema */
152     V_VT(&v) = VT_EMPTY;
153     r = IXMLDOMDocument2_putref_schemas(doc, v);
154     ok(r == S_OK, "ret %08x\n", r);
155     ref = IXMLDOMSchemaCollection_AddRef(schema);
156     ok(ref == 2, "ref %d\n", ref);
157     IXMLDOMSchemaCollection_Release(schema);
158
159     /* refs now 1 */
160     /* try setting with VT_UNKNOWN */
161     IXMLDOMSchemaCollection_AddRef(schema);
162     V_VT(&v) = VT_UNKNOWN;
163     V_UNKNOWN(&v) = (IUnknown*)schema;
164     r = IXMLDOMDocument2_putref_schemas(doc, v);
165     ok(r == S_OK, "ret %08x\n", r);
166     ref = IXMLDOMSchemaCollection_AddRef(schema);
167     ok(ref == 4, "ref %d\n", ref);
168     IXMLDOMSchemaCollection_Release(schema);
169     VariantClear(&v);
170
171     /* refs now 2 */
172     /* calling with VT_NULL releases the schema */
173     V_VT(&v) = VT_NULL;
174     r = IXMLDOMDocument2_putref_schemas(doc, v);
175     ok(r == S_OK, "ret %08x\n", r);
176     ref = IXMLDOMSchemaCollection_AddRef(schema);
177     ok(ref == 2, "ref %d\n", ref);
178     IXMLDOMSchemaCollection_Release(schema);
179
180     /* refs now 1 */
181     /* set again */
182     IXMLDOMSchemaCollection_AddRef(schema);
183     V_VT(&v) = VT_UNKNOWN;
184     V_UNKNOWN(&v) = (IUnknown*)schema;
185     r = IXMLDOMDocument2_putref_schemas(doc, v);
186     ok(r == S_OK, "ret %08x\n", r);
187     ref = IXMLDOMSchemaCollection_AddRef(schema);
188     ok(ref == 4, "ref %d\n", ref);
189     IXMLDOMSchemaCollection_Release(schema);
190     VariantClear(&v);
191
192     /* refs now 2 */
193
194     /* release the final ref on the doc which should release its ref on the schema */
195     IXMLDOMDocument2_Release(doc);
196
197     ref = IXMLDOMSchemaCollection_AddRef(schema);
198     ok(ref == 2, "ref %d\n", ref);
199     IXMLDOMSchemaCollection_Release(schema);
200     IXMLDOMSchemaCollection_Release(schema);
201 }
202
203 START_TEST(schema)
204 {
205     HRESULT r;
206
207     r = CoInitialize( NULL );
208     ok( r == S_OK, "failed to init com\n");
209
210     test_schema_refs();
211
212     CoUninitialize();
213 }