]> git.saurik.com Git - wxWidgets.git/blame - tests/controls/webtest.cpp
deal with Cocoa as we do with Carbon, see #15008
[wxWidgets.git] / tests / controls / webtest.cpp
CommitLineData
2dad08ec
SL
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
30e2c046 12#if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
2dad08ec
SL
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
27class WebTestCase : public CppUnit::TestCase
28{
29public:
30 WebTestCase() { }
31
32 void setUp();
33 void tearDown();
34
35private:
36 CPPUNIT_TEST_SUITE( WebTestCase );
6a2ef29f 37 CPPUNIT_TEST( Title );
d07fd8b0
SL
38 CPPUNIT_TEST( Url );
39 CPPUNIT_TEST( History );
f152b4b9
SL
40 CPPUNIT_TEST( HistoryEnable );
41 CPPUNIT_TEST( HistoryClear );
1a693ec8 42 CPPUNIT_TEST( HistoryList );
c7cbe308 43 CPPUNIT_TEST( Editable );
c9355a3d 44 CPPUNIT_TEST( Selection );
18cf6bb5 45 CPPUNIT_TEST( Zoom );
c9ccc09c 46 CPPUNIT_TEST( RunScript );
7f98bdd6 47 CPPUNIT_TEST( SetPage );
2dad08ec
SL
48 CPPUNIT_TEST_SUITE_END();
49
6a2ef29f 50 void Title();
d07fd8b0
SL
51 void Url();
52 void History();
f152b4b9
SL
53 void HistoryEnable();
54 void HistoryClear();
1a693ec8 55 void HistoryList();
c7cbe308 56 void Editable();
c9355a3d 57 void Selection();
18cf6bb5 58 void Zoom();
c9ccc09c 59 void RunScript();
7f98bdd6 60 void SetPage();
0609769d 61 void LoadUrl(int times = 1);
6a2ef29f 62
2dad08ec 63 wxWebView* m_browser;
1e0b0d7c 64 EventCounter* m_loaded;
2dad08ec
SL
65
66 DECLARE_NO_COPY_CLASS(WebTestCase)
67};
68
1e0b0d7c
SL
69//Convenience macro
70#define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1)
71
2dad08ec
SL
72// register in the unnamed registry so that these tests are run by default
73CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
74
75// also include in its own registry so that these tests can be run alone
76CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
77
78void WebTestCase::setUp()
79{
6a2ef29f 80 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
1e0b0d7c 81
4776f0c8 82 m_loaded = new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED);
0cd8ce0d 83 m_browser->LoadURL("about:blank");
1e0b0d7c 84 ENSURE_LOADED;
2dad08ec
SL
85}
86
87void WebTestCase::tearDown()
88{
1e0b0d7c 89 wxDELETE(m_loaded);
2dad08ec
SL
90 wxDELETE(m_browser);
91}
92
0609769d 93void WebTestCase::LoadUrl(int times)
3ce14be7 94{
0609769d
SL
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
3ce14be7
SL
97 for(int i = 0; i < times; i++)
98 {
0609769d 99 if(i % 2 == 1)
6edb3912 100 m_browser->LoadURL("about:blank");
0609769d 101 else
6edb3912 102 m_browser->LoadURL("about:");
1e0b0d7c 103 ENSURE_LOADED;
3ce14be7
SL
104 }
105}
106
6a2ef29f
SL
107void WebTestCase::Title()
108{
109 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
110
111 //Test title after loading raw html
0609769d 112 m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
1e0b0d7c 113 ENSURE_LOADED;
6a2ef29f
SL
114 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
115
116 //Test title after loading a url, we yield to let events process
0609769d 117 LoadUrl();
6a2ef29f
SL
118 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
119}
120
d07fd8b0
SL
121void WebTestCase::Url()
122{
d563e28f 123 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
d07fd8b0 124
0609769d
SL
125 //After first loading about:blank the next in the sequence is about:
126 LoadUrl();
127 CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
d07fd8b0
SL
128}
129
130void WebTestCase::History()
131{
0609769d 132 LoadUrl(3);
d07fd8b0
SL
133
134 CPPUNIT_ASSERT(m_browser->CanGoBack());
135 CPPUNIT_ASSERT(!m_browser->CanGoForward());
136
137 m_browser->GoBack();
1e0b0d7c 138 ENSURE_LOADED;
d07fd8b0
SL
139
140 CPPUNIT_ASSERT(m_browser->CanGoBack());
141 CPPUNIT_ASSERT(m_browser->CanGoForward());
142
143 m_browser->GoBack();
1e0b0d7c 144 ENSURE_LOADED;
d07fd8b0 145 m_browser->GoBack();
1e0b0d7c 146 ENSURE_LOADED;
d07fd8b0
SL
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
f152b4b9
SL
153void WebTestCase::HistoryEnable()
154{
0609769d 155 LoadUrl();
f152b4b9
SL
156 m_browser->EnableHistory(false);
157
158 CPPUNIT_ASSERT(!m_browser->CanGoForward());
159 CPPUNIT_ASSERT(!m_browser->CanGoBack());
160
0609769d 161 LoadUrl();
f152b4b9
SL
162
163 CPPUNIT_ASSERT(!m_browser->CanGoForward());
164 CPPUNIT_ASSERT(!m_browser->CanGoBack());
165}
166
167void WebTestCase::HistoryClear()
168{
0609769d 169 LoadUrl(2);
f152b4b9
SL
170
171 //Now we are in the 'middle' of the history
172 m_browser->GoBack();
1e0b0d7c 173 ENSURE_LOADED;
f152b4b9
SL
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
1a693ec8
SL
184void WebTestCase::HistoryList()
185{
0609769d 186 LoadUrl(2);
1a693ec8 187 m_browser->GoBack();
1e0b0d7c 188 ENSURE_LOADED;
1a693ec8
SL
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]);
1e0b0d7c 194 ENSURE_LOADED;
1a693ec8
SL
195
196 CPPUNIT_ASSERT(!m_browser->CanGoForward());
197 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
198}
199
c7cbe308
SL
200void 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
c9355a3d
SL
213void WebTestCase::Selection()
214{
a9c15339 215 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
1e0b0d7c 216 ENSURE_LOADED;
c9355a3d
SL
217 CPPUNIT_ASSERT(!m_browser->HasSelection());
218
219 m_browser->SelectAll();
220
221 CPPUNIT_ASSERT(m_browser->HasSelection());
a9c15339
SL
222 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
223 //We lower case the result as ie returns tags in uppercase
1e0b0d7c 224 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
526292e7 225 m_browser->GetSelectedSource().Lower());
41933aa5
SL
226
227 m_browser->ClearSelection();
1e0b0d7c 228 CPPUNIT_ASSERT(!m_browser->HasSelection());
c9355a3d
SL
229}
230
18cf6bb5
SL
231void WebTestCase::Zoom()
232{
236cff73 233 if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
18cf6bb5 234 {
236cff73
SL
235 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
236 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
18cf6bb5 237
236cff73
SL
238 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
239 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
18cf6bb5
SL
240 }
241
242 //Reset the zoom level
236cff73 243 m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
18cf6bb5 244
236cff73 245 if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT))
18cf6bb5 246 {
236cff73
SL
247 m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
248 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
18cf6bb5 249
236cff73
SL
250 m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
251 CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
18cf6bb5
SL
252 }
253}
254
c9ccc09c
SL
255void WebTestCase::RunScript()
256{
257 m_browser->RunScript("document.write(\"Hello World!\");");
258 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
259}
260
7f98bdd6
SL
261void WebTestCase::SetPage()
262{
263 m_browser->SetPage("<html><body>text</body></html>", "");
1e0b0d7c 264 ENSURE_LOADED;
7f98bdd6
SL
265 CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
266
267 m_browser->SetPage("<html><body>other text</body></html>", "");
1e0b0d7c 268 ENSURE_LOADED;
7f98bdd6
SL
269 CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
270}
271
30e2c046 272#endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)