/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include constexpr OUStringLiteral DATA_DIRECTORY = u"/sw/qa/core/doc/data/"; /// Covers sw/source/core/doc/ fixes. class SwCoreDocTest : public SwModelTestBase { }; CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testMathInsertAnchorType) { // Given an empty document. SwDoc* pDoc = createSwDoc(); // When inserting an a math object. SwWrtShell* pShell = pDoc->GetDocShell()->GetWrtShell(); SvGlobalName aGlobalName(SO3_SM_CLASSID); pShell->InsertObject(svt::EmbeddedObjectRef(), &aGlobalName); // Then the anchor type should be as-char. SwFrameFormats& rFormats = *pDoc->GetSpzFrameFormats(); CPPUNIT_ASSERT_EQUAL(static_cast(1), rFormats.size()); const SwFrameFormat& rFormat = *rFormats[0]; const SwFormatAnchor& rAnchor = rFormat.GetAnchor(); // Without the accompanying fix in place, this test would have failed with: // - Expected: 1 // - Actual : 4 // i.e. the anchor type was at-char, not as-char. CPPUNIT_ASSERT_EQUAL(RndStdIds::FLY_AS_CHAR, rAnchor.GetAnchorId()); ErrorRegistry::Reset(); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testTextboxTextRotateAngle) { // Check the writing direction of the only TextFrame in the document. SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "textbox-textrotateangle.odt"); SwFrameFormats& rFrameFormats = *pDoc->GetSpzFrameFormats(); CPPUNIT_ASSERT_EQUAL(static_cast(2), rFrameFormats.size()); CPPUNIT_ASSERT_EQUAL(o3tl::narrowing(RES_DRAWFRMFMT), rFrameFormats[0]->Which()); CPPUNIT_ASSERT_EQUAL(o3tl::narrowing(RES_FLYFRMFMT), rFrameFormats[1]->Which()); SvxFrameDirection eActual = rFrameFormats[1]->GetAttrSet().GetItem(RES_FRAMEDIR)->GetValue(); // Without the accompanying fix in place, this test would have failed with: // - Expected: 5 (btlr) // - Actual : 0 (lrtb) // i.e. the writing direction was in the ODT file, but it was lost on import in the textbox // case. CPPUNIT_ASSERT_EQUAL(SvxFrameDirection::Vertical_LR_BT, eActual); ErrorRegistry::Reset(); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testNumDownIndent) { SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "num-down-indent.docx"); SwDocShell* pDocShell = pDoc->GetDocShell(); SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); pWrtShell->Down(/*bSelect=*/false); SwEditWin& rEditWin = pDocShell->GetView()->GetEditWin(); KeyEvent aKeyEvent(0, KEY_TAB); rEditWin.KeyInput(aKeyEvent); SwTextNode* pTextNode = pWrtShell->GetCursor()->GetNode().GetTextNode(); // Without the accompanying fix in place, this test would have failed with: // - Expected: \tB // - Actual : B // i.e. pressing at the start of the paragraph did not change the layout. CPPUNIT_ASSERT_EQUAL(OUString("\tB"), pTextNode->GetText()); ErrorRegistry::Reset(); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testLocaleIndependentTemplate) { SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "locale-independent-template.odt"); SwDocShell* pDocShell = pDoc->GetDocShell(); SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); SfxItemSet aSet(pWrtShell->GetAttrPool(), svl::Items); pWrtShell->GetCurAttr(aSet); const SvxLanguageItem* pItem = aSet.GetItem(RES_CHRATR_LANGUAGE); CPPUNIT_ASSERT(pItem); LanguageType eLang = pItem->GetValue(); // Without the accompanying fix in place, this test would have failed with: // - Expected: 1033 (LANGUAGE_ENGLISH_US) // - Actual : 1023 (LANGUAGE_DONTKNOW) // i.e. the status bar and the format -> character dialog didn't fall back to the UI locale when // an explicit language was not set for the document. CPPUNIT_ASSERT_EQUAL(LANGUAGE_ENGLISH_US, eLang); ErrorRegistry::Reset(); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testTextBoxZOrder) { SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "textbox-zorder.docx"); SwFrameFormats& rFormats = *pDoc->GetSpzFrameFormats(); CPPUNIT_ASSERT_EQUAL(static_cast(3), rFormats.size()); const SwFrameFormat* pEllipse = rFormats[2]; const SdrObject* pEllipseShape = pEllipse->FindRealSdrObject(); // Make sure we test the right shape. CPPUNIT_ASSERT_EQUAL(OUString("Shape3"), pEllipseShape->GetName()); // Without the accompanying fix in place, this test would have failed with: // - Expected: 2 // - Actual : 1 // i.e. the ellipse was under the frame of the shape-frame pair, not on top of it. CPPUNIT_ASSERT_EQUAL(static_cast(2), pEllipseShape->GetOrdNum()); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testTextBoxMakeFlyFrame) { // Given a document with an as-char textbox (as-char draw format + at-char fly format): SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "textbox-makeflyframe.docx"); // When cutting the textbox and pasting it to a new document: SwView* pView = pDoc->GetDocShell()->GetView(); pView->GetViewFrame()->GetDispatcher()->Execute(FN_CNTNT_TO_NEXT_FRAME, SfxCallMode::SYNCHRON); pView->StopShellTimer(); SwDocShell* pDocShell = pDoc->GetDocShell(); SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); rtl::Reference pTransfer = new SwTransferable(*pWrtShell); pTransfer->Cut(); TransferableDataHelper aHelper(pTransfer); uno::Reference xDoc2 = loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument", {}); SwXTextDocument* pTextDoc2 = dynamic_cast(xDoc2.get()); SwDocShell* pDocShell2 = pTextDoc2->GetDocShell(); SwWrtShell* pWrtShell2 = pDocShell2->GetWrtShell(); SwTransferable::Paste(*pWrtShell2, aHelper); // Then make sure its fly frame is created. mxComponent->dispose(); mxComponent = xDoc2; xmlDocUniquePtr pLayout = parseLayoutDump(); // Without the accompanying fix in place, this test would have failed, because the first text // frame in the body frame had an SwAnchoredDrawObject anchored to it, but not a fly frame, so // a blank square was painted, not the image. assertXPath(pLayout, "/root/page/body/txt/anchored/fly", 1); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testIMEGrouping) { // TODO figure out why the ext text input in this test code reaches the wrong window on // non-headless. #if !defined MACOSX && !defined _WIN32 // Given an empty document: SwDoc* pDoc = createSwDoc(); // Make sure no idle is in action, so the ExtTextInput events go to SwEditWin. Scheduler::ProcessEventsToIdle(); // When pressing two keys via IME: SwDocShell* pDocShell = pDoc->GetDocShell(); SwEditWin& rEditWin = pDocShell->GetView()->GetEditWin(); rEditWin.PostExtTextInputEvent(VclEventId::ExtTextInput, "a"); rEditWin.PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); rEditWin.PostExtTextInputEvent(VclEventId::ExtTextInput, "b"); rEditWin.PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); // Then make sure that gets grouped together to a single undo action: SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); SwTextNode* pTextNode = pWrtShell->GetCursor()->GetNode().GetTextNode(); CPPUNIT_ASSERT_EQUAL(OUString("ab"), pTextNode->GetText()); // Without the accompanying fix in place, this test would have failed with: // - Expected: 1 // - Actual : 2 // i.e. 2 subsequent IME events got their own undo actions. CPPUNIT_ASSERT_EQUAL(static_cast(1), pDoc->GetUndoManager().GetUndoActionCount()); #endif } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testImageHyperlinkStyle) { // Given a document with an image with a hyperlink: loadURL("private:factory/swriter", nullptr); uno::Reference xFactory(mxComponent, uno::UNO_QUERY); uno::Reference xDocument(mxComponent, uno::UNO_QUERY); uno::Reference xText = xDocument->getText(); uno::Reference xCursor = xText->createTextCursor(); uno::Reference xImage( xFactory->createInstance("com.sun.star.text.TextGraphicObject"), uno::UNO_QUERY); xText->insertTextContent(xCursor, xImage, /*bAbsorb=*/false); uno::Reference xImageProps(xImage, uno::UNO_QUERY); OUString aExpected = "http://www.example.com"; xImageProps->setPropertyValue("HyperLinkURL", uno::Any(aExpected)); // When applying a frame style on it: xImageProps->setPropertyValue("FrameStyleName", uno::Any(OUString("Frame"))); // Then make sure that the hyperlink is not lost: auto aActual = getProperty(xImageProps, "HyperLinkURL"); // Without the accompanying fix in place, this test would have failed with: // - Expected: http://www.example.com // - Actual : // i.e. the link was lost, even if the frame style dialog doesn't allow specifying a link on // frames. CPPUNIT_ASSERT_EQUAL(aExpected, aActual); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testContentControlDelete) { // Given a document with a content control: SwDoc* pDoc = createSwDoc(); uno::Reference xMSF(mxComponent, uno::UNO_QUERY); uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); uno::Reference xText = xTextDocument->getText(); uno::Reference xCursor = xText->createTextCursor(); xText->insertString(xCursor, "test", /*bAbsorb=*/false); xCursor->gotoStart(/*bExpand=*/false); xCursor->gotoEnd(/*bExpand=*/true); uno::Reference xContentControl( xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); // When deleting the dummy character at the end of the content control: SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); pWrtShell->SttEndDoc(/*bStt=*/false); pWrtShell->DelLeft(); // Then make sure that we only enter the content control, to be consistent with the start dummy // character: SwTextNode* pTextNode = pWrtShell->GetCursor()->GetMark()->nNode.GetNode().GetTextNode(); // Without the accompanying fix in place, this test would have failed with: // - Expected: ^Atest^A // - Actual : ^Atest // i.e. the end dummy character got deleted, but not the first one, which is inconsistent. CPPUNIT_ASSERT_EQUAL(OUString("\x0001test\x0001"), pTextNode->GetText()); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testLinkedStyleDelete) { // Given a document with linked styles: myparastyle is linked to mycharstyle and vica versa: createSwDoc(); uno::Reference xFactory(mxComponent, uno::UNO_QUERY); uno::Reference xParaStyle( xFactory->createInstance("com.sun.star.style.ParagraphStyle"), uno::UNO_QUERY); uno::Reference xCharStyle( xFactory->createInstance("com.sun.star.style.CharacterStyle"), uno::UNO_QUERY); uno::Reference xParaStyles(getStyles("ParagraphStyles"), uno::UNO_QUERY); xParaStyles->insertByName("myparastyle", uno::Any(xParaStyle)); uno::Reference xCharStyles(getStyles("CharacterStyles"), uno::UNO_QUERY); xCharStyles->insertByName("mycharstyle", uno::Any(xCharStyle)); xParaStyle->setPropertyValue("LinkStyle", uno::Any(OUString("mycharstyle"))); xCharStyle->setPropertyValue("LinkStyle", uno::Any(OUString("myparastyle"))); // When deleting the paragraph style (and only that): xParaStyles->removeByName("myparastyle"); // Then make sure we don't crash on save: uno::Reference xStorable(mxComponent, uno::UNO_QUERY); uno::Sequence aArgs = { comphelper::makePropertyValue("FilterName", OUString("writer8")), }; xStorable->storeAsURL(maTempFile.GetURL(), aArgs); } namespace { /// This selection listener calls getAnchor() on selection change, which creates UNO cursors and is /// invoked in the middle of a bookmark deletion. struct SelectionChangeListener : public cppu::WeakImplHelper { uno::Reference m_xBookmarks; std::vector> m_aAnchors; public: SelectionChangeListener(const uno::Reference& xBookmarks); // view::XSelectionChangeListener void SAL_CALL selectionChanged(const lang::EventObject& rEvent) override; // lang::XEventListener void SAL_CALL disposing(const lang::EventObject& rSource) override; }; } SelectionChangeListener::SelectionChangeListener( const uno::Reference& xBookmarks) : m_xBookmarks(xBookmarks) { } void SelectionChangeListener::selectionChanged(const lang::EventObject& /*rEvent*/) { uno::Sequence aElementNames = m_xBookmarks->getElementNames(); for (const auto& rName : aElementNames) { uno::Reference xTextContent(m_xBookmarks->getByName(rName), uno::UNO_QUERY); m_aAnchors.push_back(xTextContent->getAnchor()); } } void SelectionChangeListener::disposing(const lang::EventObject& /*rSource*/) {} CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testBookmarkDeleteListeners) { // Given a document with 2 bookmarks: createSwDoc(); uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); uno::Reference xText = xTextDocument->getText(); uno::Reference xCursor = xText->createTextCursor(); { xText->insertString(xCursor, "test", /*bAbsorb=*/false); xCursor->gotoStart(/*bExpand=*/false); xCursor->gotoEnd(/*bExpand=*/true); uno::Reference xFactory(mxComponent, uno::UNO_QUERY); uno::Reference xBookmark( xFactory->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY); uno::Reference xBookmarkNamed(xBookmark, uno::UNO_QUERY); xBookmarkNamed->setName("mybookmark"); xText->insertTextContent(xCursor, xBookmark, /*bAbsorb=*/true); } { xCursor->gotoEnd(/*bExpand=*/false); xText->insertString(xCursor, "test2", /*bAbsorb=*/false); xCursor->goLeft(4, /*bExpand=*/true); uno::Reference xFactory(mxComponent, uno::UNO_QUERY); uno::Reference xBookmark( xFactory->createInstance("com.sun.star.text.Bookmark"), uno::UNO_QUERY); uno::Reference xBookmarkNamed(xBookmark, uno::UNO_QUERY); xBookmarkNamed->setName("mybookmark2"); xText->insertTextContent(xCursor, xBookmark, /*bAbsorb=*/true); } uno::Reference xBookmarksSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xBookmarks = xBookmarksSupplier->getBookmarks(); // When registering a selection listener that creates uno marks: uno::Reference xModel(mxComponent, uno::UNO_QUERY); uno::Reference xController(xModel->getCurrentController(), uno::UNO_QUERY); xController->addSelectionChangeListener(new SelectionChangeListener(xBookmarks)); // Then make sure that deleting a bookmark doesn't crash: uno::Reference xBookmark(xBookmarks->getByName("mybookmark2"), uno::UNO_QUERY); // Without the accompanying fix in place, this test would have crashed, an invalidated iterator // was used with erase(). xBookmark->dispose(); } CPPUNIT_TEST_FIXTURE(SwCoreDocTest, testCopyBookmarks) { // Given a document with a bookmark in a header that is linked later: SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "copy-bookmarks.docx"); // When checking the # of non-copy bookmarks in the resulting doc model: sal_Int32 nActual = 0; for (auto it = pDoc->getIDocumentMarkAccess()->getBookmarksBegin(); it != pDoc->getIDocumentMarkAccess()->getBookmarksEnd(); ++it) { if ((*it)->GetName().indexOf("Copy") == -1) { ++nActual; } } // Then make sure we have a single non-copy bookmark, with no duplications: // Without the accompanying fix in place, this test would have failed with: // - Expected: 1 // - Actual : 2 // i.e. the 2nd header had a duplicated bookmark without "Copy" in its name. CPPUNIT_ASSERT_EQUAL(static_cast(1), nActual); // Also, when checking the # of non-copy images in the resulting doc model: nActual = 0; SwFrameFormats& rFrameFormats = *pDoc->GetSpzFrameFormats(); for (size_t i = 0; i < rFrameFormats.size(); ++i) { if (rFrameFormats[i]->GetName().indexOf("Copy") == -1) { ++nActual; } } // Then make sure we have a single non-copy image, with no duplications: // Without the accompanying fix in place, this test would have failed with: // - Expected: 1 // - Actual : 2 // i.e. the 2nd header had a duplicated image without "Copy" in its name. CPPUNIT_ASSERT_EQUAL(static_cast(1), nActual); } CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */