| 1 | // ePDFView - Page 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 "DumbDocument.h" |
|---|
| 20 | #include "DumbMainView.h" |
|---|
| 21 | #include "DumbPageView.h" |
|---|
| 22 | #include "PagePterTest.h" |
|---|
| 23 | |
|---|
| 24 | using namespace ePDFView; |
|---|
| 25 | |
|---|
| 26 | // Register the test suite into the `registry'. |
|---|
| 27 | CPPUNIT_TEST_SUITE_REGISTRATION (PagePterTest); |
|---|
| 28 | |
|---|
| 29 | /// |
|---|
| 30 | /// @brief Sets up the environment for each test. |
|---|
| 31 | /// |
|---|
| 32 | void |
|---|
| 33 | PagePterTest::setUp () |
|---|
| 34 | { |
|---|
| 35 | Config::loadFile (FALSE); |
|---|
| 36 | |
|---|
| 37 | m_Document = new DumbDocument (); |
|---|
| 38 | m_MainPter = new MainPter (m_Document); |
|---|
| 39 | m_MainView = new DumbMainView (m_MainPter); |
|---|
| 40 | // We must set the main view to the presenter or it will try to |
|---|
| 41 | // use it anyway. |
|---|
| 42 | m_MainPter->setView (m_MainView); |
|---|
| 43 | m_PagePter = new PagePter (m_Document); |
|---|
| 44 | m_PagePter->setView (*m_MainView); |
|---|
| 45 | m_PageView = (DumbPageView *)m_MainView->getPageView (); |
|---|
| 46 | |
|---|
| 47 | m_Document->loadFile ("/tmp/test1.pdf", NULL, NULL); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /// |
|---|
| 51 | /// @brief Cleans up after each test. |
|---|
| 52 | /// |
|---|
| 53 | void |
|---|
| 54 | PagePterTest::tearDown () |
|---|
| 55 | { |
|---|
| 56 | // We need to delete this page presenter, because we created it ourselves |
|---|
| 57 | // and the main presenter doesn't know a thing about it. The main |
|---|
| 58 | // presenter will destroy its own page presenter though. |
|---|
| 59 | delete m_PagePter; |
|---|
| 60 | delete m_MainPter; |
|---|
| 61 | |
|---|
| 62 | Config::destroy (); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /// |
|---|
| 66 | /// @brief Tests page's zoom to width |
|---|
| 67 | /// |
|---|
| 68 | void |
|---|
| 69 | PagePterTest::pageZoomWidth () |
|---|
| 70 | { |
|---|
| 71 | CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_Document->getZoom (), 0.0001); |
|---|
| 72 | |
|---|
| 73 | // OK, now try to zoom width. Since rotation is 0 |
|---|
| 74 | // the zoom level should be 75/100 = 0.75 |
|---|
| 75 | gint width; |
|---|
| 76 | gint height; |
|---|
| 77 | m_PageView->getSize (&width, &height); |
|---|
| 78 | m_Document->zoomToWidth (width); |
|---|
| 79 | Config::getConfig ().setZoomToWidth (TRUE); |
|---|
| 80 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.75, m_Document->getZoom (), 0.0001); |
|---|
| 81 | |
|---|
| 82 | // Now change the view size. We do this calling the page presenter's |
|---|
| 83 | // function instead of the view, because the view here don't have anything |
|---|
| 84 | // to do. |
|---|
| 85 | // The new zoom should be 50/100 = 0.5 |
|---|
| 86 | m_PagePter->viewResized (50, 50); |
|---|
| 87 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.5, m_Document->getZoom (), 0.0001); |
|---|
| 88 | |
|---|
| 89 | m_PagePter->viewResized (75, 50); |
|---|
| 90 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.75, m_Document->getZoom (), 0.0001); |
|---|
| 91 | |
|---|
| 92 | // If we desactive the zoom to width, no resizing changes the |
|---|
| 93 | // zoom. |
|---|
| 94 | Config::getConfig ().setZoomToWidth (FALSE); |
|---|
| 95 | m_PagePter->viewResized (50, 50); |
|---|
| 96 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.75, m_Document->getZoom (), 0.0001); |
|---|
| 97 | m_PagePter->viewResized (100, 50); |
|---|
| 98 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.75, m_Document->getZoom (), 0.0001); |
|---|
| 99 | |
|---|
| 100 | // Now rotate and try again. |
|---|
| 101 | m_Document->rotateRight (); |
|---|
| 102 | CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ()); |
|---|
| 103 | // Since rotation is now 90 the zoom level should be 75/250 = 0.3 |
|---|
| 104 | m_Document->zoomToWidth (width); |
|---|
| 105 | Config::getConfig ().setZoomToWidth (TRUE); |
|---|
| 106 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); |
|---|
| 107 | // Resizing. 50/250 = 0.2 |
|---|
| 108 | m_PagePter->viewResized (50, 50); |
|---|
| 109 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.2, m_Document->getZoom (), 0.0001); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /// |
|---|
| 113 | /// @brief Tests page's zoom to fit |
|---|
| 114 | /// |
|---|
| 115 | void |
|---|
| 116 | PagePterTest::pageZoomFit () |
|---|
| 117 | { |
|---|
| 118 | CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_Document->getZoom (), 0.0001); |
|---|
| 119 | |
|---|
| 120 | // OK, now try to zoom to fit. Since rotation is 0 |
|---|
| 121 | // the zoom level should be 50/250 = 0.2 |
|---|
| 122 | gint width; |
|---|
| 123 | gint height; |
|---|
| 124 | m_PageView->getSize (&width, &height); |
|---|
| 125 | m_Document->zoomToFit (width, height); |
|---|
| 126 | Config::getConfig ().setZoomToFit (TRUE); |
|---|
| 127 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.2, m_Document->getZoom (), 0.0001); |
|---|
| 128 | |
|---|
| 129 | // Resizing the view size. 100/250 = 0.4 |
|---|
| 130 | m_PagePter->viewResized (100, 100); |
|---|
| 131 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.4, m_Document->getZoom (), 0.0001); |
|---|
| 132 | m_PagePter->viewResized (100, 50); |
|---|
| 133 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.2, m_Document->getZoom (), 0.0001); |
|---|
| 134 | |
|---|
| 135 | // Desactivating the zoom to fit. |
|---|
| 136 | Config::getConfig ().setZoomToFit (FALSE); |
|---|
| 137 | m_PagePter->viewResized (100, 100); |
|---|
| 138 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.2, m_Document->getZoom (), 0.0001); |
|---|
| 139 | m_PagePter->viewResized (100, 50); |
|---|
| 140 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.2, m_Document->getZoom (), 0.0001); |
|---|
| 141 | |
|---|
| 142 | // Now rotate and try again. |
|---|
| 143 | m_Document->rotateRight (); |
|---|
| 144 | CPPUNIT_ASSERT_EQUAL (90, m_Document->getRotation ()); |
|---|
| 145 | |
|---|
| 146 | // Since rotation is now 90 the zoom level should be 75/250 = 0.3 |
|---|
| 147 | m_Document->zoomToFit (width, height); |
|---|
| 148 | Config::getConfig ().setZoomToFit (TRUE); |
|---|
| 149 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.3, m_Document->getZoom (), 0.0001); |
|---|
| 150 | // 100/250 = 0.4 |
|---|
| 151 | m_PagePter->viewResized (100, 100); |
|---|
| 152 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.4, m_Document->getZoom (), 0.0001); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /// |
|---|
| 156 | /// @brief Test dragging the page with the left mouse button. |
|---|
| 157 | /// |
|---|
| 158 | void |
|---|
| 159 | PagePterTest::pageDrag () |
|---|
| 160 | { |
|---|
| 161 | // Moving the mouse without pressing the first mouse button, doesn't |
|---|
| 162 | // change the current scrolling. |
|---|
| 163 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), |
|---|
| 164 | 0.0001); |
|---|
| 165 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), |
|---|
| 166 | 0.0001); |
|---|
| 167 | m_PagePter->mouseMoved (10, 10); |
|---|
| 168 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), |
|---|
| 169 | 0.0001); |
|---|
| 170 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), |
|---|
| 171 | 0.0001); |
|---|
| 172 | m_PagePter->mouseMoved (15, 20); |
|---|
| 173 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), |
|---|
| 174 | 0.0001); |
|---|
| 175 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), |
|---|
| 176 | 0.0001); |
|---|
| 177 | |
|---|
| 178 | // Now press the first mouse button and move a little the pointer. |
|---|
| 179 | // The presenter should tell the view the difference between the position |
|---|
| 180 | // where the drag started and the current position when the mouse moves. |
|---|
| 181 | m_PagePter->mouseButtonPressed (1, 15, 20); |
|---|
| 182 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getHorizontalScroll (), |
|---|
| 183 | 0.0001); |
|---|
| 184 | CPPUNIT_ASSERT_DOUBLES_EQUAL (0.0, m_PageView->getVerticalScroll (), |
|---|
| 185 | 0.0001); |
|---|
| 186 | m_PagePter->mouseMoved (20, 21); |
|---|
| 187 | CPPUNIT_ASSERT_DOUBLES_EQUAL (5.0, m_PageView->getHorizontalScroll (), |
|---|
| 188 | 0.0001); |
|---|
| 189 | CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_PageView->getVerticalScroll (), |
|---|
| 190 | 0.0001); |
|---|
| 191 | m_PagePter->mouseMoved (16, 26); |
|---|
| 192 | CPPUNIT_ASSERT_DOUBLES_EQUAL (1.0, m_PageView->getHorizontalScroll (), |
|---|
| 193 | 0.0001); |
|---|
| 194 | CPPUNIT_ASSERT_DOUBLES_EQUAL (6.0, m_PageView->getVerticalScroll (), |
|---|
| 195 | 0.0001); |
|---|
| 196 | m_PagePter->mouseMoved (10, 13); |
|---|
| 197 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (), |
|---|
| 198 | 0.0001); |
|---|
| 199 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), |
|---|
| 200 | 0.0001); |
|---|
| 201 | |
|---|
| 202 | // Releasing the first mouse button stops the drag and doesn't change |
|---|
| 203 | // the scroll. |
|---|
| 204 | m_PagePter->mouseButtonReleased (1); |
|---|
| 205 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (), |
|---|
| 206 | 0.0001); |
|---|
| 207 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), |
|---|
| 208 | 0.0001); |
|---|
| 209 | m_PagePter->mouseMoved (20, 21); |
|---|
| 210 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-5.0, m_PageView->getHorizontalScroll (), |
|---|
| 211 | 0.0001); |
|---|
| 212 | CPPUNIT_ASSERT_DOUBLES_EQUAL (-7.0, m_PageView->getVerticalScroll (), |
|---|
| 213 | 0.0001); |
|---|
| 214 | } |
|---|