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( RunScript
);
47 CPPUNIT_TEST_SUITE_END();
59 void LoadUrl(int times
= 1);
63 DECLARE_NO_COPY_CLASS(WebTestCase
)
66 // register in the unnamed registry so that these tests are run by default
67 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
69 // also include in its own registry so that these tests can be run alone
70 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
72 void WebTestCase::setUp()
74 m_browser
= wxWebView::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
75 //We yield to let the initial page load
79 void WebTestCase::tearDown()
84 void WebTestCase::LoadUrl(int times
)
86 //We alternate between urls as otherwise webkit merges them in the history
87 //we use about and about blank to avoid the need for a network connection
88 for(int i
= 0; i
< times
; i
++)
91 m_browser
->LoadUrl("about:blank");
93 m_browser
->LoadUrl("about:");
98 void WebTestCase::Title()
100 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
102 //Test title after loading raw html
103 m_browser
->SetPage("<html><title>Title</title><body>Text</body></html>", "");
105 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
107 //Test title after loading a url, we yield to let events process
109 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
112 void WebTestCase::Url()
114 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
116 //After first loading about:blank the next in the sequence is about:
118 CPPUNIT_ASSERT_EQUAL("about:", m_browser
->GetCurrentURL());
121 void WebTestCase::History()
125 CPPUNIT_ASSERT(m_browser
->CanGoBack());
126 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
130 CPPUNIT_ASSERT(m_browser
->CanGoBack());
131 CPPUNIT_ASSERT(m_browser
->CanGoForward());
136 //We should now be at the start of the history
137 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
138 CPPUNIT_ASSERT(m_browser
->CanGoForward());
141 void WebTestCase::HistoryEnable()
144 m_browser
->EnableHistory(false);
146 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
147 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
151 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
152 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
155 void WebTestCase::HistoryClear()
159 //Now we are in the 'middle' of the history
163 CPPUNIT_ASSERT(m_browser
->CanGoForward());
164 CPPUNIT_ASSERT(m_browser
->CanGoBack());
166 m_browser
->ClearHistory();
168 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
169 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
172 void WebTestCase::HistoryList()
177 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
178 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
180 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
182 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
183 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
186 void WebTestCase::Editable()
188 CPPUNIT_ASSERT(!m_browser
->IsEditable());
190 m_browser
->SetEditable(true);
192 CPPUNIT_ASSERT(m_browser
->IsEditable());
194 m_browser
->SetEditable(false);
196 CPPUNIT_ASSERT(!m_browser
->IsEditable());
199 void WebTestCase::Selection()
201 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
203 CPPUNIT_ASSERT(!m_browser
->HasSelection());
205 m_browser
->SelectAll();
207 CPPUNIT_ASSERT(m_browser
->HasSelection());
208 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
209 //We lower case the result as ie returns tags in uppercase
210 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
211 m_browser
->GetSelectedSource().Lower());
213 m_browser
->ClearSelection();
214 CPPUNIT_ASSERT(!m_browser
->HasSelection());
217 void WebTestCase::Zoom()
219 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
))
221 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
);
222 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT
, m_browser
->GetZoomType());
224 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
225 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
228 //Reset the zoom level
229 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM
);
231 if(m_browser
->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
))
233 m_browser
->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT
);
234 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT
, m_browser
->GetZoomType());
236 m_browser
->SetZoom(wxWEB_VIEW_ZOOM_TINY
);
237 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY
, m_browser
->GetZoom());
241 void WebTestCase::RunScript()
243 m_browser
->RunScript("document.write(\"Hello World!\");");
244 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser
->GetPageText());