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
);
45 CPPUNIT_TEST_SUITE_END();
55 void LoadUrl(const wxString
& url
, int times
= 1);
59 DECLARE_NO_COPY_CLASS(WebTestCase
)
62 // register in the unnamed registry so that these tests are run by default
63 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
65 // also include in its own registry so that these tests can be run alone
66 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
68 void WebTestCase
::setUp()
70 m_browser
= wxWebView
::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
71 //We yield to let the initial page load
75 void WebTestCase
::tearDown()
80 void WebTestCase
::LoadUrl(const wxString
& url
, int times
)
82 for(int i
= 0; i
< times
; i
++)
84 m_browser
->LoadUrl(url
);
89 void WebTestCase
::Title()
91 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
93 //Test title after loading raw html
94 m_browser
->SetPage("<html><title>Title</title></html>", "");
95 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
97 //Test title after loading a url, we yield to let events process
98 LoadUrl("about:blank");
99 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
102 void WebTestCase
::Url()
104 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
106 LoadUrl("about:blank");
107 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
110 void WebTestCase
::History()
112 //We use about:blank to remove the need for a network connection
113 LoadUrl("about:blank", 3);
115 CPPUNIT_ASSERT(m_browser
->CanGoBack());
116 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
120 CPPUNIT_ASSERT(m_browser
->CanGoBack());
121 CPPUNIT_ASSERT(m_browser
->CanGoForward());
126 //We should now be at the start of the history
127 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
128 CPPUNIT_ASSERT(m_browser
->CanGoForward());
131 void WebTestCase
::HistoryEnable()
133 LoadUrl("about:blank");
134 m_browser
->EnableHistory(false);
136 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
137 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
139 LoadUrl("about:blank");
141 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
142 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
145 void WebTestCase
::HistoryClear()
147 LoadUrl("about:blank", 2);
149 //Now we are in the 'middle' of the history
152 CPPUNIT_ASSERT(m_browser
->CanGoForward());
153 CPPUNIT_ASSERT(m_browser
->CanGoBack());
155 m_browser
->ClearHistory();
157 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
158 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
161 void WebTestCase
::HistoryList()
163 LoadUrl("about:blank", 2);
166 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
167 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
169 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
171 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
172 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
175 void WebTestCase
::Editable()
177 CPPUNIT_ASSERT(!m_browser
->IsEditable());
179 m_browser
->SetEditable(true);
181 CPPUNIT_ASSERT(m_browser
->IsEditable());
183 m_browser
->SetEditable(false);
185 CPPUNIT_ASSERT(!m_browser
->IsEditable());
188 void WebTestCase
::Selection()
190 m_browser
->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
191 CPPUNIT_ASSERT(!m_browser
->HasSelection());
193 m_browser
->SelectAll();
195 CPPUNIT_ASSERT(m_browser
->HasSelection());
196 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser
->GetSelectedText());
197 //We lower case the result as ie returns tags in uppercase
198 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
199 m_browser
->GetSelectedSource().Lower());
201 m_browser
->DeleteSelection();
203 CPPUNIT_ASSERT(!m_browser
->HasSelection());