2270d4ab217d2cd4d64329b5711af6b9f65fb680
[reactos.git] / rostests / apitests / shell32 / CShellLink.cpp
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for CShellLink
5 * PROGRAMMER: Andreas Maier
6 */
7
8 #include "shelltest.h"
9 #include <atlbase.h>
10 #include <atlcom.h>
11 #include <strsafe.h>
12 #include <ndk/rtlfuncs.h>
13
14 #define NDEBUG
15 #include <debug.h>
16 #include <shellutils.h>
17
18 typedef
19 struct _TestShellLinkDef
20 {
21 const WCHAR* pathIn;
22 HRESULT hrSetPath;
23 /* Test 1 - hrGetPathX = IShellLink::GetPath(pathOutX, ... , flagsX); */
24 const WCHAR* pathOut1;
25 DWORD flags1;
26 HRESULT hrGetPath1;
27 bool expandPathOut1;
28 /* Test 2 */
29 const WCHAR* pathOut2;
30 DWORD flags2;
31 HRESULT hrGetPath2;
32 bool expandPathOut2;
33 } TestShellLinkDef;
34
35 /* Test IShellLink::SetPath with environment-variables, existing, non-existing, ...*/
36 static struct _TestShellLinkDef linkTestList[] =
37 {
38 {
39 L"%comspec%", S_OK,
40 L"%comspec%", SLGP_SHORTPATH, S_OK, TRUE,
41 L"%comspec%", SLGP_RAWPATH, S_OK, FALSE
42 },
43 {
44 L"%anyvar%", E_INVALIDARG,
45 L"", SLGP_SHORTPATH, ERROR_INVALID_FUNCTION, FALSE,
46 L"", SLGP_RAWPATH, ERROR_INVALID_FUNCTION, FALSE
47 },
48 {
49 L"%anyvar%%comspec%", S_OK,
50 L"c:\\%anyvar%%comspec%", SLGP_SHORTPATH, S_OK, TRUE,
51 L"%anyvar%%comspec%", SLGP_RAWPATH, S_OK, FALSE
52 },
53 {
54 L"%temp%", S_OK,
55 L"%temp%", SLGP_SHORTPATH, S_OK, TRUE,
56 L"%temp%", SLGP_RAWPATH, S_OK, FALSE
57 },
58 {
59 L"%shell%", S_OK,
60 L"%systemroot%\\system32\\%shell%", SLGP_SHORTPATH, S_OK, TRUE,
61 L"%shell%", SLGP_RAWPATH, S_OK, FALSE
62 },
63 {
64 L"u:\\anypath\\%anyvar%", S_OK,
65 L"u:\\anypath\\%anyvar%", SLGP_SHORTPATH, S_OK, TRUE,
66 L"u:\\anypath\\%anyvar%", SLGP_RAWPATH, S_OK, FALSE
67 },
68 {
69 L"c:\\temp", S_OK,
70 L"c:\\temp", SLGP_SHORTPATH, S_OK, FALSE,
71 L"c:\\temp", SLGP_RAWPATH, S_OK, FALSE
72 },
73 {
74 L"cmd.exe", S_OK,
75 L"%comspec%", SLGP_SHORTPATH, S_OK, TRUE,
76 L"%comspec%", SLGP_RAWPATH, S_OK, TRUE
77 },
78 {
79 L"c:\\non-existent-path\\non-existent-file", S_OK,
80 L"c:\\non-existent-path\\non-existent-file", SLGP_SHORTPATH, S_OK, FALSE,
81 L"c:\\non-existent-path\\non-existent-file", SLGP_RAWPATH, S_OK, FALSE
82 },
83 {
84 L"non-existent-file", E_INVALIDARG,
85 L"", SLGP_SHORTPATH, ERROR_INVALID_FUNCTION, FALSE,
86 L"", SLGP_RAWPATH, ERROR_INVALID_FUNCTION, FALSE
87 },
88 };
89
90 static
91 VOID
92 test_checklinkpath(UINT i, TestShellLinkDef* testDef)
93 {
94 static WCHAR evVar[MAX_PATH];
95
96 HRESULT hr, expectedHr;
97 WCHAR wPathOut[MAX_PATH];
98 bool expandPathOut;
99 const WCHAR* expectedPathOut;
100 IShellLinkW *psl;
101 UINT i1;
102 DWORD flags;
103
104 hr = CoCreateInstance(CLSID_ShellLink,
105 NULL,
106 CLSCTX_INPROC_SERVER,
107 IID_PPV_ARG(IShellLinkW, &psl));
108 ok(hr == S_OK, "CoCreateInstance, hr = %lx\n", hr);
109 if (FAILED(hr))
110 {
111 skip("Could not instantiate CShellLink\n");
112 return;
113 }
114
115 hr = psl->SetPath(testDef->pathIn);
116 ok(hr == testDef->hrSetPath, "IShellLink::SetPath(%d), got hr = %lx, expected %lx\n", i, hr, testDef->hrSetPath);
117
118 expectedPathOut = NULL;
119 for (i1 = 0; i1 <= 1; i1++ )
120 {
121 if (i1 == 1)
122 {
123 flags = testDef->flags1;
124 expandPathOut = testDef->expandPathOut1;
125 expectedPathOut = testDef->pathOut1;
126 expectedHr = testDef->hrGetPath1;
127 }
128 else
129 {
130 flags = testDef->flags2;
131 expandPathOut = testDef->expandPathOut2;
132 expectedPathOut = testDef->pathOut2;
133 expectedHr = testDef->hrGetPath2;
134 }
135
136 /* Patch some variables */
137 if (expandPathOut == TRUE)
138 {
139 ExpandEnvironmentStringsW(expectedPathOut, evVar, _countof(evVar));
140 DPRINT("** %S **\n",evVar);
141 expectedPathOut = evVar;
142 }
143
144 hr = psl->GetPath(wPathOut, _countof(wPathOut), NULL, flags);
145 ok(hr == expectedHr,
146 "IShellLink::GetPath(%d), flags %lx, got hr = %lx, expected %lx\n",
147 i, flags, hr, expectedHr);
148 ok(wcsicmp(wPathOut, expectedPathOut) == 0,
149 "IShellLink::GetPath(%d), flags %lx, in %S, got %S, expected %S\n",
150 i, flags, testDef->pathIn, wPathOut, expectedPathOut);
151 }
152
153 psl->Release();
154 }
155
156 static
157 VOID
158 TestDescription(void)
159 {
160 HRESULT hr;
161 IShellLinkW *psl;
162 WCHAR buffer[64];
163 PCWSTR testDescription = L"This is a test description";
164
165 /* Test SetDescription */
166 hr = CoCreateInstance(CLSID_ShellLink,
167 NULL,
168 CLSCTX_INPROC_SERVER,
169 IID_PPV_ARG(IShellLinkW, &psl));
170 ok(hr == S_OK, "CoCreateInstance, hr = %lx\n", hr);
171 if (FAILED(hr))
172 {
173 skip("Could not instantiate CShellLink\n");
174 return;
175 }
176
177 memset(buffer, 0x55, sizeof(buffer));
178 hr = psl->GetDescription(buffer, RTL_NUMBER_OF(buffer));
179 ok(hr == S_OK, "IShellLink::GetDescription returned hr = %lx\n", hr);
180 ok(buffer[0] == 0, "buffer[0] = %x\n", buffer[0]);
181 ok(buffer[1] == 0x5555, "buffer[1] = %x\n", buffer[1]);
182
183 hr = psl->SetDescription(testDescription);
184 ok(hr == S_OK, "IShellLink::SetDescription returned hr = %lx\n", hr);
185
186 memset(buffer, 0x55, sizeof(buffer));
187 hr = psl->GetDescription(buffer, RTL_NUMBER_OF(buffer));
188 ok(hr == S_OK, "IShellLink::GetDescription returned hr = %lx\n", hr);
189 ok(buffer[wcslen(testDescription)] == 0, "buffer[n] = %x\n", buffer[wcslen(testDescription)]);
190 ok(buffer[wcslen(testDescription) + 1] == 0x5555, "buffer[n+1] = %x\n", buffer[wcslen(testDescription) + 1]);
191 ok(!wcscmp(buffer, testDescription), "buffer = '%ls'\n", buffer);
192
193 hr = psl->SetDescription(NULL);
194 ok(hr == S_OK, "IShellLink::SetDescription returned hr = %lx\n", hr);
195
196 memset(buffer, 0x55, sizeof(buffer));
197 hr = psl->GetDescription(buffer, RTL_NUMBER_OF(buffer));
198 ok(hr == S_OK, "IShellLink::GetDescription returned hr = %lx\n", hr);
199 ok(buffer[0] == 0, "buffer[0] = %x\n", buffer[0]);
200 ok(buffer[1] == 0x5555, "buffer[1] = %x\n", buffer[1]);
201
202 psl->Release();
203 }
204
205 static
206 VOID
207 TestShellLink(void)
208 {
209 UINT i;
210
211 /* Needed for test */
212 SetEnvironmentVariableW(L"shell", L"cmd.exe");
213
214 for (i = 0; i < _countof(linkTestList); ++i)
215 {
216 DPRINT("IShellLink-Test(%d): %S\n", i, linkTestList[i].pathIn);
217 test_checklinkpath(i, &linkTestList[i]);
218 }
219
220 SetEnvironmentVariableW(L"shell",NULL);
221 }
222
223 START_TEST(CShellLink)
224 {
225 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
226
227 TestShellLink();
228 TestDescription();
229 }