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 <swmodeltestbase.hxx>
11
12 #include <vcl/transfer.hxx>
13 #include <editeng/wghtitem.hxx>
14 #include <editeng/postitem.hxx>
15 #include <editeng/udlnitem.hxx>
16
17 #include <docsh.hxx>
18 #include <swdtflvr.hxx>
19 #include <wrtsh.hxx>
20 #include <view.hxx>
21
22 /// Covers sw/source/uibase/dochdl/ fixes.
23 class SwUibaseDochdlTest : public SwModelTestBase
24 {
25 };
26
CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest,testSelectPasteFormat)27 CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest, testSelectPasteFormat)
28 {
29 // Create a new document and cut a character.
30 SwDoc* pDoc = createSwDoc();
31 SwDocShell* pDocShell = pDoc->GetDocShell();
32 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
33 pWrtShell->Insert2("x");
34 pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/true, 1, /*bBasicCall=*/false);
35 rtl::Reference<SwTransferable> pTransfer = new SwTransferable(*pWrtShell);
36 pTransfer->Cut();
37
38 // Decide what format to use when doing a Writer->Writer paste and both RTF and ODF is an
39 // available format.
40 TransferableDataHelper aHelper(pTransfer);
41 sal_uInt8 nAction = EXCHG_OUT_ACTION_INSERT_STRING;
42 SotClipboardFormatId nFormat = SotClipboardFormatId::RICHTEXT;
43 SwTransferable::SelectPasteFormat(aHelper, nAction, nFormat);
44
45 CPPUNIT_ASSERT_EQUAL(EXCHG_OUT_ACTION_INSERT_OLE, nAction);
46 // Without the accompanying fix in place, this test would have failed with:
47 // - Expected: 85 (EMBED_SOURCE)
48 // - Actual : 145 (RICHTEXT)
49 // i.e. RTF was selected for Writer->Writer out of process copying, which is worse than ODF.
50 CPPUNIT_ASSERT_EQUAL(SotClipboardFormatId::EMBED_SOURCE, nFormat);
51 }
52
CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest,testComplexSelection)53 CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest, testComplexSelection)
54 {
55 // Given a document where a text node has hints, but no as-char images.
56 SwDoc* pDoc = createSwDoc();
57 SwDocShell* pDocShell = pDoc->GetDocShell();
58 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
59 pWrtShell->Insert2("abc");
60 pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false);
61 pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/true, 1, /*bBasicCall=*/false);
62 SfxItemSet aSet(pWrtShell->GetView().GetPool(),
63 svl::Items<RES_CHRATR_BEGIN, RES_CHRATR_END - 1>{});
64 // Bold, italic, underline.
65 aSet.Put(SvxWeightItem(WEIGHT_BOLD, RES_CHRATR_WEIGHT));
66 aSet.Put(SvxPostureItem(ITALIC_NORMAL, RES_CHRATR_POSTURE));
67 aSet.Put(SvxUnderlineItem(LINESTYLE_SINGLE, RES_CHRATR_UNDERLINE));
68 pWrtShell->SetAttrSet(aSet);
69 uno::Reference<datatransfer::XTransferable2> xTransfer = new SwTransferable(*pWrtShell);
70
71 // When checking if the selection is complex, then there should be no crash.
72 // Without the accompanying fix in place, this test would have crashed, because we read past the
73 // end of the hints array.
74 CPPUNIT_ASSERT(!xTransfer->isComplex());
75 }
76
77 CPPUNIT_PLUGIN_IMPLEMENT();
78
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
80