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