1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/webtest.cpp
3 // Purpose: wxWebView unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2011 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
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_SUITE_END();
57 void LoadUrl(int times
= 1);
61 DECLARE_NO_COPY_CLASS(WebTestCase
)
64 // register in the unnamed registry so that these tests are run by default
65 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
67 // also include in its own registry so that these tests can be run alone
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
70 void WebTestCase
::setUp()
72 m_browser
= wxWebView
::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
73 //We yield to let the initial page load
77 void WebTestCase
::tearDown()
82 void WebTestCase
::LoadUrl(int times
)
84 //We alternate between urls as otherwise webkit merges them in the history
85 //we use about and about blank to avoid the need for a network connection
86 for(int i
= 0; i
< times
; i
++)
89 m_browser
->LoadUrl("about:blank");
91 m_browser
->LoadUrl("about:");
96 void WebTestCase
::Title()
98 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
100 //Test title after loading raw html
101 m_browser
->SetPage("<html><title>Title</title><body>Text</body></html>", "");
103 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
105 //Test title after loading a url, we yield to let events process
107 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
110 void WebTestCase
::Url()
112 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
114 //After first loading about:blank the next in the sequence is about:
116 CPPUNIT_ASSERT_EQUAL("about:", m_browser
->GetCurrentURL());
119 void WebTestCase
::History()
123 CPPUNIT_ASSERT(m_browser
->CanGoBack());
124 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
128 CPPUNIT_ASSERT(m_browser
->CanGoBack());
129 CPPUNIT_ASSERT(m_browser
->CanGoForward());
134 //We should now be at the start of the history
135 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
136 CPPUNIT_ASSERT(m_browser
->CanGoForward());
139 void WebTestCase
::HistoryEnable()
142 m_browser
->EnableHistory(false);
144 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
145 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
149 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
150 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
153 void WebTestCase
::HistoryClear()
157 //Now we are in the 'middle' of the history
161 CPPUNIT_ASSERT(m_browser
->CanGoForward());
162 CPPUNIT_ASSERT(m_browser
->CanGoBack());
164 m_browser
->ClearHistory();
166 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
167 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
170 void WebTestCase
::HistoryList()
175 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
176 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
178 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
180 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
181 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
184 void WebTestCase
::Editable()
186 CPPUNIT_ASSERT(!m_browser
->IsEditable());
188 m_browser
->SetEditable(true);
190 CPPUNIT_ASSERT(m_browser
->IsEditable());
192 m_browser
->SetEditable(false);
194 CPPUNIT_ASSERT(!m_browser
->IsEditable());
197 void WebTestCase
::Selection()
199 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
201 CPPUNIT_ASSERT(!m_browser
->HasSelection());
203 m_browser
->SelectAll();
205 CPPUNIT_ASSERT(m_browser
->HasSelection());
206 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
207 //We lower case the result as ie returns tags in uppercase
208 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
209 m_browser
->GetSelectedSource().Lower());
211 m_browser
->ClearSelection();
212 CPPUNIT_ASSERT(!m_browser
->HasSelection());
215 void WebTestCase
::Zoom()
217 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
219 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
220 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
, m_browser
->GetZoomType());
222 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
223 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
226 //Reset the zoom level
227 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
229 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
))
231 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
232 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT
, m_browser
->GetZoomType());
234 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
235 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());