2011-07-08 08:19:25 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/controls/webtest.cpp
|
|
|
|
// Purpose: wxWebView unit test
|
|
|
|
// Author: Steven Lamerton
|
|
|
|
// Created: 2011-07-08
|
|
|
|
// Copyright: (c) 2011 Steven Lamerton
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
2011-09-14 07:39:28 +00:00
|
|
|
#if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
|
2011-07-08 08:19:25 +00:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "testableframe.h"
|
|
|
|
#include "wx/uiaction.h"
|
|
|
|
#include "wx/webview.h"
|
|
|
|
#include "asserthelper.h"
|
|
|
|
|
|
|
|
class WebTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebTestCase() { }
|
|
|
|
|
|
|
|
void setUp();
|
|
|
|
void tearDown();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( WebTestCase );
|
2011-07-08 10:51:15 +00:00
|
|
|
CPPUNIT_TEST( Title );
|
2011-07-08 13:11:17 +00:00
|
|
|
CPPUNIT_TEST( Url );
|
|
|
|
CPPUNIT_TEST( History );
|
2011-07-08 14:21:46 +00:00
|
|
|
CPPUNIT_TEST( HistoryEnable );
|
|
|
|
CPPUNIT_TEST( HistoryClear );
|
2011-07-08 14:35:49 +00:00
|
|
|
CPPUNIT_TEST( HistoryList );
|
2011-07-08 19:34:56 +00:00
|
|
|
CPPUNIT_TEST( Editable );
|
2011-07-10 18:11:43 +00:00
|
|
|
CPPUNIT_TEST( Selection );
|
2011-07-14 14:02:14 +00:00
|
|
|
CPPUNIT_TEST( Zoom );
|
2011-07-15 12:38:47 +00:00
|
|
|
CPPUNIT_TEST( RunScript );
|
2011-12-18 11:51:52 +00:00
|
|
|
CPPUNIT_TEST( SetPage );
|
2011-07-08 08:19:25 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2011-07-08 10:51:15 +00:00
|
|
|
void Title();
|
2011-07-08 13:11:17 +00:00
|
|
|
void Url();
|
|
|
|
void History();
|
2011-07-08 14:21:46 +00:00
|
|
|
void HistoryEnable();
|
|
|
|
void HistoryClear();
|
2011-07-08 14:35:49 +00:00
|
|
|
void HistoryList();
|
2011-07-08 19:34:56 +00:00
|
|
|
void Editable();
|
2011-07-10 18:11:43 +00:00
|
|
|
void Selection();
|
2011-07-14 14:02:14 +00:00
|
|
|
void Zoom();
|
2011-07-15 12:38:47 +00:00
|
|
|
void RunScript();
|
2011-12-18 11:51:52 +00:00
|
|
|
void SetPage();
|
2011-07-14 19:31:21 +00:00
|
|
|
void LoadUrl(int times = 1);
|
2011-07-08 10:51:15 +00:00
|
|
|
|
2011-07-08 08:19:25 +00:00
|
|
|
wxWebView* m_browser;
|
2012-03-12 14:57:44 +00:00
|
|
|
EventCounter* m_loaded;
|
2011-07-08 08:19:25 +00:00
|
|
|
|
2015-04-23 11:49:01 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(WebTestCase);
|
2011-07-08 08:19:25 +00:00
|
|
|
};
|
|
|
|
|
2012-03-12 14:57:44 +00:00
|
|
|
//Convenience macro
|
|
|
|
#define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1)
|
|
|
|
|
2011-07-08 08:19:25 +00:00
|
|
|
// 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()
|
|
|
|
{
|
2011-07-08 10:51:15 +00:00
|
|
|
m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
|
2012-03-12 14:57:44 +00:00
|
|
|
|
2013-04-25 12:53:51 +00:00
|
|
|
m_loaded = new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED);
|
2013-04-29 23:00:57 +00:00
|
|
|
m_browser->LoadURL("about:blank");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 08:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebTestCase::tearDown()
|
|
|
|
{
|
2012-03-12 14:57:44 +00:00
|
|
|
wxDELETE(m_loaded);
|
2011-07-08 08:19:25 +00:00
|
|
|
wxDELETE(m_browser);
|
|
|
|
}
|
|
|
|
|
2011-07-14 19:31:21 +00:00
|
|
|
void WebTestCase::LoadUrl(int times)
|
2011-07-08 14:46:51 +00:00
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
//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
|
2011-07-08 14:46:51 +00:00
|
|
|
for(int i = 0; i < times; i++)
|
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
if(i % 2 == 1)
|
2011-09-13 19:39:38 +00:00
|
|
|
m_browser->LoadURL("about:blank");
|
2011-07-14 19:31:21 +00:00
|
|
|
else
|
2011-09-13 19:39:38 +00:00
|
|
|
m_browser->LoadURL("about:");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 14:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-08 10:51:15 +00:00
|
|
|
void WebTestCase::Title()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
|
|
|
|
|
|
|
|
//Test title after loading raw html
|
2011-07-14 19:31:21 +00:00
|
|
|
m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 10:51:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
|
|
|
|
|
|
|
|
//Test title after loading a url, we yield to let events process
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl();
|
2011-07-08 10:51:15 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
|
|
|
|
}
|
|
|
|
|
2011-07-08 13:11:17 +00:00
|
|
|
void WebTestCase::Url()
|
|
|
|
{
|
2011-07-14 13:33:20 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
|
2011-07-08 13:11:17 +00:00
|
|
|
|
2011-07-14 19:31:21 +00:00
|
|
|
//After first loading about:blank the next in the sequence is about:
|
|
|
|
LoadUrl();
|
|
|
|
CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
|
2011-07-08 13:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebTestCase::History()
|
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl(3);
|
2011-07-08 13:11:17 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoBack());
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoForward());
|
|
|
|
|
|
|
|
m_browser->GoBack();
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 13:11:17 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoBack());
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoForward());
|
|
|
|
|
|
|
|
m_browser->GoBack();
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 13:11:17 +00:00
|
|
|
m_browser->GoBack();
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 13:11:17 +00:00
|
|
|
|
|
|
|
//We should now be at the start of the history
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoBack());
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoForward());
|
|
|
|
}
|
|
|
|
|
2011-07-08 14:21:46 +00:00
|
|
|
void WebTestCase::HistoryEnable()
|
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl();
|
2011-07-08 14:21:46 +00:00
|
|
|
m_browser->EnableHistory(false);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoForward());
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoBack());
|
|
|
|
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl();
|
2011-07-08 14:21:46 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoForward());
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoBack());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTestCase::HistoryClear()
|
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl(2);
|
2011-07-08 14:21:46 +00:00
|
|
|
|
|
|
|
//Now we are in the 'middle' of the history
|
|
|
|
m_browser->GoBack();
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 14:21:46 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoForward());
|
|
|
|
CPPUNIT_ASSERT(m_browser->CanGoBack());
|
|
|
|
|
|
|
|
m_browser->ClearHistory();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoForward());
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoBack());
|
|
|
|
}
|
|
|
|
|
2011-07-08 14:35:49 +00:00
|
|
|
void WebTestCase::HistoryList()
|
|
|
|
{
|
2011-07-14 19:31:21 +00:00
|
|
|
LoadUrl(2);
|
2011-07-08 14:35:49 +00:00
|
|
|
m_browser->GoBack();
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 14:35:49 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
|
|
|
|
|
|
|
|
m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-08 14:35:49 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!m_browser->CanGoForward());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
|
|
|
|
}
|
|
|
|
|
2011-07-08 19:34:56 +00:00
|
|
|
void WebTestCase::Editable()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(!m_browser->IsEditable());
|
|
|
|
|
|
|
|
m_browser->SetEditable(true);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(m_browser->IsEditable());
|
|
|
|
|
|
|
|
m_browser->SetEditable(false);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(!m_browser->IsEditable());
|
|
|
|
}
|
|
|
|
|
2011-07-10 18:11:43 +00:00
|
|
|
void WebTestCase::Selection()
|
|
|
|
{
|
2011-07-12 08:41:11 +00:00
|
|
|
m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-07-10 18:11:43 +00:00
|
|
|
CPPUNIT_ASSERT(!m_browser->HasSelection());
|
|
|
|
|
|
|
|
m_browser->SelectAll();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(m_browser->HasSelection());
|
2011-07-12 08:41:11 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
|
2013-07-16 14:10:17 +00:00
|
|
|
|
|
|
|
// The web engine doesn't necessarily represent the HTML in the same way as
|
|
|
|
// we used above, e.g. IE uses upper case for all the tags while WebKit
|
|
|
|
// under OS X inserts plenty of its own <span> tags, so don't test for
|
|
|
|
// equality and just check that the source contains things we'd expect it
|
|
|
|
// to.
|
|
|
|
const wxString selSource = m_browser->GetSelectedSource();
|
|
|
|
WX_ASSERT_MESSAGE
|
|
|
|
(
|
|
|
|
("Unexpected selection source: \"%s\"", selSource),
|
|
|
|
selSource.Lower().Matches("*some*<strong*strong</strong>*text*")
|
|
|
|
);
|
2011-07-15 09:36:08 +00:00
|
|
|
|
|
|
|
m_browser->ClearSelection();
|
2012-03-12 14:57:44 +00:00
|
|
|
CPPUNIT_ASSERT(!m_browser->HasSelection());
|
2011-07-10 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 14:02:14 +00:00
|
|
|
void WebTestCase::Zoom()
|
|
|
|
{
|
2013-02-01 09:38:53 +00:00
|
|
|
if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
|
2011-07-14 14:02:14 +00:00
|
|
|
{
|
2013-02-01 09:38:53 +00:00
|
|
|
m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
|
2011-07-14 14:02:14 +00:00
|
|
|
|
2013-02-01 09:38:53 +00:00
|
|
|
m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
|
2011-07-14 14:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Reset the zoom level
|
2013-02-01 09:38:53 +00:00
|
|
|
m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
|
2011-07-14 14:02:14 +00:00
|
|
|
|
2013-02-01 09:38:53 +00:00
|
|
|
if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT))
|
2011-07-14 14:02:14 +00:00
|
|
|
{
|
2013-02-01 09:38:53 +00:00
|
|
|
m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
|
2011-07-14 14:02:14 +00:00
|
|
|
|
2013-02-01 09:38:53 +00:00
|
|
|
m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
|
2011-07-14 14:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:38:47 +00:00
|
|
|
void WebTestCase::RunScript()
|
|
|
|
{
|
|
|
|
m_browser->RunScript("document.write(\"Hello World!\");");
|
|
|
|
CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
|
|
|
|
}
|
|
|
|
|
2011-12-18 11:51:52 +00:00
|
|
|
void WebTestCase::SetPage()
|
|
|
|
{
|
|
|
|
m_browser->SetPage("<html><body>text</body></html>", "");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-12-18 11:51:52 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
|
|
|
|
|
|
|
|
m_browser->SetPage("<html><body>other text</body></html>", "");
|
2012-03-12 14:57:44 +00:00
|
|
|
ENSURE_LOADED;
|
2011-12-18 11:51:52 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
|
|
|
|
}
|
|
|
|
|
2011-09-14 07:39:28 +00:00
|
|
|
#endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
|