~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/tests/xkb_rules_test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2011 Andriy Rysin (rysin@kde.org)
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include <kdebug.h>
 
20
#include <QtGui/QApplication>
 
21
#include <qtest_kde.h>
 
22
 
 
23
#include "../xkb_rules.h"
 
24
 
 
25
#include <QtXml/qdom.h>
 
26
#include <QtXml/qxml.h>
 
27
 
 
28
static const Rules::ExtrasFlag readExtras = Rules::NO_EXTRAS;
 
29
 
 
30
class RulesTest : public QObject
 
31
{
 
32
    Q_OBJECT
 
33
 
 
34
        Rules* rules;
 
35
 
 
36
private Q_SLOTS:
 
37
    void initTestCase() {
 
38
        rules = Rules::readRules(readExtras);
 
39
    }
 
40
 
 
41
    void cleanupTestCase() {
 
42
        delete rules;
 
43
    }
 
44
 
 
45
    void testRules() {
 
46
        QVERIFY( rules != NULL );
 
47
        QVERIFY( rules->modelInfos.size() > 0 );
 
48
        QVERIFY( rules->layoutInfos.size() > 0 );
 
49
        QVERIFY( rules->optionGroupInfos.size() > 0 );
 
50
    }
 
51
 
 
52
    void testModel() {
 
53
        foreach(const ModelInfo* modelInfo, rules->modelInfos) {
 
54
                QVERIFY( modelInfo != NULL);
 
55
                QVERIFY( modelInfo->name.length() > 0 );
 
56
                QVERIFY( modelInfo->description.length() > 0 );
 
57
//              QVERIFY( ! modelInfo->vendor.isEmpty() );
 
58
        }
 
59
    }
 
60
 
 
61
    void testLayouts() {
 
62
        foreach(const LayoutInfo* layoutInfo, rules->layoutInfos) {
 
63
                QVERIFY( layoutInfo != NULL);
 
64
                QVERIFY( ! layoutInfo->name.isEmpty() );
 
65
//              const char* desc = layoutInfo->name.toUtf8() ;
 
66
//              qDebug() << layoutInfo->name;
 
67
                QVERIFY( ! layoutInfo->description.isEmpty() );
 
68
 
 
69
                foreach(const VariantInfo* variantInfo, layoutInfo->variantInfos) {
 
70
                        QVERIFY( variantInfo != NULL );
 
71
                        QVERIFY( ! variantInfo->name.isEmpty() );
 
72
                        QVERIFY( ! variantInfo->description.isEmpty() );
 
73
                }
 
74
                foreach(const QString& language, layoutInfo->languages) {
 
75
                        QVERIFY( ! language.isEmpty() );
 
76
                }
 
77
        }
 
78
    }
 
79
 
 
80
    void testOptionGroups() {
 
81
        foreach(const OptionGroupInfo* optionGroupInfo, rules->optionGroupInfos) {
 
82
                QVERIFY( optionGroupInfo != NULL);
 
83
                QVERIFY( ! optionGroupInfo->name.isEmpty() );
 
84
                QVERIFY( ! optionGroupInfo->description.isEmpty() );
 
85
                // optionGroupInfo->exclusive
 
86
 
 
87
                foreach(const OptionInfo* optionInfo, optionGroupInfo->optionInfos) {
 
88
                        QVERIFY( optionInfo != NULL );
 
89
                        QVERIFY( ! optionInfo->name.isEmpty() );
 
90
                        QVERIFY( ! optionInfo->description.isEmpty() );
 
91
                }
 
92
        }
 
93
    }
 
94
 
 
95
    void testExtras() {
 
96
        Rules* rulesWithExtras = Rules::readRules(Rules::READ_EXTRAS);
 
97
        QVERIFY2(rulesWithExtras->layoutInfos.size() > rules->layoutInfos.size(), "Rules with extras should have more layouts");
 
98
 
 
99
        foreach(const LayoutInfo* layoutInfo, rules->layoutInfos) {
 
100
                QVERIFY( ! layoutInfo->fromExtras );
 
101
        }
 
102
 
 
103
        bool foundFromExtras = false, foundNonExtras = false;
 
104
        foreach(const LayoutInfo* layoutInfo, rulesWithExtras->layoutInfos) {
 
105
                if( layoutInfo->fromExtras ) foundFromExtras = true;
 
106
                if( ! layoutInfo->fromExtras ) foundNonExtras = true;
 
107
                layoutInfo->languages.size();   // make sure we can access all merged objects
 
108
                layoutInfo->variantInfos.size();        // make sure we can access all merged objects
 
109
        }
 
110
        QVERIFY( foundNonExtras );
 
111
        QVERIFY( foundFromExtras );
 
112
    }
 
113
 
 
114
    void testWriteNewXml() {
 
115
        QDomDocument doc("xkbConfigRegistry");
 
116
        QDomElement root = doc.createElement("xkbConfigRegistry");
 
117
        root.setAttribute("version", "2.0");
 
118
        doc.appendChild(root);
 
119
 
 
120
        QDomElement modelList = doc.createElement("modelList");
 
121
        root.appendChild(modelList);
 
122
        foreach(const ModelInfo* modelInfo, rules->modelInfos) {
 
123
                QDomElement model = doc.createElement("model");
 
124
                model.setAttribute("name", modelInfo->name);
 
125
                model.setAttribute("description", modelInfo->description);
 
126
                model.setAttribute("vendor", modelInfo->vendor);
 
127
                modelList.appendChild(model);
 
128
        }
 
129
 
 
130
        QDomElement layoutList = doc.createElement("layoutList");
 
131
        foreach(const LayoutInfo* layoutInfo, rules->layoutInfos) {
 
132
                QDomElement layout = doc.createElement("layout");
 
133
                layout.setAttribute("name", layoutInfo->name);
 
134
                layout.setAttribute("description", layoutInfo->description);
 
135
 
 
136
                QDomElement langList = doc.createElement("languageList");
 
137
                foreach(const QString& lang, layoutInfo->languages) {
 
138
                QDomElement langNode = doc.createElement("lang");
 
139
                langNode.setAttribute("iso639Id", lang);
 
140
                langList.appendChild(langNode);
 
141
                }
 
142
                if( langList.hasChildNodes() ) {
 
143
                        layout.appendChild(langList);
 
144
                }
 
145
 
 
146
                QDomElement variantList = doc.createElement("variantList");
 
147
                foreach(const VariantInfo* variantInfo, layoutInfo->variantInfos) {
 
148
                QDomElement variant = doc.createElement("variant");
 
149
                variant.setAttribute("name", variantInfo->name);
 
150
                variant.setAttribute("description", variantInfo->description);
 
151
 
 
152
                QDomElement langList = doc.createElement("languageList");
 
153
                foreach(const QString& lang, variantInfo->languages) {
 
154
                        QDomElement langNode = doc.createElement("lang");
 
155
                        langNode.setAttribute("iso639Id", lang);
 
156
                        langList.appendChild(langNode);
 
157
                }
 
158
                if( langList.hasChildNodes() ) {
 
159
                        variant.appendChild(langList);
 
160
                }
 
161
 
 
162
                variantList.appendChild(variant);
 
163
                }
 
164
                if( variantList.hasChildNodes() ) {
 
165
                        layout.appendChild(variantList);
 
166
                }
 
167
 
 
168
                layoutList.appendChild(layout);
 
169
        }
 
170
        root.appendChild(layoutList);
 
171
 
 
172
        QDomElement optionGroupList = doc.createElement("optionList");
 
173
        foreach(const OptionGroupInfo* optionGroupInfo, rules->optionGroupInfos) {
 
174
                QDomElement optionGroup = doc.createElement("optionGroup");
 
175
                optionGroup.setAttribute("name", optionGroupInfo->name);
 
176
                optionGroup.setAttribute("description", optionGroupInfo->description);
 
177
                optionGroup.setAttribute("exclusive", optionGroupInfo->exclusive);
 
178
 
 
179
                foreach(const OptionInfo* optionGroupInfo, optionGroupInfo->optionInfos) {
 
180
                QDomElement option = doc.createElement("option");
 
181
                option.setAttribute("name", optionGroupInfo->name);
 
182
                option.setAttribute("description", optionGroupInfo->description);
 
183
                optionGroup.appendChild(option);
 
184
                }
 
185
 
 
186
                optionGroupList.appendChild(optionGroup);
 
187
        }
 
188
        root.appendChild(optionGroupList);
 
189
 
 
190
        QFile file("base2.xml");
 
191
        if( ! file.open( QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) ) {
 
192
                kWarning() << "Failed to open layout memory xml file for writing" << file.fileName();
 
193
                QFAIL("failed");
 
194
        }
 
195
 
 
196
        QTextStream out(&file);
 
197
        out << doc.toString();
 
198
    }
 
199
 
 
200
    void loadRulesBenchmark() {
 
201
        QBENCHMARK {
 
202
                Rules* rules = Rules::readRules(readExtras);
 
203
                delete rules;
 
204
        }
 
205
    }
 
206
 
 
207
};
 
208
 
 
209
// need kde libs for config-workspace.h used in xkb_rules.cpp
 
210
// need GUI for xkb protocol
 
211
QTEST_KDEMAIN( RulesTest, GUI )
 
212
 
 
213
#include "xkb_rules_test.moc"