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 <sfx2/dispatch.hxx>
13 #include <sfx2/viewfrm.hxx>
14 #include <vcl/GraphicObject.hxx>
15 #include <vcl/gdimtf.hxx>
16 #include <svx/svdpage.hxx>
17 #include <svx/svdview.hxx>
18 #include <editeng/eeitem.hxx>
19 #include <editeng/adjustitem.hxx>
20 #include <editeng/outlobj.hxx>
21 #include <editeng/editobj.hxx>
22 #include <editeng/editids.hrc>
23 #include <svx/sdtaitm.hxx>
24 
25 #include <IDocumentContentOperations.hxx>
26 #include <cmdid.h>
27 #include <fmtanchr.hxx>
28 #include <view.hxx>
29 #include <wrtsh.hxx>
30 #include <IDocumentDrawModelAccess.hxx>
31 #include <drawdoc.hxx>
32 
33 static char const DATA_DIRECTORY[] = "/sw/qa/uibase/shells/data/";
34 
35 /// Covers sw/source/uibase/shells/ fixes.
36 class SwUibaseShellsTest : public SwModelTestBase
37 {
38 public:
39     SwDoc* createDoc(const char* pName = nullptr);
40 };
41 
createDoc(const char * pName)42 SwDoc* SwUibaseShellsTest::createDoc(const char* pName)
43 {
44     if (!pName)
45         loadURL("private:factory/swriter", nullptr);
46     else
47         load(DATA_DIRECTORY, pName);
48 
49     SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
50     CPPUNIT_ASSERT(pTextDoc);
51     return pTextDoc->GetDocShell()->GetDoc();
52 }
53 
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest,testTdf130179)54 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testTdf130179)
55 {
56     SwDoc* pDoc = createDoc();
57     IDocumentContentOperations& rIDCO = pDoc->getIDocumentContentOperations();
58     SwCursorShell* pShell(pDoc->GetEditShell());
59     SfxItemSet aFrameSet(pDoc->GetAttrPool(), svl::Items<RES_FRMATR_BEGIN, RES_FRMATR_END - 1>{});
60     SfxItemSet aGrfSet(pDoc->GetAttrPool(), svl::Items<RES_GRFATR_BEGIN, RES_GRFATR_END - 1>{});
61     SwFormatAnchor aAnchor(RndStdIds::FLY_AT_PARA);
62     aFrameSet.Put(aAnchor);
63     GraphicObject aGrf;
64     CPPUNIT_ASSERT(rIDCO.InsertGraphicObject(*pShell->GetCursor(), aGrf, &aFrameSet, &aGrfSet));
65     CPPUNIT_ASSERT_EQUAL(size_t(1), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
66 
67     SwView* pView = pDoc->GetDocShell()->GetView();
68     pView->GetViewFrame()->GetDispatcher()->Execute(FN_CNTNT_TO_NEXT_FRAME, SfxCallMode::SYNCHRON);
69     // Make sure SwTextShell is replaced with SwDrawShell right now, not after 120 ms, as set in the
70     // SwView ctor.
71     pView->StopShellTimer();
72 
73     std::unique_ptr<SfxPoolItem> pItem;
74     pView->GetViewFrame()->GetBindings().QueryState(FN_POSTIT, pItem);
75     // Without the accompanying fix in place, this test would have failed with:
76     // assertion failed
77     // - Expression: !pItem
78     // i.e. comment insertion was enabled for an at-para anchored image.
79     CPPUNIT_ASSERT(!pItem);
80 }
81 
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest,testShapeTextAlignment)82 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testShapeTextAlignment)
83 {
84 // FIXME find out why this fails on macOS/Windows
85 #if !defined(MACOSX) && !defined(_WIN32)
86     // Create a document with a rectangle in it.
87     SwDoc* pDoc = createDoc();
88     SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
89     Point aStartPos(1000, 1000);
90     pWrtShell->BeginCreate(static_cast<sal_uInt16>(OBJ_RECT), aStartPos);
91     Point aMovePos(2000, 2000);
92     pWrtShell->MoveCreate(aMovePos);
93     pWrtShell->EndCreate(SdrCreateCmd::ForceEnd);
94 
95     // Start shape text edit.
96     SwView* pView = pDoc->GetDocShell()->GetView();
97     // Select the shape.
98     pView->GetViewFrame()->GetDispatcher()->Execute(FN_CNTNT_TO_NEXT_FRAME, SfxCallMode::SYNCHRON);
99     pView->StopShellTimer();
100     // Start the actual text edit.
101     SdrPage* pPage = pWrtShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
102     CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pPage->GetObjCount());
103     SdrObject* pObject = pPage->GetObj(0);
104     pView->EnterShapeDrawTextMode(pObject);
105     pView->AttrChangedNotify(nullptr);
106 
107     // Change paragraph adjustment to center.
108     pView->GetViewFrame()->GetDispatcher()->Execute(SID_ATTR_PARA_ADJUST_CENTER,
109                                                     SfxCallMode::SYNCHRON);
110 
111     // End shape text edit.
112     pWrtShell->EndTextEdit();
113 
114     const OutlinerParaObject* pOutliner = pObject->GetOutlinerParaObject();
115     // Without the accompanying fix in place, this test would have failed, because the shape had no
116     // text or text formatting. In other words the paragraph adjustment command was ignored.
117     CPPUNIT_ASSERT(pOutliner);
118     const SfxItemSet& rParaAttribs = pOutliner->GetTextObject().GetParaAttribs(0);
119     SvxAdjust eAdjust = rParaAttribs.GetItem(EE_PARA_JUST)->GetAdjust();
120     CPPUNIT_ASSERT_EQUAL(SvxAdjust::Center, eAdjust);
121 #endif
122 }
123 
124 CPPUNIT_PLUGIN_IMPLEMENT();
125 
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
127