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 <LibreOfficeKit/LibreOfficeKitEnums.h>
13 #include <comphelper/lok.hxx>
14 #include <sfx2/viewsh.hxx>
15 #include <vcl/gdimtf.hxx>
16 #include <vcl/scheduler.hxx>
17 
18 #include <IDocumentStatistics.hxx>
19 #include <fmtanchr.hxx>
20 #include <frameformats.hxx>
21 #include <wrtsh.hxx>
22 #include <unotxdoc.hxx>
23 #include <docsh.hxx>
24 
25 constexpr OUStringLiteral DATA_DIRECTORY = u"/sw/qa/core/txtnode/data/";
26 
27 /// Covers sw/source/core/txtnode/ fixes.
28 class SwCoreTxtnodeTest : public SwModelTestBase
29 {
30 };
31 
CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest,testBtlrCellChinese)32 CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testBtlrCellChinese)
33 {
34     // Load a document with a table cell, with btlr cell direction.  The cell has text which is
35     // classified as vertical, i.e. the glyph has the same direction in both the lrtb ("Latin") and
36     // tbrl ("Chinese") directions. Make sure that Chinese text is handled the same way in the btlr
37     // case as it's handled in the Latin case.
38     load(DATA_DIRECTORY, "btlr-cell-chinese.doc");
39     SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
40     SwDocShell* pShell = pTextDoc->GetDocShell();
41     std::shared_ptr<GDIMetaFile> xMetaFile = pShell->GetPreviewMetaFile();
42     MetafileXmlDump dumper;
43     xmlDocUniquePtr pXmlDoc = dumpAndParse(dumper, *xMetaFile);
44     assertXPath(pXmlDoc, "//font[1]", "orientation", "900");
45     // Without the accompanying fix in place, this test would have failed with:
46     // - Expected: false
47     // - Actual  : true
48     // i.e. the glyph was rotated further, so it was upside down.
49     assertXPath(pXmlDoc, "//font[1]", "vertical", "false");
50 }
51 
CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest,testTextBoxCopyAnchor)52 CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testTextBoxCopyAnchor)
53 {
54     load(DATA_DIRECTORY, "textbox-copy-anchor.docx");
55     SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
56     SwDocShell* pShell = pTextDoc->GetDocShell();
57     SwWrtShell* pWrtShell = pShell->GetWrtShell();
58     SwDoc aClipboard;
59     pWrtShell->SelAll();
60     pWrtShell->Copy(aClipboard);
61     pWrtShell->SttEndDoc(/*bStart=*/false);
62     pWrtShell->Paste(aClipboard);
63 
64     const SwFrameFormats& rFormats = *pShell->GetDoc()->GetSpzFrameFormats();
65     // Without the accompanying fix in place, this test would have failed with:
66     // - Expected: 4
67     // - Actual  : 6
68     // i.e. 2 fly frames were copied twice.
69     CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), rFormats.size());
70 
71     SwPosition aDrawAnchor1 = *rFormats[0]->GetAnchor().GetContentAnchor();
72     SwPosition aFlyAnchor1 = *rFormats[1]->GetAnchor().GetContentAnchor();
73     CPPUNIT_ASSERT_EQUAL(aFlyAnchor1.nNode, aDrawAnchor1.nNode);
74     SwPosition aDrawAnchor2 = *rFormats[2]->GetAnchor().GetContentAnchor();
75     SwPosition aFlyAnchor2 = *rFormats[3]->GetAnchor().GetContentAnchor();
76     // This also failed, aFlyAnchor2 was wrong, as it got out of sync with aDrawAnchor2.
77     CPPUNIT_ASSERT_EQUAL(aFlyAnchor2.nNode, aDrawAnchor2.nNode);
78 }
79 
CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest,testTextBoxNodeSplit)80 CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testTextBoxNodeSplit)
81 {
82     load(DATA_DIRECTORY, "textbox-node-split.docx");
83     SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
84     SwDocShell* pShell = pTextDoc->GetDocShell();
85     SwWrtShell* pWrtShell = pShell->GetWrtShell();
86     pWrtShell->SttEndDoc(/*bStart=*/false);
87     // Without the accompanying fix in place, this would have crashed in
88     // SwFlyAtContentFrame::SwClientNotify().
89     pWrtShell->SplitNode();
90 }
91 
92 namespace
93 {
94 struct ViewCallback
95 {
96     int m_nInvalidations = 0;
97 
98     static void callback(int nType, const char* pPayload, void* pData);
99     void callbackImpl(int nType, const char* pPayload);
100 };
101 
callback(int nType,const char * pPayload,void * pData)102 void ViewCallback::callback(int nType, const char* pPayload, void* pData)
103 {
104     static_cast<ViewCallback*>(pData)->callbackImpl(nType, pPayload);
105 }
106 
callbackImpl(int nType,const char *)107 void ViewCallback::callbackImpl(int nType, const char* /*pPayload*/)
108 {
109     switch (nType)
110     {
111         case LOK_CALLBACK_INVALIDATE_TILES:
112         {
113             ++m_nInvalidations;
114         }
115         break;
116     }
117 }
118 }
119 
CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest,testTitleFieldInvalidate)120 CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testTitleFieldInvalidate)
121 {
122     // Set up LOK to track invalidations.
123     comphelper::LibreOfficeKit::setActive(true);
124 
125     // Given a document with a title field:
126     load(DATA_DIRECTORY, "title-field-invalidate.fodt");
127     SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
128     pTextDoc->initializeForTiledRendering({});
129     SwDocShell* pShell = pTextDoc->GetDocShell();
130     SwDoc* pDoc = pShell->GetDoc();
131     SwWrtShell* pWrtShell = pShell->GetWrtShell();
132     pWrtShell->SttEndDoc(/*bStt=*/false);
133     ViewCallback aCallback;
134     pWrtShell->GetSfxViewShell()->registerLibreOfficeKitViewCallback(&ViewCallback::callback,
135                                                                      &aCallback);
136     Scheduler::ProcessEventsToIdle();
137     aCallback.m_nInvalidations = 0;
138 
139     // When typing to the document:
140     pWrtShell->Insert("x");
141 
142     // Then make sure that only the text frame at the cursor is invalidated:
143     pDoc->getIDocumentStatistics().GetUpdatedDocStat(/*bCompleteAsync=*/true, /*bFields=*/false);
144     // Without the accompanying fix in place, this test would have failed with:
145     // - Expected: 1
146     // - Actual  : 2
147     // i.e. the footer was also invalidated on each keypress.
148     CPPUNIT_ASSERT_EQUAL(1, aCallback.m_nInvalidations);
149 
150     // Tear down LOK.
151     pWrtShell->GetSfxViewShell()->registerLibreOfficeKitViewCallback(nullptr, nullptr);
152     mxComponent->dispose();
153     mxComponent.clear();
154     comphelper::LibreOfficeKit::setActive(false);
155 }
156 
157 CPPUNIT_PLUGIN_IMPLEMENT();
158 
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
160