summaryrefslogtreecommitdiffstats
path: root/lib/kotext/tests/KoUserStyleTester.cpp
blob: 559375fd796c38f6934a88f88e537d2563a6eac1 (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
156
157
158
159
160
161
162
/* This file is part of the KDE project
   Copyright (C) 2005 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// KoUserStyle/KoUserStyleCollection test

#include <tdeunittest/runner.h>
#include <tdeunittest/module.h>

#include <KoUserStyleCollection.h>
#include <KoUserStyle.h>
#include <kdebug.h>
#include <tdeglobal.h>

#include "KoUserStyleTester.h"
#include "KoUserStyleTester.moc"

TDEUNITTEST_MODULE(tdeunittest_KoUserStyleTester, "KoUserStyle Tester");
TDEUNITTEST_MODULE_REGISTER_TESTER(KoUserStyleTester);

#undef COMPARE
/// for source-compat with qttestlib: use COMPARE(x,y) if you plan to port to qttestlib later.
#define COMPARE CHECK

/// for source-compat with qttestlib: use VERIFY(x) if you plan to port to qttestlib later.
#undef VERIFY
#define VERIFY( x ) CHECK( x, true )

void KoUserStyleTester::testEmptyCollection()
{
    KoUserStyleCollection coll( "test" );
    VERIFY( coll.isEmpty() );
    COMPARE( coll.count(), 0 );
    VERIFY( coll.styleList().isEmpty() );
}

void KoUserStyleTester::testAddStyle()
{
    KoUserStyleCollection coll( "test" );

    KoUserStyle* style = new KoUserStyle( "test1" );
    COMPARE( style->name(), TQString( "test1" ) );
    COMPARE( style->displayName(), TQString( "test1" ) );
    const TQString displayName = "A lovely name";
    style->setDisplayName( displayName );
    COMPARE( style->displayName(), displayName );

    KoUserStyle* ret = coll.addStyle( style );
    COMPARE( ret, style );

    KoUserStyle* style2 = new KoUserStyle( "test1" );
    COMPARE( style2->name(), TQString( "test1" ) );
    style2->setDisplayName( displayName );
    ret = coll.addStyle( style2 );
    // here style2 got deleted.
    COMPARE( ret, style );

    VERIFY( !coll.isEmpty() );
    COMPARE( coll.count(), 1 );
    COMPARE( (int)coll.styleList().count(), 1 );

    // Add another style for good this time
    KoUserStyle* style3 = new KoUserStyle( "test3" );
    COMPARE( style3->name(), TQString( "test3" ) );
    ret = coll.addStyle( style3 );

    TQStringList displayNames = coll.displayNameList();
    COMPARE( (int)displayNames.count(), 2 );
    COMPARE( displayNames[0], displayName );
    COMPARE( displayNames[1], style3->name() );
}

void KoUserStyleTester::testFindStyle()
{
    KoUserStyleCollection coll( "test" );
    KoUserStyle* style = new KoUserStyle( "test1" );
    const TQString displayName = "A lovely name";
    style->setDisplayName( displayName );
    coll.addStyle( style );

    // --- findStyle tests ---
    KoUserStyle* ret = coll.findStyle( "test1", TQString() );
    COMPARE( ret, style );

    ret = coll.findStyle( "foo", TQString() );
    COMPARE( ret, (KoUserStyle*)0 );

    ret = coll.findStyle( "foo", "test1" ); // fallback not used for style 'foo'
    COMPARE( ret, (KoUserStyle*)0 );

    ret = coll.findStyle( "test1", "test1" ); // fallback used for standard style test1
    COMPARE( ret, style );

    // --- findStyleByDisplayName tests ---
    ret = coll.findStyleByDisplayName( displayName );
    COMPARE( ret, style );

    ret = coll.findStyleByDisplayName( "foo" );
    COMPARE( ret, (KoUserStyle*)0 );

    // --- indexOf tests ---
    int pos = coll.indexOf( style );
    COMPARE( pos, 0 );

    KoUserStyle* style2 = new KoUserStyle( "test1" );
    pos = coll.indexOf( style2 );
    COMPARE( pos, -1 );
    delete style2;
}

void KoUserStyleTester::testRemoveStyle()
{
    KoUserStyleCollection coll( "test" );
    KoUserStyle* style = new KoUserStyle( "test1" );
    coll.addStyle( style );
    COMPARE( coll.count(), 1 );

    // Try removing an unrelated style (noop)
    KoUserStyle* style2 = new KoUserStyle( "test1" );
    coll.removeStyle( style2 );
    delete style2;
    COMPARE( coll.count(), 1 );

    coll.removeStyle( style );
    COMPARE( coll.count(), 0 );
}

void KoUserStyleTester::testReorder()
{
    KoUserStyleCollection coll( "test" );
    KoUserStyle* style = new KoUserStyle( "test1" );
    coll.addStyle( style );
    style = new KoUserStyle( "test2" );
    coll.addStyle( style );
    style = new KoUserStyle( "test3" );
    coll.addStyle( style );
    COMPARE( coll.count(), 3 );

    TQStringList newOrder;
    newOrder << "test3";
    newOrder << "test2";
    newOrder << "test1";
    coll.updateStyleListOrder( newOrder );
    COMPARE( coll.count(), 3 );
    TQStringList displayNames = coll.displayNameList();
    COMPARE( (int)displayNames.count(), 3 );
    COMPARE( displayNames.join(","), newOrder.join(",") );
}