Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include <sal/config.h>
11 : #include <test/bootstrapfixture.hxx>
12 :
13 : #include <vcl/svapp.hxx>
14 : #include <smdll.hxx>
15 : #include <document.hxx>
16 : #include <view.hxx>
17 :
18 : #include <sfx2/sfxmodelfactory.hxx>
19 : #include <sfx2/bindings.hxx>
20 : #include <sfx2/request.hxx>
21 : #include <sfx2/dispatch.hxx>
22 :
23 : #include <svl/stritem.hxx>
24 :
25 : #include <editeng/editeng.hxx>
26 : #include <editeng/editview.hxx>
27 :
28 : #include <sfx2/zoomitem.hxx>
29 :
30 : typedef tools::SvRef<SmDocShell> SmDocShellRef;
31 :
32 : using namespace ::com::sun::star;
33 :
34 : namespace {
35 :
36 12 : class Test : public test::BootstrapFixture
37 : {
38 : public:
39 : // init
40 : virtual void setUp() SAL_OVERRIDE;
41 : virtual void tearDown() SAL_OVERRIDE;
42 :
43 : // tests
44 : void editUndoRedo();
45 : void editMarker();
46 : void editFailure();
47 :
48 : void viewZoom();
49 :
50 2 : CPPUNIT_TEST_SUITE(Test);
51 1 : CPPUNIT_TEST(editUndoRedo);
52 1 : CPPUNIT_TEST(editMarker);
53 1 : CPPUNIT_TEST(editFailure);
54 1 : CPPUNIT_TEST(viewZoom);
55 2 : CPPUNIT_TEST_SUITE_END();
56 :
57 : private:
58 : uno::Reference<uno::XComponentContext> m_xContext;
59 : uno::Reference<lang::XMultiComponentFactory> m_xFactory;
60 :
61 : SfxBindings m_aBindings;
62 : SfxDispatcher *m_pDispatcher;
63 : SmCmdBoxWindow *m_pSmCmdBoxWindow;
64 : SmEditWindow *m_pEditWindow;
65 : SmDocShellRef m_xDocShRef;
66 : SmViewShell *m_pViewShell;
67 : };
68 :
69 4 : void Test::setUp()
70 : {
71 4 : BootstrapFixture::setUp();
72 :
73 4 : SmGlobals::ensure();
74 :
75 12 : m_xDocShRef = new SmDocShell(
76 : SFXMODEL_STANDARD |
77 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS |
78 8 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY);
79 4 : m_xDocShRef->DoInitNew(0);
80 :
81 4 : SfxViewFrame *pViewFrame = SfxViewFrame::LoadHiddenDocument(*m_xDocShRef, 0);
82 :
83 4 : CPPUNIT_ASSERT_MESSAGE("Should have a SfxViewFrame", pViewFrame);
84 :
85 4 : m_pDispatcher = new SfxDispatcher(pViewFrame);
86 4 : m_aBindings.SetDispatcher(m_pDispatcher);
87 4 : m_aBindings.EnterRegistrations();
88 4 : m_pSmCmdBoxWindow = new SmCmdBoxWindow(&m_aBindings, NULL, NULL);
89 4 : m_aBindings.LeaveRegistrations();
90 4 : m_pEditWindow = new SmEditWindow(*m_pSmCmdBoxWindow);
91 4 : m_pViewShell = m_pEditWindow->GetView();
92 4 : CPPUNIT_ASSERT_MESSAGE("Should have a SmViewShell", m_pViewShell);
93 4 : }
94 :
95 4 : void Test::tearDown()
96 : {
97 4 : delete m_pEditWindow;
98 4 : delete m_pSmCmdBoxWindow;
99 4 : delete m_pDispatcher;
100 4 : m_xDocShRef.Clear();
101 :
102 4 : BootstrapFixture::tearDown();
103 4 : }
104 :
105 1 : void Test::editMarker()
106 : {
107 : {
108 1 : OUString sMarkedText("<?> under <?> under <?>");
109 1 : m_pEditWindow->SetText(sMarkedText);
110 1 : m_pEditWindow->Flush();
111 2 : OUString sFinalText = m_pEditWindow->GetText();
112 2 : CPPUNIT_ASSERT_MESSAGE("Should be equal text", sFinalText == sMarkedText);
113 : }
114 :
115 : {
116 1 : OUString sTargetText("a under b under c");
117 :
118 1 : m_pEditWindow->SelNextMark();
119 1 : m_pEditWindow->Delete();
120 1 : m_pEditWindow->InsertText("a");
121 :
122 1 : m_pEditWindow->SelNextMark();
123 1 : m_pEditWindow->SelNextMark();
124 1 : m_pEditWindow->Delete();
125 1 : m_pEditWindow->InsertText("c");
126 :
127 1 : m_pEditWindow->SelPrevMark();
128 1 : m_pEditWindow->Delete();
129 1 : m_pEditWindow->InsertText("b");
130 :
131 1 : m_pEditWindow->Flush();
132 2 : OUString sFinalText = m_pEditWindow->GetText();
133 2 : CPPUNIT_ASSERT_MESSAGE("Should be a under b under c", sFinalText == sTargetText);
134 : }
135 :
136 : {
137 1 : m_pEditWindow->SetText(OUString());
138 1 : m_pEditWindow->Flush();
139 : }
140 1 : }
141 :
142 1 : void Test::editFailure()
143 : {
144 1 : m_xDocShRef->SetText("color a b over {a/}");
145 :
146 1 : const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
147 :
148 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_COLOR_EXPECTED",
149 1 : pErrorDesc && pErrorDesc->Type == PE_COLOR_EXPECTED);
150 :
151 1 : pErrorDesc = m_xDocShRef->GetParser().PrevError();
152 :
153 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_UNEXPECTED_CHAR",
154 1 : pErrorDesc && pErrorDesc->Type == PE_UNEXPECTED_CHAR);
155 :
156 1 : pErrorDesc = m_xDocShRef->GetParser().PrevError();
157 :
158 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_RGROUP_EXPECTED",
159 1 : pErrorDesc && pErrorDesc->Type == PE_RGROUP_EXPECTED);
160 :
161 1 : const SmErrorDesc *pLastErrorDesc = m_xDocShRef->GetParser().PrevError();
162 :
163 2 : CPPUNIT_ASSERT_MESSAGE("Should be three syntax errors",
164 1 : pLastErrorDesc && pLastErrorDesc == pErrorDesc);
165 1 : }
166 :
167 1 : void Test::editUndoRedo()
168 : {
169 1 : EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
170 :
171 1 : OUString sStringOne("a under b");
172 : {
173 1 : rEditEngine.SetText(0, sStringOne);
174 1 : m_xDocShRef->UpdateText();
175 1 : OUString sFinalText = m_xDocShRef->GetText();
176 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
177 : }
178 :
179 2 : OUString sStringTwo("a over b");
180 : {
181 1 : rEditEngine.SetText(0, sStringTwo);
182 1 : m_xDocShRef->UpdateText();
183 1 : OUString sFinalText = m_xDocShRef->GetText();
184 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringTwo == sFinalText);
185 : }
186 :
187 2 : SfxRequest aUndo(SID_UNDO, SFX_CALLMODE_SYNCHRON, m_xDocShRef->GetPool());
188 :
189 : {
190 1 : m_xDocShRef->Execute(aUndo);
191 1 : m_xDocShRef->UpdateText();
192 1 : OUString sFinalText = m_xDocShRef->GetText();
193 1 : CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
194 : }
195 :
196 : {
197 1 : m_xDocShRef->Execute(aUndo);
198 1 : m_xDocShRef->UpdateText();
199 1 : OUString sFinalText = m_xDocShRef->GetText();
200 1 : CPPUNIT_ASSERT_MESSAGE("Must now be empty", !sFinalText.getLength());
201 : }
202 :
203 2 : SfxRequest aRedo(SID_REDO, SFX_CALLMODE_SYNCHRON, m_xDocShRef->GetPool());
204 : {
205 1 : m_xDocShRef->Execute(aRedo);
206 1 : m_xDocShRef->UpdateText();
207 1 : OUString sFinalText = m_xDocShRef->GetText();
208 1 : CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
209 : }
210 :
211 : {
212 1 : rEditEngine.SetText(0, OUString());
213 1 : m_xDocShRef->UpdateText();
214 1 : rEditEngine.ClearModifyFlag();
215 1 : OUString sFinalText = m_xDocShRef->GetText();
216 1 : CPPUNIT_ASSERT_MESSAGE("Must be empty", !sFinalText.getLength());
217 1 : }
218 :
219 1 : }
220 :
221 1 : void Test::viewZoom()
222 : {
223 : sal_uInt16 nOrigZoom, nNextZoom, nFinalZoom;
224 :
225 1 : EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
226 :
227 1 : OUString sStringOne("a under b");
228 : {
229 1 : rEditEngine.SetText(0, sStringOne);
230 1 : m_xDocShRef->UpdateText();
231 1 : OUString sFinalText = m_xDocShRef->GetText();
232 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
233 : }
234 :
235 1 : SmGraphicWindow &rGraphicWindow = m_pViewShell->GetGraphicWindow();
236 1 : rGraphicWindow.SetSizePixel(Size(1024, 800));
237 1 : nOrigZoom = rGraphicWindow.GetZoom();
238 :
239 : {
240 1 : SfxRequest aZoomIn(SID_ZOOMIN, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
241 1 : m_pViewShell->Execute(aZoomIn);
242 1 : nNextZoom = rGraphicWindow.GetZoom();
243 1 : CPPUNIT_ASSERT_MESSAGE("Should be bigger", nNextZoom > nOrigZoom);
244 : }
245 :
246 : {
247 1 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
248 1 : m_pViewShell->Execute(aZoomOut);
249 1 : nFinalZoom = rGraphicWindow.GetZoom();
250 1 : CPPUNIT_ASSERT_MESSAGE("Should be equal", nFinalZoom == nOrigZoom);
251 : }
252 :
253 1 : sal_uInt16 nOptimalZoom=0;
254 :
255 : {
256 1 : SfxRequest aZoom(SID_ZOOM_OPTIMAL, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
257 1 : m_pViewShell->Execute(aZoom);
258 1 : nOptimalZoom = rGraphicWindow.GetZoom();
259 1 : CPPUNIT_ASSERT_MESSAGE("Should be about 800%", nOptimalZoom > nOrigZoom);
260 : }
261 :
262 : {
263 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
264 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_OPTIMAL, 0));
265 2 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
266 1 : m_pViewShell->Execute(aZoom);
267 1 : nFinalZoom = rGraphicWindow.GetZoom();
268 2 : CPPUNIT_ASSERT_MESSAGE("Should be optimal zoom", nFinalZoom == nOptimalZoom);
269 : }
270 :
271 : //To-Do: investigate GetPrinter logic of SVX_ZOOM_PAGEWIDTH/SVX_ZOOM_WHOLEPAGE to ensure
272 : //consistent value regardless of
273 : #if 0
274 : {
275 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
276 : m_pViewShell->Execute(aZoomOut);
277 : nFinalZoom = rGraphicWindow.GetZoom();
278 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
279 :
280 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
281 : aSet.Put(SvxZoomItem(SVX_ZOOM_PAGEWIDTH, 0));
282 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
283 : m_pViewShell->Execute(aZoom);
284 : nFinalZoom = rGraphicWindow.GetZoom();
285 : CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
286 : }
287 :
288 : {
289 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
290 : m_pViewShell->Execute(aZoomOut);
291 : nFinalZoom = rGraphicWindow.GetZoom();
292 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
293 :
294 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
295 : aSet.Put(SvxZoomItem(SVX_ZOOM_WHOLEPAGE, 0));
296 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
297 : m_pViewShell->Execute(aZoom);
298 : nFinalZoom = rGraphicWindow.GetZoom();
299 : CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
300 : }
301 : #endif
302 :
303 : {
304 1 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
305 1 : m_pViewShell->Execute(aZoomOut);
306 1 : nFinalZoom = rGraphicWindow.GetZoom();
307 1 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
308 :
309 2 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
310 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 50));
311 2 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
312 1 : m_pViewShell->Execute(aZoom);
313 1 : nFinalZoom = rGraphicWindow.GetZoom();
314 2 : CPPUNIT_ASSERT_MESSAGE("Should be 50%", nFinalZoom == 50);
315 : }
316 :
317 : {
318 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
319 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 5));
320 2 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
321 1 : m_pViewShell->Execute(aZoom);
322 1 : nFinalZoom = rGraphicWindow.GetZoom();
323 2 : CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 25%", nFinalZoom == 25);
324 : }
325 :
326 : {
327 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
328 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 1000));
329 2 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
330 1 : m_pViewShell->Execute(aZoom);
331 1 : nFinalZoom = rGraphicWindow.GetZoom();
332 2 : CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 800%", nFinalZoom == 800);
333 1 : }
334 :
335 1 : }
336 :
337 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
338 :
339 : }
340 :
341 4 : CPPUNIT_PLUGIN_IMPLEMENT();
342 :
343 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|