From 94874973769fee81f8ab35b2aeb0dc22812a2b48 Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Tue, 8 Jan 2013 01:20:22 -0500 Subject: Add new ods import test for rich-text cell contents. Change-Id: I23eb8d53b8a2c48514900a5ab07ed7ec71d3ef99 --- sc/qa/unit/data/ods/rich-text-cells.ods | Bin 0 -> 11904 bytes sc/qa/unit/subsequent_filters-test.cxx | 183 ++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 sc/qa/unit/data/ods/rich-text-cells.ods (limited to 'sc/qa') diff --git a/sc/qa/unit/data/ods/rich-text-cells.ods b/sc/qa/unit/data/ods/rich-text-cells.ods new file mode 100644 index 000000000000..d60747a05c53 Binary files /dev/null and b/sc/qa/unit/data/ods/rich-text-cells.ods differ diff --git a/sc/qa/unit/subsequent_filters-test.cxx b/sc/qa/unit/subsequent_filters-test.cxx index 04a7ffeee618..f56bd6676e47 100644 --- a/sc/qa/unit/subsequent_filters-test.cxx +++ b/sc/qa/unit/subsequent_filters-test.cxx @@ -40,6 +40,10 @@ #include #include "svx/svdpage.hxx" +#include "editeng/wghtitem.hxx" +#include "editeng/postitem.hxx" +#include "editeng/udlnitem.hxx" +#include "editeng/editobj.hxx" #include #include #include @@ -59,6 +63,7 @@ #include #include #include +#include #define CALC_DEBUG_OUTPUT 0 #define TEST_BUG_FILES 0 @@ -171,6 +176,7 @@ public: void testFormulaDependency(); void testRowHeightODS(); + void testRichTextContentODS(); CPPUNIT_TEST_SUITE(ScFiltersTest); CPPUNIT_TEST(testRangeNameXLS); @@ -215,6 +221,7 @@ public: CPPUNIT_TEST(testPivotTableBasicODS); CPPUNIT_TEST(testRowHeightODS); CPPUNIT_TEST(testFormulaDependency); + CPPUNIT_TEST(testRichTextContentODS); //disable testPassword on MacOSX due to problems with libsqlite3 //also crashes on DragonFly due to problems with nss/nspr headers @@ -1516,6 +1523,182 @@ void ScFiltersTest::testRowHeightODS() xDocSh->DoClose(); } +void ScFiltersTest::testRichTextContentODS() +{ + ScDocShellRef xDocSh = loadDoc("rich-text-cells.", ODS); + CPPUNIT_ASSERT_MESSAGE("Failed to load rich-text-cells.ods", xDocSh.Is()); + ScDocument* pDoc = xDocSh->GetDocument(); + + OUString aTabName; + CPPUNIT_ASSERT_MESSAGE("Failed to get the name of the first sheet.", pDoc->GetName(0, aTabName)); + + // All tested cells are in the first column. + ScAddress aPos(0, 0, 0); + + // Normal simple string with no formatting. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_STRING, pDoc->GetCellType(aPos)); + CPPUNIT_ASSERT_EQUAL(OUString("Normal"), pDoc->GetString(aPos.Col(), aPos.Row(), aPos.Tab())); + + // Normal string with bold applied to the whole cell. + { + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_STRING, pDoc->GetCellType(aPos)); + CPPUNIT_ASSERT_EQUAL(OUString("All bold"), pDoc->GetString(aPos.Col(), aPos.Row(), aPos.Tab())); + const ScPatternAttr* pAttr = pDoc->GetPattern(aPos.Col(), aPos.Row(), aPos.Tab()); + CPPUNIT_ASSERT_MESSAGE("Failed to get cell attribute.", pAttr); + const SvxWeightItem& rWeightItem = + static_cast(pAttr->GetItem(ATTR_FONT_WEIGHT)); + CPPUNIT_ASSERT_EQUAL(WEIGHT_BOLD, rWeightItem.GetWeight()); + } + + // This cell has an unformatted but multi-line content. Multi-line text is + // stored in edit cell even if it has no formatting applied. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + const EditTextObject* pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(3), pEditText->GetParagraphCount()); + OUString aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_EQUAL(OUString("one"), aParaText); + aParaText = pEditText->GetText(1); + CPPUNIT_ASSERT_EQUAL(OUString("two"), aParaText); + aParaText = pEditText->GetText(2); + CPPUNIT_ASSERT_EQUAL(OUString("three"), aParaText); + + // Cell with sheet name field item. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(1), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_MESSAGE("Unexpected text.", aParaText.indexOf("Sheet name is ") == 0); + CPPUNIT_ASSERT_MESSAGE("Sheet name field item not found.", pEditText->HasField(text::textfield::Type::TABLE)); + + // Cell with URL field item. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(1), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_MESSAGE("Unexpected text.", aParaText.indexOf("URL: ") == 0); + CPPUNIT_ASSERT_MESSAGE("URL field item not found.", pEditText->HasField(text::textfield::Type::URL)); + + // Cell with Date field item. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(1), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_MESSAGE("Unexpected text.", aParaText.indexOf("Date: ") == 0); + CPPUNIT_ASSERT_MESSAGE("Date field item not found.", pEditText->HasField(text::textfield::Type::DATE)); + + // Cell with DocInfo title field item. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(1), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_MESSAGE("Unexpected text.", aParaText.indexOf("Title: ") == 0); + CPPUNIT_ASSERT_MESSAGE("DocInfo title field item not found.", pEditText->HasField(text::textfield::Type::DOCINFO_TITLE)); + + // Cell with sentence with both bold and italic sequences. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(1), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_EQUAL(OUString("Sentence with bold and italic."), aParaText); + std::vector aAttribs; + pEditText->GetCharAttribs(0, aAttribs); + std::vector::const_iterator it = aAttribs.begin(), itEnd = aAttribs.end(); + { + bool bHasBold = false, bHasItalic = false; + for (; it != itEnd; ++it) + { + OUString aSeg = aParaText.copy(it->nStart, it->nEnd - it->nStart); + const SfxPoolItem* pAttr = it->pAttr; + if (aSeg == "bold" && pAttr->Which() == EE_CHAR_WEIGHT && !bHasBold) + { + const SvxWeightItem& rItem = static_cast(*pAttr); + bHasBold = (rItem.GetWeight() == WEIGHT_BOLD); + } + else if (aSeg == "italic" && pAttr->Which() == EE_CHAR_ITALIC && !bHasItalic) + { + const SvxPostureItem& rItem = static_cast(*pAttr); + bHasItalic = (rItem.GetPosture() == ITALIC_NORMAL); + + } + } + CPPUNIT_ASSERT_MESSAGE("This sentence is expected to have both bold and italic sequences.", bHasBold && bHasItalic); + } + + // Cell with multi-line content with formatting applied. + aPos.IncRow(); + CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, pDoc->GetCellType(aPos)); + pEditText = pDoc->GetEditText(aPos); + CPPUNIT_ASSERT_MESSAGE("Failed to retrieve edit text object.", pEditText); + CPPUNIT_ASSERT_EQUAL(static_cast(3), pEditText->GetParagraphCount()); + aParaText = pEditText->GetText(0); + CPPUNIT_ASSERT_EQUAL(OUString("bold"), aParaText); + aParaText = pEditText->GetText(1); + CPPUNIT_ASSERT_EQUAL(OUString("italic"), aParaText); + aParaText = pEditText->GetText(2); + CPPUNIT_ASSERT_EQUAL(OUString("underlined"), aParaText); + + // first line is bold. + pEditText->GetCharAttribs(0, aAttribs); + bool bHasBold = false; + for (it = aAttribs.begin(), itEnd = aAttribs.end(); it != itEnd; ++it) + { + if (it->pAttr->Which() == EE_CHAR_WEIGHT) + { + const SvxWeightItem& rItem = static_cast(*it->pAttr); + bHasBold = (rItem.GetWeight() == WEIGHT_BOLD); + if (bHasBold) + break; + } + } + CPPUNIT_ASSERT_MESSAGE("First line should be bold.", bHasBold); + + // second line is italic. + pEditText->GetCharAttribs(1, aAttribs); + bool bHasItalic = false; + for (it = aAttribs.begin(), itEnd = aAttribs.end(); it != itEnd; ++it) + { + if (it->pAttr->Which() == EE_CHAR_ITALIC) + { + const SvxPostureItem& rItem = static_cast(*it->pAttr); + bHasItalic = (rItem.GetPosture() == ITALIC_NORMAL); + if (bHasItalic) + break; + } + } + CPPUNIT_ASSERT_MESSAGE("Second line should be italic.", bHasItalic); + + // third line is underlined. + pEditText->GetCharAttribs(2, aAttribs); + bool bHasUnderline = false; + for (it = aAttribs.begin(), itEnd = aAttribs.end(); it != itEnd; ++it) + { + if (it->pAttr->Which() == EE_CHAR_UNDERLINE) + { + const SvxUnderlineItem& rItem = static_cast(*it->pAttr); + bHasUnderline = (rItem.GetLineStyle() == UNDERLINE_SINGLE); + if (bHasUnderline) + break; + } + } + CPPUNIT_ASSERT_MESSAGE("Second line should be underlined.", bHasUnderline); + + xDocSh->DoClose(); +} + namespace { void testColorScaleFormat_Impl(const rtl::OUString& rFilePath, const ScConditionalFormat* pFormat) -- cgit