1 /*
2  * Copyright 2009 Andrew Eikum for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #define COBJMACROS
20 
21 #include <stdio.h>
22 
23 #include <hlink.h>
24 
25 #include "wine/test.h"
26 
27 static void test_SetInitialHlink(void)
28 {
29     IHlinkBrowseContext *bc;
30     IHlink *found_hlink;
31     IMoniker *dummy, *found_moniker;
32     IBindCtx *bindctx;
33     WCHAR one[] = {'1',0};
34     WCHAR five[] = {'5',0};
35     WCHAR *found_name, *exp_name;
36     HRESULT hres;
37 
38     hres = CreateBindCtx(0, &bindctx);
39     ok(hres == S_OK, "CreateBindCtx failed: 0x%08x\n", hres);
40 
41     hres = CreateItemMoniker(one, five, &dummy);
42     ok(hres == S_OK, "CreateItemMoniker failed: 0x%08x\n", hres);
43 
44     hres = IMoniker_GetDisplayName(dummy, bindctx, NULL, &exp_name);
45     ok(hres == S_OK, "GetDisplayName failed: 0x%08x\n", hres);
46 
47     hres = HlinkCreateBrowseContext(NULL, &IID_IHlinkBrowseContext, (void**)&bc);
48     ok(hres == S_OK, "HlinkCreateBrowseContext failed: 0x%08x\n", hres);
49 
50     hres = IHlinkBrowseContext_SetInitialHlink(bc, dummy, one, NULL);
51     ok(hres == S_OK, "SetInitialHlink failed: 0x%08x\n", hres);
52 
53     hres = IHlinkBrowseContext_SetInitialHlink(bc, dummy, one, NULL);
54     ok(hres == CO_E_ALREADYINITIALIZED, "got 0x%08x\n", hres);
55 
56     hres = IHlinkBrowseContext_SetInitialHlink(bc, dummy, five, NULL);
57     ok(hres == CO_E_ALREADYINITIALIZED, "got 0x%08x\n", hres);
58 
59     /* there's only one */
60     hres = IHlinkBrowseContext_GetHlink(bc, HLID_PREVIOUS, &found_hlink);
61     ok(hres == E_FAIL, "got 0x%08x\n", hres);
62 
63     hres = IHlinkBrowseContext_GetHlink(bc, HLID_NEXT, &found_hlink);
64     ok(hres == E_FAIL, "got 0x%08x\n", hres);
65 
66     hres = IHlinkBrowseContext_GetHlink(bc, HLID_CURRENT, &found_hlink);
67     ok(hres == S_OK, "GetHlink failed: 0x%08x\n", hres);
68 
69     hres = IHlink_GetMonikerReference(found_hlink, HLINKGETREF_DEFAULT, &found_moniker, NULL);
70     ok(hres == S_OK, "GetMonikerReference failed: 0x%08x\n", hres);
71 
72     hres = IMoniker_GetDisplayName(found_moniker, bindctx, NULL, &found_name);
73     ok(hres == S_OK, "GetDisplayName failed: 0x%08x\n", hres);
74     ok(!lstrcmpW(found_name, exp_name), "Found display name should have been %s, was: %s\n", wine_dbgstr_w(exp_name), wine_dbgstr_w(found_name));
75 
76     CoTaskMemFree(exp_name);
77     CoTaskMemFree(found_name);
78 
79     IBindCtx_Release(bindctx);
80     IMoniker_Release(found_moniker);
81     IHlink_Release(found_hlink);
82     IHlinkBrowseContext_Release(bc);
83     IMoniker_Release(dummy);
84 }
85 
86 static void test_BrowseWindowInfo(void)
87 {
88     IHlinkBrowseContext *bc;
89     HLBWINFO bwinfo_set, bwinfo_get;
90     HRESULT hres;
91 
92     hres = HlinkCreateBrowseContext(NULL, &IID_IHlinkBrowseContext, (void**)&bc);
93     ok(hres == S_OK, "HlinkCreateBrowseContext failed: 0x%08x\n", hres);
94 
95     hres = IHlinkBrowseContext_GetBrowseWindowInfo(bc, NULL);
96     ok(hres == E_INVALIDARG, "GetBrowseWindow failed with wrong code: 0x%08x\n", hres);
97 
98     hres = IHlinkBrowseContext_SetBrowseWindowInfo(bc, NULL);
99     ok(hres == E_INVALIDARG, "SetBrowseWindow failed with wrong code: 0x%08x\n", hres);
100 
101     memset(&bwinfo_get, -1, sizeof(HLBWINFO));
102 
103     hres = IHlinkBrowseContext_GetBrowseWindowInfo(bc, &bwinfo_get);
104     ok(hres == S_OK, "GetBrowseWindowInfo failed: 0x%08x\n", hres);
105     ok(bwinfo_get.cbSize == 0, "Got wrong size: %x\n", bwinfo_get.cbSize);
106 
107     bwinfo_set.cbSize = sizeof(HLBWINFO);
108     bwinfo_set.grfHLBWIF = HLBWIF_WEBTOOLBARHIDDEN;
109     bwinfo_set.rcFramePos.left = 1;
110     bwinfo_set.rcFramePos.right = 2;
111     bwinfo_set.rcFramePos.top = 3;
112     bwinfo_set.rcFramePos.bottom = 4;
113     bwinfo_set.rcDocPos.left = 5;
114     bwinfo_set.rcDocPos.right = 6;
115     bwinfo_set.rcDocPos.top = 7;
116     bwinfo_set.rcDocPos.bottom = 8;
117     bwinfo_set.hltbinfo.uDockType = 4321;
118     bwinfo_set.hltbinfo.rcTbPos.left = 9;
119     bwinfo_set.hltbinfo.rcTbPos.right = 10;
120     bwinfo_set.hltbinfo.rcTbPos.top = 11;
121     bwinfo_set.hltbinfo.rcTbPos.bottom = 12;
122     hres = IHlinkBrowseContext_SetBrowseWindowInfo(bc, &bwinfo_set);
123     ok(hres == S_OK, "SetBrowseWindowInfo failed: 0x%08x\n", hres);
124 
125     memset(&bwinfo_get, 0, sizeof(HLBWINFO));
126 
127     hres = IHlinkBrowseContext_GetBrowseWindowInfo(bc, &bwinfo_get);
128     ok(hres == S_OK, "GetBrowseWindowInfo failed: 0x%08x\n", hres);
129     ok(!memcmp(&bwinfo_set, &bwinfo_get, sizeof(HLBWINFO)), "Set and Get differ\n");
130 
131     IHlinkBrowseContext_Release(bc);
132 }
133 
134 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
135 {
136     *ppv = NULL;
137 
138     if (IsEqualIID(riid, &IID_IUnknown))
139     {
140         *ppv = iface;
141         return S_OK;
142     }
143     return E_NOINTERFACE;
144 }
145 
146 static ULONG WINAPI Unknown_AddRef(IUnknown *iface)
147 {
148     return 2;
149 }
150 
151 static ULONG WINAPI Unknown_Release(IUnknown *iface)
152 {
153     return 1;
154 }
155 
156 static IUnknownVtbl UnknownVtbl = {
157     Unknown_QueryInterface,
158     Unknown_AddRef,
159     Unknown_Release,
160 };
161 
162 static IUnknown Unknown = { &UnknownVtbl };
163 
164 static void test_GetObject(void)
165 {
166     IHlinkBrowseContext *bc;
167     IMoniker *dummy;
168     IBindCtx *bindctx;
169     IUnknown *unk;
170     WCHAR one[] = {'1',0};
171     WCHAR five[] = {'5',0};
172     DWORD cookie;
173     HRESULT hres;
174 
175     hres = CreateBindCtx(0, &bindctx);
176     ok(hres == S_OK, "CreateBindCtx() failed: 0x%08x\n", hres);
177 
178     hres = CreateItemMoniker(one, five, &dummy);
179     ok(hres == S_OK, "CreateItemMoniker() failed: 0x%08x\n", hres);
180 
181     hres = HlinkCreateBrowseContext(NULL, &IID_IHlinkBrowseContext, (void **)&bc);
182     ok(hres == S_OK, "HlinkCreateBrowseContext() failed: 0x%08x\n", hres);
183 
184     hres = IHlinkBrowseContext_GetObject(bc, dummy, FALSE, &unk);
185     ok(hres == MK_E_UNAVAILABLE, "expected MK_E_UNAVAILABLE, got 0x%08x\n", hres);
186 
187     hres = IHlinkBrowseContext_Register(bc, 0, &Unknown, dummy, &cookie);
188     ok(hres == S_OK, "Register() failed: 0x%08x\n", hres);
189 
190     hres = IHlinkBrowseContext_GetObject(bc, dummy, FALSE, &unk);
191     ok(hres == S_OK, "GetObject() failed: 0x%08x\n", hres);
192     ok(unk == &Unknown, "wrong object returned\n");
193 
194     hres = IHlinkBrowseContext_Revoke(bc, cookie);
195     ok(hres == S_OK, "Revoke() failed: 0x%08x\n", hres);
196 
197     hres = IHlinkBrowseContext_GetObject(bc, dummy, FALSE, &unk);
198     ok(hres == MK_E_UNAVAILABLE, "expected MK_E_UNAVAILABLE, got 0x%08x\n", hres);
199 
200     IHlinkBrowseContext_Release(bc);
201 }
202 
203 START_TEST(browse_ctx)
204 {
205     CoInitialize(NULL);
206 
207     test_SetInitialHlink();
208     test_BrowseWindowInfo();
209     test_GetObject();
210 
211     CoUninitialize();
212 }
213