[USER32_WINETEST] Sync with Wine Staging 4.18 except win.c (PR #1980). CORE-16441
[reactos.git] / modules / rostests / winetests / user32 / monitor.c
index 886d63c..26ee28a 100644 (file)
@@ -23,6 +23,8 @@
 #include "winbase.h"
 #include "wingdi.h"
 #include "winuser.h"
+#include "winreg.h"
+#include <stdio.h>
 
 static HMODULE hdll;
 static LONG (WINAPI *pChangeDisplaySettingsExA)(LPCSTR, LPDEVMODEA, HWND, DWORD, LPVOID);
@@ -74,12 +76,154 @@ static BOOL CALLBACK monitor_enum_proc(HMONITOR hmon, HDC hdc, LPRECT lprc,
     return TRUE;
 }
 
+static int adapter_count = 0;
+static int monitor_count = 0;
+
+static void test_enumdisplaydevices_adapter(int index, const DISPLAY_DEVICEA *device, DWORD flags)
+{
+    char video_name[32];
+    char video_value[128];
+    char buffer[128];
+    int number;
+    int vendor_id;
+    int device_id;
+    int subsys_id;
+    int revision_id;
+    size_t length;
+    HKEY hkey;
+    HDC hdc;
+    DWORD size;
+    LSTATUS ls;
+
+    adapter_count++;
+
+    /* DeviceName */
+    ok(sscanf(device->DeviceName, "\\\\.\\DISPLAY%d", &number) == 1, "#%d: wrong DeviceName %s\n", index,
+       device->DeviceName);
+
+    /* DeviceKey */
+    /* win7 is the only OS version where \Device\Video? value in HLKM\HARDWARE\DEVICEMAP\VIDEO are not in order with adapter index. */
+    if (GetVersion() != 0x1db10106 || !strcmp(winetest_platform, "wine"))
+    {
+        sprintf(video_name, "\\Device\\Video%d", index);
+        ls = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\VIDEO", 0, KEY_READ, &hkey);
+        ok(!ls, "#%d: failed to open registry, error: %#x\n", index, ls);
+        if (!ls)
+        {
+            memset(video_value, 0, sizeof(video_value));
+            size = sizeof(video_value);
+            ls = RegQueryValueExA(hkey, video_name, NULL, NULL, (unsigned char *)video_value, &size);
+            ok(!ls, "#%d: failed to get registry value, error: %#x\n", index, ls);
+            RegCloseKey(hkey);
+            ok(!strcmp(video_value, device->DeviceKey), "#%d: wrong DeviceKey: %s\n", index, device->DeviceKey);
+        }
+    }
+    else
+        ok(sscanf(device->DeviceKey, "\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Video\\%[^\\]\\%04d", buffer, &number) == 2,
+           "#%d: wrong DeviceKey %s\n", index, device->DeviceKey);
+
+    /* DeviceString */
+    length = strlen(device->DeviceString);
+    ok(broken(length == 0) || /* XP on Testbot will return an empty string, whereas XP on real machine doesn't. Probably a bug in virtual adapter driver */
+       length > 0, "#%d: expect DeviceString not empty\n", index);
+
+    /* StateFlags */
+    if (index == 0)
+        ok(device->StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE, "#%d: adapter should be primary\n", index);
+    else
+        ok(!(device->StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE), "#%d: adapter should not be primary\n", index);
+
+    if (device->StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
+    {
+        /* Test creating DC */
+        hdc = CreateDCA(device->DeviceName, NULL, NULL, NULL);
+        ok(hdc != NULL, "#%d: failed to CreateDC(\"%s\") err=%d\n", index, device->DeviceName, GetLastError());
+        DeleteDC(hdc);
+    }
+
+    /* DeviceID */
+    /* DeviceID should equal to the first string of HardwareID value data in PCI GPU instance. You can verify this
+     * by changing the data and rerun EnumDisplayDevices. But it's difficult to find corresponding PCI device on
+     * userland. So here we check the expected format instead. */
+    if (flags & EDD_GET_DEVICE_INTERFACE_NAME)
+        ok(strlen(device->DeviceID) == 0 || /* vista+ */
+           sscanf(device->DeviceID, "PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X&REV_%02X",
+                  &vendor_id, &device_id, &subsys_id, &revision_id) == 4, /* XP/2003 ignores EDD_GET_DEVICE_INTERFACE_NAME */
+           "#%d: got %s\n", index, device->DeviceID);
+    else
+    {
+        ok(broken(strlen(device->DeviceID) == 0) || /* XP on Testbot returns an empty string, whereas real machine doesn't */
+           sscanf(device->DeviceID, "PCI\\VEN_%04X&DEV_%04X&SUBSYS_%08X&REV_%02X", &vendor_id, &device_id, &subsys_id,
+                  &revision_id) == 4, "#%d: wrong DeviceID %s\n", index, device->DeviceID);
+    }
+}
+
+static void test_enumdisplaydevices_monitor(int adapter_index, int monitor_index, const char *adapter_name,
+                                            const DISPLAY_DEVICEA *device, DWORD flags)
+{
+    static const char device_id_prefix[] = "MONITOR\\Default_Monitor\\{4d36e96e-e325-11ce-bfc1-08002be10318}\\";
+    static const char device_key_prefix[] = "\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Class"
+                                            "\\{4d36e96e-e325-11ce-bfc1-08002be10318}\\";
+    char monitor_name[32];
+    char buffer[128];
+    int number;
+
+    monitor_count++;
+
+    /* DeviceName */
+    lstrcpyA(monitor_name, adapter_name);
+    sprintf(monitor_name + strlen(monitor_name), "\\Monitor%d", monitor_index);
+    ok(!strcmp(monitor_name, device->DeviceName), "#%d: expect %s, got %s\n", monitor_index, monitor_name, device->DeviceName);
+
+    /* DeviceString */
+    ok(strlen(device->DeviceString) > 0, "#%d: expect DeviceString not empty\n", monitor_index);
+
+    /* StateFlags */
+    if (adapter_index == 0 && monitor_index == 0)
+        ok(device->StateFlags & DISPLAY_DEVICE_ATTACHED, "#%d expect to have a primary monitor attached\n", monitor_index);
+    else
+        ok(device->StateFlags <= (DISPLAY_DEVICE_ATTACHED | DISPLAY_DEVICE_ACTIVE), "#%d wrong state %#x\n", monitor_index,
+           device->StateFlags);
+
+    /* DeviceID */
+    lstrcpynA(buffer, device->DeviceID, sizeof(device_id_prefix));
+    if (flags & EDD_GET_DEVICE_INTERFACE_NAME)
+    {   /* HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\Default_Monitor\4&2abfaa30&0&UID0 GUID_DEVINTERFACE_MONITOR
+         *                                                   ^                ^                     ^
+         * Expect format                  \\?\DISPLAY#Default_Monitor#4&2abfaa30&0&UID0#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7} */
+        ok(strlen(device->DeviceID) == 0 || /* vista ~ win7 */
+            sscanf(device->DeviceID, "\\\\?\\DISPLAY#Default_Monitor#%[^#]#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}", buffer) == 1 || /* win8+ */
+            (!lstrcmpiA(buffer, device_id_prefix) &&
+             sscanf(device->DeviceID + sizeof(device_id_prefix) - 1, "%04d", &number) == 1), /* XP/2003 ignores EDD_GET_DEVICE_INTERFACE_NAME */
+            "#%d: wrong DeviceID : %s\n", monitor_index, device->DeviceID);
+    }
+    else
+    {
+        /* Expect HarewareID value data + Driver value data in HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\Default_Monitor\{Instance} */
+        /* But we don't know which monitor instance this belongs to, so check format instead */
+        ok(!lstrcmpiA(buffer, device_id_prefix), "#%d wrong DeviceID : %s\n", monitor_index, device->DeviceID);
+        ok(sscanf(device->DeviceID + sizeof(device_id_prefix) - 1, "%04d", &number) == 1,
+           "#%d wrong DeviceID : %s\n", monitor_index, device->DeviceID);
+    }
+
+    /* DeviceKey */
+    lstrcpynA(buffer, device->DeviceKey, sizeof(device_key_prefix));
+    ok(!lstrcmpiA(buffer, device_key_prefix), "#%d: wrong DeviceKey : %s\n", monitor_index, device->DeviceKey);
+    ok(sscanf(device->DeviceKey + sizeof(device_key_prefix) - 1, "%04d", &number) == 1,
+       "#%d wrong DeviceKey : %s\n", monitor_index, device->DeviceKey);
+}
+
 static void test_enumdisplaydevices(void)
 {
+    static const DWORD flags[] = {0, EDD_GET_DEVICE_INTERFACE_NAME};
     DISPLAY_DEVICEA dd;
     char primary_device_name[32];
     char primary_monitor_device_name[32];
-    DWORD primary_num = -1, num = 0;
+    char adapter_name[32];
+    int number;
+    int flag_index;
+    int adapter_index;
+    int monitor_index;
     BOOL ret;
 
     if (!pEnumDisplayDevicesA)
@@ -88,50 +232,50 @@ static void test_enumdisplaydevices(void)
         return;
     }
 
+    /* Doesn't accept \\.\DISPLAY */
     dd.cb = sizeof(dd);
-    for (num = 0;; num++)
-    {
-        HDC dc;
-        ret = pEnumDisplayDevicesA(NULL, num, &dd, 0);
-        if(!ret) break;
+    ret = pEnumDisplayDevicesA("\\\\.\\DISPLAY", 0, &dd, 0);
+    ok(!ret, "Expect failure\n");
 
-        if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
-        {
-            strcpy(primary_device_name, dd.DeviceName);
-            primary_num = num;
-        }
-        if(dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
+    /* Enumeration */
+    for (flag_index = 0; flag_index < ARRAY_SIZE(flags); flag_index++)
+        for (adapter_index = 0; pEnumDisplayDevicesA(NULL, adapter_index, &dd, flags[flag_index]); adapter_index++)
         {
-            /* test creating DC */
-            dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
-            ok(dc != NULL, "Failed to CreateDC(\"%s\") err=%d\n", dd.DeviceName, GetLastError());
-            DeleteDC(dc);
+            lstrcpyA(adapter_name, dd.DeviceName);
+
+            if (sscanf(adapter_name, "\\\\.\\DISPLAYV%d", &number) == 1)
+            {
+                skip("Skipping software devices %s:%s\n", adapter_name, dd.DeviceString);
+                continue;
+            }
+
+            test_enumdisplaydevices_adapter(adapter_index, &dd, flags[flag_index]);
+
+            for (monitor_index = 0; pEnumDisplayDevicesA(adapter_name, monitor_index, &dd, flags[flag_index]);
+                 monitor_index++)
+                test_enumdisplaydevices_monitor(adapter_index, monitor_index, adapter_name, &dd, flags[flag_index]);
         }
-    }
 
-    if (primary_num == -1 || !pEnumDisplayMonitors || !pGetMonitorInfoA)
+    ok(adapter_count > 0, "Expect at least one adapter found\n");
+    /* XP on Testbot doesn't report a monitor, whereas XP on real machine does */
+    ok(broken(monitor_count == 0) || monitor_count > 0, "Expect at least one monitor found\n");
+
+    if (!pEnumDisplayMonitors || !pGetMonitorInfoA)
     {
         win_skip("EnumDisplayMonitors or GetMonitorInfoA are not available\n");
         return;
     }
 
+    ret = pEnumDisplayDevicesA(NULL, 0, &dd, 0);
+    ok(ret, "Expect success\n");
+    lstrcpyA(primary_device_name, dd.DeviceName);
+
     primary_monitor_device_name[0] = 0;
     ret = pEnumDisplayMonitors(NULL, NULL, monitor_enum_proc, (LPARAM)primary_monitor_device_name);
     ok(ret, "EnumDisplayMonitors failed\n");
     ok(!strcmp(primary_monitor_device_name, primary_device_name),
        "monitor device name %s, device name %s\n", primary_monitor_device_name,
        primary_device_name);
-
-    dd.cb = sizeof(dd);
-    for (num = 0;; num++)
-    {
-        ret = pEnumDisplayDevicesA(primary_device_name, num, &dd, 0);
-        if (!ret) break;
-
-        dd.DeviceID[63] = 0;
-        ok(!strcasecmp(dd.DeviceID, "Monitor\\Default_Monitor\\{4D36E96E-E325-11CE-BFC1-08002BE10318}\\"),
-           "DeviceID \"%s\" does not start with \"Monitor\\Default_Monitor\\...\" prefix\n", dd.DeviceID);
-    }
 }
 
 struct vid_mode
@@ -160,7 +304,6 @@ static const struct vid_mode vid_modes_test[] = {
     {0, 0, 0, 0, DM_DISPLAYFREQUENCY, 0}
     */
 };
-#define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
 
 static void test_ChangeDisplaySettingsEx(void)
 {
@@ -247,7 +390,7 @@ static void test_ChangeDisplaySettingsEx(void)
     memset(&dm, 0, sizeof(dm));
     dm.dmSize = sizeof(dm);
 
-    for (i = 0; i < vid_modes_cnt; i++)
+    for (i = 0; i < ARRAY_SIZE(vid_modes_test); i++)
     {
         dm.dmPelsWidth        = vid_modes_test[i].w;
         dm.dmPelsHeight       = vid_modes_test[i].h;
@@ -409,7 +552,7 @@ static void test_monitors(void)
 
     /* tests for cbSize in MONITORINFO */
     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
-    for (i = 0; i < (sizeof(testdatami) / sizeof(testdatami[0])); i++)
+    for (i = 0; i < ARRAY_SIZE(testdatami); i++)
     {
         memset( &mi, 0, sizeof(mi) );
         mi.cbSize = testdatami[i].cbSize;
@@ -431,7 +574,7 @@ static void test_monitors(void)
     }
 
     /* tests for cbSize in MONITORINFOEXA */
-    for (i = 0; i < (sizeof(testdatamiexa) / sizeof(testdatamiexa[0])); i++)
+    for (i = 0; i < ARRAY_SIZE(testdatamiexa); i++)
     {
         memset( &miexa, 0, sizeof(miexa) );
         miexa.cbSize = testdatamiexa[i].cbSize;
@@ -444,7 +587,7 @@ static void test_monitors(void)
     }
 
     /* tests for cbSize in MONITORINFOEXW */
-    for (i = 0; i < (sizeof(testdatamiexw) / sizeof(testdatamiexw[0])); i++)
+    for (i = 0; i < ARRAY_SIZE(testdatamiexw); i++)
     {
         memset( &miexw, 0, sizeof(miexw) );
         miexw.cbSize = testdatamiexw[i].cbSize;
@@ -596,7 +739,7 @@ static void test_display_config(void)
     paths = modes = 100;
     ret = pGetDisplayConfigBufferSizes(0, &paths, &modes);
     ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_NOT_SUPPORTED, "got %d\n", ret);
-    ok(modes == 0 && paths == 0, "got %u, %u\n", modes, paths);
+    ok((modes == 0 || modes == 100) && paths == 0, "got %u, %u\n", modes, paths);
 }
 
 START_TEST(monitor)