1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Test for PrivateExtractIcons
5  * PROGRAMMER:      Hermes Belusca-Maito
6  *                  Doug Lyons <douglyons@douglyons.com>
7  */
8 
9 #include "precomp.h"
10 #include <stdio.h>
11 
12 BOOL FileExists(PCWSTR FileName)
13 {
14     DWORD Attribute = GetFileAttributesW(FileName);
15 
16     return (Attribute != INVALID_FILE_ATTRIBUTES &&
17             !(Attribute & FILE_ATTRIBUTE_DIRECTORY));
18 }
19 
20 BOOL ResourceToFile(INT i, PCWSTR FileName)
21 {
22     FILE *fout;
23     HGLOBAL hData;
24     HRSRC hRes;
25     PVOID pResLock;
26     UINT iSize;
27 
28     if (FileExists(FileName))
29     {
30         skip("'%S' already exists. Exiting now\n", FileName);
31         return FALSE;
32     }
33 
34     hRes = FindResourceW(NULL, MAKEINTRESOURCEW(i), MAKEINTRESOURCEW(RT_RCDATA));
35     if (hRes == NULL)
36     {
37         skip("Could not locate resource (%d). Exiting now\n", i);
38         return FALSE;
39     }
40 
41     iSize = SizeofResource(NULL, hRes);
42 
43     hData = LoadResource(NULL, hRes);
44     if (hData == NULL)
45     {
46         skip("Could not load resource (%d). Exiting now\n", i);
47         return FALSE;
48     }
49 
50     // Lock the resource into global memory.
51     pResLock = LockResource(hData);
52     if (pResLock == NULL)
53     {
54         skip("Could not lock resource (%d). Exiting now\n", i);
55         return FALSE;
56     }
57 
58     fout = _wfopen(FileName, L"wb");
59     fwrite(pResLock, iSize, 1, fout);
60     fclose(fout);
61     return TRUE;
62 }
63 
64 static struct
65 {
66     PCWSTR FilePath;
67     UINT cIcons;        // Return value of the first icon group extracted (should be 1 if no error)
68     UINT cTotalIcons;   // Return value of total icon groups in file
69     BOOL bhIconValid;   // Whether or not the returned icon handle is not NULL.
70 } IconTests[] =
71 {
72     /* Executables with just one icon group */
73     {L"notepad.exe", 1, 1, TRUE},
74     {L"%SystemRoot%\\System32\\cmd.exe", 1, 1, TRUE},
75 
76     /* Executable without icon groups */
77     {L"%SystemRoot%\\System32\\autochk.exe", 0, 0, FALSE},
78 
79     /* Existing file (shell32 has 233 icon groups in ReactOS only) */
80     {L"%SystemRoot%\\System32\\shell32.dll", 1, 233, TRUE},
81 
82     /* Non-existing files */
83     {L"%SystemRoot%\\non-existent-file.sdf", 0xFFFFFFFF, 0, FALSE},
84 
85     /* Executable with 18 icon groups */
86     {L"%SystemRoot%\\explorer.exe", 1, 18, TRUE},
87 
88     /* Icon group file containing 6 icons */
89     {L"%temp%\\sysicon.ico", 1, 1, TRUE},
90 
91     /* Icon group file containing one PNG icon and one normal icon */
92     {L"%temp%\\ROS.ico", 1, 1, TRUE},
93 };
94 
95 static struct
96 {
97     PCWSTR FileName;
98     INT ResourceId;
99 } IconFiles[] =
100 {
101     {L"%temp%\\ROS.ico", IDR_ICONS_PNG},
102     {L"%temp%\\sysicon.ico", IDR_ICONS_NORMAL},
103 };
104 
105 START_TEST(PrivateExtractIcons)
106 {
107     HICON ahIcon;
108     UINT i, aIconId, cIcons, cIcoTotal;
109     WCHAR PathBuffer[MAX_PATH];
110 
111     /* Extract icons */
112     for (i = 0; i < _countof(IconFiles); ++i)
113     {
114         ExpandEnvironmentStringsW(IconFiles[i].FileName, PathBuffer, _countof(PathBuffer));
115 
116         if (!ResourceToFile(IconFiles[i].ResourceId, PathBuffer))
117             goto Cleanup;
118     }
119 
120     for (i = 0; i < _countof(IconTests); ++i)
121     {
122         /* Get total number of icon groups in file.
123          * None of the hard numbers in the function matter since we have
124          * two NULLs for the Icon Handle and Count to be set. */
125         cIcoTotal = PrivateExtractIconsW(IconTests[i].FilePath, 0, 16, 16, NULL, NULL, 0, 0);
126         ok((i == 3 ?
127               cIcoTotal > 232 && cIcoTotal < 240 :    /* shell32 case: ROS has 233, W2K2SP2 has 239 icon groups. */
128               cIcoTotal == IconTests[i].cTotalIcons),
129            "PrivateExtractIconsW(%u): "
130            "got %u, expected %u\n", i, cIcoTotal, IconTests[i].cTotalIcons);
131 
132         /* Always test extraction of the FIRST icon (index 0) */
133         ahIcon = (HICON)UlongToHandle(0xdeadbeef);
134         aIconId = 0xdeadbeef;
135         cIcons = PrivateExtractIconsW(IconTests[i].FilePath, 0, 16, 16, &ahIcon, &aIconId, 1, 0);
136         ok(cIcons == IconTests[i].cIcons, "PrivateExtractIconsW(%u): got %u, expected %u\n", i, cIcons, IconTests[i].cIcons);
137         ok(ahIcon != (HICON)UlongToHandle(0xdeadbeef), "PrivateExtractIconsW(%u): icon not set\n", i);
138         ok((IconTests[i].bhIconValid && ahIcon) || (!IconTests[i].bhIconValid && !ahIcon),
139             "PrivateExtractIconsW(%u): icon expected to be %s, but got 0x%p\n",
140             i, IconTests[i].bhIconValid ? "valid" : "not valid", ahIcon);
141         if (cIcons == 0xFFFFFFFF)
142         {
143             ok(aIconId == 0xdeadbeef,
144                "PrivateExtractIconsW(%u): id should not be set to 0x%x\n",
145                i, aIconId);
146         }
147         else
148         {
149             ok(aIconId != 0xdeadbeef, "PrivateExtractIconsW(%u): id not set\n", i);
150         }
151         if (ahIcon && ahIcon != (HICON)UlongToHandle(0xdeadbeef))
152             DestroyIcon(ahIcon);
153     }
154 
155 Cleanup:
156     for (i = 0; i < _countof(IconFiles); ++i)
157     {
158         ExpandEnvironmentStringsW(IconFiles[i].FileName, PathBuffer, _countof(PathBuffer));
159         DeleteFileW(PathBuffer);
160     }
161 }
162