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_SUITE_END();
53 void LoadUrl(const wxString
& url
, int times
= 1);
57 DECLARE_NO_COPY_CLASS(WebTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase
, "WebTestCase" );
66 void WebTestCase
::setUp()
68 m_browser
= wxWebView
::New(wxTheApp
->GetTopWindow(), wxID_ANY
);
71 void WebTestCase
::tearDown()
76 void WebTestCase
::LoadUrl(const wxString
& url
, int times
)
78 for(int i
= 0; i
< times
; i
++)
80 m_browser
->LoadUrl(url
);
85 void WebTestCase
::Title()
87 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
89 //Test title after loading raw html
90 m_browser
->SetPage("<html><title>Title</title></html>", "");
91 CPPUNIT_ASSERT_EQUAL("Title", m_browser
->GetCurrentTitle());
93 //Test title after loading a url, we yield to let events process
94 LoadUrl("about:blank");
95 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentTitle());
98 void WebTestCase
::Url()
100 CPPUNIT_ASSERT_EQUAL("", m_browser
->GetCurrentURL());
102 LoadUrl("about:blank");
103 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser
->GetCurrentURL());
106 void WebTestCase
::History()
108 //We use about:blank to remove the need for a network connection
109 LoadUrl("about:blank", 3);
111 CPPUNIT_ASSERT(m_browser
->CanGoBack());
112 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
116 CPPUNIT_ASSERT(m_browser
->CanGoBack());
117 CPPUNIT_ASSERT(m_browser
->CanGoForward());
122 //We should now be at the start of the history
123 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
124 CPPUNIT_ASSERT(m_browser
->CanGoForward());
127 void WebTestCase
::HistoryEnable()
129 LoadUrl("about:blank");
130 m_browser
->EnableHistory(false);
132 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
133 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
135 LoadUrl("about:blank");
137 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
138 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
141 void WebTestCase
::HistoryClear()
143 LoadUrl("about:blank", 2);
145 //Now we are in the 'middle' of the history
148 CPPUNIT_ASSERT(m_browser
->CanGoForward());
149 CPPUNIT_ASSERT(m_browser
->CanGoBack());
151 m_browser
->ClearHistory();
153 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
154 CPPUNIT_ASSERT(!m_browser
->CanGoBack());
157 void WebTestCase
::HistoryList()
159 LoadUrl("about:blank", 2);
162 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetBackwardHistory().size());
163 CPPUNIT_ASSERT_EQUAL(1, m_browser
->GetForwardHistory().size());
165 m_browser
->LoadHistoryItem(m_browser
->GetForwardHistory()[0]);
167 CPPUNIT_ASSERT(!m_browser
->CanGoForward());
168 CPPUNIT_ASSERT_EQUAL(2, m_browser
->GetBackwardHistory().size());
171 void WebTestCase
::Editable()
173 CPPUNIT_ASSERT(!m_browser
->IsEditable());
175 m_browser
->SetEditable(true);
177 CPPUNIT_ASSERT(m_browser
->IsEditable());
179 m_browser
->SetEditable(false);
181 CPPUNIT_ASSERT(!m_browser
->IsEditable());