1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/webtest.cpp
3 // Purpose: wxWebView unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2011 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
12 #if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
24 #include "wx/webview.h"
25 #include "asserthelper.h"
27 class WebTestCase
: public CppUnit::TestCase
36 CPPUNIT_TEST_SUITE( WebTestCase
);
37 CPPUNIT_TEST( Title
);
39 CPPUNIT_TEST( History
);
40 CPPUNIT_TEST( HistoryEnable
);
41 CPPUNIT_TEST( HistoryClear
);
42 CPPUNIT_TEST( HistoryList
);
43 CPPUNIT_TEST( Editable
);
44 CPPUNIT_TEST( Selection
);
46 CPPUNIT_TEST( RunScript
);
47 CPPUNIT_TEST( SetPage
);
48 CPPUNIT_TEST_SUITE_END();
61 void LoadUrl(int times
= 1);
65 DECLARE_NO_COPY_CLASS(WebTestCase
)
68 // register in the unnamed registry so that these tests are run by default
69 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
71 // also include in its own registry so that these tests can be run alone
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
74 void WebTestCase::setUp()
76 m_browser
= wxWebView::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
77 //We yield to let the initial page load
81 void WebTestCase::tearDown()
86 void WebTestCase::LoadUrl(int times
)
88 //We alternate between urls as otherwise webkit merges them in the history
89 //we use about and about blank to avoid the need for a network connection
90 for(int i
= 0; i
< times
; i
++)
93 m_browser
->LoadURL("about:blank");
95 m_browser
->LoadURL("about:");
100 void WebTestCase::Title()
102 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
104 //Test title after loading raw html
105 m_browser
->SetPage("<html><title>Title</title><body>Text</body></html>", "");
107 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
109 //Test title after loading a url, we yield to let events process
111 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
114 void WebTestCase::Url()
116 // FIXME: This test fails on MSW buildbot slaves although works fine on
117 // development machine.
118 if ( wxGetUserId().Lower().Matches("buildslave*") )
121 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
123 //After first loading about:blank the next in the sequence is about:
125 CPPUNIT_ASSERT_EQUAL("about:", m_browser
->GetCurrentURL());
128 void WebTestCase::History()
130 // FIXME: This test fails on MSW buildbot slaves although works fine on
131 // development machine.
132 if ( wxGetUserId().Lower().Matches("buildslave*") )
137 CPPUNIT_ASSERT(m_browser
->CanGoBack());
138 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
143 CPPUNIT_ASSERT(m_browser
->CanGoBack());
144 CPPUNIT_ASSERT(m_browser
->CanGoForward());
151 //We should now be at the start of the history
152 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
153 CPPUNIT_ASSERT(m_browser
->CanGoForward());
156 void WebTestCase::HistoryEnable()
159 m_browser
->EnableHistory(false);
161 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
162 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
166 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
167 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
170 void WebTestCase::HistoryClear()
172 // FIXME: This test fails on MSW buildbot slaves although works fine on
173 // development machine.
174 if ( wxGetUserId().Lower().Matches("buildslave*") )
179 //Now we are in the 'middle' of the history
183 CPPUNIT_ASSERT(m_browser
->CanGoForward());
184 CPPUNIT_ASSERT(m_browser
->CanGoBack());
186 m_browser
->ClearHistory();
188 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
189 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
192 void WebTestCase::HistoryList()
194 // FIXME: This test fails on MSW buildbot slaves although works fine on
195 // development machine.
196 if ( wxGetUserId().Lower().Matches("buildslave*") )
203 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
204 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
206 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
209 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
210 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
213 void WebTestCase::Editable()
215 CPPUNIT_ASSERT(!m_browser
->IsEditable());
217 m_browser
->SetEditable(true);
219 CPPUNIT_ASSERT(m_browser
->IsEditable());
221 m_browser
->SetEditable(false);
223 CPPUNIT_ASSERT(!m_browser
->IsEditable());
226 void WebTestCase::Selection()
228 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
230 CPPUNIT_ASSERT(!m_browser
->HasSelection());
232 m_browser
->SelectAll();
234 CPPUNIT_ASSERT(m_browser
->HasSelection());
235 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
236 //We lower case the result as ie returns tags in uppercase
237 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
238 m_browser
->GetSelectedSource().Lower());
240 m_browser
->ClearSelection();
241 CPPUNIT_ASSERT(!m_browser
->HasSelection());
244 void WebTestCase::Zoom()
246 // FIXME: This test fails on MSW buildbot slaves although works fine on
247 // development machine.
248 if ( wxGetUserId().Lower().Matches("buildslave*") )
251 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
253 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
254 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
, m_browser
->GetZoomType());
256 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
257 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
260 //Reset the zoom level
261 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
263 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
))
265 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
266 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT
, m_browser
->GetZoomType());
268 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
269 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
273 void WebTestCase::RunScript()
275 m_browser
->RunScript("document.write(\"Hello World!\");");
276 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser
->GetPageText());
279 void WebTestCase::SetPage()
281 m_browser
->SetPage("<html><body>text</body></html>", "");
282 CPPUNIT_ASSERT_EQUAL("text", m_browser
->GetPageText());
284 m_browser
->SetPage("<html><body>other text</body></html>", "");
285 CPPUNIT_ASSERT_EQUAL("other text", m_browser
->GetPageText());
288 #endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)