Correct a typo in the wxWebView tests.
[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
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_SUITE_END();
48
49 void Title();
50 void Url();
51 void History();
52 void HistoryEnable();
53 void HistoryClear();
54 void HistoryList();
55 void Editable();
56 void Selection();
57 void Zoom();
58 void RunScript();
59 void LoadUrl(int times = 1);
60
61 wxWebView* m_browser;
62
63 DECLARE_NO_COPY_CLASS(WebTestCase)
64 };
65
66 // register in the unnamed registry so that these tests are run by default
67 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
68
69 // also include in its own registry so that these tests can be run alone
70 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
71
72 void WebTestCase::setUp()
73 {
74 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
75 //We yield to let the initial page load
76 wxYield();
77 }
78
79 void WebTestCase::tearDown()
80 {
81 wxDELETE(m_browser);
82 }
83
84 void WebTestCase::LoadUrl(int times)
85 {
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++)
89 {
90 if(i % 2 == 1)
91 m_browser->LoadURL("about:blank");
92 else
93 m_browser->LoadURL("about:");
94 wxYield();
95 }
96 }
97
98 void WebTestCase::Title()
99 {
100 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
101
102 //Test title after loading raw html
103 m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
104 wxYield();
105 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
106
107 //Test title after loading a url, we yield to let events process
108 LoadUrl();
109 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
110 }
111
112 void WebTestCase::Url()
113 {
114 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
115
116 //After first loading about:blank the next in the sequence is about:
117 LoadUrl();
118 CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
119 }
120
121 void WebTestCase::History()
122 {
123 LoadUrl(3);
124
125 CPPUNIT_ASSERT(m_browser->CanGoBack());
126 CPPUNIT_ASSERT(!m_browser->CanGoForward());
127
128 m_browser->GoBack();
129
130 CPPUNIT_ASSERT(m_browser->CanGoBack());
131 CPPUNIT_ASSERT(m_browser->CanGoForward());
132
133 m_browser->GoBack();
134 m_browser->GoBack();
135
136 //We should now be at the start of the history
137 CPPUNIT_ASSERT(!m_browser->CanGoBack());
138 CPPUNIT_ASSERT(m_browser->CanGoForward());
139 }
140
141 void WebTestCase::HistoryEnable()
142 {
143 LoadUrl();
144 m_browser->EnableHistory(false);
145
146 CPPUNIT_ASSERT(!m_browser->CanGoForward());
147 CPPUNIT_ASSERT(!m_browser->CanGoBack());
148
149 LoadUrl();
150
151 CPPUNIT_ASSERT(!m_browser->CanGoForward());
152 CPPUNIT_ASSERT(!m_browser->CanGoBack());
153 }
154
155 void WebTestCase::HistoryClear()
156 {
157 LoadUrl(2);
158
159 //Now we are in the 'middle' of the history
160 m_browser->GoBack();
161 wxYield();
162
163 CPPUNIT_ASSERT(m_browser->CanGoForward());
164 CPPUNIT_ASSERT(m_browser->CanGoBack());
165
166 m_browser->ClearHistory();
167
168 CPPUNIT_ASSERT(!m_browser->CanGoForward());
169 CPPUNIT_ASSERT(!m_browser->CanGoBack());
170 }
171
172 void WebTestCase::HistoryList()
173 {
174 LoadUrl(2);
175 m_browser->GoBack();
176
177 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
178 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
179
180 m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
181
182 CPPUNIT_ASSERT(!m_browser->CanGoForward());
183 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
184 }
185
186 void WebTestCase::Editable()
187 {
188 CPPUNIT_ASSERT(!m_browser->IsEditable());
189
190 m_browser->SetEditable(true);
191
192 CPPUNIT_ASSERT(m_browser->IsEditable());
193
194 m_browser->SetEditable(false);
195
196 CPPUNIT_ASSERT(!m_browser->IsEditable());
197 }
198
199 void WebTestCase::Selection()
200 {
201 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
202 wxYield();
203 CPPUNIT_ASSERT(!m_browser->HasSelection());
204
205 m_browser->SelectAll();
206
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());
212
213 m_browser->ClearSelection();
214 CPPUNIT_ASSERT(!m_browser->HasSelection());
215 }
216
217 void WebTestCase::Zoom()
218 {
219 if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
220 {
221 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
222 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
223
224 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
225 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom());
226 }
227
228 //Reset the zoom level
229 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
230
231 if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT))
232 {
233 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
234 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
235
236 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
237 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom());
238 }
239 }
240
241 void WebTestCase::RunScript()
242 {
243 m_browser->RunScript("document.write(\"Hello World!\");");
244 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
245 }
246
247 #endif //wxUSE_WEBVIEW