1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
13 #include <vcl/svapp.hxx>
15 #include <document.hxx>
18 #include <sfx2/sfxmodelfactory.hxx>
19 #include <sfx2/bindings.hxx>
20 #include <sfx2/request.hxx>
21 #include <sfx2/dispatch.hxx>
23 #include <svl/stritem.hxx>
25 #include <editeng/editeng.hxx>
26 #include <editeng/editview.hxx>
28 #include <sfx2/zoomitem.hxx>
30 SV_DECL_REF(SmDocShell
)
31 SV_IMPL_REF(SmDocShell
)
33 using namespace ::com::sun::star
;
37 class Test
: public test::BootstrapFixture
42 virtual void tearDown();
51 CPPUNIT_TEST_SUITE(Test
);
52 CPPUNIT_TEST(editUndoRedo
);
53 CPPUNIT_TEST(editMarker
);
54 CPPUNIT_TEST(editFailure
);
55 CPPUNIT_TEST(viewZoom
);
56 CPPUNIT_TEST_SUITE_END();
59 uno::Reference
<uno::XComponentContext
> m_xContext
;
60 uno::Reference
<lang::XMultiComponentFactory
> m_xFactory
;
62 SfxBindings m_aBindings
;
63 SfxDispatcher
*m_pDispatcher
;
64 SmCmdBoxWindow
*m_pSmCmdBoxWindow
;
65 SmEditWindow
*m_pEditWindow
;
66 SmDocShellRef m_xDocShRef
;
67 SmViewShell
*m_pViewShell
;
72 BootstrapFixture::setUp();
76 m_xDocShRef
= new SmDocShell(
78 SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
|
79 SFXMODEL_DISABLE_DOCUMENT_RECOVERY
);
80 m_xDocShRef
->DoInitNew(0);
82 SfxViewFrame
*pViewFrame
= SfxViewFrame::LoadHiddenDocument(*m_xDocShRef
, 0);
84 CPPUNIT_ASSERT_MESSAGE("Should have a SfxViewFrame", pViewFrame
);
86 m_pDispatcher
= new SfxDispatcher(pViewFrame
);
87 m_aBindings
.SetDispatcher(m_pDispatcher
);
88 m_aBindings
.EnterRegistrations();
89 m_pSmCmdBoxWindow
= new SmCmdBoxWindow(&m_aBindings
, NULL
, NULL
);
90 m_aBindings
.LeaveRegistrations();
91 m_pEditWindow
= new SmEditWindow(*m_pSmCmdBoxWindow
);
92 m_pViewShell
= m_pEditWindow
->GetView();
93 CPPUNIT_ASSERT_MESSAGE("Should have a SmViewShell", m_pViewShell
);
99 delete m_pSmCmdBoxWindow
;
100 delete m_pDispatcher
;
103 BootstrapFixture::tearDown();
106 void Test::editMarker()
109 OUString
sMarkedText("<?> under <?> under <?>");
110 m_pEditWindow
->SetText(sMarkedText
);
111 m_pEditWindow
->Flush();
112 OUString sFinalText
= m_pEditWindow
->GetText();
113 CPPUNIT_ASSERT_MESSAGE("Should be equal text", sFinalText
== sMarkedText
);
117 OUString
sTargetText("a under b under c");
119 m_pEditWindow
->SelNextMark();
120 m_pEditWindow
->Delete();
121 m_pEditWindow
->InsertText("a");
123 m_pEditWindow
->SelNextMark();
124 m_pEditWindow
->SelNextMark();
125 m_pEditWindow
->Delete();
126 m_pEditWindow
->InsertText("c");
128 m_pEditWindow
->SelPrevMark();
129 m_pEditWindow
->Delete();
130 m_pEditWindow
->InsertText("b");
132 m_pEditWindow
->Flush();
133 OUString sFinalText
= m_pEditWindow
->GetText();
134 CPPUNIT_ASSERT_MESSAGE("Should be a under b under c", sFinalText
== sTargetText
);
138 m_pEditWindow
->SetText(OUString());
139 m_pEditWindow
->Flush();
143 void Test::editFailure()
145 m_xDocShRef
->SetText(String("color a b over {a/}"));
147 const SmErrorDesc
*pErrorDesc
= m_xDocShRef
->GetParser().NextError();
149 CPPUNIT_ASSERT_MESSAGE("Should be a PE_COLOR_EXPECTED",
150 pErrorDesc
&& pErrorDesc
->Type
== PE_COLOR_EXPECTED
);
152 pErrorDesc
= m_xDocShRef
->GetParser().PrevError();
154 CPPUNIT_ASSERT_MESSAGE("Should be a PE_UNEXPECTED_CHAR",
155 pErrorDesc
&& pErrorDesc
->Type
== PE_UNEXPECTED_CHAR
);
157 pErrorDesc
= m_xDocShRef
->GetParser().PrevError();
159 CPPUNIT_ASSERT_MESSAGE("Should be a PE_RGROUP_EXPECTED",
160 pErrorDesc
&& pErrorDesc
->Type
== PE_RGROUP_EXPECTED
);
162 const SmErrorDesc
*pLastErrorDesc
= m_xDocShRef
->GetParser().PrevError();
164 CPPUNIT_ASSERT_MESSAGE("Should be three syntax errors",
165 pLastErrorDesc
&& pLastErrorDesc
== pErrorDesc
);
168 void Test::editUndoRedo()
170 EditEngine
&rEditEngine
= m_xDocShRef
->GetEditEngine();
172 OUString
sStringOne("a under b");
174 rEditEngine
.SetText(0, sStringOne
);
175 m_xDocShRef
->UpdateText();
176 OUString sFinalText
= m_xDocShRef
->GetText();
177 CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne
== sFinalText
);
180 OUString
sStringTwo("a over b");
182 rEditEngine
.SetText(0, sStringTwo
);
183 m_xDocShRef
->UpdateText();
184 OUString sFinalText
= m_xDocShRef
->GetText();
185 CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringTwo
== sFinalText
);
188 SfxRequest
aUndo(SID_UNDO
, SFX_CALLMODE_SYNCHRON
, m_xDocShRef
->GetPool());
191 m_xDocShRef
->Execute(aUndo
);
192 m_xDocShRef
->UpdateText();
193 OUString sFinalText
= m_xDocShRef
->GetText();
194 CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne
== sFinalText
);
198 m_xDocShRef
->Execute(aUndo
);
199 m_xDocShRef
->UpdateText();
200 OUString sFinalText
= m_xDocShRef
->GetText();
201 CPPUNIT_ASSERT_MESSAGE("Must now be empty", !sFinalText
.getLength());
204 SfxRequest
aRedo(SID_REDO
, SFX_CALLMODE_SYNCHRON
, m_xDocShRef
->GetPool());
206 m_xDocShRef
->Execute(aRedo
);
207 m_xDocShRef
->UpdateText();
208 OUString sFinalText
= m_xDocShRef
->GetText();
209 CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne
== sFinalText
);
213 rEditEngine
.SetText(0, OUString());
214 m_xDocShRef
->UpdateText();
215 rEditEngine
.ClearModifyFlag();
216 OUString sFinalText
= m_xDocShRef
->GetText();
217 CPPUNIT_ASSERT_MESSAGE("Must be empty", !sFinalText
.getLength());
222 void Test::viewZoom()
224 sal_uInt16 nOrigZoom
, nNextZoom
, nFinalZoom
;
226 EditEngine
&rEditEngine
= m_xDocShRef
->GetEditEngine();
228 OUString
sStringOne("a under b");
230 rEditEngine
.SetText(0, sStringOne
);
231 m_xDocShRef
->UpdateText();
232 OUString sFinalText
= m_xDocShRef
->GetText();
233 CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne
== sFinalText
);
236 SmGraphicWindow
&rGraphicWindow
= m_pViewShell
->GetGraphicWindow();
237 rGraphicWindow
.SetSizePixel(Size(1024, 800));
238 nOrigZoom
= rGraphicWindow
.GetZoom();
241 SfxRequest
aZoomIn(SID_ZOOMIN
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
242 m_pViewShell
->Execute(aZoomIn
);
243 nNextZoom
= rGraphicWindow
.GetZoom();
244 CPPUNIT_ASSERT_MESSAGE("Should be bigger", nNextZoom
> nOrigZoom
);
248 SfxRequest
aZoomOut(SID_ZOOMOUT
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
249 m_pViewShell
->Execute(aZoomOut
);
250 nFinalZoom
= rGraphicWindow
.GetZoom();
251 CPPUNIT_ASSERT_MESSAGE("Should be equal", nFinalZoom
== nOrigZoom
);
254 sal_uInt16 nOptimalZoom
=0;
257 SfxRequest
aZoom(SID_FITINWINDOW
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
258 m_pViewShell
->Execute(aZoom
);
259 nOptimalZoom
= rGraphicWindow
.GetZoom();
260 CPPUNIT_ASSERT_MESSAGE("Should be about 800%", nOptimalZoom
> nOrigZoom
);
264 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
265 aSet
.Put(SvxZoomItem(SVX_ZOOM_OPTIMAL
, 0));
266 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
267 m_pViewShell
->Execute(aZoom
);
268 nFinalZoom
= rGraphicWindow
.GetZoom();
269 CPPUNIT_ASSERT_MESSAGE("Should be optimal zoom", nFinalZoom
== nOptimalZoom
);
272 //To-Do: investigate GetPrinter logic of SVX_ZOOM_PAGEWIDTH/SVX_ZOOM_WHOLEPAGE to ensure
273 //consistent value regardless of
276 SfxRequest
aZoomOut(SID_ZOOMOUT
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
277 m_pViewShell
->Execute(aZoomOut
);
278 nFinalZoom
= rGraphicWindow
.GetZoom();
279 CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom
!= nOptimalZoom
);
281 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
282 aSet
.Put(SvxZoomItem(SVX_ZOOM_PAGEWIDTH
, 0));
283 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
284 m_pViewShell
->Execute(aZoom
);
285 nFinalZoom
= rGraphicWindow
.GetZoom();
286 CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom
== nOptimalZoom
);
290 SfxRequest
aZoomOut(SID_ZOOMOUT
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
291 m_pViewShell
->Execute(aZoomOut
);
292 nFinalZoom
= rGraphicWindow
.GetZoom();
293 CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom
!= nOptimalZoom
);
295 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
296 aSet
.Put(SvxZoomItem(SVX_ZOOM_WHOLEPAGE
, 0));
297 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
298 m_pViewShell
->Execute(aZoom
);
299 nFinalZoom
= rGraphicWindow
.GetZoom();
300 CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom
== nOptimalZoom
);
305 SfxRequest
aZoomOut(SID_ZOOMOUT
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
306 m_pViewShell
->Execute(aZoomOut
);
307 nFinalZoom
= rGraphicWindow
.GetZoom();
308 CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom
!= nOptimalZoom
);
310 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
311 aSet
.Put(SvxZoomItem(SVX_ZOOM_PERCENT
, 50));
312 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
313 m_pViewShell
->Execute(aZoom
);
314 nFinalZoom
= rGraphicWindow
.GetZoom();
315 CPPUNIT_ASSERT_MESSAGE("Should be 50%", nFinalZoom
== 50);
319 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
320 aSet
.Put(SvxZoomItem(SVX_ZOOM_PERCENT
, 5));
321 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
322 m_pViewShell
->Execute(aZoom
);
323 nFinalZoom
= rGraphicWindow
.GetZoom();
324 CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 25%", nFinalZoom
== 25);
328 SfxItemSet
aSet(m_xDocShRef
->GetPool(), SID_ATTR_ZOOM
, SID_ATTR_ZOOM
);
329 aSet
.Put(SvxZoomItem(SVX_ZOOM_PERCENT
, 1000));
330 SfxRequest
aZoom(SID_ATTR_ZOOM
, SFX_CALLMODE_SYNCHRON
, aSet
);
331 m_pViewShell
->Execute(aZoom
);
332 nFinalZoom
= rGraphicWindow
.GetZoom();
333 CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 800%", nFinalZoom
== 800);
337 SfxRequest
aZoom(SID_ADJUST
, SFX_CALLMODE_SYNCHRON
, m_pViewShell
->GetPool());
338 m_pViewShell
->Execute(aZoom
);
339 nFinalZoom
= rGraphicWindow
.GetZoom();
340 CPPUNIT_ASSERT_MESSAGE("Should be the same as optimal", nOptimalZoom
== nFinalZoom
);
345 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
349 CPPUNIT_PLUGIN_IMPLEMENT();
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */