summaryrefslogtreecommitdiff
path: root/sc/qa/unit/cond_format_merge.cxx
blob: 0ce3f21909bd0fc408f09cffa7764d7e06df4c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 <sal/config.h>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/sheet/XConditionalFormats.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <test/bootstrapfixture.hxx>
#include <test/calc_unoapi_test.hxx>

#include <global.hxx>
#include <document.hxx>

#include "helper/qahelper.hxx"

using namespace css;

class ScCondFormatMergeTest : public CalcUnoApiTest
{
public:
    ScCondFormatMergeTest();

    void testCondFormatMerge();

    CPPUNIT_TEST_SUITE(ScCondFormatMergeTest);
    CPPUNIT_TEST(testCondFormatMerge);
    CPPUNIT_TEST_SUITE_END();
};

ScCondFormatMergeTest::ScCondFormatMergeTest()
    : CalcUnoApiTest("sc/qa/extras/testdocuments/")
{
}

void ScCondFormatMergeTest::testCondFormatMerge()
{
    OUString aFileURL;
    createFileURL("cond_format_merge.ods", aFileURL);
    uno::Reference<lang::XComponent> mxComponent = loadFromDesktop(aFileURL);

    CPPUNIT_ASSERT_MESSAGE("Component not loaded", mxComponent.is());

    // get the first sheet
    uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
    uno::Reference<container::XIndexAccess> xIndex(xDoc->getSheets(), uno::UNO_QUERY_THROW);
    uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW);

    uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
    uno::Any aAny = xProps->getPropertyValue("ConditionalFormats");
    uno::Reference<sheet::XConditionalFormats> xCondFormats;

    CPPUNIT_ASSERT(aAny >>= xCondFormats);
    CPPUNIT_ASSERT(xCondFormats.is());

    CPPUNIT_ASSERT_EQUAL(sal_Int32(5), xCondFormats->getLength());

    uno::Sequence<uno::Reference<sheet::XConditionalFormat>> xCondFormatSeq
        = xCondFormats->getConditionalFormats();
    CPPUNIT_ASSERT_EQUAL(sal_Int32(5), xCondFormatSeq.getLength());

    int nRanges = 0;
    for (sal_Int32 i = 0, n = xCondFormatSeq.getLength(); i < n; ++i)
    {
        CPPUNIT_ASSERT(xCondFormatSeq[i].is());

        uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormatSeq[i];
        CPPUNIT_ASSERT(xCondFormat.is());

        uno::Reference<beans::XPropertySet> xPropSet(xCondFormat, uno::UNO_QUERY_THROW);

        aAny = xPropSet->getPropertyValue("Range");
        uno::Reference<sheet::XSheetCellRanges> xCellRanges;
        CPPUNIT_ASSERT(aAny >>= xCellRanges);
        CPPUNIT_ASSERT(xCellRanges.is());

        uno::Sequence<table::CellRangeAddress> aRanges = xCellRanges->getRangeAddresses();
        CPPUNIT_ASSERT_GREATEREQUAL(sal_Int32(1), aRanges.getLength());

        table::CellRangeAddress aRange0 = aRanges[0];
        CPPUNIT_ASSERT_EQUAL(sal_Int16(0), aRange0.Sheet);
        CPPUNIT_ASSERT_EQUAL(aRange0.StartColumn, aRange0.EndColumn);

        table::CellRangeAddress aRange1;

        switch (aRange0.StartColumn)
        {
            case 3:
                switch (aRange0.StartRow)
                {
                    case 0: // D1:D2,D5::D8
                        nRanges++;
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRange0.EndRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aRanges.getLength());
                        aRange1 = aRanges[1];
                        CPPUNIT_ASSERT_EQUAL(sal_Int16(0), aRange1.Sheet);
                        CPPUNIT_ASSERT_EQUAL(aRange1.StartColumn, aRange1.EndColumn);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(3), aRange1.StartColumn);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(4), aRange1.StartRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(7), aRange1.EndRow);
                        break;
                    default:
                        CPPUNIT_FAIL("Unexpected range in column D");
                }
                break;
            case 5:
                switch (aRange0.StartRow)
                {
                    case 0: // F1:F2
                        nRanges++;
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRange0.EndRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
                        break;
                    case 2: // F3
                        nRanges++;
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aRange0.EndRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
                        break;
                    case 3: // F4
                        nRanges++;
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(3), aRange0.EndRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
                        break;
                    case 4: // F5
                        nRanges++;
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(4), aRange0.EndRow);
                        CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
                        break;
                    default:
                        CPPUNIT_FAIL("Unexpected range in column F");
                }
                break;
            default:
                CPPUNIT_FAIL("Unexpected range");
        }
    }

    CPPUNIT_ASSERT_EQUAL(5, nRanges);

    closeDocument(mxComponent);
    mxComponent.clear();
}

CPPUNIT_TEST_SUITE_REGISTRATION(ScCondFormatMergeTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */