xref: /aoo41x/main/slideshow/test/slidetest.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <testshl/simpleheader.hxx>
29 #include <cppuhelper/compbase1.hxx>
30 #include <comphelper/broadcasthelper.hxx>
31 
32 #include <basegfx/matrix/b2dhommatrix.hxx>
33 #include <basegfx/range/b2drectangle.hxx>
34 #include <cppcanvas/spritecanvas.hxx>
35 
36 #include "view.hxx"
37 #include "unoview.hxx"
38 #include "unoviewcontainer.hxx"
39 #include "shape.hxx"
40 #include "tests.hxx"
41 #include "../engine/slide/layermanager.hxx"
42 #include "../engine/slide/layer.hxx"
43 #include "com/sun/star/presentation/XSlideShowView.hpp"
44 
45 namespace target = slideshow::internal;
46 using namespace ::com::sun::star;
47 
48 namespace
49 {
50 
51 class LayerManagerTest : public CppUnit::TestFixture
52 {
53     target::UnoViewContainer      maViews;
54     target::LayerManagerSharedPtr mpLayerManager;
55     TestViewSharedPtr             mpTestView;
56     TestShapeSharedPtr            mpTestShape;
57 
58 public:
59     void setUp()
60     {
61         mpTestShape = createTestShape(
62             basegfx::B2DRange(0.0,0.0,10.0,10.0),
63             1.0);
64         mpTestView = createTestView();
65         maViews.addView( mpTestView );
66 
67         mpLayerManager.reset(
68             new target::LayerManager(
69                 maViews,
70                 basegfx::B2DRange(0.0,0.0,100.0,100.0),
71                 false ));
72     }
73 
74     void tearDown()
75     {
76         mpLayerManager.reset();
77         maViews.dispose();
78     }
79 
80     void testLayer()
81     {
82         target::LayerSharedPtr pBgLayer(
83             target::Layer::createBackgroundLayer( basegfx::B2DRange(0,0,100,100) ) );
84         pBgLayer->addView( mpTestView );
85 
86         target::LayerSharedPtr pFgLayer(
87             target::Layer::createLayer( basegfx::B2DRange(0,0,100,100) ) );
88         pFgLayer->addView( mpTestView );
89 
90         CPPUNIT_ASSERT_MESSAGE( "BG layer must confess that!",
91                                 pBgLayer->isBackgroundLayer() );
92         CPPUNIT_ASSERT_MESSAGE( "FG layer lies!",
93                                 !pFgLayer->isBackgroundLayer() );
94 
95         CPPUNIT_ASSERT_MESSAGE( "BG layer must not have pending updates!",
96                                 !pBgLayer->isUpdatePending() );
97         pBgLayer->addUpdateRange( basegfx::B2DRange(0,0,10,10) );
98         CPPUNIT_ASSERT_MESSAGE( "BG layer must have pending updates!",
99                                 pBgLayer->isUpdatePending() );
100 
101         TestShapeSharedPtr pTestShape = createTestShape(
102             basegfx::B2DRange(0.0,0.0,1000.0,1000.0),
103             1.0);
104         pBgLayer->updateBounds( pTestShape );
105         CPPUNIT_ASSERT_MESSAGE( "BG layer must not resize!",
106                                 !pBgLayer->commitBounds() );
107 
108         TestShapeSharedPtr pTestShape2 = createTestShape(
109             basegfx::B2DRange(0.0,0.0,1.0,1.0),
110             1.0);
111         pFgLayer->updateBounds( pTestShape2 );
112         CPPUNIT_ASSERT_MESSAGE( "FG layer must resize!",
113                                 pFgLayer->commitBounds() );
114     }
115 
116     void testBasics()
117     {
118         mpLayerManager->activate( false );
119 
120         CPPUNIT_ASSERT_MESSAGE( "Un-added shape must have zero view layers",
121                                 mpTestShape->getViewLayers().empty() );
122         mpLayerManager->addShape(mpTestShape);
123         CPPUNIT_ASSERT_MESSAGE( "Adding a shape requires a LayerManager update",
124                                 mpLayerManager->isUpdatePending() );
125 
126         // update does the delayed viewAdded call to the shape
127         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager",
128                                 mpLayerManager->update() );
129         CPPUNIT_ASSERT_MESSAGE( "Added shape must have one view layer",
130                                 mpTestShape->getViewLayers().size() == 1 );
131         CPPUNIT_ASSERT_MESSAGE( "Shape must been rendered",
132                                 mpTestShape->getNumRenders() );
133         CPPUNIT_ASSERT_MESSAGE( "Shape must not been updated",
134                                 !mpTestShape->getNumUpdates() );
135 
136         // test second view, check whether shape gets additional view
137         TestViewSharedPtr pTestView( createTestView() );
138         CPPUNIT_ASSERT_MESSAGE( "Adding second View failed",
139                                 maViews.addView( pTestView ) );
140         CPPUNIT_ASSERT_MESSAGE( "View container must have two views",
141                                 maViews.end() - maViews.begin() == 2 );
142         mpLayerManager->viewAdded(pTestView);
143         CPPUNIT_ASSERT_MESSAGE( "Added shape must have two view layers",
144                                 mpTestShape->getViewLayers().size() == 2 );
145 
146         CPPUNIT_ASSERT_MESSAGE( "Removing second View failed",
147                                 maViews.removeView( pTestView ) );
148         mpLayerManager->viewRemoved(pTestView);
149         CPPUNIT_ASSERT_MESSAGE( "Added shape must have one view layer",
150                                 mpTestShape->getViewLayers().size() == 1 );
151 
152         mpLayerManager->deactivate();
153     }
154 
155     void testShapeOrdering()
156     {
157         TestShapeSharedPtr pShape2( createTestShape(
158             basegfx::B2DRange(0.0,0.0,10.0,10.0),
159             2.0));
160         TestShapeSharedPtr pShape3( createTestShape(
161             basegfx::B2DRange(0.0,0.0,10.0,10.0),
162             3.0));
163         TestShapeSharedPtr pShape4( createTestShape(
164             basegfx::B2DRange(0.0,0.0,10.0,10.0),
165             4.0));
166 
167         mpLayerManager->addShape(mpTestShape);
168         mpLayerManager->addShape(pShape2);
169         mpLayerManager->addShape(pShape3);
170         mpLayerManager->addShape(pShape4);
171 
172         mpLayerManager->activate( false );
173 
174         // update does the delayed viewAdded call to the shape
175         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager",
176                                 mpLayerManager->update() );
177         CPPUNIT_ASSERT_MESSAGE( "View must have background layer only",
178                                 mpTestView->getViewLayers().empty() );
179 
180         // LayerManager must now generate one extra view layer
181         mpLayerManager->enterAnimationMode(pShape2);
182         CPPUNIT_ASSERT_MESSAGE( "No update pending on LayerManager",
183                                 mpLayerManager->isUpdatePending() );
184         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager",
185                                 mpLayerManager->update() );
186         CPPUNIT_ASSERT_MESSAGE( "View must have one extra layer only",
187                                 mpTestView->getViewLayers().size() == 1 );
188         CPPUNIT_ASSERT_MESSAGE( "View layer must have 10x10 size",
189                                 mpTestView->getViewLayers().at(0)->getBounds() ==
190                                 basegfx::B2DRange(0.0,0.0,10.0,10.0) );
191 
192         // LayerManager must now remove the extra view layer
193         mpLayerManager->leaveAnimationMode(pShape2);
194         CPPUNIT_ASSERT_MESSAGE( "No update pending on LayerManager",
195                                 mpLayerManager->isUpdatePending() );
196         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager #2",
197                                 mpLayerManager->update() );
198         CPPUNIT_ASSERT_MESSAGE( "Shape 1 must be on background layer",
199                                 mpTestShape->getViewLayers().at(0).first == mpTestView );
200         CPPUNIT_ASSERT_MESSAGE( "Shape 2 must be on background layer",
201                                 pShape2->getViewLayers().at(0).first == mpTestView );
202         CPPUNIT_ASSERT_MESSAGE( "Shape 3 must have one layer",
203                                 pShape3->getViewLayers().size() == 1 );
204         CPPUNIT_ASSERT_MESSAGE( "Shape 3 must be on background layer",
205                                 pShape3->getViewLayers().at(0).first == mpTestView );
206         CPPUNIT_ASSERT_MESSAGE( "Shape 4 must be on background layer",
207                                 pShape4->getViewLayers().at(0).first == mpTestView );
208 
209         // checking deactivation (all layers except background layer
210         // must vanish)
211         mpLayerManager->enterAnimationMode(pShape3);
212         CPPUNIT_ASSERT_MESSAGE( "No update pending on LayerManager",
213                                 mpLayerManager->isUpdatePending() );
214         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager",
215                                 mpLayerManager->update() );
216         CPPUNIT_ASSERT_MESSAGE( "Shape 4 must not be on background layer",
217                                 pShape4->getViewLayers().at(0).first != mpTestView );
218         mpLayerManager->leaveAnimationMode(pShape3);
219         CPPUNIT_ASSERT_MESSAGE( "Update failed on LayerManager",
220                                 mpLayerManager->update() );
221         CPPUNIT_ASSERT_MESSAGE( "Shape 4 must be on background layer",
222                                 pShape4->getViewLayers().at(0).first == mpTestView );
223 
224         mpLayerManager->deactivate();
225         CPPUNIT_ASSERT_MESSAGE( "Update pending on deactivated LayerManager",
226                                 !mpLayerManager->isUpdatePending() );
227     }
228 
229     void testShapeRepaint()
230     {
231         TestShapeSharedPtr pShape2( createTestShape(
232             basegfx::B2DRange(0.0,0.0,10.0,10.0),
233             2.0));
234         TestShapeSharedPtr pShape3( createTestShape(
235             basegfx::B2DRange(0.0,0.0,10.0,10.0),
236             3.0));
237         TestShapeSharedPtr pShape4( createTestShape(
238             basegfx::B2DRange(0.0,0.0,10.0,10.0),
239             4.0));
240         TestShapeSharedPtr pShape5( createTestShape(
241             basegfx::B2DRange(20.0,20.0,30.0,30.0),
242             4.0));
243 
244         mpLayerManager->addShape(mpTestShape);
245         mpLayerManager->addShape(pShape2);
246         mpLayerManager->enterAnimationMode(pShape2);
247         mpLayerManager->addShape(pShape3);
248         mpLayerManager->addShape(pShape4);
249         mpLayerManager->addShape(pShape5);
250 
251         mpLayerManager->activate( false );
252         mpLayerManager->update();
253 
254         CPPUNIT_ASSERT_MESSAGE( "First shape not rendered",
255                                 mpTestShape->getNumRenders() == 1 );
256         CPPUNIT_ASSERT_MESSAGE( "Second shape not rendered",
257                                 pShape2->getNumRenders() == 1 );
258         CPPUNIT_ASSERT_MESSAGE( "Third shape not rendered",
259                                 pShape3->getNumRenders() == 1 );
260         CPPUNIT_ASSERT_MESSAGE( "Fourth shape not rendered",
261                                 pShape4->getNumRenders() == 1 );
262         CPPUNIT_ASSERT_MESSAGE( "Fifth shape not rendered",
263                                 pShape5->getNumRenders() == 1 );
264 
265         mpLayerManager->enterAnimationMode(pShape4);
266         mpLayerManager->update();
267 
268         CPPUNIT_ASSERT_MESSAGE( "First shape not rendered",
269                                 mpTestShape->getNumRenders() == 1 );
270         CPPUNIT_ASSERT_MESSAGE( "Second shape not rendered",
271                                 pShape2->getNumRenders() == 1 );
272         CPPUNIT_ASSERT_MESSAGE( "Third shape not rendered",
273                                 pShape3->getNumRenders() == 2 );
274         CPPUNIT_ASSERT_MESSAGE( "Fourth shape not rendered",
275                                 pShape4->getNumRenders() == 2 );
276         CPPUNIT_ASSERT_MESSAGE( "Fifth shape not rendered",
277                                 pShape5->getNumRenders() == 2 );
278 
279         mpLayerManager->leaveAnimationMode(pShape2);
280         mpLayerManager->leaveAnimationMode(pShape4);
281         mpLayerManager->update();
282 
283         CPPUNIT_ASSERT_MESSAGE( "First shape not rendered #2",
284                                 mpTestShape->getNumRenders() == 2 );
285         CPPUNIT_ASSERT_MESSAGE( "Second shape not rendered #2",
286                                 pShape2->getNumRenders() == 2 );
287         CPPUNIT_ASSERT_MESSAGE( "Third shape not rendered #2",
288                                 pShape3->getNumRenders() == 3 );
289         CPPUNIT_ASSERT_MESSAGE( "Fourth shape not rendered #2",
290                                 pShape4->getNumRenders() == 3 );
291         CPPUNIT_ASSERT_MESSAGE( "Fifth shape not rendered #2",
292                                 pShape5->getNumRenders() == 3 );
293     }
294 
295     void testRefCounting()
296     {
297         TestShapeSharedPtr pShape2( createTestShape(
298             basegfx::B2DRange(0.0,0.0,10.0,10.0),
299             2.0));
300         TestShapeSharedPtr pShape3( createTestShape(
301             basegfx::B2DRange(0.0,0.0,10.0,10.0),
302             3.0));
303         TestShapeSharedPtr pShape4( createTestShape(
304             basegfx::B2DRange(0.0,0.0,10.0,10.0),
305             4.0));
306 
307         mpLayerManager->addShape(mpTestShape);
308         mpLayerManager->addShape(pShape2);
309         mpLayerManager->addShape(pShape3);
310         mpLayerManager->addShape(pShape4);
311 
312         mpLayerManager->removeShape(mpTestShape);
313         mpLayerManager->removeShape(pShape2);
314         mpLayerManager->removeShape(pShape3);
315         mpLayerManager->removeShape(pShape4);
316 
317         CPPUNIT_ASSERT_MESSAGE( "Shape 1 must have refcount of 1",
318                                 mpTestShape.use_count() == 1 );
319         CPPUNIT_ASSERT_MESSAGE( "Shape 2 must have refcount of ",
320                                 pShape2.use_count() == 1 );
321         CPPUNIT_ASSERT_MESSAGE( "Shape 3 must have refcount of 1",
322                                 pShape3.use_count() == 1 );
323         CPPUNIT_ASSERT_MESSAGE( "Shape 4 must have refcount of",
324                                 pShape4.use_count() == 1 );
325 
326 
327         mpLayerManager->addShape(mpTestShape);
328         mpLayerManager->addShape(pShape2);
329         mpLayerManager->addShape(pShape3);
330         mpLayerManager->addShape(pShape4);
331 
332         mpLayerManager->activate( false );
333         mpLayerManager->update();
334 
335         mpLayerManager->removeShape(mpTestShape);
336         mpLayerManager->removeShape(pShape2);
337         mpLayerManager->removeShape(pShape3);
338         mpLayerManager->removeShape(pShape4);
339 
340         CPPUNIT_ASSERT_MESSAGE( "Shape 1 must have refcount of 1",
341                                 mpTestShape.use_count() == 1 );
342         CPPUNIT_ASSERT_MESSAGE( "Shape 2 must have refcount of ",
343                                 pShape2.use_count() == 1 );
344         CPPUNIT_ASSERT_MESSAGE( "Shape 3 must have refcount of 1",
345                                 pShape3.use_count() == 1 );
346         CPPUNIT_ASSERT_MESSAGE( "Shape 4 must have refcount of 1",
347                                 pShape4.use_count() == 1 );
348 
349         maViews.removeView(mpTestView);
350         mpLayerManager->viewRemoved(mpTestView);
351         CPPUNIT_ASSERT_MESSAGE( "View must have refcount of 1",
352                                 mpTestView.use_count() == 1 );
353     }
354 
355     // hook up the test
356     CPPUNIT_TEST_SUITE(LayerManagerTest);
357     CPPUNIT_TEST(testBasics);
358     CPPUNIT_TEST(testLayer);
359     CPPUNIT_TEST(testShapeOrdering);
360     CPPUNIT_TEST(testShapeRepaint);
361     CPPUNIT_TEST(testRefCounting);
362     CPPUNIT_TEST_SUITE_END();
363 
364 }; // class LayerManagerTest
365 
366 // -----------------------------------------------------------------------------
367 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(LayerManagerTest, "LayerManagerTest");
368 } // namespace
369 
370 
371