1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <swmodeltestbase.hxx>
12 #include <vcl/transfer.hxx>
13 #include <editeng/wghtitem.hxx>
14 #include <editeng/postitem.hxx>
15 #include <editeng/udlnitem.hxx>
18 #include <swdtflvr.hxx>
21 #include <fmtanchr.hxx>
23 /// Covers sw/source/uibase/dochdl/ fixes.
24 class SwUibaseDochdlTest
: public SwModelTestBase
28 CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest
, testSelectPasteFormat
)
30 // Create a new document and cut a character.
32 SwDocShell
* pDocShell
= getSwDocShell();
33 SwWrtShell
* pWrtShell
= pDocShell
->GetWrtShell();
34 pWrtShell
->Insert2(u
"x"_ustr
);
35 pWrtShell
->Left(SwCursorSkipMode::Chars
, /*bSelect=*/true, 1, /*bBasicCall=*/false);
36 rtl::Reference
<SwTransferable
> pTransfer
= new SwTransferable(*pWrtShell
);
39 // Decide what format to use when doing a Writer->Writer paste and both RTF and ODF is an
41 TransferableDataHelper
aHelper(pTransfer
);
42 sal_uInt8 nAction
= EXCHG_OUT_ACTION_INSERT_STRING
;
43 SotClipboardFormatId nFormat
= SotClipboardFormatId::RICHTEXT
;
44 SwTransferable::SelectPasteFormat(aHelper
, nAction
, nFormat
);
46 CPPUNIT_ASSERT_EQUAL(EXCHG_OUT_ACTION_INSERT_OLE
, nAction
);
47 // Without the accompanying fix in place, this test would have failed with:
48 // - Expected: 85 (EMBED_SOURCE)
49 // - Actual : 145 (RICHTEXT)
50 // i.e. RTF was selected for Writer->Writer out of process copying, which is worse than ODF.
51 CPPUNIT_ASSERT_EQUAL(SotClipboardFormatId::EMBED_SOURCE
, nFormat
);
54 CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest
, testComplexSelection
)
56 // Given a document where a text node has hints, but no as-char images.
58 SwDocShell
* pDocShell
= getSwDocShell();
59 SwWrtShell
* pWrtShell
= pDocShell
->GetWrtShell();
60 pWrtShell
->Insert2(u
"abc"_ustr
);
61 pWrtShell
->Left(SwCursorSkipMode::Chars
, /*bSelect=*/false, 1, /*bBasicCall=*/false);
62 pWrtShell
->Left(SwCursorSkipMode::Chars
, /*bSelect=*/true, 1, /*bBasicCall=*/false);
63 SfxItemSet
aSet(pWrtShell
->GetView().GetPool(),
64 svl::Items
<RES_CHRATR_BEGIN
, RES_CHRATR_END
- 1>);
65 // Bold, italic, underline.
66 aSet
.Put(SvxWeightItem(WEIGHT_BOLD
, RES_CHRATR_WEIGHT
));
67 aSet
.Put(SvxPostureItem(ITALIC_NORMAL
, RES_CHRATR_POSTURE
));
68 aSet
.Put(SvxUnderlineItem(LINESTYLE_SINGLE
, RES_CHRATR_UNDERLINE
));
69 pWrtShell
->SetAttrSet(aSet
);
70 uno::Reference
<datatransfer::XTransferable2
> xTransfer
= new SwTransferable(*pWrtShell
);
72 // When checking if the selection is complex, then there should be no crash.
73 // Without the accompanying fix in place, this test would have crashed, because we read past the
74 // end of the hints array.
75 CPPUNIT_ASSERT(!xTransfer
->isComplex());
78 CPPUNIT_TEST_FIXTURE(SwUibaseDochdlTest
, testComplexSelectionAtChar
)
80 // Given a document with an at-char anchored image:
82 SwDoc
* pDoc
= getSwDoc();
83 SwDocShell
* pDocShell
= getSwDocShell();
84 SwWrtShell
* pWrtShell
= pDocShell
->GetWrtShell();
85 SfxItemSet
aFrameSet(pDoc
->GetAttrPool(), svl::Items
<RES_FRMATR_BEGIN
, RES_FRMATR_END
- 1>);
86 SwFormatAnchor
aAnchor(RndStdIds::FLY_AT_CHAR
);
87 aFrameSet
.Put(aAnchor
);
89 pWrtShell
->SwFEShell::Insert(OUString(), OUString(), &aGrf
, &aFrameSet
);
90 pWrtShell
->UnSelectFrame();
92 // When checking if the selection is simple or complex:
94 uno::Reference
<datatransfer::XTransferable2
> xTransfer
= new SwTransferable(*pWrtShell
);
95 bool bComplex
= xTransfer
->isComplex();
97 // Then make sure it's complex:
98 // Without the accompanying fix in place, this test would have failed, a selection containing an
99 // image was considered simple.
100 CPPUNIT_ASSERT(bComplex
);
103 CPPUNIT_PLUGIN_IMPLEMENT();
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */