root/trunk/tests/FindPterTest.cxx

Revision 198, 11.9 kB (checked in by jordi, 2 years ago)

Added the PreferencesPter? and IPreferencesView classes as well as they test suite PreferencesPterTest? and DumbPreferencesView? classes. The first test, that tests that setting the browser command line changes the configuration, works.

Line 
1// ePDFView - Find Presenter Test Fixture.
2// Copyright (C) 2006 Emma's Software.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18#include <epdfview.h>
19#include "DumbDocumentObserver.h"
20#include "DumbFindView.h"
21#include "Utils.h"
22#include "FindPterTest.h"
23
24using namespace ePDFView;
25
26G_LOCK_EXTERN (JobRender);
27
28// Register the test suite into the `registry'.
29CPPUNIT_TEST_SUITE_REGISTRATION (FindPterTest);
30
31///
32/// @brief Sets up the environment for each test.
33///
34void
35FindPterTest::setUp ()
36{
37    m_Document = new PDFDocument ();
38    m_Observer = new DumbDocumentObserver ();
39    m_Document->attach (m_Observer);
40    gchar *testFile = getTestFile ("test1.pdf");
41    m_Document->load (testFile, NULL);
42    while ( !m_Observer->loadFinished () ) { }
43    m_FindPter = new FindPter (m_Document);
44    m_View = new DumbFindView ();
45    m_FindPter->setView (m_View);
46
47    G_LOCK (JobRender);
48    JobRender::m_CanProcessJobs = TRUE;
49    G_UNLOCK (JobRender);
50}
51
52///
53/// @brief Cleans up after each test.
54///
55void
56FindPterTest::tearDown ()
57{
58    G_LOCK (JobRender);
59    JobRender::m_CanProcessJobs = FALSE;
60    IJob::clearQueue ();
61    G_UNLOCK (JobRender);
62
63    // Deleting the FindPter will not delete the DumbFindView.
64    delete m_FindPter;
65    m_Document->detach (m_Observer);
66    delete m_Observer;
67    delete m_Document;
68    delete m_View;
69}
70
71///
72/// @brief Checks the find view's controls sensitivity.
73///
74/// The Find Next and Find Previous buttons will be insensitive while the text
75/// is empty (i.e., nothing to search), but will become sensitive when
76/// there's text to search.
77///
78void
79FindPterTest::viewSensitivity ()
80{
81    CPPUNIT_ASSERT (NULL != m_View->getPresenter ());
82
83    CPPUNIT_ASSERT_EQUAL (0, g_ascii_strcasecmp ("", m_View->getTextToFind ()));
84    CPPUNIT_ASSERT (!m_View->isFindNextSensitive ());
85    CPPUNIT_ASSERT (!m_View->isFindPreviousSensitive ());
86
87    m_View->setTextToFind ("f");
88    while ( m_Observer->isStillSearching () ) { }
89    CPPUNIT_ASSERT (m_View->isFindNextSensitive ());
90    CPPUNIT_ASSERT (m_View->isFindPreviousSensitive ());
91
92    m_View->setTextToFind ("");
93    CPPUNIT_ASSERT (!m_View->isFindNextSensitive ());
94    CPPUNIT_ASSERT (!m_View->isFindPreviousSensitive ());
95}
96
97///
98/// @brief Checks looking for an inexistent text.
99///
100/// If the text to search doesn't exist on the document, then the
101/// find dialog will receive the text "Not found!" and all observers will
102/// receive a notifyFindFinished() event.
103///
104void
105FindPterTest::textDoesNotExist ()
106{
107    m_View->setTextToFind ("AnStupidLongTextThatShouldntAppearInTheDocument");
108    while ( m_Observer->isStillSearching () ) { }
109    const gchar *infoText = m_View->getInformationText ();
110    CPPUNIT_ASSERT (0 == g_ascii_strcasecmp ("No Results Found!", infoText));
111}
112
113///
114/// @brief Tests the Find Next button.
115///
116/// When a search starts it tried to find the next text in the document.
117/// If the user presses the Find Next button, it will try to find the next
118/// found text in the document, searching forward.
119///
120void
121FindPterTest::findNext ()
122{
123    m_View->setTextToFind ("first");   
124    volatile gboolean stillSearching = TRUE;
125    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
126   
127    // The 'first' text appears twice in page 4 and also twice in
128    // page 5. So the first time it will set a rect on page 4.
129    CPPUNIT_ASSERT_EQUAL (4, m_Observer->getCurrentPage ());
130    {
131        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
132        CPPUNIT_ASSERT (NULL != rect);
133        CPPUNIT_ASSERT_DOUBLES_EQUAL (82.0000, rect->getX1 (), 0.0001);
134        CPPUNIT_ASSERT_DOUBLES_EQUAL (137.1100, rect->getY1 (), 0.0001);
135        CPPUNIT_ASSERT_DOUBLES_EQUAL (100.3400, rect->getX2 (), 0.0001);
136        CPPUNIT_ASSERT_DOUBLES_EQUAL (146.1100, rect->getY2 (), 0.0001);
137    }
138   
139    m_Observer->notifyFindStarted ();
140    m_FindPter->findNextActivated ();
141    stillSearching = TRUE;
142    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
143    CPPUNIT_ASSERT_EQUAL (4, m_Observer->getCurrentPage ());
144    {
145        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
146        CPPUNIT_ASSERT (NULL != rect);
147        CPPUNIT_ASSERT_DOUBLES_EQUAL (96.0000, rect->getX1 (), 0.0001);
148        CPPUNIT_ASSERT_DOUBLES_EQUAL (148.1100, rect->getY1 (), 0.0001);
149        CPPUNIT_ASSERT_DOUBLES_EQUAL (114.3400, rect->getX2 (), 0.0001);
150        CPPUNIT_ASSERT_DOUBLES_EQUAL (157.1100, rect->getY2 (), 0.0001);
151    }
152   
153    m_Observer->notifyFindStarted ();
154    m_FindPter->findNextActivated ();
155    stillSearching = TRUE;
156    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
157    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
158    {
159        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
160        CPPUNIT_ASSERT (NULL != rect);
161        CPPUNIT_ASSERT_DOUBLES_EQUAL (200.5943, rect->getX1 (), 0.0001);
162        CPPUNIT_ASSERT_DOUBLES_EQUAL (84.5980, rect->getY1 (), 0.0001);
163        CPPUNIT_ASSERT_DOUBLES_EQUAL (254.5158, rect->getX2 (), 0.0001);
164        CPPUNIT_ASSERT_DOUBLES_EQUAL (107.6147, rect->getY2 (), 0.0001);
165    }
166   
167    m_Observer->notifyFindStarted ();
168    m_FindPter->findNextActivated ();
169    stillSearching = TRUE;
170    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
171    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
172    {
173        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
174        CPPUNIT_ASSERT (NULL != rect);
175        CPPUNIT_ASSERT_DOUBLES_EQUAL (164.1700, rect->getX1 (), 0.0001);
176        CPPUNIT_ASSERT_DOUBLES_EQUAL (121.1020, rect->getY1 (), 0.0001);
177        CPPUNIT_ASSERT_DOUBLES_EQUAL (180.2800, rect->getX2 (), 0.0001);
178        CPPUNIT_ASSERT_DOUBLES_EQUAL (130.1020, rect->getY2 (), 0.0001);
179    }
180   
181    m_Observer->notifyFindStarted ();
182    m_FindPter->findNextActivated ();
183    stillSearching = TRUE;
184    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
185    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
186    {
187        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
188        CPPUNIT_ASSERT (NULL != rect);
189        CPPUNIT_ASSERT_DOUBLES_EQUAL (72.0000, rect->getX1 (), 0.0001);
190        CPPUNIT_ASSERT_DOUBLES_EQUAL (143.1745, rect->getY1 (), 0.0001);
191        CPPUNIT_ASSERT_DOUBLES_EQUAL (116.9349, rect->getX2 (), 0.0001);
192        CPPUNIT_ASSERT_DOUBLES_EQUAL (162.3553, rect->getY2 (), 0.0001);
193    }
194   
195    // Clicking after the last result will start again.
196    m_Observer->notifyFindStarted ();
197    m_FindPter->findNextActivated ();
198    stillSearching = TRUE;
199    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
200    CPPUNIT_ASSERT_EQUAL (4, m_Observer->getCurrentPage ());
201    {
202        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
203        CPPUNIT_ASSERT (NULL != rect);
204        CPPUNIT_ASSERT_DOUBLES_EQUAL (82.0000, rect->getX1 (), 0.0001);
205        CPPUNIT_ASSERT_DOUBLES_EQUAL (137.1100, rect->getY1 (), 0.0001);
206        CPPUNIT_ASSERT_DOUBLES_EQUAL (100.3400, rect->getX2 (), 0.0001);
207        CPPUNIT_ASSERT_DOUBLES_EQUAL (146.1100, rect->getY2 (), 0.0001);
208    }
209}
210
211///
212/// @brief Tests the Find Previous button.
213///
214/// When a search starts it tried to find the next text in the document.
215/// If the user presses the Find Previous button, it will try to find the
216/// previous found text in the document, searching backwards.
217///
218void
219FindPterTest::findPrevious ()
220{   
221    m_View->setTextToFind ("first");
222    volatile gboolean stillSearching = TRUE;
223    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
224   
225    // The 'first' text appears twice in page 4 and also twice in
226    // page 5. So the first time it will set a rect on page 4.
227    CPPUNIT_ASSERT_EQUAL (4, m_Observer->getCurrentPage ());
228    {
229        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
230        CPPUNIT_ASSERT (NULL != rect);
231        CPPUNIT_ASSERT_DOUBLES_EQUAL (82.0000, rect->getX1 (), 0.0001);
232        CPPUNIT_ASSERT_DOUBLES_EQUAL (137.1100, rect->getY1 (), 0.0001);
233        CPPUNIT_ASSERT_DOUBLES_EQUAL (100.3400, rect->getX2 (), 0.0001);
234        CPPUNIT_ASSERT_DOUBLES_EQUAL (146.1100, rect->getY2 (), 0.0001);
235    }
236   
237    m_Observer->notifyFindStarted ();
238    m_FindPter->findPreviousActivated ();
239    stillSearching = TRUE;
240    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
241    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
242    {
243        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
244        CPPUNIT_ASSERT (NULL != rect);
245        CPPUNIT_ASSERT_DOUBLES_EQUAL (72.0000, rect->getX1 (), 0.0001);
246        CPPUNIT_ASSERT_DOUBLES_EQUAL (143.1745, rect->getY1 (), 0.0001);
247        CPPUNIT_ASSERT_DOUBLES_EQUAL (116.9349, rect->getX2 (), 0.0001);
248        CPPUNIT_ASSERT_DOUBLES_EQUAL (162.3553, rect->getY2 (), 0.0001);
249    }
250   
251    m_Observer->notifyFindStarted ();
252    m_FindPter->findPreviousActivated ();
253    stillSearching = TRUE;
254    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
255    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
256    {
257        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
258        CPPUNIT_ASSERT (NULL != rect);
259        CPPUNIT_ASSERT_DOUBLES_EQUAL (164.1700, rect->getX1 (), 0.0001);
260        CPPUNIT_ASSERT_DOUBLES_EQUAL (121.1020, rect->getY1 (), 0.0001);
261        CPPUNIT_ASSERT_DOUBLES_EQUAL (180.2800, rect->getX2 (), 0.0001);
262        CPPUNIT_ASSERT_DOUBLES_EQUAL (130.1020, rect->getY2 (), 0.0001);
263    }
264
265    m_Observer->notifyFindStarted ();
266    m_FindPter->findPreviousActivated ();
267    stillSearching = TRUE;
268    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
269    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
270    {
271        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
272        CPPUNIT_ASSERT (NULL != rect);
273        CPPUNIT_ASSERT_DOUBLES_EQUAL (200.5943, rect->getX1 (), 0.0001);
274        CPPUNIT_ASSERT_DOUBLES_EQUAL (84.5980, rect->getY1 (), 0.0001);
275        CPPUNIT_ASSERT_DOUBLES_EQUAL (254.5158, rect->getX2 (), 0.0001);
276        CPPUNIT_ASSERT_DOUBLES_EQUAL (107.6147, rect->getY2 (), 0.0001);
277    }
278 
279    m_Observer->notifyFindStarted ();
280    m_FindPter->findPreviousActivated ();
281    stillSearching = TRUE;
282    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
283    CPPUNIT_ASSERT_EQUAL (4, m_Observer->getCurrentPage ());
284    {
285        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
286        CPPUNIT_ASSERT (NULL != rect);
287        CPPUNIT_ASSERT_DOUBLES_EQUAL (96.0000, rect->getX1 (), 0.0001);
288        CPPUNIT_ASSERT_DOUBLES_EQUAL (148.1100, rect->getY1 (), 0.0001);
289        CPPUNIT_ASSERT_DOUBLES_EQUAL (114.3400, rect->getX2 (), 0.0001);
290        CPPUNIT_ASSERT_DOUBLES_EQUAL (157.1100, rect->getY2 (), 0.0001);
291    }
292
293    m_Observer->notifyFindStarted ();
294    m_FindPter->findNextActivated ();
295    stillSearching = TRUE;
296    while ( stillSearching ) {stillSearching = m_Observer->isStillSearching ();}
297    CPPUNIT_ASSERT_EQUAL (5, m_Observer->getCurrentPage ());
298    {
299        DocumentRectangle *rect = m_Observer->getFindMatchRect ();
300        CPPUNIT_ASSERT (NULL != rect);
301        CPPUNIT_ASSERT_DOUBLES_EQUAL (200.5943, rect->getX1 (), 0.0001);
302        CPPUNIT_ASSERT_DOUBLES_EQUAL (84.5980, rect->getY1 (), 0.0001);
303        CPPUNIT_ASSERT_DOUBLES_EQUAL (254.5158, rect->getX2 (), 0.0001);
304        CPPUNIT_ASSERT_DOUBLES_EQUAL (107.6147, rect->getY2 (), 0.0001);
305    }
306}
Note: See TracBrowser for help on using the browser.