~ubuntu-branches/ubuntu/quantal/arora/quantal

« back to all changes in this revision

Viewing changes to autotests/utils/languagemanager/tst_languagemanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Roderick B. Greening
  • Date: 2009-10-01 16:08:58 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20091001160858-h1wnt3ddyzl07nih
Tags: 0.10.0-0ubuntu1
* New upstream release 
* Remove patches
  - kubuntu_01_google_lucky.diff - Open Search now used upstream
  - kubuntu_02_default_bookmarks.diff - bookmarks fixed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (c) 2008, Benjamin C. Meyer
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
 
13
 *    may be used to endorse or promote products derived from this software
 
14
 *    without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
26
 * SUCH DAMAGE.
 
27
 */
 
28
 
 
29
#include <qtest.h>
 
30
#include <qsignalspy.h>
 
31
 
 
32
#include <languagemanager.h>
 
33
#include <qlocale.h>
 
34
 
 
35
class tst_LanguageManager : public QObject
 
36
{
 
37
    Q_OBJECT
 
38
 
 
39
public slots:
 
40
    void initTestCase();
 
41
    void cleanupTestCase();
 
42
    void init();
 
43
    void cleanup();
 
44
 
 
45
private slots:
 
46
    void languagemanager_data();
 
47
    void languagemanager();
 
48
 
 
49
    void isLanguageAvailable_data();
 
50
    void isLanguageAvailable();
 
51
    void chooseNewLanguage_data();
 
52
    void chooseNewLanguage();
 
53
    void setCurrentLanguage_data();
 
54
    void setCurrentLanguage();
 
55
};
 
56
 
 
57
// Subclass that exposes the protected functions.
 
58
class SubLanguageManager : public LanguageManager
 
59
{
 
60
public:
 
61
    SubLanguageManager() : LanguageManager() {
 
62
        addLocaleDirectory(qApp->applicationDirPath() + QLatin1String("/.qm/locale"));
 
63
    }
 
64
 
 
65
};
 
66
 
 
67
class TestWidget : public QWidget
 
68
{
 
69
public:
 
70
    void changeEvent(QEvent *event) {
 
71
        if (event->type() == QEvent::LanguageChange)
 
72
            retranslate = true;
 
73
        QWidget::changeEvent(event);
 
74
    }
 
75
    bool retranslate;
 
76
};
 
77
 
 
78
 
 
79
// This will be called before the first test function is executed.
 
80
// It is only called once.
 
81
void tst_LanguageManager::initTestCase()
 
82
{
 
83
}
 
84
 
 
85
// This will be called after the last test function is executed.
 
86
// It is only called once.
 
87
void tst_LanguageManager::cleanupTestCase()
 
88
{
 
89
}
 
90
 
 
91
// This will be called before each test function is executed.
 
92
void tst_LanguageManager::init()
 
93
{
 
94
    SubLanguageManager manager;
 
95
    manager.setCurrentLanguage(QString());
 
96
}
 
97
 
 
98
// This will be called after every test function.
 
99
void tst_LanguageManager::cleanup()
 
100
{
 
101
}
 
102
 
 
103
void tst_LanguageManager::languagemanager_data()
 
104
{
 
105
}
 
106
 
 
107
void tst_LanguageManager::languagemanager()
 
108
{
 
109
    SubLanguageManager manager;
 
110
    manager.languages();
 
111
    // spawns an event loop...
 
112
    // manager.chooseNewLanguage();
 
113
 
 
114
    QString fallbackLanguage;
 
115
    if (manager.isLanguageAvailable(QLocale::system().name()))
 
116
        fallbackLanguage = QLocale::system().name();
 
117
 
 
118
    QCOMPARE(manager.currentLanguage(), fallbackLanguage);
 
119
    manager.setCurrentLanguage(QString());
 
120
}
 
121
 
 
122
void tst_LanguageManager::isLanguageAvailable_data()
 
