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->DoClose();
65 xDocShRef.clear();
66 BootstrapFixture::tearDown();
67 }
68
testCopyPaste()69 void Test::testCopyPaste()
70 {
71 auto xTree = SmParser5().Parse("a * b + c");
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 auto xTree = SmParser5().Parse("a * b + c");
94 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
95
96 SmCursor aCursor(xTree.get(), xDocShRef.get());
97 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
98
99 // go to the right end
100 for (int i = 0; i < 5; i++)
101 aCursor.Move(pOutputDevice, MoveRight);
102 // select "b + c" and then copy
103 aCursor.Move(pOutputDevice, MoveLeft, false);
104 aCursor.Move(pOutputDevice, MoveLeft, false);
105 aCursor.Move(pOutputDevice, MoveLeft, false);
106 aCursor.Copy();
107 // go to the left end
108 aCursor.Move(pOutputDevice, MoveLeft);
109 aCursor.Move(pOutputDevice, MoveLeft);
110 // select "a" and then paste
111 aCursor.Move(pOutputDevice, MoveRight, false);
112 aCursor.Paste();
113
114 CPPUNIT_ASSERT_EQUAL(OUString("{ b + c * b + c }"), xDocShRef->GetText());
115 }
116
testCutPaste()117 void Test::testCutPaste()
118 {
119 auto xTree = SmParser5().Parse("a * b + c");
120 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
121
122 SmCursor aCursor(xTree.get(), xDocShRef.get());
123 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
124
125 // go to the position at "*"
126 aCursor.Move(pOutputDevice, MoveRight);
127 // select "* b" and then cut
128 aCursor.Move(pOutputDevice, MoveRight, false);
129 aCursor.Move(pOutputDevice, MoveRight, false);
130 aCursor.Cut();
131 // go to the left end and then paste
132 aCursor.Move(pOutputDevice, MoveRight);
133 aCursor.Move(pOutputDevice, MoveRight);
134 aCursor.Paste();
135
136 CPPUNIT_ASSERT_EQUAL(OUString("{ a + c * b }"), xDocShRef->GetText());
137 }
138
testCutSelectPaste()139 void Test::testCutSelectPaste()
140 {
141 auto xTree = SmParser5().Parse("a * b + c");
142 xTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
143
144 SmCursor aCursor(xTree.get(), xDocShRef.get());
145 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
146
147 // go to the right end
148 for (int i = 0; i < 5; i++)
149 aCursor.Move(pOutputDevice, MoveRight);
150 // select "b + c" and then cut
151 aCursor.Move(pOutputDevice, MoveLeft, false);
152 aCursor.Move(pOutputDevice, MoveLeft, false);
153 aCursor.Move(pOutputDevice, MoveLeft, false);
154 aCursor.Cut();
155 // go to the left end
156 aCursor.Move(pOutputDevice, MoveLeft);
157 aCursor.Move(pOutputDevice, MoveLeft);
158 // select "a" and then paste
159 aCursor.Move(pOutputDevice, MoveRight, false);
160 aCursor.Paste();
161
162 CPPUNIT_ASSERT_EQUAL(OUString("{ b + c * }"), xDocShRef->GetText());
163 }
164
165 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
166 }
167
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
169