From 33086b085743cad725c1f611d33afa2ecbce1a10 Mon Sep 17 00:00:00 2001 From: "aroben@apple.com" Date: Mon, 26 Sep 2011 15:55:05 +0000 Subject: [PATCH] Clean up code imported from WebKitAPITest Fixes WebViewDestruction tests and related code don't match TestWebKitAPI conventions Reviewed by David Levin. * TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp: Changed tests to use gtest macros and to share functionality via test fixtures. Prefixed Win32 API calls with ::. Updated namespace name. (TestWebKitAPI::WebViewDestruction::SetUp): (TestWebKitAPI::WebViewDestruction::webViewCount): (TestWebKitAPI::WebViewDestructionWithHostWindow::SetUp): (TestWebKitAPI::WebViewDestruction::runMessagePump): (TestWebKitAPI::WebViewDestruction::TearDown): (TestWebKitAPI::WebViewDestructionWithHostWindow::TearDown): Moved functionality from free functions into these new test fixtures. * TestWebKitAPI/win/HostWindow.cpp: * TestWebKitAPI/win/HostWindow.h: Prefixed Win32 API calls with ::. Updated namespace name. git-svn-id: http://svn.webkit.org/repository/webkit/trunk@95955 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Tools/ChangeLog | 25 +++ .../Tests/WebKit/win/WebViewDestruction.cpp | 178 +++++++++------------ Tools/TestWebKitAPI/win/HostWindow.cpp | 14 +- Tools/TestWebKitAPI/win/HostWindow.h | 2 +- 4 files changed, 107 insertions(+), 112 deletions(-) diff --git a/Tools/ChangeLog b/Tools/ChangeLog index 2db7a74..612eb05 100644 --- a/Tools/ChangeLog +++ b/Tools/ChangeLog @@ -1,3 +1,28 @@ +2011-09-26 Adam Roben + + Clean up code imported from WebKitAPITest + + Fixes WebViewDestruction tests and related code don't match + TestWebKitAPI conventions + + Reviewed by David Levin. + + * TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp: Changed tests to use gtest macros + and to share functionality via test fixtures. Prefixed Win32 API calls with ::. Updated + namespace name. + + (TestWebKitAPI::WebViewDestruction::SetUp): + (TestWebKitAPI::WebViewDestruction::webViewCount): + (TestWebKitAPI::WebViewDestructionWithHostWindow::SetUp): + (TestWebKitAPI::WebViewDestruction::runMessagePump): + (TestWebKitAPI::WebViewDestruction::TearDown): + (TestWebKitAPI::WebViewDestructionWithHostWindow::TearDown): + Moved functionality from free functions into these new test fixtures. + + * TestWebKitAPI/win/HostWindow.cpp: + * TestWebKitAPI/win/HostWindow.h: + Prefixed Win32 API calls with ::. Updated namespace name. + 2011-09-23 Adam Roben Merge WebKitAPITest into TestWebKitAPI diff --git a/Tools/TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp b/Tools/TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp index e3930b0..3e6630f 100644 --- a/Tools/TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp +++ b/Tools/TestWebKitAPI/Tests/WebKit/win/WebViewDestruction.cpp @@ -31,9 +31,7 @@ #include #include -#define TEST_ASSERT(x) ASSERT_TRUE(x) - -namespace WebKitAPITest { +namespace TestWebKitAPI { template static HRESULT WebKitCreateInstance(REFCLSID clsid, T** object) @@ -41,7 +39,32 @@ static HRESULT WebKitCreateInstance(REFCLSID clsid, T** object) return WebKitCreateInstance(clsid, 0, __uuidof(T), reinterpret_cast(object)); } -static int webViewCount() +class WebViewDestruction : public ::testing::Test { +protected: + virtual void SetUp(); + virtual void TearDown(); + + static int webViewCount(); + static void runMessagePump(DWORD timeoutMilliseconds); + + COMPtr m_webView; +}; + +class WebViewDestructionWithHostWindow : public WebViewDestruction { +protected: + virtual void SetUp(); + virtual void TearDown(); + + HostWindow m_window; + HWND m_viewWindow; +}; + +void WebViewDestruction::SetUp() +{ + EXPECT_HRESULT_SUCCEEDED(WebKitCreateInstance(__uuidof(WebView), &m_webView)); +} + +int WebViewDestruction::webViewCount() { COMPtr statistics; if (FAILED(WebKitCreateInstance(__uuidof(WebKitStatistics), &statistics))) @@ -52,157 +75,104 @@ static int webViewCount() return count; } -static void createAndInitializeWebView(COMPtr& outWebView, HostWindow& window, HWND& viewWindow) +void WebViewDestructionWithHostWindow::SetUp() { - COMPtr webView; - TEST_ASSERT(SUCCEEDED(WebKitCreateInstance(__uuidof(WebView), &webView))); - - TEST_ASSERT(window.initialize()); - TEST_ASSERT(SUCCEEDED(webView->setHostWindow(reinterpret_cast(window.window())))); - TEST_ASSERT(SUCCEEDED(webView->initWithFrame(window.clientRect(), 0, 0))); + WebViewDestruction::SetUp(); - COMPtr viewPrivate(Query, webView); - TEST_ASSERT(viewPrivate); - TEST_ASSERT(SUCCEEDED(viewPrivate->viewWindow(reinterpret_cast(&viewWindow)))); - TEST_ASSERT(IsWindow(viewWindow)); + EXPECT_TRUE(m_window.initialize()); + EXPECT_HRESULT_SUCCEEDED(m_webView->setHostWindow(reinterpret_cast(m_window.window()))); + EXPECT_HRESULT_SUCCEEDED(m_webView->initWithFrame(m_window.clientRect(), 0, 0)); - outWebView.adoptRef(webView.releaseRef()); + COMPtr viewPrivate(Query, m_webView); + ASSERT_NOT_NULL(viewPrivate); + EXPECT_HRESULT_SUCCEEDED(viewPrivate->viewWindow(reinterpret_cast(&m_viewWindow))); + EXPECT_TRUE(::IsWindow(m_viewWindow)); } -static void runMessagePump(DWORD timeoutMilliseconds) +void WebViewDestruction::runMessagePump(DWORD timeoutMilliseconds) { - DWORD startTickCount = GetTickCount(); + // FIXME: We should move this functionality to PlatformUtilities at some point. + + DWORD startTickCount = ::GetTickCount(); MSG msg; BOOL result; - while ((result = PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) && GetTickCount() - startTickCount <= timeoutMilliseconds) { + while ((result = ::PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) && ::GetTickCount() - startTickCount <= timeoutMilliseconds) { if (result == -1) break; - TranslateMessage(&msg); - DispatchMessage(&msg); + ::TranslateMessage(&msg); + ::DispatchMessage(&msg); } } -static void finishWebViewDestructionTest(COMPtr& webView, HWND viewWindow) +void WebViewDestruction::TearDown() { // Allow window messages to be processed, because in some cases that would trigger a crash (e.g., ). runMessagePump(50); // We haven't crashed. Release the WebView and ensure that its view window has been destroyed and the WebView doesn't leak. int currentWebViewCount = webViewCount(); - TEST_ASSERT(currentWebViewCount > 0); + EXPECT_GT(currentWebViewCount, 0); - webView = 0; + m_webView = 0; - TEST_ASSERT(webViewCount() == currentWebViewCount - 1); - TEST_ASSERT(!IsWindow(viewWindow)); + EXPECT_EQ(webViewCount(), currentWebViewCount - 1); } -// Tests that releasing a WebView without calling IWebView::initWithFrame works. -TEST(WebViewDestruction, NoInitWithFrame) +void WebViewDestructionWithHostWindow::TearDown() { - COMPtr webView; - TEST_ASSERT(SUCCEEDED(WebKitCreateInstance(__uuidof(WebView), &webView))); + WebViewDestruction::TearDown(); - finishWebViewDestructionTest(webView, 0); + EXPECT_FALSE(::IsWindow(m_viewWindow)); } -TEST(WebViewDestruction, CloseWithoutInitWithFrame) +// Tests that releasing a WebView without calling IWebView::initWithFrame works. +TEST_F(WebViewDestruction, NoInitWithFrame) { - COMPtr webView; - TEST_ASSERT(SUCCEEDED(WebKitCreateInstance(__uuidof(WebView), &webView))); - - TEST_ASSERT(SUCCEEDED(webView->close())); +} - finishWebViewDestructionTest(webView, 0); +TEST_F(WebViewDestruction, CloseWithoutInitWithFrame) +{ + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); } // Tests that calling IWebView::close without calling DestroyWindow, then releasing a WebView doesn't crash. -TEST(WebViewDestruction, CloseWithoutDestroyViewWindow) +TEST_F(WebViewDestructionWithHostWindow, CloseWithoutDestroyViewWindow) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - TEST_ASSERT(SUCCEEDED(webView->close())); - - finishWebViewDestructionTest(webView, viewWindow); + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); } -TEST(WebViewDestruction, DestroyViewWindowWithoutClose) +TEST_F(WebViewDestructionWithHostWindow, DestroyViewWindowWithoutClose) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - DestroyWindow(viewWindow); - - finishWebViewDestructionTest(webView, viewWindow); + ::DestroyWindow(m_viewWindow); } -TEST(WebViewDestruction, CloseThenDestroyViewWindow) +TEST_F(WebViewDestructionWithHostWindow, CloseThenDestroyViewWindow) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - TEST_ASSERT(SUCCEEDED(webView->close())); - DestroyWindow(viewWindow); - - finishWebViewDestructionTest(webView, viewWindow); + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); + ::DestroyWindow(m_viewWindow); } -TEST(WebViewDestruction, DestroyViewWindowThenClose) +TEST_F(WebViewDestructionWithHostWindow, DestroyViewWindowThenClose) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - DestroyWindow(viewWindow); - TEST_ASSERT(SUCCEEDED(webView->close())); - - finishWebViewDestructionTest(webView, viewWindow); + ::DestroyWindow(m_viewWindow); + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); } -TEST(WebViewDestruction, DestroyHostWindow) +TEST_F(WebViewDestructionWithHostWindow, DestroyHostWindow) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - DestroyWindow(window.window()); - - finishWebViewDestructionTest(webView, viewWindow); + ::DestroyWindow(m_window.window()); } -TEST(WebViewDestruction, DestroyHostWindowThenClose) +TEST_F(WebViewDestructionWithHostWindow, DestroyHostWindowThenClose) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - DestroyWindow(window.window()); - TEST_ASSERT(SUCCEEDED(webView->close())); - - finishWebViewDestructionTest(webView, viewWindow); + ::DestroyWindow(m_window.window()); + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); } -TEST(WebViewDestruction, CloseThenDestroyHostWindow) +TEST_F(WebViewDestructionWithHostWindow, CloseThenDestroyHostWindow) { - COMPtr webView; - HostWindow window; - HWND viewWindow; - createAndInitializeWebView(webView, window, viewWindow); - - TEST_ASSERT(SUCCEEDED(webView->close())); - DestroyWindow(window.window()); - - finishWebViewDestructionTest(webView, viewWindow); + EXPECT_HRESULT_SUCCEEDED(m_webView->close()); + ::DestroyWindow(m_window.window()); } } // namespace WebKitAPITest diff --git a/Tools/TestWebKitAPI/win/HostWindow.cpp b/Tools/TestWebKitAPI/win/HostWindow.cpp index e35e96c..74c947d 100644 --- a/Tools/TestWebKitAPI/win/HostWindow.cpp +++ b/Tools/TestWebKitAPI/win/HostWindow.cpp @@ -25,7 +25,7 @@ #include "config.h" #include "HostWindow.h" -namespace WebKitAPITest { +namespace TestWebKitAPI { static LPCWSTR hostWindowClassName = L"HostWindow"; @@ -37,21 +37,21 @@ HostWindow::HostWindow() bool HostWindow::initialize() { registerWindowClass(); - m_window = CreateWindowExW(0, hostWindowClassName, L"WebKitAPITest", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, GetModuleHandle(0), 0); + m_window = ::CreateWindowExW(0, hostWindowClassName, L"TestWebKitAPI", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, ::GetModuleHandle(0), 0); return m_window; } HostWindow::~HostWindow() { - if (!IsWindow(m_window)) + if (!::IsWindow(m_window)) return; - DestroyWindow(m_window); + ::DestroyWindow(m_window); } RECT HostWindow::clientRect() const { RECT rect = {0}; - if (!GetClientRect(m_window, &rect)) { + if (!::GetClientRect(m_window, &rect)) { RECT emptyRect = {0}; return emptyRect; } @@ -73,12 +73,12 @@ void HostWindow::registerWindowClass() wndClass.hInstance = GetModuleHandle(0); wndClass.lpszClassName = hostWindowClassName; - RegisterClassExW(&wndClass); + ::RegisterClassExW(&wndClass); } LRESULT HostWindow::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - return DefWindowProcW(hWnd, uMsg, wParam, lParam); + return ::DefWindowProcW(hWnd, uMsg, wParam, lParam); } } // namespace WebKitAPITest diff --git a/Tools/TestWebKitAPI/win/HostWindow.h b/Tools/TestWebKitAPI/win/HostWindow.h index 2aaa85f..ddb53f1 100644 --- a/Tools/TestWebKitAPI/win/HostWindow.h +++ b/Tools/TestWebKitAPI/win/HostWindow.h @@ -28,7 +28,7 @@ #include #include -namespace WebKitAPITest { +namespace TestWebKitAPI { class HostWindow { WTF_MAKE_NONCOPYABLE(HostWindow); -- 2.7.4