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(const wxString
& url
, 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(const wxString
& url
, int times
)
84 for(int i
= 0; i
< times
; i
++)
86 m_browser
->LoadUrl(url
);
91 void WebTestCase
::Title()
93 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
95 //Test title after loading raw html
96 m_browser
->SetPage("<html><title>Title</title></html>", "");
97 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
99 //Test title after loading a url, we yield to let events process
100 LoadUrl("about:blank");
101 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
104 void WebTestCase
::Url()
106 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
108 LoadUrl("about:blank");
109 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
112 void WebTestCase
::History()
114 //We use about:blank to remove the need for a network connection
115 LoadUrl("about:blank", 3);
117 CPPUNIT_ASSERT(m_browser
->CanGoBack());
118 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
122 CPPUNIT_ASSERT(m_browser
->CanGoBack());
123 CPPUNIT_ASSERT(m_browser
->CanGoForward());
128 //We should now be at the start of the history
129 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
130 CPPUNIT_ASSERT(m_browser
->CanGoForward());
133 void WebTestCase
::HistoryEnable()
135 LoadUrl("about:blank");
136 m_browser
->EnableHistory(false);
138 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
139 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
141 LoadUrl("about:blank");
143 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
144 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
147 void WebTestCase
::HistoryClear()
149 LoadUrl("about:blank", 2);
151 //Now we are in the 'middle' of the history
154 CPPUNIT_ASSERT(m_browser
->CanGoForward());
155 CPPUNIT_ASSERT(m_browser
->CanGoBack());
157 m_browser
->ClearHistory();
159 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
160 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
163 void WebTestCase
::HistoryList()
165 LoadUrl("about:blank", 2);
168 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
169 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
171 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
173 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
174 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
177 void WebTestCase
::Editable()
179 CPPUNIT_ASSERT(!m_browser
->IsEditable());
181 m_browser
->SetEditable(true);
183 CPPUNIT_ASSERT(m_browser
->IsEditable());
185 m_browser
->SetEditable(false);
187 CPPUNIT_ASSERT(!m_browser
->IsEditable());
190 void WebTestCase
::Selection()
192 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
193 CPPUNIT_ASSERT(!m_browser
->HasSelection());
195 m_browser
->SelectAll();
197 CPPUNIT_ASSERT(m_browser
->HasSelection());
198 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
199 //We lower case the result as ie returns tags in uppercase
200 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
201 m_browser
->GetSelectedSource().Lower());
203 m_browser
->DeleteSelection();
205 CPPUNIT_ASSERT(!m_browser
->HasSelection());
208 void WebTestCase
::Zoom()
210 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_MEDIUM
, m_browser
->GetZoom());
212 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
214 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
215 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
, m_browser
->GetZoomType());
217 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
218 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
221 //Reset the zoom level
222 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
224 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
))
226 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
227 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT
, m_browser
->GetZoomType());
229 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
230 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());