Skip to content

Commit

Permalink
test: Make mock ICD tests work on windows
Browse files Browse the repository at this point in the history
This does not enable testing in CI on windows, due to the loader ignoring
VK_DRIVER_FILES when running in admin mode.
  • Loading branch information
charles-lunarg committed Aug 21, 2023
1 parent d5bbe72 commit ce34ab6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
13 changes: 12 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,30 @@ find_package(GTest REQUIRED CONFIG QUIET)
add_executable(vulkan_tools_tests)
target_sources(vulkan_tools_tests PRIVATE
main.cpp
test_common.h
icd/mock_icd_tests.cpp
)
get_target_property(TEST_SOURCES vulkan_tools_tests SOURCES)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${TEST_SOURCES})

target_include_directories(vulkan_tools_tests PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(vulkan_tools_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(vulkan_tools_tests GTest::gtest Vulkan::Vulkan)
add_dependencies(vulkan_tools_tests generate_binary_locations)
if (WIN32)
target_compile_definitions(vulkan_tools_tests PUBLIC -DVK_USE_PLATFORM_WIN32_KHR -DWIN32_LEAN_AND_MEAN -DNOMINMAX)
endif()
set_target_properties(vulkan_tools_tests PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

if (ENABLE_ADDRESS_SANITIZER)
target_compile_options(vulkan_tools_tests PUBLIC -fsanitize=address)
target_link_options(vulkan_tools_tests PUBLIC -fsanitize=address)
endif ()

if (WIN32)
# Copy the loader shared lib (if built) to the test application directory so the test app finds it.
add_custom_command(TARGET vulkan_tools_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Vulkan::Vulkan> $<TARGET_FILE_DIR:vulkan_tools_tests>)
endif()

include(GoogleTest)
gtest_discover_tests(vulkan_tools_tests DISCOVERY_TIMEOUT 100)
38 changes: 10 additions & 28 deletions tests/icd/mock_icd_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,7 @@
*
*/

#include <stdlib.h>

#include <array>
#include <iostream>
#include <vector>

#include "gtest/gtest.h"
#include "vulkan/vulkan.h"

// Location of the built binaries in this repo
#include "binary_locations.h"

#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int set_environment_var(const char* name, const char* value) { return SetEnvironmentVariable(name, value); }
#else
int set_environment_var(const char* name, const char* value) { return setenv(name, value, 1); }
#endif
#include "test_common.h"

void setup_mock_icd_env_vars() {
// Necessary to point the loader at the mock driver
Expand Down Expand Up @@ -256,7 +238,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfacePresentModesKHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
uint32_t count = 0;
std::array<VkPresentModeKHR, 6> present_modes{};
res = vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &count, nullptr);
Expand All @@ -278,7 +260,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceFormatsKHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
uint32_t count = 0;
std::array<VkSurfaceFormatKHR, 2> surface_formats{};
res = vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &count, nullptr);
Expand All @@ -298,7 +280,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceFormats2KHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
uint32_t count = 0;
std::array<VkSurfaceFormat2KHR, 2> surface_formats2{};
VkPhysicalDeviceSurfaceInfo2KHR surface_info{};
Expand All @@ -322,7 +304,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceSupportKHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
VkBool32 supported = false;
res = vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, 0, surface, &supported);
ASSERT_EQ(res, VK_SUCCESS);
Expand All @@ -335,7 +317,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceCapabilitiesKHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
VkSurfaceCapabilitiesKHR surface_capabilities{};
res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &surface_capabilities);
ASSERT_EQ(res, VK_SUCCESS);
Expand All @@ -354,7 +336,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceCapabilities2KHR) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);
VkSurfaceCapabilities2KHR surface_capabilities2{};
VkPhysicalDeviceSurfaceInfo2KHR surface_info{};
surface_info.surface = surface;
Expand Down Expand Up @@ -899,15 +881,15 @@ TEST_F(MockICD, SwapchainLifeCycle) {
VkSurfaceKHR surface{};
res = create_surface(instance, surface);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(surface, nullptr);
ASSERT_NE(surface, VK_NULL_HANDLE);

VkSwapchainCreateInfoKHR swapchain_create_info{};
swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = 1;
VkSwapchainKHR swapchain{};
res = vkCreateSwapchainKHR(device, &swapchain_create_info, nullptr, &swapchain);
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(swapchain, nullptr);
ASSERT_NE(swapchain, VK_NULL_HANDLE);

uint32_t count = 0;
res = vkGetSwapchainImagesKHR(device, swapchain, &count, nullptr);
Expand All @@ -916,7 +898,7 @@ TEST_F(MockICD, SwapchainLifeCycle) {
std::array<VkImage, 1> swapchain_images;
res = vkGetSwapchainImagesKHR(device, swapchain, &count, swapchain_images.data());
ASSERT_EQ(res, VK_SUCCESS);
ASSERT_NE(swapchain_images[0], nullptr);
ASSERT_NE(swapchain_images[0], VK_NULL_HANDLE);

uint32_t image_index = 10; // arbitrary non zero value
res = vkAcquireNextImageKHR(device, swapchain, 0, VK_NULL_HANDLE, VK_NULL_HANDLE, &image_index);
Expand Down
37 changes: 37 additions & 0 deletions tests/test_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 The Khronos Group Inc.
* Copyright (c) 2023 Valve Corporation
* Copyright (c) 2023 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <stdlib.h>

#include <array>
#include <iostream>
#include <vector>

#include "gtest/gtest.h"
#include "vulkan/vulkan.h"

// Location of the built binaries in this repo
#include "binary_locations.h"

#if defined(WIN32)
#include <windows.h>
inline int set_environment_var(const char* name, const char* value) { return SetEnvironmentVariable(name, value); }
#else
inline int set_environment_var(const char* name, const char* value) { return setenv(name, value, 1); }
#endif

0 comments on commit ce34ab6

Please sign in to comment.