123
{
 
124
    QTest::addColumn<QString>("language");
 
125
    QTest::addColumn<bool>("available");
 
126
    QTest::newRow("null") << QString() << true;
 
127
    QTest::newRow("fallback0") << "ca_ES" << true;
 
128
    QTest::newRow("fallback1") << "ca_ES.UTF-8" << true;
 
129
    QTest::newRow("fallback2") << "de_AT" << true;
 
130
}
 
131
 
 
132
void tst_LanguageManager::isLanguageAvailable()
 
133
{
 
134
    QFETCH(QString, language);
 
135
    QFETCH(bool, available);
 
136
    SubLanguageManager manager;
 
137
    QCOMPARE(manager.isLanguageAvailable(language), available);
 
138
}
 
139
 
 
140
void tst_LanguageManager::chooseNewLanguage_data()
 
141
{
 
142
    QTest::addColumn<int>("foo");
 
143
    QTest::newRow("null") << 0;
 
144
}
 
145
 
 
146
// public void chooseNewLanguage()
 
147
void tst_LanguageManager::chooseNewLanguage()
 
148
{
 
149
    // how do you test this?
 
150
#if 0
 
151
    QFETCH(int, foo);
 
152
 
 
153
    SubLanguageManager manager;
 
154
 
 
155
    manager.chooseNewLanguage();
 
156
#endif
 
157
    QSKIP("Test is not implemented.", SkipAll);
 
158
}
 
159
 
 
160
void tst_LanguageManager::setCurrentLanguage_data()
 
161
{
 
162
    SubLanguageManager manager;
 
163
 
 
164
    QString fallbackLanguage;
 
165
    if (manager.isLanguageAvailable(QLocale::system().name()))
 
166
        fallbackLanguage = QLocale::system().name();
 
167
 
 
168
    QTest::addColumn<QString>("language");
 
169
    QTest::addColumn<bool>("success");
 
170
    QTest::addColumn<QString>("result");
 
171
    QTest::newRow("null-foo") << QString("foo") << false << fallbackLanguage;
 
172
    QTest::newRow("null-null") << QString() << false << fallbackLanguage;
 
173
 
 
174
    QString validLanguage = manager.languages().value(0);
 
175
    if (validLanguage.isEmpty())
 
176
        QSKIP("no languages to test with", SkipAll);
 
177
    QTest::newRow(validLanguage.toLatin1()) << validLanguage << true << validLanguage;
 
178
    QTest::newRow("fallback") << "ca_ES" << true << "ca_ES";
 
179
}
 
180
 
 
181
// public void setCurrentLanguage(QString const &language)
 
182
void tst_LanguageManager::setCurrentLanguage()
 
183
{
 
184
    QFETCH(QString, language);
 
185
    QFETCH(bool, success);
 
186
    QFETCH(QString, result);
 
187
 
 
188
    SubLanguageManager manager;
 
189
    QString initialLanguage = manager.currentLanguage();
 
190
    QSignalSpy spy(&manager, SIGNAL(languageChanged(const QString &)));
 
191
 
 
192
    TestWidget widget;
 
193
    widget.retranslate = false;
 
194
    QCOMPARE(manager.setCurrentLanguage(language), success);
 
195
    QCOMPARE(manager.currentLanguage(), result);
 
196
 
 
197
    // test if we set the default locale properly
 
198
    // this is essential for opensearch localization
 
199
    QLocale currentLocale(manager.currentLanguage());
 
200
    if (manager.currentLanguage().isEmpty())
 
201
        currentLocale = QLocale();
 
202
    QCOMPARE(currentLocale, QLocale());
 
203
 
 
204
    qApp->processEvents();
 
205
    QCOMPARE(widget.retranslate, success);
 
206
    QCOMPARE(spy.count(), success ? 1 : 0);
 
207
    if (success) {
 
208
        QVERIFY(manager.setCurrentLanguage(QString()));
 
209
        QCOMPARE(spy.count(), 2);
 
210
    }
 
211
}
 
212
 
 
213
QTEST_MAIN(tst_LanguageManager)
 
214
#include "tst_languagemanager.moc"
 
215