Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2010 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
17 : * (initial developer)
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include <sal/config.h>
31 : #include <test/bootstrapfixture.hxx>
32 :
33 : #include <vcl/svapp.hxx>
34 : #include <smdll.hxx>
35 : #include <document.hxx>
36 : #include <view.hxx>
37 :
38 : #include <sfx2/sfxmodelfactory.hxx>
39 : #include <sfx2/bindings.hxx>
40 : #include <sfx2/request.hxx>
41 : #include <sfx2/dispatch.hxx>
42 :
43 : #include <svl/stritem.hxx>
44 :
45 : #include <editeng/editeng.hxx>
46 : #include <editeng/editview.hxx>
47 :
48 : #include <sfx2/zoomitem.hxx>
49 :
50 0 : SV_DECL_REF(SmDocShell)
51 0 : SV_IMPL_REF(SmDocShell)
52 :
53 : using namespace ::com::sun::star;
54 :
55 : namespace {
56 :
57 12 : class Test : public test::BootstrapFixture
58 : {
59 : public:
60 : // init
61 : virtual void setUp();
62 : virtual void tearDown();
63 :
64 : // tests
65 : void editUndoRedo();
66 : void editMarker();
67 : void editFailure();
68 :
69 : void viewZoom();
70 :
71 2 : CPPUNIT_TEST_SUITE(Test);
72 1 : CPPUNIT_TEST(editUndoRedo);
73 1 : CPPUNIT_TEST(editMarker);
74 1 : CPPUNIT_TEST(editFailure);
75 1 : CPPUNIT_TEST(viewZoom);
76 2 : CPPUNIT_TEST_SUITE_END();
77 :
78 : private:
79 : uno::Reference<uno::XComponentContext> m_xContext;
80 : uno::Reference<lang::XMultiComponentFactory> m_xFactory;
81 :
82 : SfxBindings m_aBindings;
83 : SfxDispatcher *m_pDispatcher;
84 : SmCmdBoxWindow *m_pSmCmdBoxWindow;
85 : SmEditWindow *m_pEditWindow;
86 : SmDocShellRef m_xDocShRef;
87 : SmViewShell *m_pViewShell;
88 : };
89 :
90 4 : void Test::setUp()
91 : {
92 4 : BootstrapFixture::setUp();
93 :
94 4 : SmGlobals::ensure();
95 :
96 : m_xDocShRef = new SmDocShell(
97 : SFXMODEL_STANDARD |
98 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS |
99 4 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY);
100 4 : m_xDocShRef->DoInitNew(0);
101 :
102 4 : SfxViewFrame *pViewFrame = SfxViewFrame::LoadHiddenDocument(*m_xDocShRef, 0);
103 :
104 4 : CPPUNIT_ASSERT_MESSAGE("Should have a SfxViewFrame", pViewFrame);
105 :
106 4 : m_pDispatcher = new SfxDispatcher(pViewFrame);
107 4 : m_aBindings.SetDispatcher(m_pDispatcher);
108 4 : m_aBindings.EnterRegistrations();
109 4 : m_pSmCmdBoxWindow = new SmCmdBoxWindow(&m_aBindings, NULL, NULL);
110 4 : m_aBindings.LeaveRegistrations();
111 4 : m_pEditWindow = new SmEditWindow(*m_pSmCmdBoxWindow);
112 4 : m_pViewShell = m_pEditWindow->GetView();
113 4 : CPPUNIT_ASSERT_MESSAGE("Should have a SmViewShell", m_pViewShell);
114 4 : }
115 :
116 4 : void Test::tearDown()
117 : {
118 4 : delete m_pEditWindow;
119 4 : delete m_pSmCmdBoxWindow;
120 4 : delete m_pDispatcher;
121 4 : m_xDocShRef.Clear();
122 :
123 4 : BootstrapFixture::tearDown();
124 4 : }
125 :
126 1 : void Test::editMarker()
127 : {
128 : {
129 1 : rtl::OUString sMarkedText("<?> under <?> under <?>");
130 1 : m_pEditWindow->SetText(sMarkedText);
131 1 : m_pEditWindow->Flush();
132 1 : rtl::OUString sFinalText = m_pEditWindow->GetText();
133 1 : CPPUNIT_ASSERT_MESSAGE("Should be equal text", sFinalText == sMarkedText);
134 : }
135 :
136 : {
137 1 : rtl::OUString sTargetText("a under b under c");
138 :
139 1 : m_pEditWindow->SelNextMark();
140 1 : m_pEditWindow->Delete();
141 1 : m_pEditWindow->InsertText("a");
142 :
143 1 : m_pEditWindow->SelNextMark();
144 1 : m_pEditWindow->SelNextMark();
145 1 : m_pEditWindow->Delete();
146 1 : m_pEditWindow->InsertText("c");
147 :
148 1 : m_pEditWindow->SelPrevMark();
149 1 : m_pEditWindow->Delete();
150 1 : m_pEditWindow->InsertText("b");
151 :
152 1 : m_pEditWindow->Flush();
153 1 : rtl::OUString sFinalText = m_pEditWindow->GetText();
154 1 : CPPUNIT_ASSERT_MESSAGE("Should be a under b under c", sFinalText == sTargetText);
155 : }
156 :
157 : {
158 1 : m_pEditWindow->SetText(rtl::OUString());
159 1 : m_pEditWindow->Flush();
160 : }
161 1 : }
162 :
163 1 : void Test::editFailure()
164 : {
165 1 : m_xDocShRef->SetText(String("color a b over {a/}"));
166 :
167 1 : const SmErrorDesc *pErrorDesc = m_xDocShRef->GetParser().NextError();
168 :
169 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_COLOR_EXPECTED",
170 1 : pErrorDesc && pErrorDesc->Type == PE_COLOR_EXPECTED);
171 :
172 1 : pErrorDesc = m_xDocShRef->GetParser().PrevError();
173 :
174 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_UNEXPECTED_CHAR",
175 1 : pErrorDesc && pErrorDesc->Type == PE_UNEXPECTED_CHAR);
176 :
177 1 : pErrorDesc = m_xDocShRef->GetParser().PrevError();
178 :
179 2 : CPPUNIT_ASSERT_MESSAGE("Should be a PE_RGROUP_EXPECTED",
180 1 : pErrorDesc && pErrorDesc->Type == PE_RGROUP_EXPECTED);
181 :
182 1 : const SmErrorDesc *pLastErrorDesc = m_xDocShRef->GetParser().PrevError();
183 :
184 2 : CPPUNIT_ASSERT_MESSAGE("Should be three syntax errors",
185 1 : pLastErrorDesc && pLastErrorDesc == pErrorDesc);
186 1 : }
187 :
188 1 : void Test::editUndoRedo()
189 : {
190 1 : EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
191 :
192 1 : rtl::OUString sStringOne("a under b");
193 : {
194 1 : rEditEngine.SetText(0, sStringOne);
195 1 : m_xDocShRef->UpdateText();
196 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
197 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
198 : }
199 :
200 1 : rtl::OUString sStringTwo("a over b");
201 : {
202 1 : rEditEngine.SetText(0, sStringTwo);
203 1 : m_xDocShRef->UpdateText();
204 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
205 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringTwo == sFinalText);
206 : }
207 :
208 1 : SfxRequest aUndo(SID_UNDO, SFX_CALLMODE_SYNCHRON, m_xDocShRef->GetPool());
209 :
210 : {
211 1 : m_xDocShRef->Execute(aUndo);
212 1 : rtl::OUString sFoo = rEditEngine.GetText();
213 1 : m_xDocShRef->UpdateText();
214 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
215 1 : CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
216 : }
217 :
218 : {
219 1 : m_xDocShRef->Execute(aUndo);
220 1 : rtl::OUString sFoo = rEditEngine.GetText();
221 1 : m_xDocShRef->UpdateText();
222 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
223 1 : CPPUNIT_ASSERT_MESSAGE("Must now be empty", !sFinalText.getLength());
224 : }
225 :
226 1 : SfxRequest aRedo(SID_REDO, SFX_CALLMODE_SYNCHRON, m_xDocShRef->GetPool());
227 : {
228 1 : m_xDocShRef->Execute(aRedo);
229 1 : rtl::OUString sFoo = rEditEngine.GetText();
230 1 : m_xDocShRef->UpdateText();
231 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
232 1 : CPPUNIT_ASSERT_MESSAGE("Strings much match", sStringOne == sFinalText);
233 : }
234 :
235 : {
236 1 : rEditEngine.SetText(0, rtl::OUString());
237 1 : m_xDocShRef->UpdateText();
238 1 : rEditEngine.ClearModifyFlag();
239 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
240 1 : CPPUNIT_ASSERT_MESSAGE("Must be empty", !sFinalText.getLength());
241 1 : }
242 :
243 1 : }
244 :
245 1 : void Test::viewZoom()
246 : {
247 : sal_uInt16 nOrigZoom, nNextZoom, nFinalZoom;
248 :
249 1 : EditEngine &rEditEngine = m_xDocShRef->GetEditEngine();
250 :
251 1 : rtl::OUString sStringOne("a under b");
252 : {
253 1 : rEditEngine.SetText(0, sStringOne);
254 1 : m_xDocShRef->UpdateText();
255 1 : rtl::OUString sFinalText = m_xDocShRef->GetText();
256 1 : CPPUNIT_ASSERT_MESSAGE("Strings must match", sStringOne == sFinalText);
257 : }
258 :
259 1 : SmGraphicWindow &rGraphicWindow = m_pViewShell->GetGraphicWindow();
260 1 : rGraphicWindow.SetSizePixel(Size(1024, 800));
261 1 : nOrigZoom = rGraphicWindow.GetZoom();
262 :
263 : {
264 1 : SfxRequest aZoomIn(SID_ZOOMIN, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
265 1 : m_pViewShell->Execute(aZoomIn);
266 1 : nNextZoom = rGraphicWindow.GetZoom();
267 1 : CPPUNIT_ASSERT_MESSAGE("Should be bigger", nNextZoom > nOrigZoom);
268 : }
269 :
270 : {
271 1 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
272 1 : m_pViewShell->Execute(aZoomOut);
273 1 : nFinalZoom = rGraphicWindow.GetZoom();
274 1 : CPPUNIT_ASSERT_MESSAGE("Should be equal", nFinalZoom == nOrigZoom);
275 : }
276 :
277 1 : sal_uInt16 nOptimalZoom=0;
278 :
279 : {
280 1 : SfxRequest aZoom(SID_FITINWINDOW, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
281 1 : m_pViewShell->Execute(aZoom);
282 1 : nOptimalZoom = rGraphicWindow.GetZoom();
283 1 : CPPUNIT_ASSERT_MESSAGE("Should be about 800%", nOptimalZoom > nOrigZoom);
284 : }
285 :
286 : {
287 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
288 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_OPTIMAL, 0));
289 1 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
290 1 : m_pViewShell->Execute(aZoom);
291 1 : nFinalZoom = rGraphicWindow.GetZoom();
292 1 : CPPUNIT_ASSERT_MESSAGE("Should be optimal zoom", nFinalZoom == nOptimalZoom);
293 : }
294 :
295 : //To-Do: investigate GetPrinter logic of SVX_ZOOM_PAGEWIDTH/SVX_ZOOM_WHOLEPAGE to ensure
296 : //consistent value regardless of
297 : #if 0
298 : {
299 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
300 : m_pViewShell->Execute(aZoomOut);
301 : nFinalZoom = rGraphicWindow.GetZoom();
302 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
303 :
304 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
305 : aSet.Put(SvxZoomItem(SVX_ZOOM_PAGEWIDTH, 0));
306 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
307 : m_pViewShell->Execute(aZoom);
308 : nFinalZoom = rGraphicWindow.GetZoom();
309 : CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
310 : }
311 :
312 : {
313 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
314 : m_pViewShell->Execute(aZoomOut);
315 : nFinalZoom = rGraphicWindow.GetZoom();
316 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
317 :
318 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
319 : aSet.Put(SvxZoomItem(SVX_ZOOM_WHOLEPAGE, 0));
320 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
321 : m_pViewShell->Execute(aZoom);
322 : nFinalZoom = rGraphicWindow.GetZoom();
323 : CPPUNIT_ASSERT_MESSAGE("Should be same as optimal zoom", nFinalZoom == nOptimalZoom);
324 : }
325 : #endif
326 :
327 : {
328 1 : SfxRequest aZoomOut(SID_ZOOMOUT, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
329 1 : m_pViewShell->Execute(aZoomOut);
330 1 : nFinalZoom = rGraphicWindow.GetZoom();
331 1 : CPPUNIT_ASSERT_MESSAGE("Should not be optimal zoom", nFinalZoom != nOptimalZoom);
332 :
333 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
334 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 50));
335 1 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
336 1 : m_pViewShell->Execute(aZoom);
337 1 : nFinalZoom = rGraphicWindow.GetZoom();
338 1 : CPPUNIT_ASSERT_MESSAGE("Should be 50%", nFinalZoom == 50);
339 : }
340 :
341 : {
342 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
343 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 5));
344 1 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
345 1 : m_pViewShell->Execute(aZoom);
346 1 : nFinalZoom = rGraphicWindow.GetZoom();
347 1 : CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 25%", nFinalZoom == 25);
348 : }
349 :
350 : {
351 1 : SfxItemSet aSet(m_xDocShRef->GetPool(), SID_ATTR_ZOOM, SID_ATTR_ZOOM);
352 1 : aSet.Put(SvxZoomItem(SVX_ZOOM_PERCENT, 1000));
353 1 : SfxRequest aZoom(SID_ATTR_ZOOM, SFX_CALLMODE_SYNCHRON, aSet);
354 1 : m_pViewShell->Execute(aZoom);
355 1 : nFinalZoom = rGraphicWindow.GetZoom();
356 1 : CPPUNIT_ASSERT_MESSAGE("Should be Clipped to 800%", nFinalZoom == 800);
357 : }
358 :
359 : {
360 1 : SfxRequest aZoom(SID_ADJUST, SFX_CALLMODE_SYNCHRON, m_pViewShell->GetPool());
361 1 : m_pViewShell->Execute(aZoom);
362 1 : nFinalZoom = rGraphicWindow.GetZoom();
363 1 : CPPUNIT_ASSERT_MESSAGE("Should be the same as optimal", nOptimalZoom == nFinalZoom);
364 1 : }
365 :
366 1 : }
367 :
368 1 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
369 :
370 : }
371 :
372 4 : CPPUNIT_PLUGIN_IMPLEMENT();
373 :
374 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|