Add unit tests for history clearing and enabling / disabling.
[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_SUITE_END();
43
44 void Title();
45 void Url();
46 void History();
47 void HistoryEnable();
48 void HistoryClear();
49
50 wxWebView* m_browser;
51
52 DECLARE_NO_COPY_CLASS(WebTestCase)
53 };
54
55 // register in the unnamed registry so that these tests are run by default
56 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
57
58 // also include in its own registry so that these tests can be run alone
59 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
60
61 void WebTestCase::setUp()
62 {
63 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
64 }
65
66 void WebTestCase::tearDown()
67 {
68 wxDELETE(m_browser);
69 }
70
71 void WebTestCase::Title()
72 {
73 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
74
75 //Test title after loading raw html
76 m_browser->SetPage("<html><title>Title</title></html>", "");
77 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
78
79 //Test title after loading a url, we yield to let events process
80 m_browser->LoadUrl("about:blank");
81 wxYield();
82 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
83 }
84
85 void WebTestCase::Url()
86 {
87 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentURL());
88
89 m_browser->LoadUrl("about:blank");
90 wxYield();
91 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
92 }
93
94 void WebTestCase::History()
95 {
96 //We use about:blank to remove the need for a network connection
97 m_browser->LoadUrl("about:blank");
98 wxYield();
99
100 m_browser->LoadUrl("about:blank");
101 wxYield();
102
103 m_browser->LoadUrl("about:blank");
104 wxYield();
105
106 CPPUNIT_ASSERT(m_browser->CanGoBack());
107 CPPUNIT_ASSERT(!m_browser->CanGoForward());
108
109 m_browser->GoBack();
110
111 CPPUNIT_ASSERT(m_browser->CanGoBack());
112 CPPUNIT_ASSERT(m_browser->CanGoForward());
113
114 m_browser->GoBack();
115 m_browser->GoBack();
116
117 //We should now be at the start of the history
118 CPPUNIT_ASSERT(!m_browser->CanGoBack());
119 CPPUNIT_ASSERT(m_browser->CanGoForward());
120 }
121
122 void WebTestCase::HistoryEnable()
123 {
124 m_browser->LoadUrl("about:blank");
125 wxYield();
126
127 m_browser->EnableHistory(false);
128
129 CPPUNIT_ASSERT(!m_browser->CanGoForward());
130 CPPUNIT_ASSERT(!m_browser->CanGoBack());
131
132 m_browser->LoadUrl("about:blank");
133 wxYield();
134
135 CPPUNIT_ASSERT(!m_browser->CanGoForward());
136 CPPUNIT_ASSERT(!m_browser->CanGoBack());
137 }
138
139 void WebTestCase::HistoryClear()
140 {
141 m_browser->LoadUrl("about:blank");
142 wxYield();
143
144 m_browser->LoadUrl("about:blank");
145 wxYield();
146
147 //Now we are in the 'middle' of the history
148 m_browser->GoBack();
149
150 CPPUNIT_ASSERT(m_browser->CanGoForward());
151 CPPUNIT_ASSERT(m_browser->CanGoBack());
152
153 m_browser->ClearHistory();
154
155 CPPUNIT_ASSERT(!m_browser->CanGoForward());
156 CPPUNIT_ASSERT(!m_browser->CanGoBack());
157 }
158
159 #endif //wxUSE_WEB