glib/tests: Add C++ test for g_win32_clear_com()

This tests the C++ version of g_win32_clear_com() as there are some
COM interfaces that are only available in C++, such as DirectWrite from
the headers shipped with the Windows SDK.

This mimicks the test for the same function in glib/tests/win32.c but
done in a C++ fashion.
This commit is contained in:
Chun-wei Fan 2024-12-09 18:41:53 +08:00
parent 5642331603
commit a3a95c2a0b
2 changed files with 32 additions and 0 deletions

View File

@ -19,6 +19,10 @@
#include <glib.h>
#ifdef G_OS_WIN32
#include <wincodec.h>
#endif
#if !defined (G_CXX_STD_VERSION) || !defined (G_CXX_STD_CHECK_VERSION)
#error G_CXX_STD_VERSION is not defined
#endif
@ -532,6 +536,29 @@ test_string_free (void)
g_free (data);
}
#ifdef G_OS_WIN32
static void
test_clear_com (void)
{
IWICImagingFactory *tmp;
IWICImagingFactory *o = NULL;
CoInitialize (NULL);
g_win32_clear_com (&o);
g_assert_null (o);
g_assert_true (SUCCEEDED (CoCreateInstance (CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void **)&tmp)));
g_assert_nonnull (tmp);
tmp->QueryInterface (IID_IWICImagingFactory, (void **)&o); /* IWICImagingFactory::QueryInterface increments the refcount */
g_assert_nonnull (o);
g_assert_cmpint (o->AddRef (), ==, 3); /* refcount incremented again */
g_win32_clear_com (&o);
g_assert_null (o);
tmp->Release ();
CoUninitialize ();
}
#endif
int
main (int argc, char *argv[])
{
@ -566,5 +593,8 @@ main (int argc, char *argv[])
g_test_add_func ("/C++/string-append", test_string_append);
g_test_add_func ("/C++/string-free", test_string_free);
#ifdef G_OS_WIN32
g_test_add_func ("/C++/test_clear_com", test_clear_com);
#endif
return g_test_run ();
}

View File

@ -209,6 +209,7 @@ if have_cxx
'cxx' : {
'source' : ['cxx.cpp'],
'suite': ['cpp'],
'dependencies' : [wincodecs],
}
}
@ -218,6 +219,7 @@ if have_cxx
'source' : ['cxx.cpp'],
'suite' : ['cpp'],
'cpp_args' : [arg, '-D_G_EXPECTED_CXX_STANDARD="@0@"'.format(std)],
'dependencies' : [wincodecs],
},
}
endforeach