Don't open device if already open.
[wine] / dlls / shell32 / tests / shlfolder.c
1 /*
2  * Unit test of the IShellFolder functions.
3  *
4  * Copyright 2004 Vitaliy Margolen
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wtypes.h"
29 #include "shellapi.h"
30
31
32 #include "shlguid.h"
33 #include "shlobj.h"
34 #include "shobjidl.h"
35
36
37 #include "wine/unicode.h"
38 #include "wine/test.h"
39
40
41 IMalloc *ppM;
42
43 /* creates a file with the specified name for tests */
44 void CreateTestFile(const CHAR *name)
45 {
46     HANDLE file;
47     DWORD written;
48
49     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
50     if (file != INVALID_HANDLE_VALUE)
51     {
52         WriteFile(file, name, strlen(name), &written, NULL);
53         WriteFile(file, "\n", strlen("\n"), &written, NULL);
54         CloseHandle(file);
55     }
56 }
57
58
59 /* initializes the tests */
60 void CreateFilesFolders(void)
61 {
62     CreateDirectoryA(".\\testdir", NULL);
63     CreateDirectoryA(".\\testdir\\test.txt", NULL);
64     CreateTestFile  (".\\testdir\\test1.txt ");
65     CreateTestFile  (".\\testdir\\test2.txt ");
66     CreateTestFile  (".\\testdir\\test3.txt ");
67     CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
68     CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
69 }
70
71 /* cleans after tests */
72 void Cleanup(void)
73 {
74     DeleteFileA(".\\testdir\\test1.txt");
75     DeleteFileA(".\\testdir\\test2.txt");
76     DeleteFileA(".\\testdir\\test3.txt");
77     RemoveDirectoryA(".\\testdir\\test.txt");
78     RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
79     RemoveDirectoryA(".\\testdir\\testdir2");
80     RemoveDirectoryA(".\\testdir");
81 }
82
83
84 /* perform test */
85 void test_EnumObjects(IShellFolder *iFolder)
86 {
87     IEnumIDList *iEnumList;
88     ITEMIDLIST *newPIDL, *(idlArr [5]);
89     ULONG NumPIDLs;
90     int i=0, j;
91     HRESULT hr;
92
93     static const WORD iResults [5][5] =
94     {
95         { 0,-1,-1,-1,-1},
96         { 1, 0,-1,-1,-1},
97         { 1, 1, 0,-1,-1},
98         { 1, 1, 1, 0,-1},
99         { 1, 1, 1, 1, 0}
100     };
101
102     /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
103     static const ULONG attrs[5] =
104     {
105         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
106         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
107         SFGAO_FILESYSTEM,
108         SFGAO_FILESYSTEM,
109         SFGAO_FILESYSTEM,
110     };
111
112     hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
113     ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
114
115     while (IEnumIDList_Next(iEnumList, 1, &newPIDL, &NumPIDLs) == S_OK)
116     {
117         idlArr[i++] = newPIDL;
118     }
119
120     hr = IEnumIDList_Release(iEnumList);
121     ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
122     
123     /* Sort them first in case of wrong order from system */
124     for (i=0;i<5;i++) for (j=0;j<5;j++)
125         if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
126         {
127             newPIDL = idlArr[i];
128             idlArr[i] = idlArr[j];
129             idlArr[j] = newPIDL;
130         }
131             
132     for (i=0;i<5;i++) for (j=0;j<5;j++)
133     {
134         hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
135         ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
136     }
137
138
139     for (i = 0; i < 5; i++)
140     {
141         SFGAOF flags;
142         flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
143         hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
144         flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
145         ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
146         ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
147     }
148
149     for (i=0;i<5;i++)
150         IMalloc_Free(ppM, idlArr[i]);
151 }
152
153     
154 START_TEST(shlfolder)
155 {
156     ITEMIDLIST *newPIDL;
157     IShellFolder *IDesktopFolder, *testIShellFolder;
158     WCHAR cCurrDirW [MAX_PATH];
159     static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
160     HRESULT hr;
161     
162     GetCurrentDirectoryW(MAX_PATH, cCurrDirW);
163     strcatW(cCurrDirW, cTestDirW);
164
165     OleInitialize(NULL);
166
167     hr = SHGetMalloc(&ppM);
168     ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
169
170     CreateFilesFolders();
171     
172     hr = SHGetDesktopFolder(&IDesktopFolder);
173     ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
174
175     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
176     ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
177
178     hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
179     ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
180         
181     test_EnumObjects(testIShellFolder);
182
183     hr = IShellFolder_Release(testIShellFolder);
184     ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
185
186     IMalloc_Free(ppM, newPIDL);
187
188     Cleanup();
189 }