]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/webtest.cpp
fbb1bcd2ca01d141f9298a2c6e98f1511cfd0483
[wxWidgets.git] / tests / controls / webtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/webtest.cpp
3 // Purpose: wxWebView unit test
4 // Author: Steven Lamerton
5 // Created: 2011-07-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2011 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #endif // WX_PRECOMP
21
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
24 #include "wx/webview.h"
25 #include "asserthelper.h"
26
27 class WebTestCase : public CppUnit::TestCase
28 {
29 public:
30 WebTestCase() { }
31
32 void setUp();
33 void tearDown();
34
35 private:
36 CPPUNIT_TEST_SUITE( WebTestCase );
37 CPPUNIT_TEST( Title );
38 CPPUNIT_TEST( Url );
39 CPPUNIT_TEST( History );
40 CPPUNIT_TEST( HistoryEnable );
41 CPPUNIT_TEST( HistoryClear );
42 CPPUNIT_TEST( HistoryList );
43 CPPUNIT_TEST( Editable );
44 CPPUNIT_TEST( Selection );
45 CPPUNIT_TEST( Zoom );
46 CPPUNIT_TEST( RunScript );
47 CPPUNIT_TEST( SetPage );
48 CPPUNIT_TEST_SUITE_END();
49
50 void Title();
51 void Url();
52 void History();
53 void HistoryEnable();
54 void HistoryClear();
55 void HistoryList();
56 void Editable();
57 void Selection();
58 void Zoom();
59 void RunScript();
60 void SetPage();
61 void LoadUrl(int times = 1);
62
63 wxWebView* m_browser;
64 EventCounter* m_loaded;
65
66 DECLARE_NO_COPY_CLASS(WebTestCase)
67 };
68
69 //Convenience macro
70 #define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1)
71
72 // register in the unnamed registry so that these tests are run by default
73 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
74
75 // also include in its own registry so that these tests can be run alone
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
77
78 void WebTestCase::setUp()
79 {
80 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
81
82 m_loaded = new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED);
83 m_browser->LoadURL("about:blank");
84 ENSURE_LOADED;
85 }
86
87 void WebTestCase::tearDown()
88 {
89 wxDELETE(m_loaded);
90 wxDELETE(m_browser);
91 }
92
93 void WebTestCase::LoadUrl(int times)
94 {
95 //We alternate between urls as otherwise webkit merges them in the history
96 //we use about and about blank to avoid the need for a network connection
97 for(int i = 0; i < times; i++)
98 {
99 if(i % 2 == 1)
100 m_browser->LoadURL("about:blank");
101 else
102 m_browser->LoadURL("about:");
103 ENSURE_LOADED;
104 }
105 }
106
107 void WebTestCase::Title()
108 {
109 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
110
111 //Test title after loading raw html
112 m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
113 ENSURE_LOADED;
114 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
115
116 //Test title after loading a url, we yield to let events process
117 LoadUrl();
118 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
119 }
120
121 void WebTestCase::Url()
122 {
123 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
124
125 //After first loading about:blank the next in the sequence is about:
126 LoadUrl();
127 CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
128 }
129
130 void WebTestCase::History()
131 {
132 LoadUrl(3);
133
134 CPPUNIT_ASSERT(m_browser->CanGoBack());
135 CPPUNIT_ASSERT(!m_browser->CanGoForward());
136
137 m_browser->GoBack();
138 ENSURE_LOADED;
139
140 CPPUNIT_ASSERT(m_browser->CanGoBack());
141 CPPUNIT_ASSERT(m_browser->CanGoForward());
142
143 m_browser->GoBack();
144 ENSURE_LOADED;
145 m_browser->GoBack();
146 ENSURE_LOADED;
147
148 //We should now be at the start of the history
149 CPPUNIT_ASSERT(!m_browser->CanGoBack());
150 CPPUNIT_ASSERT(m_browser->CanGoForward());
151 }
152
153 void WebTestCase::HistoryEnable()
154 {
155 LoadUrl();
156 m_browser->EnableHistory(false);
157
158 CPPUNIT_ASSERT(!m_browser->CanGoForward());
159 CPPUNIT_ASSERT(!m_browser->CanGoBack());
160
161 LoadUrl();
162
163 CPPUNIT_ASSERT(!m_browser->CanGoForward());
164 CPPUNIT_ASSERT(!m_browser->CanGoBack());
165 }
166
167 void WebTestCase::HistoryClear()
168 {
169 LoadUrl(2);
170
171 //Now we are in the 'middle' of the history
172 m_browser->GoBack();
173 ENSURE_LOADED;
174
175 CPPUNIT_ASSERT(m_browser->CanGoForward());
176 CPPUNIT_ASSERT(m_browser->CanGoBack());
177
178 m_browser->ClearHistory();
179
180 CPPUNIT_ASSERT(!m_browser->CanGoForward());
181 CPPUNIT_ASSERT(!m_browser->CanGoBack());
182 }
183
184 void WebTestCase::HistoryList()
185 {
186 LoadUrl(2);
187 m_browser->GoBack();
188 ENSURE_LOADED;
189
190 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
191 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
192
193 m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
194 ENSURE_LOADED;
195
196 CPPUNIT_ASSERT(!m_browser->CanGoForward());
197 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
198 }
199
200 void WebTestCase::Editable()
201 {
202 CPPUNIT_ASSERT(!m_browser->IsEditable());
203
204 m_browser->SetEditable(true);
205
206 CPPUNIT_ASSERT(m_browser->IsEditable());
207
208 m_browser->SetEditable(false);
209
210 CPPUNIT_ASSERT(!m_browser->IsEditable());
211 }
212
213 void WebTestCase::Selection()
214 {
215 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
216 ENSURE_LOADED;
217 CPPUNIT_ASSERT(!m_browser->HasSelection());
218
219 m_browser->SelectAll();
220
221 CPPUNIT_ASSERT(m_browser->HasSelection());
222 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
223
224 // The web engine doesn't necessarily represent the HTML in the same way as
225 // we used above, e.g. IE uses upper case for all the tags while WebKit
226 // under OS X inserts plenty of its own <span> tags, so don't test for
227 // equality and just check that the source contains things we'd expect it
228 // to.
229 const wxString selSource = m_browser->GetSelectedSource();
230 WX_ASSERT_MESSAGE
231 (
232 ("Unexpected selection source: \"%s\"", selSource),
233 selSource.Lower().Matches("*some*<strong*strong</strong>*text*")
234 );
235
236 m_browser->ClearSelection();
237 CPPUNIT_ASSERT(!m_browser->HasSelection());
238 }
239
240 void WebTestCase::Zoom()
241 {
242 if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
243 {
244 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
245 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
246
247 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
248 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
249 }
250
251 //Reset the zoom level
252 m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
253
254 if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT))
255 {
256 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
257 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
258
259 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
260 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
261 }
262 }
263
264 void WebTestCase::RunScript()
265 {
266 m_browser->RunScript("document.write(\"Hello World!\");");
267 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
268 }
269
270 void WebTestCase::SetPage()
271 {
272 m_browser->SetPage("<html><body>text</body></html>", "");
273 ENSURE_LOADED;
274 CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
275
276 m_browser->SetPage("<html><body>other text</body></html>", "");
277 ENSURE_LOADED;
278 CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
279 }
280
281 #endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)