62eefdb6c335af834617554effa8ec49aea4345e
[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 { NULL, 0, NULL, 0, 0, NULL, 0, 0 }
89 };
90
91 static const UINT evVarChLen = 255;
92 static WCHAR evVar[evVarChLen];
93
94 static
95 VOID
96 test_checklinkpath(TestShellLinkDef* testDef)
97 {
98 HRESULT hr, expectedHr;
99 WCHAR wPathOut[255];
100 bool expandPathOut;
101 const WCHAR* expectedPathOut;
102 IShellLinkW *psl;
103 UINT i1;
104 DWORD flags;
105
106 hr = CoCreateInstance(CLSID_ShellLink,
107 NULL,
108 CLSCTX_INPROC_SERVER,
109 IID_PPV_ARG(IShellLinkW, &psl));
110 ok(hr == S_OK, "CoCreateInstance, hr = %lx\n", hr);
111 if (FAILED(hr))
112 {
113 skip("Could not instantiate CShellLink\n");
114 return;
115 }
116
117 hr = psl->SetPath(testDef->pathIn);
118 ok(hr == testDef->hrSetPath, "IShellLink::SetPath, got hr = %lx, expected %lx\n", hr, testDef->hrSetPath);
119
120 expectedPathOut = NULL;
121 for (i1 = 0; i1 <= 1; i1++ )
122 {
123 if (i1 == 1)
124 {
125 flags = testDef->flags1;
126 expandPathOut = testDef->expandPathOut1;
127 expectedPathOut = testDef->pathOut1;
128 expectedHr = testDef->hrGetPath1;
129 }
130 else
131 {
132 flags = testDef->flags2;
133 expandPathOut = testDef->expandPathOut2;
134 expectedPathOut = testDef->pathOut2;
135 expectedHr = testDef->hrGetPath2;
136 }
137
138 /* Patch some variables */
139 if (expandPathOut == TRUE)
140 {
141 ExpandEnvironmentStringsW(expectedPathOut,evVar,evVarChLen);
142 DPRINT("** %S **\n",evVar);
143 expectedPathOut = evVar;
144 }
145
146 hr = psl->GetPath(wPathOut,sizeof(wPathOut),NULL,flags);
147 ok(hr == expectedHr,
148 "IShellLink::GetPath, flags %lx, got hr = %lx, expected %lx\n",
149 flags, hr, expectedHr);
150 ok(wcsicmp(wPathOut,expectedPathOut) == 0,
151 "IShellLink::GetPath, flags %lx, in %S, got %S, expected %S\n",
152 flags,testDef->pathIn,wPathOut,expectedPathOut);
153 }
154
155 psl->Release();
156 }
157
158 static
159 VOID
160 TestShellLink(void)
161 {
162 TestShellLinkDef *testDef;
163 UINT i1 = 0;
164
165 /* needed for test */
166 SetEnvironmentVariableW(L"shell",L"cmd.exe");
167
168 testDef = &linkTestList[i1];
169 while (testDef->pathIn != NULL)
170 {
171
172 DPRINT("IShellLink-Test: %S\n", testDef->pathIn);
173 test_checklinkpath(testDef);
174 i1++;
175 testDef = &linkTestList[i1];
176 }
177
178 SetEnvironmentVariableW(L"shell",NULL);
179 }
180
181 START_TEST(CShellLink)
182 {
183 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
184
185 TestShellLink();
186 }