1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/webtest.cpp
3 // Purpose: wxWebView unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2011 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
11 #if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
21 #include "testableframe.h"
22 #include "wx/uiaction.h"
23 #include "wx/webview.h"
24 #include "asserthelper.h"
26 class WebTestCase
: public CppUnit::TestCase
35 CPPUNIT_TEST_SUITE( WebTestCase
);
36 CPPUNIT_TEST( Title
);
38 CPPUNIT_TEST( History
);
39 CPPUNIT_TEST( HistoryEnable
);
40 CPPUNIT_TEST( HistoryClear
);
41 CPPUNIT_TEST( HistoryList
);
42 CPPUNIT_TEST( Editable
);
43 CPPUNIT_TEST( Selection
);
45 CPPUNIT_TEST( RunScript
);
46 CPPUNIT_TEST( SetPage
);
47 CPPUNIT_TEST_SUITE_END();
60 void LoadUrl(int times
= 1);
63 EventCounter
* m_loaded
;
65 DECLARE_NO_COPY_CLASS(WebTestCase
)
69 #define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1)
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
74 // also include in its own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
77 void WebTestCase::setUp()
79 m_browser
= wxWebView::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
81 m_loaded
= new EventCounter(m_browser
, wxEVT_WEBVIEW_LOADED
);
82 m_browser
->LoadURL("about:blank");
86 void WebTestCase::tearDown()
92 void WebTestCase::LoadUrl(int times
)
94 //We alternate between urls as otherwise webkit merges them in the history
95 //we use about and about blank to avoid the need for a network connection
96 for(int i
= 0; i
< times
; i
++)
99 m_browser
->LoadURL("about:blank");
101 m_browser
->LoadURL("about:");
106 void WebTestCase::Title()
108 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
110 //Test title after loading raw html
111 m_browser
->SetPage("<html><title>Title</title><body>Text</body></html>", "");
113 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
115 //Test title after loading a url, we yield to let events process
117 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
120 void WebTestCase::Url()
122 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
124 //After first loading about:blank the next in the sequence is about:
126 CPPUNIT_ASSERT_EQUAL("about:", m_browser
->GetCurrentURL());
129 void WebTestCase::History()
133 CPPUNIT_ASSERT(m_browser
->CanGoBack());
134 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
139 CPPUNIT_ASSERT(m_browser
->CanGoBack());
140 CPPUNIT_ASSERT(m_browser
->CanGoForward());
147 //We should now be at the start of the history
148 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
149 CPPUNIT_ASSERT(m_browser
->CanGoForward());
152 void WebTestCase::HistoryEnable()
155 m_browser
->EnableHistory(false);
157 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
158 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
162 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
163 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
166 void WebTestCase::HistoryClear()
170 //Now we are in the 'middle' of the history
174 CPPUNIT_ASSERT(m_browser
->CanGoForward());
175 CPPUNIT_ASSERT(m_browser
->CanGoBack());
177 m_browser
->ClearHistory();
179 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
180 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
183 void WebTestCase::HistoryList()
189 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
190 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
192 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
195 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
196 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
199 void WebTestCase::Editable()
201 CPPUNIT_ASSERT(!m_browser
->IsEditable());
203 m_browser
->SetEditable(true);
205 CPPUNIT_ASSERT(m_browser
->IsEditable());
207 m_browser
->SetEditable(false);
209 CPPUNIT_ASSERT(!m_browser
->IsEditable());
212 void WebTestCase::Selection()
214 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
216 CPPUNIT_ASSERT(!m_browser
->HasSelection());
218 m_browser
->SelectAll();
220 CPPUNIT_ASSERT(m_browser
->HasSelection());
221 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
223 // The web engine doesn't necessarily represent the HTML in the same way as
224 // we used above, e.g. IE uses upper case for all the tags while WebKit
225 // under OS X inserts plenty of its own <span> tags, so don't test for
226 // equality and just check that the source contains things we'd expect it
228 const wxString selSource
= m_browser
->GetSelectedSource();
231 ("Unexpected selection source: \"%s\"", selSource
),
232 selSource
.Lower().Matches("*some*<strong*strong</strong>*text*")
235 m_browser
->ClearSelection();
236 CPPUNIT_ASSERT(!m_browser
->HasSelection());
239 void WebTestCase::Zoom()
241 if(m_browser
->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
))
243 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT
);
244 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_LAYOUT
, m_browser
->GetZoomType());
246 m_browser
->SetZoom(wxWEBVIEW_ZOOM_TINY
);
247 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY
, m_browser
->GetZoom());
250 //Reset the zoom level
251 m_browser
->SetZoom(wxWEBVIEW_ZOOM_MEDIUM
);
253 if(m_browser
->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT
))
255 m_browser
->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT
);
256 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_TEXT
, m_browser
->GetZoomType());
258 m_browser
->SetZoom(wxWEBVIEW_ZOOM_TINY
);
259 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY
, m_browser
->GetZoom());
263 void WebTestCase::RunScript()
265 m_browser
->RunScript("document.write(\"Hello World!\");");
266 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser
->GetPageText());
269 void WebTestCase::SetPage()
271 m_browser
->SetPage("<html><body>text</body></html>", "");
273 CPPUNIT_ASSERT_EQUAL("text", m_browser
->GetPageText());
275 m_browser
->SetPage("<html><body>other text</body></html>", "");
277 CPPUNIT_ASSERT_EQUAL("other text", m_browser
->GetPageText());
280 #endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)