1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
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 3 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, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.comic; |
17 | |
18 | import java.awt.Dimension; |
19 | import java.awt.Image; |
20 | import java.awt.Toolkit; |
21 | import java.awt.datatransfer.Clipboard; |
22 | import java.awt.datatransfer.DataFlavor; |
23 | import java.awt.datatransfer.Transferable; |
24 | import java.awt.datatransfer.UnsupportedFlavorException; |
25 | import java.awt.image.RenderedImage; |
26 | import java.io.IOException; |
27 | |
28 | import javax.swing.JScrollPane; |
29 | import javax.swing.TransferHandler; |
30 | |
31 | import junit.extensions.abbot.ComponentTestFixture; |
32 | import net.sf.jomic.jaiunit.Tools; |
33 | import net.sf.jomic.tools.TestTools; |
34 | import org.apache.commons.logging.Log; |
35 | import org.apache.commons.logging.LogFactory; |
36 | |
37 | /** |
38 | * TestCase for ComicView. |
39 | * |
40 | * @author Thomas Aglassinger |
41 | */ |
42 | public class ComicViewTest extends ComponentTestFixture |
43 | { |
44 | private Log logger; |
45 | private TestTools testTools; |
46 | |
47 | protected void setUp() |
48 | throws Exception { |
49 | super.setUp(); |
50 | testTools = TestTools.instance(); |
51 | testTools.setupCache(); |
52 | logger = LogFactory.getLog(ComicViewTest.class); |
53 | } |
54 | |
55 | public void testCopyToClipboard() |
56 | throws IOException, InterruptedException, UnsupportedFlavorException { |
57 | ComicModel model = new ComicModel(testTools.getTestFile(TestTools.SINGLE_PAGE_COMIC)); |
58 | |
59 | try { |
60 | ComicView view = new ComicView(); |
61 | |
62 | try { |
63 | view.setModel(model, null); |
64 | showFrame(view); |
65 | |
66 | ComicViewTransferHandler handler = new ComicViewTransferHandler(); |
67 | Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
68 | |
69 | handler.exportToClipboard(view, systemClipboard, TransferHandler.COPY); |
70 | |
71 | Transferable transferable = systemClipboard.getContents(null); |
72 | |
73 | if (logger.isDebugEnabled()) { |
74 | logger.debug("transferable = " + transferable); |
75 | } |
76 | Image image = (Image) transferable.getTransferData(DataFlavor.imageFlavor); |
77 | |
78 | assertNotNull("image", image); |
79 | assert image instanceof RenderedImage; |
80 | Tools jaiTools = Tools.instance(); |
81 | |
82 | jaiTools.imagesAreEqual(view.getRenderedImage(), (RenderedImage) image); |
83 | } finally { |
84 | view.dispose(); |
85 | } |
86 | } finally { |
87 | model.dispose(); |
88 | } |
89 | } |
90 | |
91 | public void testCreate() |
92 | throws Exception { |
93 | ComicModel model = new ComicModel(testTools.getTestComicFile()); |
94 | |
95 | try { |
96 | ComicView view = new ComicView(); |
97 | |
98 | try { |
99 | view.setTwoPageMode(false); |
100 | view.setModel(model, null); |
101 | } finally { |
102 | view.dispose(); |
103 | } |
104 | } finally { |
105 | model.dispose(); |
106 | } |
107 | } |
108 | |
109 | public void testGetPageText() |
110 | throws IOException, InterruptedException { |
111 | ComicModel model = new ComicModel(testTools.getTestComicFile()); |
112 | |
113 | try { |
114 | boolean doublePageTested = false; |
115 | boolean twoImagesTested = false; |
116 | boolean singlePageAndImageTested = false; |
117 | ComicView view = new ComicView(); |
118 | |
119 | view.setTwoPageMode(false); |
120 | view.setModel(model, null); |
121 | logger.info("start"); |
122 | while (!(view.isLast() && doublePageTested && singlePageAndImageTested && twoImagesTested)) { |
123 | int currentSheetIndex = view.getCurrentSheetIndex(); |
124 | ComicSheet sheet = view.getSheet(currentSheetIndex); |
125 | boolean isLandscapeImage = view.getComicImage(sheet.getLeftImageIndex()).isDoublePage(); |
126 | boolean hasTwoImages = sheet.hasRightImage(); |
127 | |
128 | logger.info("sheet=" + currentSheetIndex + ", isLandscape=" + isLandscapeImage |
129 | + ", hasTwoImages=" + hasTwoImages); |
130 | if (isLandscapeImage) { |
131 | if (!doublePageTested) { |
132 | testGetPageText(view, false, false, true); |
133 | doublePageTested = true; |
134 | } |
135 | } else if (hasTwoImages) { |
136 | if (!twoImagesTested) { |
137 | testGetPageText(view, true, false, false); |
138 | twoImagesTested = true; |
139 | } |
140 | } else { |
141 | if (!singlePageAndImageTested) { |
142 | testGetPageText(view, true, true, true); |
143 | singlePageAndImageTested = true; |
144 | } |
145 | } |
146 | view.setTwoPageMode(true); |
147 | view.goNext(); |
148 | logger.info("isLast=" + view.isLast() + ", dpT=" + doublePageTested |
149 | + ", spT=" + singlePageAndImageTested + ", tiT=" + twoImagesTested); |
150 | } |
151 | |
152 | assertTrue(doublePageTested && singlePageAndImageTested && twoImagesTested); |
153 | } finally { |
154 | model.dispose(); |
155 | } |
156 | } |
157 | |
158 | public void testSinglePageMode() |
159 | throws Exception { |
160 | testPageMode(false); |
161 | } |
162 | |
163 | public void testTwoPageMode() |
164 | throws Exception { |
165 | testPageMode(true); |
166 | } |
167 | |
168 | protected void tearDown() |
169 | throws Exception { |
170 | testTools = null; |
171 | logger = null; |
172 | super.tearDown(); |
173 | } |
174 | |
175 | private void testGetPageText(ComicView view, |
176 | boolean noTwoPageEqualsIndependantOfRightFromLeft, |
177 | boolean twoPageEqualsIndependantOfRightFromLeft, |
178 | boolean twoPageEqualsSinglePage) { |
179 | view.setTwoPageMode(false); |
180 | view.setSwapLeftAndRightImage(false); |
181 | |
182 | String singleModeText = view.getPageText(); |
183 | |
184 | view.setSwapLeftAndRightImage(true); |
185 | |
186 | String singleModeFromRightToLeftText = view.getPageText(); |
187 | |
188 | assertEquals(noTwoPageEqualsIndependantOfRightFromLeft, |
189 | singleModeText.equals(singleModeFromRightToLeftText)); |
190 | view.setTwoPageMode(true); |
191 | view.setSwapLeftAndRightImage(false); |
192 | |
193 | String doubleModeText = view.getPageText(); |
194 | |
195 | assertEquals(twoPageEqualsSinglePage, singleModeText.equals(doubleModeText)); |
196 | view.setSwapLeftAndRightImage(true); |
197 | |
198 | String doubleModeFromRightToLeftText = view.getPageText(); |
199 | |
200 | assertEquals(twoPageEqualsIndependantOfRightFromLeft, |
201 | doubleModeText.equals(doubleModeFromRightToLeftText)); |
202 | } |
203 | |
204 | private void testPageMode(boolean mode) |
205 | throws Exception { |
206 | ComicModel model = new ComicModel(testTools.getTestComicFile()); |
207 | ComicView view = new ComicView(); |
208 | |
209 | try { |
210 | JScrollPane scrollPane = new JScrollPane(view); |
211 | |
212 | showFrame(scrollPane, new Dimension(TestTools.FRAME_WIDTH, TestTools.FRAME_HEIGHT)); |
213 | view.setTwoPageMode(mode); |
214 | view.setModel(model, null); |
215 | while (!view.isLast()) { |
216 | view.goNext(); |
217 | testTools.waitSomeTime(); |
218 | } |
219 | while (!view.isFirst()) { |
220 | view.goPrevious(); |
221 | testTools.waitSomeTime(); |
222 | } |
223 | } finally { |
224 | model.dispose(); |
225 | } |
226 | } |
227 | } |