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/virdev.hxx>
14 #include <sfx2/sfxmodelfactory.hxx>
15 #include <smdll.hxx>
16
17 #include <document.hxx>
18 #include <node.hxx>
19 #include <cursor.hxx>
20
21 #include <memory>
22
23 typedef tools::SvRef<SmDocShell> SmDocShellRef;
24
25 using namespace ::com::sun::star;
26
27 namespace {
28
29 class Test : public test::BootstrapFixture {
30
31 public:
32 // init
33 virtual void setUp() override;
34 virtual void tearDown() override;
35
36 // tests
37 void testCopyPaste();
38 void testCopySelectPaste();
39 void testCutPaste();
40 void testCutSelectPaste();
41
42 CPPUNIT_TEST_SUITE(Test);
43 CPPUNIT_TEST(testCopyPaste);
44 CPPUNIT_TEST(testCopySelectPaste);
45 CPPUNIT_TEST(testCutPaste);
46 CPPUNIT_TEST(testCutSelectPaste);
47 CPPUNIT_TEST_SUITE_END();
48
49 private:
50 SmDocShellRef xDocShRef;
51 };
52
setUp()53 void Test::setUp()
54 {
55 BootstrapFixture::setUp();
56
57 SmGlobals::ensure();
58
59 xDocShRef = new SmDocShell(SfxModelFlags::EMBEDDED_OBJECT);
60 }
61
tearDown()62 void Test::tearDown()
63 {
64 xDocShRef.clear();
65 BootstrapFixture::tearDown();
66 }
67
testCopyPaste()68 void Test::testCopyPaste()
69 {
70 OUString const sInput("a * b + c");
71 auto xTree = SmParser().Parse(sInput);
72 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
73
74 SmCursor aCursor(xTree.get(), xDocShRef.get());
75 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
76
77 // go to the position at "*"
78 aCursor.Move(pOutputDevice, MoveRight);
79 // select "* b" and then copy
80 aCursor.Move(pOutputDevice, MoveRight, false);
81 aCursor.Move(pOutputDevice, MoveRight, false);
82 aCursor.Copy();
83 // go to the right end and then paste
84 aCursor.Move(pOutputDevice, MoveRight);
85 aCursor.Move(pOutputDevice, MoveRight);
86 aCursor.Paste();
87
88 CPPUNIT_ASSERT_EQUAL(OUString(" { a * b + c * b } "), xDocShRef->GetText());
89 }
90
testCopySelectPaste()91 void Test::testCopySelectPaste()
92 {
93 OUString const sInput("a * b + c");
94 auto xTree = SmParser().Parse(sInput);
95 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
96
97 SmCursor aCursor(xTree.get(), xDocShRef.get());
98 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
99
100 // go to the right end
101 for (int i=0;i<5;i++)
102 aCursor.Move(pOutputDevice, MoveRight);
103 // select "b + c" and then copy
104 aCursor.Move(pOutputDevice, MoveLeft, false);
105 aCursor.Move(pOutputDevice, MoveLeft, false);
106 aCursor.Move(pOutputDevice, MoveLeft, false);
107 aCursor.Copy();
108 // go to the left end
109 aCursor.Move(pOutputDevice, MoveLeft);
110 aCursor.Move(pOutputDevice, MoveLeft);
111 // select "a" and then paste
112 aCursor.Move(pOutputDevice, MoveRight, false);
113 aCursor.Paste();
114
115 CPPUNIT_ASSERT_EQUAL(OUString(" { b + c * b + c } "), xDocShRef->GetText());
116 }
117
testCutPaste()118 void Test::testCutPaste()
119 {
120 OUString const sInput("a * b + c");
121 auto xTree = SmParser().Parse(sInput);
122 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
123
124 SmCursor aCursor(xTree.get(), xDocShRef.get());
125 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
126
127 // go to the position at "*"
128 aCursor.Move(pOutputDevice, MoveRight);
129 // select "* b" and then cut
130 aCursor.Move(pOutputDevice, MoveRight, false);
131 aCursor.Move(pOutputDevice, MoveRight, false);
132 aCursor.Cut();
133 // go to the left end and then paste
134 aCursor.Move(pOutputDevice, MoveRight);
135 aCursor.Move(pOutputDevice, MoveRight);
136 aCursor.Paste();
137
138 CPPUNIT_ASSERT_EQUAL(OUString(" { a + c * b } "), xDocShRef->GetText());
139 }
140
testCutSelectPaste()141 void Test::testCutSelectPaste()
142 {
143 OUString const sInput("a * b + c");
144 auto xTree = SmParser().Parse(sInput);
145 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
146
147 SmCursor aCursor(xTree.get(), xDocShRef.get());
148 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
149
150 // go to the right end
151 for (int i=0;i<5;i++)
152 aCursor.Move(pOutputDevice, MoveRight);
153 // select "b + c" and then cut
154 aCursor.Move(pOutputDevice, MoveLeft, false);
155 aCursor.Move(pOutputDevice, MoveLeft, false);
156 aCursor.Move(pOutputDevice, MoveLeft, false);
157 aCursor.Cut();
158 // go to the left end
159 aCursor.Move(pOutputDevice, MoveLeft);
160 aCursor.Move(pOutputDevice, MoveLeft);
161 // select "a" and then paste
162 aCursor.Move(pOutputDevice, MoveRight, false);
163 aCursor.Paste();
164
165 CPPUNIT_ASSERT_EQUAL(OUString(" { b + c * } "), xDocShRef->GetText());
166 }
167
168 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
169
170 }
171
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
173