Get rid of CppUnit boilerplate in wxWebView unit test

This not only cuts down on its size, but will make it simpler to skip
this test in the environments where web access is unavailable in the
upcoming commit.
This commit is contained in:
Vadim Zeitlin
2019-07-15 13:59:51 +02:00
parent f8bdf123f2
commit 2b71ce3664

View File

@@ -25,77 +25,27 @@
#include "wx/msw/webview_ie.h"
#endif
class WebTestCase : public CppUnit::TestCase
{
public:
WebTestCase() { }
void setUp() wxOVERRIDE;
void tearDown() wxOVERRIDE;
private:
CPPUNIT_TEST_SUITE( WebTestCase );
CPPUNIT_TEST( Title );
CPPUNIT_TEST( Url );
CPPUNIT_TEST( History );
#if !wxUSE_WEBVIEW_WEBKIT2
//This is not implemented on WEBKIT2. See implementation.
CPPUNIT_TEST( HistoryEnable );
CPPUNIT_TEST( HistoryClear );
#endif
CPPUNIT_TEST( HistoryList );
CPPUNIT_TEST( Editable );
CPPUNIT_TEST( Selection );
CPPUNIT_TEST( Zoom );
CPPUNIT_TEST( RunScript );
CPPUNIT_TEST( SetPage );
CPPUNIT_TEST_SUITE_END();
void Title();
void Url();
void History();
void HistoryEnable();
void HistoryClear();
void HistoryList();
void Editable();
void Selection();
void Zoom();
void RunScript();
void SetPage();
void LoadUrl(int times = 1);
wxWebView* m_browser;
EventCounter* m_loaded;
wxDECLARE_NO_COPY_CLASS(WebTestCase);
};
//Convenience macro
#define ENSURE_LOADED CHECK( m_loaded->WaitEvent() )
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
void WebTestCase::setUp()
class WebViewTestCase
{
m_browser = wxWebView::New();
m_loaded = new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED);
public:
WebViewTestCase()
: m_browser(wxWebView::New()),
m_loaded(new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED))
{
}
m_browser -> Create(wxTheApp->GetTopWindow(), wxID_ANY);
ENSURE_LOADED;
}
~WebViewTestCase()
{
delete m_loaded;
delete m_browser;
}
void WebTestCase::tearDown()
{
wxDELETE(m_loaded);
wxDELETE(m_browser);
}
void WebTestCase::LoadUrl(int times)
{
protected:
void LoadUrl(int times = 1)
{
//We alternate between urls as otherwise webkit merges them in the history
//we use about and about blank to avoid the need for a network connection
for(int i = 0; i < times; i++)
@@ -106,10 +56,19 @@ void WebTestCase::LoadUrl(int times)
m_browser->LoadURL("about:");
ENSURE_LOADED;
}
}
}
void WebTestCase::Title()
wxWebView* const m_browser;
EventCounter* const m_loaded;
};
TEST_CASE_METHOD(WebViewTestCase, "WebView", "[wxWebView]")
{
m_browser -> Create(wxTheApp->GetTopWindow(), wxID_ANY);
ENSURE_LOADED;
SECTION("Title")
{
CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
//Test title after loading raw html
@@ -120,19 +79,19 @@ void WebTestCase::Title()
//Test title after loading a url, we yield to let events process
LoadUrl();
CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
}
}
void WebTestCase::Url()
{
SECTION("URL")
{
CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
//After first loading about:blank the next in the sequence is about:
LoadUrl();
CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
}
}
void WebTestCase::History()
{
SECTION("History")
{
LoadUrl(3);
CPPUNIT_ASSERT(m_browser->CanGoBack());
@@ -152,10 +111,10 @@ void WebTestCase::History()
//We should now be at the start of the history
CPPUNIT_ASSERT(!m_browser->CanGoBack());
CPPUNIT_ASSERT(m_browser->CanGoForward());
}
}
void WebTestCase::HistoryEnable()
{
SECTION("HistoryEnable")
{
LoadUrl();
m_browser->EnableHistory(false);
@@ -166,10 +125,10 @@ void WebTestCase::HistoryEnable()
CPPUNIT_ASSERT(!m_browser->CanGoForward());
CPPUNIT_ASSERT(!m_browser->CanGoBack());
}
}
void WebTestCase::HistoryClear()
{
SECTION("HistoryClear")
{
LoadUrl(2);
//Now we are in the 'middle' of the history
@@ -183,10 +142,10 @@ void WebTestCase::HistoryClear()
CPPUNIT_ASSERT(!m_browser->CanGoForward());
CPPUNIT_ASSERT(!m_browser->CanGoBack());
}
}
void WebTestCase::HistoryList()
{
SECTION("HistoryList")
{
LoadUrl(2);
m_browser->GoBack();
ENSURE_LOADED;
@@ -199,10 +158,10 @@ void WebTestCase::HistoryList()
CPPUNIT_ASSERT(!m_browser->CanGoForward());
CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
}
}
void WebTestCase::Editable()
{
SECTION("Editable")
{
CPPUNIT_ASSERT(!m_browser->IsEditable());
m_browser->SetEditable(true);
@@ -212,10 +171,10 @@ void WebTestCase::Editable()
m_browser->SetEditable(false);
CPPUNIT_ASSERT(!m_browser->IsEditable());
}
}
void WebTestCase::Selection()
{
SECTION("Selection")
{
m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
ENSURE_LOADED;
CPPUNIT_ASSERT(!m_browser->HasSelection());
@@ -239,10 +198,10 @@ void WebTestCase::Selection()
m_browser->ClearSelection();
CPPUNIT_ASSERT(!m_browser->HasSelection());
}
}
void WebTestCase::Zoom()
{
SECTION("Zoom")
{
if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
{
m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
@@ -263,16 +222,16 @@ void WebTestCase::Zoom()
m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
}
}
}
void WebTestCase::RunScript()
{
SECTION("RunScript")
{
m_browser->
SetPage("<html><head><script></script></head><body></body></html>", "");
ENSURE_LOADED;
wxString result;
#if wxUSE_WEBVIEW_IE
#if wxUSE_WEBVIEW_IE
CPPUNIT_ASSERT(wxWebViewIE::MSWSetModernEmulationLevel());
// Define a specialized scope guard ensuring that we reset the emulation
@@ -314,7 +273,7 @@ void WebTestCase::RunScript()
CPPUNIT_ASSERT_EQUAL("\"2017-10-08T21:30:40.000Z\"", result);
CPPUNIT_ASSERT(resetEmulationLevel.DoReset());
#endif // wxUSE_WEBVIEW_IE
#endif // wxUSE_WEBVIEW_IE
CPPUNIT_ASSERT(m_browser->RunScript("document.write(\"Hello World!\");"));
CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
@@ -373,10 +332,10 @@ void WebTestCase::RunScript()
CPPUNIT_ASSERT(!m_browser->RunScript("syntax(error"));
CPPUNIT_ASSERT(!m_browser->RunScript("syntax(error", &result));
CPPUNIT_ASSERT(!m_browser->RunScript("x.y.z"));
}
}
void WebTestCase::SetPage()
{
SECTION("SetPage")
{
m_browser->SetPage("<html><body>text</body></html>", "");
ENSURE_LOADED;
CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
@@ -384,6 +343,7 @@ void WebTestCase::SetPage()
m_browser->SetPage("<html><body>other text</body></html>", "");
ENSURE_LOADED;
CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
}
}
#endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_WEBKIT2 || wxUSE_WEBVIEW_IE)