author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 17 Sep 2010 08:34:18 +0300 | |
changeset 36 | ef0373b55136 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
||
46 |
#include <qtextdocument.h> |
|
47 |
#include <qdebug.h> |
|
48 |
||
49 |
#include <qtextcursor.h> |
|
50 |
#include <qtextdocumentfragment.h> |
|
51 |
#include <qtextformat.h> |
|
52 |
#include <qtextobject.h> |
|
53 |
#include <qtexttable.h> |
|
54 |
#include <qabstracttextdocumentlayout.h> |
|
55 |
#include <qtextlist.h> |
|
56 |
#include <qtextcodec.h> |
|
57 |
#include <qurl.h> |
|
58 |
#include <qpainter.h> |
|
59 |
#include <qfontmetrics.h> |
|
60 |
#include <qimage.h> |
|
61 |
#include <qtextlayout.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
#include <QDomDocument> |
0 | 63 |
#include "common.h" |
64 |
||
65 |
||
66 |
QT_FORWARD_DECLARE_CLASS(QTextDocument) |
|
67 |
||
68 |
//TESTED_CLASS= |
|
69 |
//TESTED_FILES= |
|
70 |
||
71 |
class tst_QTextDocument : public QObject |
|
72 |
{ |
|
73 |
Q_OBJECT |
|
74 |
||
75 |
public: |
|
76 |
tst_QTextDocument(); |
|
77 |
virtual ~tst_QTextDocument(); |
|
78 |
||
79 |
public slots: |
|
80 |
void init(); |
|
81 |
void cleanup(); |
|
82 |
private slots: |
|
83 |
void getSetCheck(); |
|
84 |
void isEmpty(); |
|
85 |
void find_data(); |
|
86 |
void find(); |
|
87 |
void find2(); |
|
88 |
void findWithRegExp_data(); |
|
89 |
void findWithRegExp(); |
|
90 |
void findMultiple(); |
|
91 |
void basicIsModifiedChecks(); |
|
92 |
void moreIsModified(); |
|
93 |
void isModified2(); |
|
94 |
void isModified3(); |
|
95 |
void isModified4(); |
|
96 |
void noundo_basicIsModifiedChecks(); |
|
97 |
void noundo_moreIsModified(); |
|
98 |
void noundo_isModified2(); |
|
99 |
void noundo_isModified3(); |
|
100 |
void mightBeRichText(); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
101 |
void mightBeRichText_data(); |
0 | 102 |
|
103 |
void task240325(); |
|
104 |
||
105 |
void stylesheetFont_data(); |
|
106 |
void stylesheetFont(); |
|
107 |
||
108 |
void toHtml_data(); |
|
109 |
void toHtml(); |
|
110 |
void toHtml2(); |
|
111 |
||
112 |
void setFragmentMarkersInHtmlExport(); |
|
113 |
||
114 |
void toHtmlBodyBgColor(); |
|
115 |
void toHtmlRootFrameProperties(); |
|
116 |
void capitalizationHtmlInExport(); |
|
117 |
void wordspacingHtmlExport(); |
|
118 |
||
119 |
void cursorPositionChanged(); |
|
120 |
void cursorPositionChangedOnSetText(); |
|
121 |
||
122 |
void textFrameIterator(); |
|
123 |
||
124 |
void codecForHtml(); |
|
125 |
||
126 |
void markContentsDirty(); |
|
127 |
||
128 |
void clonePreservesMetaInformation(); |
|
129 |
void clonePreservesPageSize(); |
|
130 |
void clonePreservesPageBreakPolicies(); |
|
131 |
void clonePreservesDefaultFont(); |
|
132 |
void clonePreservesRootFrameFormat(); |
|
133 |
void clonePreservesResources(); |
|
134 |
void clonePreservesUserStates(); |
|
135 |
void clonePreservesIndentWidth(); |
|
136 |
void blockCount(); |
|
137 |
void defaultStyleSheet(); |
|
138 |
||
139 |
void resolvedFontInEmptyFormat(); |
|
140 |
||
141 |
void defaultRootFrameMargin(); |
|
142 |
||
143 |
void clearResources(); |
|
144 |
||
145 |
void setPlainText(); |
|
146 |
void toPlainText(); |
|
147 |
||
148 |
void deleteTextObjectsOnClear(); |
|
149 |
||
150 |
void maximumBlockCount(); |
|
151 |
void adjustSize(); |
|
152 |
void initialUserData(); |
|
153 |
||
154 |
void html_defaultFont(); |
|
155 |
||
156 |
void blockCountChanged(); |
|
157 |
||
158 |
void nonZeroDocumentLengthOnClear(); |
|
159 |
||
160 |
void setTextPreservesUndoRedoEnabled(); |
|
161 |
||
162 |
void firstLast(); |
|
163 |
||
164 |
void backgroundImage_toHtml(); |
|
165 |
void backgroundImage_toHtml2(); |
|
166 |
void backgroundImage_clone(); |
|
167 |
void backgroundImage_copy(); |
|
168 |
||
169 |
void documentCleanup(); |
|
170 |
||
171 |
void characterAt(); |
|
172 |
void revisions(); |
|
173 |
void revisionWithUndoCompressionAndUndo(); |
|
174 |
||
175 |
void testUndoCommandAdded(); |
|
176 |
||
177 |
void testUndoBlocks(); |
|
178 |
||
179 |
void receiveCursorPositionChangedAfterContentsChange(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
void escape_data(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
void escape(); |
0 | 182 |
|
183 |
private: |
|
184 |
void backgroundImage_checkExpectedHtml(const QTextDocument &doc); |
|
185 |
||
186 |
QTextDocument *doc; |
|
187 |
QTextCursor cursor; |
|
188 |
QFont defaultFont; |
|
189 |
QString htmlHead; |
|
190 |
QString htmlTail; |
|
191 |
}; |
|
192 |
||
193 |
class MyAbstractTextDocumentLayout : public QAbstractTextDocumentLayout |
|
194 |
{ |
|
195 |
public: |
|
196 |
MyAbstractTextDocumentLayout(QTextDocument *doc) : QAbstractTextDocumentLayout(doc) {} |
|
197 |
void draw(QPainter *, const PaintContext &) {} |
|
198 |
int hitTest(const QPointF &, Qt::HitTestAccuracy) const { return 0; } |
|
199 |
int pageCount() const { return 0; } |
|
200 |
QSizeF documentSize() const { return QSizeF(); } |
|
201 |
QRectF frameBoundingRect(QTextFrame *) const { return QRectF(); } |
|
202 |
QRectF blockBoundingRect(const QTextBlock &) const { return QRectF(); } |
|
203 |
void documentChanged(int, int, int) {} |
|
204 |
}; |
|
205 |
||
206 |
// Testing get/set functions |
|
207 |
void tst_QTextDocument::getSetCheck() |
|
208 |
{ |
|
209 |
QTextDocument obj1; |
|
210 |
// QAbstractTextDocumentLayout * QTextDocument::documentLayout() |
|
211 |
// void QTextDocument::setDocumentLayout(QAbstractTextDocumentLayout *) |
|
212 |
QPointer<MyAbstractTextDocumentLayout> var1 = new MyAbstractTextDocumentLayout(0); |
|
213 |
obj1.setDocumentLayout(var1); |
|
214 |
QCOMPARE(static_cast<QAbstractTextDocumentLayout *>(var1), obj1.documentLayout()); |
|
215 |
obj1.setDocumentLayout((QAbstractTextDocumentLayout *)0); |
|
216 |
QVERIFY(var1.isNull()); |
|
217 |
QVERIFY(obj1.documentLayout()); |
|
218 |
||
219 |
// bool QTextDocument::useDesignMetrics() |
|
220 |
// void QTextDocument::setUseDesignMetrics(bool) |
|
221 |
obj1.setUseDesignMetrics(false); |
|
222 |
QCOMPARE(false, obj1.useDesignMetrics()); |
|
223 |
obj1.setUseDesignMetrics(true); |
|
224 |
QCOMPARE(true, obj1.useDesignMetrics()); |
|
225 |
} |
|
226 |
||
227 |
tst_QTextDocument::tst_QTextDocument() |
|
228 |
{ |
|
229 |
QImage img(16, 16, QImage::Format_ARGB32_Premultiplied); |
|
230 |
img.save("foo.png"); |
|
231 |
} |
|
232 |
||
233 |
tst_QTextDocument::~tst_QTextDocument() |
|
234 |
{ |
|
235 |
QFile::remove(QLatin1String("foo.png")); |
|
236 |
} |
|
237 |
||
238 |
void tst_QTextDocument::init() |
|
239 |
{ |
|
240 |
doc = new QTextDocument; |
|
241 |
cursor = QTextCursor(doc); |
|
242 |
defaultFont = QFont(); |
|
243 |
||
244 |
htmlHead = QString("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" " |
|
245 |
"\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" |
|
246 |
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" |
|
247 |
"p, li { white-space: pre-wrap; }\n" |
|
248 |
"</style></head>" |
|
249 |
"<body style=\" font-family:'%1'; font-size:%2pt; font-weight:%3; font-style:%4;\">\n"); |
|
250 |
htmlHead = htmlHead.arg(defaultFont.family()).arg(defaultFont.pointSizeF()).arg(defaultFont.weight() * 8).arg((defaultFont.italic() ? "italic" : "normal")); |
|
251 |
||
252 |
htmlTail = QString("</body></html>"); |
|
253 |
} |
|
254 |
||
255 |
void tst_QTextDocument::cleanup() |
|
256 |
{ |
|
257 |
cursor = QTextCursor(); |
|
258 |
delete doc; |
|
259 |
doc = 0; |
|
260 |
} |
|
261 |
||
262 |
void tst_QTextDocument::isEmpty() |
|
263 |
{ |
|
264 |
QVERIFY(doc->isEmpty()); |
|
265 |
} |
|
266 |
||
267 |
void tst_QTextDocument::find_data() |
|
268 |
{ |
|
269 |
QTest::addColumn<QString>("haystack"); |
|
270 |
QTest::addColumn<QString>("needle"); |
|
271 |
QTest::addColumn<int>("flags"); |
|
272 |
QTest::addColumn<int>("from"); |
|
273 |
QTest::addColumn<int>("anchor"); |
|
274 |
QTest::addColumn<int>("position"); |
|
275 |
||
276 |
QTest::newRow("1") << "Hello World" << "World" << int(QTextDocument::FindCaseSensitively) << 0 << 6 << 11; |
|
277 |
||
278 |
QTest::newRow("2") << QString::fromAscii("Hello") + QString(QChar::ParagraphSeparator) + QString::fromAscii("World") |
|
279 |
<< "World" << int(QTextDocument::FindCaseSensitively) << 1 << 6 << 11; |
|
280 |
||
281 |
QTest::newRow("3") << QString::fromAscii("Hello") + QString(QChar::ParagraphSeparator) + QString::fromAscii("World") |
|
282 |
<< "Hello" << int(QTextDocument::FindCaseSensitively | QTextDocument::FindBackward) << 10 << 0 << 5; |
|
283 |
QTest::newRow("4wholewords") << QString::fromAscii("Hello Blah World") |
|
284 |
<< "Blah" << int(QTextDocument::FindWholeWords) << 0 << 6 << 10; |
|
285 |
QTest::newRow("5wholewords") << QString::fromAscii("HelloBlahWorld") |
|
286 |
<< "Blah" << int(QTextDocument::FindWholeWords) << 0 << -1 << -1; |
|
287 |
QTest::newRow("6wholewords") << QString::fromAscii("HelloBlahWorld Blah Hah") |
|
288 |
<< "Blah" << int(QTextDocument::FindWholeWords) << 0 << 15 << 19; |
|
289 |
QTest::newRow("7wholewords") << QString::fromAscii("HelloBlahWorld Blah Hah") |
|
290 |
<< "Blah" << int(QTextDocument::FindWholeWords | QTextDocument::FindBackward) << 23 << 15 << 19; |
|
291 |
QTest::newRow("8wholewords") << QString::fromAscii("Hello: World\n") |
|
292 |
<< "orld" << int(QTextDocument::FindWholeWords) << 0 << -1 << -1; |
|
293 |
||
294 |
QTest::newRow("across-paragraphs") << QString::fromAscii("First Parag\nSecond Parag with a lot more text") |
|
295 |
<< "Parag" << int(QTextDocument::FindBackward) |
|
296 |
<< 15 << 6 << 11; |
|
297 |
||
298 |
QTest::newRow("nbsp") << "Hello" + QString(QChar(QChar::Nbsp)) +"World" << " " << int(QTextDocument::FindCaseSensitively) << 0 << 5 << 6; |
|
299 |
} |
|
300 |
||
301 |
void tst_QTextDocument::find() |
|
302 |
{ |
|
303 |
QFETCH(QString, haystack); |
|
304 |
QFETCH(QString, needle); |
|
305 |
QFETCH(int, flags); |
|
306 |
QFETCH(int, from); |
|
307 |
QFETCH(int, anchor); |
|
308 |
QFETCH(int, position); |
|
309 |
||
310 |
cursor.insertText(haystack); |
|
311 |
cursor = doc->find(needle, from, QTextDocument::FindFlags(flags)); |
|
312 |
||
313 |
if (anchor != -1) { |
|
314 |
QCOMPARE(cursor.anchor(), anchor); |
|
315 |
QCOMPARE(cursor.position(), position); |
|
316 |
} else { |
|
317 |
QVERIFY(cursor.isNull()); |
|
318 |
} |
|
319 |
||
320 |
//search using a regular expression |
|
321 |
QRegExp expr(needle); |
|
322 |
expr.setPatternSyntax(QRegExp::FixedString); |
|
323 |
QTextDocument::FindFlags flg(flags); |
|
324 |
expr.setCaseSensitivity((flg & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); |
|
325 |
cursor = doc->find(expr, from, flg); |
|
326 |
||
327 |
if (anchor != -1) { |
|
328 |
QCOMPARE(cursor.anchor(), anchor); |
|
329 |
QCOMPARE(cursor.position(), position); |
|
330 |
} else { |
|
331 |
QVERIFY(cursor.isNull()); |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
void tst_QTextDocument::findWithRegExp_data() |
|
336 |
{ |
|
337 |
QTest::addColumn<QString>("haystack"); |
|
338 |
QTest::addColumn<QString>("needle"); |
|
339 |
QTest::addColumn<int>("flags"); |
|
340 |
QTest::addColumn<int>("from"); |
|
341 |
QTest::addColumn<int>("anchor"); |
|
342 |
QTest::addColumn<int>("position"); |
|
343 |
||
344 |
// match integers 0 to 99 |
|
345 |
QTest::newRow("1") << "23" << "^\\d\\d?$" << int(QTextDocument::FindCaseSensitively) << 0 << 0 << 2; |
|
346 |
// match ampersands but not & |
|
347 |
QTest::newRow("2") << "His & hers & theirs" << "&(?!amp;)"<< int(QTextDocument::FindCaseSensitively) << 0 << 15 << 16; |
|
348 |
//backward search |
|
349 |
QTest::newRow("3") << QString::fromAscii("HelloBlahWorld Blah Hah") |
|
350 |
<< "h" << int(QTextDocument::FindBackward) << 18 << 8 << 9; |
|
351 |
||
352 |
} |
|
353 |
||
354 |
void tst_QTextDocument::findWithRegExp() |
|
355 |
{ |
|
356 |
QFETCH(QString, haystack); |
|
357 |
QFETCH(QString, needle); |
|
358 |
QFETCH(int, flags); |
|
359 |
QFETCH(int, from); |
|
360 |
QFETCH(int, anchor); |
|
361 |
QFETCH(int, position); |
|
362 |
||
363 |
cursor.insertText(haystack); |
|
364 |
//search using a regular expression |
|
365 |
QRegExp expr(needle); |
|
366 |
QTextDocument::FindFlags flg(flags); |
|
367 |
expr.setCaseSensitivity((flg & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); |
|
368 |
cursor = doc->find(expr, from, flg); |
|
369 |
||
370 |
if (anchor != -1) { |
|
371 |
QCOMPARE(cursor.anchor(), anchor); |
|
372 |
QCOMPARE(cursor.position(), position); |
|
373 |
} else { |
|
374 |
QVERIFY(cursor.isNull()); |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
void tst_QTextDocument::find2() |
|
379 |
{ |
|
380 |
doc->setPlainText("aaa"); |
|
381 |
cursor.movePosition(QTextCursor::Start); |
|
382 |
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); |
|
383 |
QTextCursor hit = doc->find("a", cursor); |
|
384 |
QCOMPARE(hit.position(), 2); |
|
385 |
QCOMPARE(hit.anchor(), 1); |
|
386 |
} |
|
387 |
||
388 |
void tst_QTextDocument::findMultiple() |
|
389 |
{ |
|
390 |
const QString text("foo bar baz foo bar baz"); |
|
391 |
doc->setPlainText(text); |
|
392 |
||
393 |
cursor.movePosition(QTextCursor::Start); |
|
394 |
cursor = doc->find("bar", cursor); |
|
395 |
QCOMPARE(cursor.selectionStart(), text.indexOf("bar")); |
|
396 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
397 |
cursor = doc->find("bar", cursor); |
|
398 |
QCOMPARE(cursor.selectionStart(), text.lastIndexOf("bar")); |
|
399 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
400 |
||
401 |
cursor.movePosition(QTextCursor::End); |
|
402 |
cursor = doc->find("bar", cursor, QTextDocument::FindBackward); |
|
403 |
QCOMPARE(cursor.selectionStart(), text.lastIndexOf("bar")); |
|
404 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
405 |
cursor = doc->find("bar", cursor, QTextDocument::FindBackward); |
|
406 |
QCOMPARE(cursor.selectionStart(), text.indexOf("bar")); |
|
407 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
408 |
||
409 |
||
410 |
QRegExp expr("bar"); |
|
411 |
expr.setPatternSyntax(QRegExp::FixedString); |
|
412 |
||
413 |
cursor.movePosition(QTextCursor::End); |
|
414 |
cursor = doc->find(expr, cursor, QTextDocument::FindBackward); |
|
415 |
QCOMPARE(cursor.selectionStart(), text.lastIndexOf("bar")); |
|
416 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
417 |
cursor = doc->find(expr, cursor, QTextDocument::FindBackward); |
|
418 |
QCOMPARE(cursor.selectionStart(), text.indexOf("bar")); |
|
419 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
420 |
||
421 |
cursor.movePosition(QTextCursor::Start); |
|
422 |
cursor = doc->find(expr, cursor); |
|
423 |
QCOMPARE(cursor.selectionStart(), text.indexOf("bar")); |
|
424 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
425 |
cursor = doc->find(expr, cursor); |
|
426 |
QCOMPARE(cursor.selectionStart(), text.lastIndexOf("bar")); |
|
427 |
QCOMPARE(cursor.selectionEnd(), cursor.selectionStart() + 3); |
|
428 |
} |
|
429 |
||
430 |
void tst_QTextDocument::basicIsModifiedChecks() |
|
431 |
{ |
|
432 |
QSignalSpy spy(doc, SIGNAL(modificationChanged(bool))); |
|
433 |
||
434 |
QVERIFY(!doc->isModified()); |
|
435 |
cursor.insertText("Hello World"); |
|
436 |
QVERIFY(doc->isModified()); |
|
437 |
QCOMPARE(spy.count(), 1); |
|
438 |
QVERIFY(spy.takeFirst().at(0).toBool()); |
|
439 |
||
440 |
doc->undo(); |
|
441 |
QVERIFY(!doc->isModified()); |
|
442 |
QCOMPARE(spy.count(), 1); |
|
443 |
QVERIFY(!spy.takeFirst().at(0).toBool()); |
|
444 |
||
445 |
doc->redo(); |
|
446 |
QVERIFY(doc->isModified()); |
|
447 |
QCOMPARE(spy.count(), 1); |
|
448 |
QVERIFY(spy.takeFirst().at(0).toBool()); |
|
449 |
} |
|
450 |
||
451 |
void tst_QTextDocument::moreIsModified() |
|
452 |
{ |
|
453 |
QVERIFY(!doc->isModified()); |
|
454 |
||
455 |
cursor.insertText("Hello"); |
|
456 |
QVERIFY(doc->isModified()); |
|
457 |
||
458 |
doc->undo(); |
|
459 |
QVERIFY(!doc->isModified()); |
|
460 |
||
461 |
cursor.insertText("Hello"); |
|
462 |
||
463 |
doc->undo(); |
|
464 |
QVERIFY(!doc->isModified()); |
|
465 |
} |
|
466 |
||
467 |
void tst_QTextDocument::isModified2() |
|
468 |
{ |
|
469 |
// reported on qt4-preview-feedback |
|
470 |
QVERIFY(!doc->isModified()); |
|
471 |
||
472 |
cursor.insertText("Hello"); |
|
473 |
QVERIFY(doc->isModified()); |
|
474 |
||
475 |
doc->setModified(false); |
|
476 |
QVERIFY(!doc->isModified()); |
|
477 |
||
478 |
cursor.insertText("Hello"); |
|
479 |
QVERIFY(doc->isModified()); |
|
480 |
} |
|
481 |
||
482 |
void tst_QTextDocument::isModified3() |
|
483 |
{ |
|
484 |
QVERIFY(!doc->isModified()); |
|
485 |
||
486 |
doc->setUndoRedoEnabled(false); |
|
487 |
doc->setUndoRedoEnabled(true); |
|
488 |
||
489 |
cursor.insertText("Hello"); |
|
490 |
||
491 |
QVERIFY(doc->isModified()); |
|
492 |
doc->undo(); |
|
493 |
QVERIFY(!doc->isModified()); |
|
494 |
} |
|
495 |
||
496 |
void tst_QTextDocument::isModified4() |
|
497 |
{ |
|
498 |
QVERIFY(!doc->isModified()); |
|
499 |
||
500 |
cursor.insertText("Hello"); |
|
501 |
cursor.insertText("World"); |
|
502 |
||
503 |
doc->setModified(false); |
|
504 |
||
505 |
QVERIFY(!doc->isModified()); |
|
506 |
||
507 |
cursor.insertText("Again"); |
|
508 |
QVERIFY(doc->isModified()); |
|
509 |
||
510 |
doc->undo(); |
|
511 |
QVERIFY(!doc->isModified()); |
|
512 |
doc->undo(); |
|
513 |
QVERIFY(doc->isModified()); |
|
514 |
||
515 |
doc->redo(); |
|
516 |
QVERIFY(!doc->isModified()); |
|
517 |
doc->redo(); |
|
518 |
QVERIFY(doc->isModified()); |
|
519 |
||
520 |
doc->undo(); |
|
521 |
QVERIFY(!doc->isModified()); |
|
522 |
doc->undo(); |
|
523 |
QVERIFY(doc->isModified()); |
|
524 |
||
525 |
//task 197769 |
|
526 |
cursor.insertText("Hello"); |
|
527 |
QVERIFY(doc->isModified()); |
|
528 |
} |
|
529 |
||
530 |
void tst_QTextDocument::noundo_basicIsModifiedChecks() |
|
531 |
{ |
|
532 |
doc->setUndoRedoEnabled(false); |
|
533 |
QSignalSpy spy(doc, SIGNAL(modificationChanged(bool))); |
|
534 |
||
535 |
QVERIFY(!doc->isModified()); |
|
536 |
cursor.insertText("Hello World"); |
|
537 |
QVERIFY(doc->isModified()); |
|
538 |
QCOMPARE(spy.count(), 1); |
|
539 |
QVERIFY(spy.takeFirst().at(0).toBool()); |
|
540 |
||
541 |
doc->undo(); |
|
542 |
QVERIFY(doc->isModified()); |
|
543 |
QCOMPARE(spy.count(), 0); |
|
544 |
||
545 |
doc->redo(); |
|
546 |
QVERIFY(doc->isModified()); |
|
547 |
QCOMPARE(spy.count(), 0); |
|
548 |
} |
|
549 |
||
550 |
void tst_QTextDocument::task240325() |
|
551 |
{ |
|
552 |
doc->setHtml("<html><img width=\"100\" height=\"100\" align=\"right\"/>Foobar Foobar Foobar Foobar</html>"); |
|
553 |
||
554 |
QImage img(1000, 7000, QImage::Format_ARGB32_Premultiplied); |
|
555 |
QPainter p(&img); |
|
556 |
QFontMetrics fm(p.font()); |
|
557 |
||
558 |
// Set page size to contain image and one "Foobar" |
|
559 |
doc->setPageSize(QSize(100 + fm.width("Foobar")*2, 1000)); |
|
560 |
||
561 |
// Force layout |
|
562 |
doc->drawContents(&p); |
|
563 |
||
564 |
QCOMPARE(doc->blockCount(), 1); |
|
565 |
for (QTextBlock block = doc->begin() ; block!=doc->end() ; block = block.next()) { |
|
566 |
QTextLayout *layout = block.layout(); |
|
567 |
QCOMPARE(layout->lineCount(), 4); |
|
568 |
for (int lineIdx=0;lineIdx<layout->lineCount();++lineIdx) { |
|
569 |
QTextLine line = layout->lineAt(lineIdx); |
|
570 |
||
571 |
QString text = block.text().mid(line.textStart(), line.textLength()).trimmed(); |
|
572 |
||
573 |
// Remove start token |
|
574 |
if (lineIdx == 0) |
|
575 |
text = text.mid(1); |
|
576 |
||
577 |
QCOMPARE(text, QString::fromLatin1("Foobar")); |
|
578 |
} |
|
579 |
} |
|
580 |
} |
|
581 |
||
582 |
void tst_QTextDocument::stylesheetFont_data() |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
583 |
{ |
0 | 584 |
QTest::addColumn<QString>("stylesheet"); |
585 |
QTest::addColumn<QFont>("font"); |
|
586 |
||
587 |
{ |
|
588 |
QFont font; |
|
589 |
font.setBold(true); |
|
590 |
font.setPixelSize(64); |
|
591 |
||
592 |
QTest::newRow("Regular font specification") |
|
593 |
<< "font-size: 64px; font-weight: bold;" |
|
594 |
<< font; |
|
595 |
} |
|
596 |
||
597 |
||
598 |
{ |
|
599 |
QFont font; |
|
600 |
font.setBold(true); |
|
601 |
font.setPixelSize(64); |
|
602 |
||
603 |
QTest::newRow("Shorthand font specification") |
|
604 |
<< "font: normal bold 64px Arial;" |
|
605 |
<< font; |
|
606 |
} |
|
607 |
||
608 |
} |
|
609 |
||
610 |
void tst_QTextDocument::stylesheetFont() |
|
611 |
{ |
|
612 |
QFETCH(QString, stylesheet); |
|
613 |
QFETCH(QFont, font); |
|
614 |
||
615 |
QString html = QString::fromLatin1("<html>" |
|
616 |
"<body>" |
|
617 |
"<div style=\"%1\" >" |
|
618 |
"Foobar" |
|
619 |
"</div>" |
|
620 |
"</body>" |
|
621 |
"</html>").arg(stylesheet); |
|
622 |
||
623 |
qDebug() << html; |
|
624 |
doc->setHtml(html); |
|
625 |
QCOMPARE(doc->blockCount(), 1); |
|
626 |
||
627 |
// First and only block |
|
628 |
QTextBlock block = doc->firstBlock(); |
|
629 |
||
630 |
QString text = block.text(); |
|
631 |
QCOMPARE(text, QString::fromLatin1("Foobar")); |
|
632 |
||
633 |
QFont actualFont = block.charFormat().font(); |
|
634 |
||
635 |
QCOMPARE(actualFont.bold(), font.bold()); |
|
636 |
QCOMPARE(actualFont.pixelSize(), font.pixelSize()); |
|
637 |
} |
|
638 |
||
639 |
void tst_QTextDocument::noundo_moreIsModified() |
|
640 |
{ |
|
641 |
doc->setUndoRedoEnabled(false); |
|
642 |
QVERIFY(!doc->isModified()); |
|
643 |
||
644 |
cursor.insertText("Hello"); |
|
645 |
QVERIFY(doc->isModified()); |
|
646 |
||
647 |
doc->undo(); |
|
648 |
QVERIFY(doc->isModified()); |
|
649 |
||
650 |
cursor.insertText("Hello"); |
|
651 |
||
652 |
doc->undo(); |
|
653 |
QVERIFY(doc->isModified()); |
|
654 |
} |
|
655 |
||
656 |
void tst_QTextDocument::noundo_isModified2() |
|
657 |
{ |
|
658 |
// reported on qt4-preview-feedback |
|
659 |
QVERIFY(!doc->isModified()); |
|
660 |
||
661 |
cursor.insertText("Hello"); |
|
662 |
QVERIFY(doc->isModified()); |
|
663 |
||
664 |
doc->setModified(false); |
|
665 |
QVERIFY(!doc->isModified()); |
|
666 |
||
667 |
cursor.insertText("Hello"); |
|
668 |
QVERIFY(doc->isModified()); |
|
669 |
} |
|
670 |
||
671 |
void tst_QTextDocument::noundo_isModified3() |
|
672 |
{ |
|
673 |
doc->setUndoRedoEnabled(false); |
|
674 |
QVERIFY(!doc->isModified()); |
|
675 |
||
676 |
cursor.insertText("Hello"); |
|
677 |
||
678 |
QVERIFY(doc->isModified()); |
|
679 |
doc->undo(); |
|
680 |
QVERIFY(doc->isModified()); |
|
681 |
} |
|
682 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
683 |
void tst_QTextDocument::mightBeRichText_data() |
0 | 684 |
{ |
685 |
const char qtDocuHeader[] = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" |
|
686 |
"<!DOCTYPE html\n" |
|
687 |
" PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n" |
|
688 |
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"; |
|
689 |
QVERIFY(Qt::mightBeRichText(QString::fromLatin1(qtDocuHeader))); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
690 |
QTest::addColumn<QString>("input"); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
691 |
QTest::addColumn<bool>("result"); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
692 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
693 |
QTest::newRow("documentation-header") << QString("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
694 |
"<!DOCTYPE html\n" |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
695 |
" PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">\n" |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
696 |
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">") |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
697 |
<< true; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
698 |
QTest::newRow("br-nospace") << QString("Test <br/> new line") << true; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
699 |
QTest::newRow("br-space") << QString("Test <br /> new line") << true; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
700 |
QTest::newRow("br-invalidspace") << QString("Test <br/ > new line") << false; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
701 |
QTest::newRow("invalid closing tag") << QString("Test <br/ line") << false; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
702 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
703 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
704 |
void tst_QTextDocument::mightBeRichText() |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
705 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
706 |
QFETCH(QString, input); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
707 |
QFETCH(bool, result); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
708 |
QVERIFY(result == Qt::mightBeRichText(input)); |
0 | 709 |
} |
710 |
||
711 |
Q_DECLARE_METATYPE(QTextDocumentFragment) |
|
712 |
||
713 |
#define CREATE_DOC_AND_CURSOR() \ |
|
714 |
QTextDocument doc; \ |
|
715 |
doc.setDefaultFont(defaultFont); \ |
|
716 |
QTextCursor cursor(&doc); |
|
717 |
||
718 |
void tst_QTextDocument::toHtml_data() |
|
719 |
{ |
|
720 |
QTest::addColumn<QTextDocumentFragment>("input"); |
|
721 |
QTest::addColumn<QString>("expectedOutput"); |
|
722 |
||
723 |
{ |
|
724 |
CREATE_DOC_AND_CURSOR(); |
|
725 |
||
726 |
cursor.insertText("Blah"); |
|
727 |
||
728 |
QTest::newRow("simple") << QTextDocumentFragment(&doc) << QString("<p DEFAULTBLOCKSTYLE>Blah</p>"); |
|
729 |
} |
|
730 |
||
731 |
{ |
|
732 |
CREATE_DOC_AND_CURSOR(); |
|
733 |
||
734 |
cursor.insertText("&<>"); |
|
735 |
||
736 |
QTest::newRow("entities") << QTextDocumentFragment(&doc) << QString("<p DEFAULTBLOCKSTYLE>&<></p>"); |
|
737 |
} |
|
738 |
||
739 |
{ |
|
740 |
CREATE_DOC_AND_CURSOR(); |
|
741 |
||
742 |
QTextCharFormat fmt; |
|
743 |
fmt.setFontFamily("Times"); |
|
744 |
cursor.insertText("Blah", fmt); |
|
745 |
||
746 |
QTest::newRow("font-family") << QTextDocumentFragment(&doc) |
|
747 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:'Times';\">Blah</span></p>"); |
|
748 |
} |
|
749 |
||
750 |
{ |
|
751 |
CREATE_DOC_AND_CURSOR(); |
|
752 |
||
753 |
QTextCharFormat fmt; |
|
754 |
fmt.setFontFamily("Foo's Family"); |
|
755 |
cursor.insertText("Blah", fmt); |
|
756 |
||
757 |
QTest::newRow("font-family-with-quotes1") << QTextDocumentFragment(&doc) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
758 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:"Foo's Family";\">Blah</span></p>"); |
0 | 759 |
} |
760 |
||
761 |
{ |
|
762 |
CREATE_DOC_AND_CURSOR(); |
|
763 |
||
764 |
QTextCharFormat fmt; |
|
765 |
fmt.setFontFamily("Foo\"s Family"); |
|
766 |
cursor.insertText("Blah", fmt); |
|
767 |
||
768 |
QTest::newRow("font-family-with-quotes2") << QTextDocumentFragment(&doc) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
769 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:'Foo"s Family';\">Blah</span></p>"); |
0 | 770 |
} |
771 |
||
772 |
{ |
|
773 |
CREATE_DOC_AND_CURSOR(); |
|
774 |
||
775 |
QTextBlockFormat fmt; |
|
776 |
fmt.setNonBreakableLines(true); |
|
777 |
cursor.insertBlock(fmt); |
|
778 |
cursor.insertText("Blah"); |
|
779 |
||
780 |
QTest::newRow("pre") << QTextDocumentFragment(&doc) |
|
781 |
<< |
|
782 |
QString("EMPTYBLOCK") + |
|
783 |
QString("<pre DEFAULTBLOCKSTYLE>Blah</pre>"); |
|
784 |
} |
|
785 |
||
786 |
{ |
|
787 |
CREATE_DOC_AND_CURSOR(); |
|
788 |
||
789 |
QTextCharFormat fmt; |
|
790 |
fmt.setFontPointSize(40); |
|
791 |
cursor.insertText("Blah", fmt); |
|
792 |
||
793 |
QTest::newRow("font-size") << QTextDocumentFragment(&doc) |
|
794 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-size:40pt;\">Blah</span></p>"); |
|
795 |
} |
|
796 |
||
797 |
{ |
|
798 |
CREATE_DOC_AND_CURSOR(); |
|
799 |
||
800 |
QTextCharFormat fmt; |
|
801 |
fmt.setProperty(QTextFormat::FontSizeIncrement, 2); |
|
802 |
cursor.insertText("Blah", fmt); |
|
803 |
||
804 |
QTest::newRow("logical-font-size") << QTextDocumentFragment(&doc) |
|
805 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-size:x-large;\">Blah</span></p>"); |
|
806 |
} |
|
807 |
||
808 |
{ |
|
809 |
CREATE_DOC_AND_CURSOR(); |
|
810 |
||
811 |
cursor.insertText("Foo"); |
|
812 |
||
813 |
QTextCharFormat fmt; |
|
814 |
fmt.setFontPointSize(40); |
|
815 |
cursor.insertBlock(QTextBlockFormat(), fmt); |
|
816 |
||
817 |
fmt.clearProperty(QTextFormat::FontPointSize); |
|
818 |
cursor.insertText("Blub", fmt); |
|
819 |
||
820 |
QTest::newRow("no-font-size") << QTextDocumentFragment(&doc) |
|
821 |
<< QString("<p DEFAULTBLOCKSTYLE>Foo</p>\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Blub</p>"); |
|
822 |
} |
|
823 |
||
824 |
{ |
|
825 |
CREATE_DOC_AND_CURSOR(); |
|
826 |
||
827 |
QTextBlockFormat fmt; |
|
828 |
fmt.setLayoutDirection(Qt::RightToLeft); |
|
829 |
cursor.insertBlock(fmt); |
|
830 |
cursor.insertText("Blah"); |
|
831 |
||
832 |
QTest::newRow("rtl") << QTextDocumentFragment(&doc) |
|
833 |
<< |
|
834 |
QString("EMPTYBLOCK") + |
|
835 |
QString("<p dir='rtl' DEFAULTBLOCKSTYLE>Blah</p>"); |
|
836 |
} |
|
837 |
||
838 |
{ |
|
839 |
CREATE_DOC_AND_CURSOR(); |
|
840 |
||
841 |
QTextBlockFormat fmt; |
|
842 |
fmt.setAlignment(Qt::AlignJustify); |
|
843 |
cursor.insertBlock(fmt); |
|
844 |
cursor.insertText("Blah"); |
|
845 |
||
846 |
QTest::newRow("blockalign") << QTextDocumentFragment(&doc) |
|
847 |
<< |
|
848 |
QString("EMPTYBLOCK") + |
|
849 |
QString("<p align=\"justify\" DEFAULTBLOCKSTYLE>Blah</p>"); |
|
850 |
} |
|
851 |
||
852 |
{ |
|
853 |
CREATE_DOC_AND_CURSOR(); |
|
854 |
||
855 |
QTextBlockFormat fmt; |
|
856 |
fmt.setAlignment(Qt::AlignCenter); |
|
857 |
cursor.insertBlock(fmt); |
|
858 |
cursor.insertText("Blah"); |
|
859 |
||
860 |
QTest::newRow("blockalign2") << QTextDocumentFragment(&doc) |
|
861 |
<< |
|
862 |
QString("EMPTYBLOCK") + |
|
863 |
QString("<p align=\"center\" DEFAULTBLOCKSTYLE>Blah</p>"); |
|
864 |
} |
|
865 |
||
866 |
{ |
|
867 |
CREATE_DOC_AND_CURSOR(); |
|
868 |
||
869 |
QTextBlockFormat fmt; |
|
870 |
fmt.setAlignment(Qt::AlignRight | Qt::AlignAbsolute); |
|
871 |
cursor.insertBlock(fmt); |
|
872 |
cursor.insertText("Blah"); |
|
873 |
||
874 |
QTest::newRow("blockalign3") << QTextDocumentFragment(&doc) |
|
875 |
<< |
|
876 |
QString("EMPTYBLOCK") + |
|
877 |
QString("<p align=\"right\" DEFAULTBLOCKSTYLE>Blah</p>"); |
|
878 |
} |
|
879 |
||
880 |
{ |
|
881 |
CREATE_DOC_AND_CURSOR(); |
|
882 |
||
883 |
QTextBlockFormat fmt; |
|
884 |
fmt.setBackground(QColor("#0000ff")); |
|
885 |
cursor.insertBlock(fmt); |
|
886 |
cursor.insertText("Blah"); |
|
887 |
||
888 |
QTest::newRow("bgcolor") << QTextDocumentFragment(&doc) |
|
889 |
<< QString("EMPTYBLOCK") + |
|
890 |
QString("<p OPENDEFAULTBLOCKSTYLE background-color:#0000ff;\">Blah</p>"); |
|
891 |
} |
|
892 |
||
893 |
{ |
|
894 |
CREATE_DOC_AND_CURSOR(); |
|
895 |
||
896 |
QTextCharFormat fmt; |
|
897 |
fmt.setFontWeight(40); |
|
898 |
cursor.insertText("Blah", fmt); |
|
899 |
||
900 |
QTest::newRow("font-weight") << QTextDocumentFragment(&doc) |
|
901 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-weight:320;\">Blah</span></p>"); |
|
902 |
} |
|
903 |
||
904 |
{ |
|
905 |
CREATE_DOC_AND_CURSOR(); |
|
906 |
||
907 |
QTextCharFormat fmt; |
|
908 |
fmt.setFontItalic(true); |
|
909 |
cursor.insertText("Blah", fmt); |
|
910 |
||
911 |
QTest::newRow("font-italic") << QTextDocumentFragment(&doc) |
|
912 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" font-style:italic;\">Blah</span></p>"); |
|
913 |
} |
|
914 |
||
915 |
{ |
|
916 |
CREATE_DOC_AND_CURSOR(); |
|
917 |
||
918 |
QTextCharFormat fmt; |
|
919 |
fmt.setFontUnderline(true); |
|
920 |
fmt.setFontOverline(false); |
|
921 |
cursor.insertText("Blah", fmt); |
|
922 |
||
923 |
QTest::newRow("text-decoration-1") << QTextDocumentFragment(&doc) |
|
924 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" text-decoration: underline;\">Blah</span></p>"); |
|
925 |
} |
|
926 |
||
927 |
{ |
|
928 |
CREATE_DOC_AND_CURSOR(); |
|
929 |
||
930 |
QTextCharFormat fmt; |
|
931 |
fmt.setForeground(QColor("#00ff00")); |
|
932 |
cursor.insertText("Blah", fmt); |
|
933 |
||
934 |
QTest::newRow("color") << QTextDocumentFragment(&doc) |
|
935 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" color:#00ff00;\">Blah</span></p>"); |
|
936 |
} |
|
937 |
||
938 |
{ |
|
939 |
CREATE_DOC_AND_CURSOR(); |
|
940 |
||
941 |
QTextCharFormat fmt; |
|
942 |
fmt.setBackground(QColor("#00ff00")); |
|
943 |
cursor.insertText("Blah", fmt); |
|
944 |
||
945 |
QTest::newRow("span-bgcolor") << QTextDocumentFragment(&doc) |
|
946 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" background-color:#00ff00;\">Blah</span></p>"); |
|
947 |
} |
|
948 |
||
949 |
{ |
|
950 |
CREATE_DOC_AND_CURSOR(); |
|
951 |
||
952 |
QTextCharFormat fmt; |
|
953 |
fmt.setVerticalAlignment(QTextCharFormat::AlignSubScript); |
|
954 |
cursor.insertText("Blah", fmt); |
|
955 |
||
956 |
QTest::newRow("valign-sub") << QTextDocumentFragment(&doc) |
|
957 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" vertical-align:sub;\">Blah</span></p>"); |
|
958 |
||
959 |
} |
|
960 |
||
961 |
{ |
|
962 |
CREATE_DOC_AND_CURSOR(); |
|
963 |
||
964 |
QTextCharFormat fmt; |
|
965 |
fmt.setVerticalAlignment(QTextCharFormat::AlignSuperScript); |
|
966 |
cursor.insertText("Blah", fmt); |
|
967 |
||
968 |
QTest::newRow("valign-super") << QTextDocumentFragment(&doc) |
|
969 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" vertical-align:super;\">Blah</span></p>"); |
|
970 |
||
971 |
} |
|
972 |
||
973 |
{ |
|
974 |
CREATE_DOC_AND_CURSOR(); |
|
975 |
||
976 |
QTextCharFormat fmt; |
|
977 |
fmt.setAnchor(true); |
|
978 |
fmt.setAnchorName("blub"); |
|
979 |
cursor.insertText("Blah", fmt); |
|
980 |
||
981 |
QTest::newRow("named anchor") << QTextDocumentFragment(&doc) |
|
982 |
<< QString("<p DEFAULTBLOCKSTYLE><a name=\"blub\"></a>Blah</p>"); |
|
983 |
} |
|
984 |
||
985 |
{ |
|
986 |
CREATE_DOC_AND_CURSOR(); |
|
987 |
||
988 |
QTextCharFormat fmt; |
|
989 |
fmt.setAnchor(true); |
|
990 |
fmt.setAnchorHref("http://www.kde.org/"); |
|
991 |
cursor.insertText("Blah", fmt); |
|
992 |
||
993 |
QTest::newRow("href anchor") << QTextDocumentFragment(&doc) |
|
994 |
<< QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/\">Blah</a></p>"); |
|
995 |
} |
|
996 |
||
997 |
{ |
|
998 |
CREATE_DOC_AND_CURSOR(); |
|
999 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1000 |
QTextCharFormat fmt; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1001 |
fmt.setAnchor(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1002 |
fmt.setAnchorHref("http://www.kde.org/?a=1&b=2"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1003 |
cursor.insertText("Blah", fmt); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1004 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1005 |
QTest::newRow("href anchor with &") << QTextDocumentFragment(&doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1006 |
<< QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/?a=1&b=2\">Blah</a></p>"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1007 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1008 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1009 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1010 |
CREATE_DOC_AND_CURSOR(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1011 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1012 |
QTextCharFormat fmt; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1013 |
fmt.setAnchor(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1014 |
fmt.setAnchorHref("http://www.kde.org/?a='&b=\""); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1015 |
cursor.insertText("Blah", fmt); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1016 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1017 |
QTest::newRow("href anchor with ' and \"") << QTextDocumentFragment(&doc) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1018 |
<< QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/?a='&b="\">Blah</a></p>"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1019 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1020 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1021 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1022 |
CREATE_DOC_AND_CURSOR(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1023 |
|
0 | 1024 |
cursor.insertTable(2, 2); |
1025 |
||
1026 |
QTest::newRow("simpletable") << QTextDocumentFragment(&doc) |
|
1027 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1028 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1029 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1030 |
"</table>"); |
|
1031 |
} |
|
1032 |
||
1033 |
{ |
|
1034 |
CREATE_DOC_AND_CURSOR(); |
|
1035 |
||
1036 |
QTextTable *table = cursor.insertTable(1, 4); |
|
1037 |
table->mergeCells(0, 0, 1, 2); |
|
1038 |
table->mergeCells(0, 2, 1, 2); |
|
1039 |
||
1040 |
QTest::newRow("tablespans") << QTextDocumentFragment(&doc) |
|
1041 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1042 |
"\n<tr>\n<td colspan=\"2\"></td>\n<td colspan=\"2\"></td></tr>" |
|
1043 |
"</table>"); |
|
1044 |
} |
|
1045 |
||
1046 |
{ |
|
1047 |
CREATE_DOC_AND_CURSOR(); |
|
1048 |
||
1049 |
QTextTableFormat fmt; |
|
1050 |
fmt.setBorder(1); |
|
1051 |
fmt.setCellSpacing(3); |
|
1052 |
fmt.setCellPadding(3); |
|
1053 |
fmt.setBackground(QColor("#ff00ff")); |
|
1054 |
fmt.setWidth(QTextLength(QTextLength::PercentageLength, 50)); |
|
1055 |
fmt.setAlignment(Qt::AlignHCenter); |
|
1056 |
fmt.setPosition(QTextFrameFormat::FloatRight); |
|
1057 |
cursor.insertTable(2, 2, fmt); |
|
1058 |
||
1059 |
QTest::newRow("tableattrs") << QTextDocumentFragment(&doc) |
|
1060 |
<< QString("<table border=\"1\" style=\" float: right;\" align=\"center\" width=\"50%\" cellspacing=\"3\" cellpadding=\"3\" bgcolor=\"#ff00ff\">" |
|
1061 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1062 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1063 |
"</table>"); |
|
1064 |
} |
|
1065 |
||
1066 |
{ |
|
1067 |
CREATE_DOC_AND_CURSOR(); |
|
1068 |
||
1069 |
QTextTableFormat fmt; |
|
1070 |
fmt.setBorder(1); |
|
1071 |
fmt.setCellSpacing(3); |
|
1072 |
fmt.setCellPadding(3); |
|
1073 |
fmt.setBackground(QColor("#ff00ff")); |
|
1074 |
fmt.setWidth(QTextLength(QTextLength::PercentageLength, 50)); |
|
1075 |
fmt.setAlignment(Qt::AlignHCenter); |
|
1076 |
fmt.setPosition(QTextFrameFormat::FloatRight); |
|
1077 |
fmt.setLeftMargin(25); |
|
1078 |
fmt.setBottomMargin(35); |
|
1079 |
cursor.insertTable(2, 2, fmt); |
|
1080 |
||
1081 |
QTest::newRow("tableattrs2") << QTextDocumentFragment(&doc) |
|
1082 |
<< QString("<table border=\"1\" style=\" float: right; margin-top:0px; margin-bottom:35px; margin-left:25px; margin-right:0px;\" align=\"center\" width=\"50%\" cellspacing=\"3\" cellpadding=\"3\" bgcolor=\"#ff00ff\">" |
|
1083 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1084 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1085 |
"</table>"); |
|
1086 |
} |
|
1087 |
||
1088 |
{ |
|
1089 |
CREATE_DOC_AND_CURSOR(); |
|
1090 |
||
1091 |
QTextTableFormat fmt; |
|
1092 |
fmt.setHeaderRowCount(2); |
|
1093 |
cursor.insertTable(4, 2, fmt); |
|
1094 |
||
1095 |
QTest::newRow("tableheader") << QTextDocumentFragment(&doc) |
|
1096 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1097 |
"<thead>\n<tr>\n<td></td>\n<td></td></tr>" |
|
1098 |
"\n<tr>\n<td></td>\n<td></td></tr></thead>" |
|
1099 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1100 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1101 |
"</table>"); |
|
1102 |
} |
|
1103 |
||
1104 |
{ |
|
1105 |
CREATE_DOC_AND_CURSOR(); |
|
1106 |
||
1107 |
QTextTable *table = cursor.insertTable(2, 2); |
|
1108 |
QTextTable *subTable = table->cellAt(0, 1).firstCursorPosition().insertTable(1, 1); |
|
1109 |
subTable->cellAt(0, 0).firstCursorPosition().insertText("Hey"); |
|
1110 |
||
1111 |
QTest::newRow("nestedtable") << QTextDocumentFragment(&doc) |
|
1112 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1113 |
"\n<tr>\n<td></td>\n<td>\n<table border=\"1\" cellspacing=\"2\">\n<tr>\n<td>\n<p DEFAULTBLOCKSTYLE>Hey</p></td></tr></table></td></tr>" |
|
1114 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1115 |
"</table>"); |
|
1116 |
} |
|
1117 |
||
1118 |
{ |
|
1119 |
CREATE_DOC_AND_CURSOR(); |
|
1120 |
||
1121 |
QTextTableFormat fmt; |
|
1122 |
QVector<QTextLength> widths; |
|
1123 |
widths.append(QTextLength()); |
|
1124 |
widths.append(QTextLength(QTextLength::PercentageLength, 30)); |
|
1125 |
widths.append(QTextLength(QTextLength::FixedLength, 40)); |
|
1126 |
fmt.setColumnWidthConstraints(widths); |
|
1127 |
cursor.insertTable(1, 3, fmt); |
|
1128 |
||
1129 |
QTest::newRow("colwidths") << QTextDocumentFragment(&doc) |
|
1130 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1131 |
"\n<tr>\n<td></td>\n<td width=\"30%\"></td>\n<td width=\"40\"></td></tr>" |
|
1132 |
"</table>"); |
|
1133 |
} |
|
1134 |
||
1135 |
// ### rowspan/colspan tests, once texttable api for that is back again |
|
1136 |
// |
|
1137 |
{ |
|
1138 |
CREATE_DOC_AND_CURSOR(); |
|
1139 |
||
1140 |
QTextTable *table = cursor.insertTable(1, 1); |
|
1141 |
QTextCursor cellCurs = table->cellAt(0, 0).firstCursorPosition(); |
|
1142 |
QTextCharFormat fmt; |
|
1143 |
fmt.setBackground(QColor("#ffffff")); |
|
1144 |
cellCurs.mergeBlockCharFormat(fmt); |
|
1145 |
||
1146 |
QTest::newRow("cellproperties") << QTextDocumentFragment(&doc) |
|
1147 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1148 |
"\n<tr>\n<td bgcolor=\"#ffffff\"></td></tr>" |
|
1149 |
"</table>"); |
|
1150 |
} |
|
1151 |
||
1152 |
{ |
|
1153 |
CREATE_DOC_AND_CURSOR(); |
|
1154 |
||
1155 |
// ### fixme: use programmatic api as soon as we can create floats through it |
|
1156 |
const char html[] = "<html><body>Blah<img src=\"image.png\" width=\"10\" height=\"20\" style=\"float: right;\" />Blubb</body></html>"; |
|
1157 |
||
1158 |
QTest::newRow("image") << QTextDocumentFragment::fromHtml(QString::fromLatin1(html)) |
|
1159 |
<< QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Blah<img src=\"image.png\" width=\"10\" height=\"20\" style=\"float: right;\" />Blubb</p>"); |
|
1160 |
} |
|
1161 |
||
1162 |
{ |
|
1163 |
CREATE_DOC_AND_CURSOR(); |
|
1164 |
||
1165 |
QTextImageFormat fmt; |
|
1166 |
fmt.setName("foo"); |
|
1167 |
fmt.setVerticalAlignment(QTextCharFormat::AlignMiddle); |
|
1168 |
cursor.insertImage(fmt); |
|
1169 |
||
1170 |
QTest::newRow("image-malign") << QTextDocumentFragment(&doc) |
|
1171 |
<< QString("<p DEFAULTBLOCKSTYLE><img src=\"foo\" style=\"vertical-align: middle;\" /></p>"); |
|
1172 |
} |
|
1173 |
||
1174 |
{ |
|
1175 |
CREATE_DOC_AND_CURSOR(); |
|
1176 |
||
1177 |
QTextImageFormat fmt; |
|
1178 |
fmt.setName("foo"); |
|
1179 |
fmt.setVerticalAlignment(QTextCharFormat::AlignTop); |
|
1180 |
cursor.insertImage(fmt); |
|
1181 |
||
1182 |
QTest::newRow("image-malign") << QTextDocumentFragment(&doc) |
|
1183 |
<< QString("<p DEFAULTBLOCKSTYLE><img src=\"foo\" style=\"vertical-align: top;\" /></p>"); |
|
1184 |
} |
|
1185 |
||
1186 |
{ |
|
1187 |
CREATE_DOC_AND_CURSOR(); |
|
1188 |
||
1189 |
QTextImageFormat fmt; |
|
1190 |
fmt.setName("foo"); |
|
1191 |
cursor.insertImage(fmt); |
|
1192 |
cursor.insertImage(fmt); |
|
1193 |
||
1194 |
QTest::newRow("2images") << QTextDocumentFragment(&doc) |
|
1195 |
<< QString("<p DEFAULTBLOCKSTYLE><img src=\"foo\" /><img src=\"foo\" /></p>"); |
|
1196 |
} |
|
1197 |
||
1198 |
{ |
|
1199 |
CREATE_DOC_AND_CURSOR(); |
|
1200 |
||
1201 |
QString txt = QLatin1String("Blah"); |
|
1202 |
txt += QChar::LineSeparator; |
|
1203 |
txt += QLatin1String("Bar"); |
|
1204 |
cursor.insertText(txt); |
|
1205 |
||
1206 |
QTest::newRow("linebreaks") << QTextDocumentFragment(&doc) |
|
1207 |
<< QString("<p DEFAULTBLOCKSTYLE>Blah<br />Bar</p>"); |
|
1208 |
} |
|
1209 |
||
1210 |
{ |
|
1211 |
CREATE_DOC_AND_CURSOR(); |
|
1212 |
||
1213 |
QTextBlockFormat fmt; |
|
1214 |
fmt.setTopMargin(10); |
|
1215 |
fmt.setBottomMargin(20); |
|
1216 |
fmt.setLeftMargin(30); |
|
1217 |
fmt.setRightMargin(40); |
|
1218 |
cursor.insertBlock(fmt); |
|
1219 |
cursor.insertText("Blah"); |
|
1220 |
||
1221 |
QTest::newRow("blockmargins") << QTextDocumentFragment(&doc) |
|
1222 |
<< |
|
1223 |
QString("EMPTYBLOCK") + |
|
1224 |
QString("<p style=\" margin-top:10px; margin-bottom:20px; margin-left:30px; margin-right:40px; -qt-block-indent:0; text-indent:0px;\">Blah</p>"); |
|
1225 |
} |
|
1226 |
||
1227 |
{ |
|
1228 |
CREATE_DOC_AND_CURSOR(); |
|
1229 |
||
1230 |
QTextList *list = cursor.insertList(QTextListFormat::ListDisc); |
|
1231 |
cursor.insertText("Blubb"); |
|
1232 |
cursor.insertBlock(); |
|
1233 |
cursor.insertText("Blah"); |
|
1234 |
QCOMPARE(list->count(), 2); |
|
1235 |
||
1236 |
QTest::newRow("lists") << QTextDocumentFragment(&doc) |
|
1237 |
<< |
|
1238 |
QString("EMPTYBLOCK") + |
|
1239 |
QString("<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;\"><li DEFAULTBLOCKSTYLE>Blubb</li>\n<li DEFAULTBLOCKSTYLE>Blah</li></ul>"); |
|
1240 |
} |
|
1241 |
||
1242 |
{ |
|
1243 |
CREATE_DOC_AND_CURSOR(); |
|
1244 |
||
1245 |
QTextList *list = cursor.insertList(QTextListFormat::ListDisc); |
|
1246 |
cursor.insertText("Blubb"); |
|
1247 |
||
1248 |
cursor.insertBlock(); |
|
1249 |
||
1250 |
QTextCharFormat blockCharFmt; |
|
1251 |
blockCharFmt.setForeground(QColor("#0000ff")); |
|
1252 |
cursor.mergeBlockCharFormat(blockCharFmt); |
|
1253 |
||
1254 |
QTextCharFormat fmt; |
|
1255 |
fmt.setForeground(QColor("#ff0000")); |
|
1256 |
cursor.insertText("Blah", fmt); |
|
1257 |
QCOMPARE(list->count(), 2); |
|
1258 |
||
1259 |
QTest::newRow("charfmt-for-list-item") << QTextDocumentFragment(&doc) |
|
1260 |
<< |
|
1261 |
QString("EMPTYBLOCK") + |
|
1262 |
QString("<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;\"><li DEFAULTBLOCKSTYLE>Blubb</li>\n<li style=\" color:#0000ff;\" DEFAULTBLOCKSTYLE><span style=\" color:#ff0000;\">Blah</span></li></ul>"); |
|
1263 |
} |
|
1264 |
||
1265 |
{ |
|
1266 |
CREATE_DOC_AND_CURSOR(); |
|
1267 |
||
1268 |
QTextBlockFormat fmt; |
|
1269 |
fmt.setIndent(3); |
|
1270 |
fmt.setTextIndent(30); |
|
1271 |
cursor.insertBlock(fmt); |
|
1272 |
cursor.insertText("Test"); |
|
1273 |
||
1274 |
QTest::newRow("block-indent") << QTextDocumentFragment(&doc) |
|
1275 |
<< |
|
1276 |
QString("EMPTYBLOCK") + |
|
1277 |
QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:3; text-indent:30px;\">Test</p>"); |
|
1278 |
} |
|
1279 |
||
1280 |
{ |
|
1281 |
CREATE_DOC_AND_CURSOR(); |
|
1282 |
||
1283 |
QTextListFormat fmt; |
|
1284 |
fmt.setStyle(QTextListFormat::ListDisc); |
|
1285 |
fmt.setIndent(4); |
|
1286 |
cursor.insertList(fmt); |
|
1287 |
cursor.insertText("Blah"); |
|
1288 |
||
1289 |
QTest::newRow("list-indent") << QTextDocumentFragment(&doc) |
|
1290 |
<< |
|
1291 |
QString("EMPTYBLOCK") + |
|
1292 |
QString("<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 4;\"><li DEFAULTBLOCKSTYLE>Blah</li></ul>"); |
|
1293 |
} |
|
1294 |
||
1295 |
{ |
|
1296 |
CREATE_DOC_AND_CURSOR(); |
|
1297 |
||
1298 |
cursor.insertBlock(); |
|
1299 |
||
1300 |
||
1301 |
QTest::newRow("emptyblock") << QTextDocumentFragment(&doc) |
|
1302 |
// after insertBlock() we /do/ have two blocks in the document, so also expect |
|
1303 |
// these in the html output |
|
1304 |
<< QString("EMPTYBLOCK") + QString("EMPTYBLOCK"); |
|
1305 |
} |
|
1306 |
||
1307 |
{ |
|
1308 |
CREATE_DOC_AND_CURSOR(); |
|
1309 |
||
1310 |
// if you press enter twice in an empty textedit and then insert 'Test' |
|
1311 |
// you actually get three visible paragraphs, two empty leading ones and |
|
1312 |
// a third with the actual text. the corresponding html representation |
|
1313 |
// therefore should also contain three paragraphs. |
|
1314 |
||
1315 |
cursor.insertBlock(); |
|
1316 |
QTextCharFormat fmt; |
|
1317 |
fmt.setForeground(QColor("#00ff00")); |
|
1318 |
fmt.setProperty(QTextFormat::FontSizeIncrement, 1); |
|
1319 |
cursor.mergeBlockCharFormat(fmt); |
|
1320 |
||
1321 |
fmt.setProperty(QTextFormat::FontSizeIncrement, 2); |
|
1322 |
cursor.insertText("Test", fmt); |
|
1323 |
||
1324 |
QTest::newRow("blockcharfmt") << QTextDocumentFragment(&doc) |
|
1325 |
<< QString("EMPTYBLOCK<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:x-large; color:#00ff00;\">Test</span></p>"); |
|
1326 |
} |
|
1327 |
||
1328 |
{ |
|
1329 |
CREATE_DOC_AND_CURSOR(); |
|
1330 |
||
1331 |
QTextCharFormat fmt; |
|
1332 |
fmt.setForeground(QColor("#00ff00")); |
|
1333 |
cursor.setBlockCharFormat(fmt); |
|
1334 |
fmt.setForeground(QColor("#0000ff")); |
|
1335 |
cursor.insertText("Test", fmt); |
|
1336 |
||
1337 |
QTest::newRow("blockcharfmt2") << QTextDocumentFragment(&doc) |
|
1338 |
<< QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" color:#0000ff;\">Test</span></p>"); |
|
1339 |
} |
|
1340 |
||
1341 |
{ |
|
1342 |
QTest::newRow("horizontal-ruler") << QTextDocumentFragment::fromHtml("<hr />") |
|
1343 |
<< |
|
1344 |
QString("EMPTYBLOCK") + |
|
1345 |
QString("<hr />"); |
|
1346 |
} |
|
1347 |
{ |
|
1348 |
QTest::newRow("horizontal-ruler-with-width") << QTextDocumentFragment::fromHtml("<hr width=\"50%\"/>") |
|
1349 |
<< |
|
1350 |
QString("EMPTYBLOCK") + |
|
1351 |
QString("<hr width=\"50%\"/>"); |
|
1352 |
} |
|
1353 |
{ |
|
1354 |
CREATE_DOC_AND_CURSOR(); |
|
1355 |
||
1356 |
QTextFrame *mainFrame = cursor.currentFrame(); |
|
1357 |
||
1358 |
QTextFrameFormat ffmt; |
|
1359 |
ffmt.setBorder(1); |
|
1360 |
ffmt.setPosition(QTextFrameFormat::FloatRight); |
|
1361 |
ffmt.setMargin(2); |
|
1362 |
ffmt.setWidth(100); |
|
1363 |
ffmt.setHeight(50); |
|
1364 |
ffmt.setBackground(QColor("#00ff00")); |
|
1365 |
cursor.insertFrame(ffmt); |
|
1366 |
cursor.insertText("Hello World"); |
|
1367 |
cursor = mainFrame->lastCursorPosition(); |
|
1368 |
||
1369 |
QTest::newRow("frame") << QTextDocumentFragment(&doc) |
|
1370 |
<< QString("<table border=\"1\" style=\"-qt-table-type: frame; float: right; margin-top:2px; margin-bottom:2px; margin-left:2px; margin-right:2px;\" width=\"100\" height=\"50\" bgcolor=\"#00ff00\">\n<tr>\n<td style=\"border: none;\">\n<p DEFAULTBLOCKSTYLE>Hello World</p></td></tr></table>"); |
|
1371 |
} |
|
1372 |
||
1373 |
{ |
|
1374 |
CREATE_DOC_AND_CURSOR(); |
|
1375 |
||
1376 |
QTextCharFormat fmt; |
|
1377 |
fmt.setForeground(QColor("#00ff00")); |
|
1378 |
// fmt.setBackground(QColor("#0000ff")); |
|
1379 |
cursor.setBlockCharFormat(fmt); |
|
1380 |
||
1381 |
fmt.setForeground(QBrush()); |
|
1382 |
// fmt.setBackground(QBrush()); |
|
1383 |
cursor.insertText("Test", fmt); |
|
1384 |
||
1385 |
// QTest::newRow("nostylebrush") << QTextDocumentFragment(&doc) << QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; color:#00ff00; -qt-blockcharfmt-background-color:#0000ff;\">Test</p>"); |
|
1386 |
QTest::newRow("nostylebrush") << QTextDocumentFragment(&doc) << QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Test</p>"); |
|
1387 |
} |
|
1388 |
||
1389 |
{ |
|
1390 |
CREATE_DOC_AND_CURSOR(); |
|
1391 |
||
1392 |
QTextTable *table = cursor.insertTable(2, 2); |
|
1393 |
table->mergeCells(0, 0, 1, 2); |
|
1394 |
QTextTableFormat fmt = table->format(); |
|
1395 |
QVector<QTextLength> widths; |
|
1396 |
widths.append(QTextLength(QTextLength::FixedLength, 20)); |
|
1397 |
widths.append(QTextLength(QTextLength::FixedLength, 40)); |
|
1398 |
fmt.setColumnWidthConstraints(widths); |
|
1399 |
table->setFormat(fmt); |
|
1400 |
||
1401 |
QTest::newRow("mergedtablecolwidths") << QTextDocumentFragment(&doc) |
|
1402 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1403 |
"\n<tr>\n<td colspan=\"2\"></td></tr>" |
|
1404 |
"\n<tr>\n<td width=\"20\"></td>\n<td width=\"40\"></td></tr>" |
|
1405 |
"</table>"); |
|
1406 |
} |
|
1407 |
||
1408 |
{ |
|
1409 |
CREATE_DOC_AND_CURSOR(); |
|
1410 |
||
1411 |
QTextCharFormat fmt; |
|
1412 |
||
1413 |
cursor.insertText("Blah\nGreen yellow green"); |
|
1414 |
cursor.setPosition(0); |
|
1415 |
cursor.setPosition(23, QTextCursor::KeepAnchor); |
|
1416 |
fmt.setBackground(Qt::green); |
|
1417 |
cursor.mergeCharFormat(fmt); |
|
1418 |
cursor.clearSelection(); |
|
1419 |
cursor.setPosition(11); |
|
1420 |
cursor.setPosition(17, QTextCursor::KeepAnchor); |
|
1421 |
fmt.setBackground(Qt::yellow); |
|
1422 |
cursor.mergeCharFormat(fmt); |
|
1423 |
cursor.clearSelection(); |
|
1424 |
||
1425 |
QTest::newRow("multiparagraph-bgcolor") << QTextDocumentFragment(&doc) |
|
1426 |
<< QString("<p DEFAULTBLOCKSTYLE><span style=\" background-color:#00ff00;\">Blah</span></p>\n" |
|
1427 |
"<p DEFAULTBLOCKSTYLE><span style=\" background-color:#00ff00;\">Green </span>" |
|
1428 |
"<span style=\" background-color:#ffff00;\">yellow</span>" |
|
1429 |
"<span style=\" background-color:#00ff00;\"> green</span></p>"); |
|
1430 |
} |
|
1431 |
||
1432 |
{ |
|
1433 |
CREATE_DOC_AND_CURSOR(); |
|
1434 |
||
1435 |
QTextBlockFormat fmt; |
|
1436 |
fmt.setBackground(QColor("#0000ff")); |
|
1437 |
cursor.insertBlock(fmt); |
|
1438 |
||
1439 |
QTextCharFormat charfmt; |
|
1440 |
charfmt.setBackground(QColor("#0000ff")); |
|
1441 |
cursor.insertText("Blah", charfmt); |
|
1442 |
||
1443 |
QTest::newRow("nospan-bgcolor") << QTextDocumentFragment(&doc) |
|
1444 |
<< QString("EMPTYBLOCK") + |
|
1445 |
QString("<p OPENDEFAULTBLOCKSTYLE background-color:#0000ff;\"><span style=\" background-color:#0000ff;\">Blah</span></p>"); |
|
1446 |
} |
|
1447 |
||
1448 |
{ |
|
1449 |
CREATE_DOC_AND_CURSOR(); |
|
1450 |
||
1451 |
QTextTable *table = cursor.insertTable(2, 2); |
|
1452 |
QTextCharFormat fmt = table->cellAt(0, 0).format(); |
|
1453 |
fmt.setVerticalAlignment(QTextCharFormat::AlignMiddle); |
|
1454 |
table->cellAt(0, 0).setFormat(fmt); |
|
1455 |
fmt = table->cellAt(0, 1).format(); |
|
1456 |
fmt.setVerticalAlignment(QTextCharFormat::AlignTop); |
|
1457 |
table->cellAt(0, 1).setFormat(fmt); |
|
1458 |
fmt = table->cellAt(1, 0).format(); |
|
1459 |
fmt.setVerticalAlignment(QTextCharFormat::AlignBottom); |
|
1460 |
table->cellAt(1, 0).setFormat(fmt); |
|
1461 |
||
1462 |
table->cellAt(0, 0).firstCursorPosition().insertText("Blah"); |
|
1463 |
||
1464 |
QTest::newRow("table-vertical-alignment") << QTextDocumentFragment(&doc) |
|
1465 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1466 |
"\n<tr>\n<td style=\" vertical-align:middle;\">\n" |
|
1467 |
"<p DEFAULTBLOCKSTYLE>Blah</p></td>" |
|
1468 |
"\n<td style=\" vertical-align:top;\"></td></tr>" |
|
1469 |
"\n<tr>\n<td style=\" vertical-align:bottom;\"></td>" |
|
1470 |
"\n<td></td></tr>" |
|
1471 |
"</table>"); |
|
1472 |
} |
|
1473 |
||
1474 |
{ |
|
1475 |
CREATE_DOC_AND_CURSOR(); |
|
1476 |
||
1477 |
QTextTable *table = cursor.insertTable(2, 2); |
|
1478 |
QTextTableCellFormat fmt = table->cellAt(0, 0).format().toTableCellFormat(); |
|
1479 |
fmt.setLeftPadding(1); |
|
1480 |
table->cellAt(0, 0).setFormat(fmt); |
|
1481 |
fmt = table->cellAt(0, 1).format().toTableCellFormat(); |
|
1482 |
fmt.setRightPadding(1); |
|
1483 |
table->cellAt(0, 1).setFormat(fmt); |
|
1484 |
fmt = table->cellAt(1, 0).format().toTableCellFormat(); |
|
1485 |
fmt.setTopPadding(1); |
|
1486 |
table->cellAt(1, 0).setFormat(fmt); |
|
1487 |
fmt = table->cellAt(1, 1).format().toTableCellFormat(); |
|
1488 |
fmt.setBottomPadding(1); |
|
1489 |
table->cellAt(1, 1).setFormat(fmt); |
|
1490 |
||
1491 |
table->cellAt(0, 0).firstCursorPosition().insertText("Blah"); |
|
1492 |
||
1493 |
QTest::newRow("table-cell-paddings") << QTextDocumentFragment(&doc) |
|
1494 |
<< QString("<table border=\"1\" cellspacing=\"2\">" |
|
1495 |
"\n<tr>\n<td style=\" padding-left:1;\">\n" |
|
1496 |
"<p DEFAULTBLOCKSTYLE>Blah</p></td>" |
|
1497 |
"\n<td style=\" padding-right:1;\"></td></tr>" |
|
1498 |
"\n<tr>\n<td style=\" padding-top:1;\"></td>" |
|
1499 |
"\n<td style=\" padding-bottom:1;\"></td></tr>" |
|
1500 |
"</table>"); |
|
1501 |
} |
|
1502 |
||
1503 |
{ |
|
1504 |
CREATE_DOC_AND_CURSOR(); |
|
1505 |
||
1506 |
QTextTableFormat fmt; |
|
1507 |
fmt.setBorderBrush(QColor("#0000ff")); |
|
1508 |
fmt.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); |
|
1509 |
cursor.insertTable(2, 2, fmt); |
|
1510 |
||
1511 |
QTest::newRow("tableborder") << QTextDocumentFragment(&doc) |
|
1512 |
<< QString("<table border=\"1\" style=\" border-color:#0000ff; border-style:solid;\" cellspacing=\"2\">" |
|
1513 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1514 |
"\n<tr>\n<td></td>\n<td></td></tr>" |
|
1515 |
"</table>"); |
|
1516 |
} |
|
1517 |
||
1518 |
{ |
|
1519 |
CREATE_DOC_AND_CURSOR(); |
|
1520 |
||
1521 |
cursor.insertBlock(); |
|
1522 |
cursor.insertText("Foo"); |
|
1523 |
||
1524 |
cursor.block().setUserState(42); |
|
1525 |
||
1526 |
QTest::newRow("userstate") << QTextDocumentFragment(&doc) |
|
1527 |
<< QString("EMPTYBLOCK") + |
|
1528 |
QString("<p OPENDEFAULTBLOCKSTYLE -qt-user-state:42;\">Foo</p>"); |
|
1529 |
} |
|
1530 |
||
1531 |
{ |
|
1532 |
CREATE_DOC_AND_CURSOR(); |
|
1533 |
||
1534 |
QTextBlockFormat blockFmt; |
|
1535 |
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore); |
|
1536 |
||
1537 |
cursor.insertBlock(blockFmt); |
|
1538 |
cursor.insertText("Foo"); |
|
1539 |
||
1540 |
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore | QTextFormat::PageBreak_AlwaysAfter); |
|
1541 |
||
1542 |
cursor.insertBlock(blockFmt); |
|
1543 |
cursor.insertText("Bar"); |
|
1544 |
||
1545 |
QTextTableFormat tableFmt; |
|
1546 |
tableFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter); |
|
1547 |
||
1548 |
cursor.insertTable(1, 1, tableFmt); |
|
1549 |
||
1550 |
QTest::newRow("pagebreak") << QTextDocumentFragment(&doc) |
|
1551 |
<< QString("EMPTYBLOCK") + |
|
1552 |
QString("<p OPENDEFAULTBLOCKSTYLE page-break-before:always;\">Foo</p>" |
|
1553 |
"\n<p OPENDEFAULTBLOCKSTYLE page-break-before:always; page-break-after:always;\">Bar</p>" |
|
1554 |
"\n<table border=\"1\" style=\" page-break-after:always;\" cellspacing=\"2\">\n<tr>\n<td></td></tr></table>"); |
|
1555 |
} |
|
1556 |
||
1557 |
{ |
|
1558 |
CREATE_DOC_AND_CURSOR(); |
|
1559 |
||
1560 |
QTextListFormat listFmt; |
|
1561 |
listFmt.setStyle(QTextListFormat::ListDisc); |
|
1562 |
||
1563 |
cursor.insertList(listFmt); |
|
1564 |
cursor.insertText("Blah"); |
|
1565 |
||
1566 |
QTest::newRow("list-ul-margin") << QTextDocumentFragment(&doc) |
|
1567 |
<< QString("EMPTYBLOCK") + |
|
1568 |
QString("<ul style=\"margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;\"><li DEFAULTBLOCKSTYLE>Blah</li></ul>"); |
|
1569 |
} |
|
1570 |
} |
|
1571 |
||
1572 |
void tst_QTextDocument::toHtml() |
|
1573 |
{ |
|
1574 |
QFETCH(QTextDocumentFragment, input); |
|
1575 |
QFETCH(QString, expectedOutput); |
|
1576 |
||
1577 |
cursor.insertFragment(input); |
|
1578 |
||
1579 |
expectedOutput.prepend(htmlHead); |
|
1580 |
||
1581 |
expectedOutput.replace("OPENDEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"); |
|
1582 |
expectedOutput.replace("DEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\""); |
|
1583 |
expectedOutput.replace("EMPTYBLOCK", "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"></p>\n"); |
|
1584 |
if (expectedOutput.endsWith(QLatin1Char('\n'))) |
|
1585 |
expectedOutput.chop(1); |
|
1586 |
expectedOutput.append(htmlTail); |
|
1587 |
||
1588 |
QString output = doc->toHtml(); |
|
1589 |
||
1590 |
QCOMPARE(output, expectedOutput); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1591 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1592 |
QDomDocument document; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1593 |
QVERIFY2(document.setContent(output), "Output was not valid XML"); |
0 | 1594 |
} |
1595 |
||
1596 |
void tst_QTextDocument::toHtml2() |
|
1597 |
{ |
|
1598 |
QTextDocument doc; |
|
1599 |
doc.setHtml("<p>text <img src=\"\"> text</p>"); // 4 spaces before the second 'text' |
|
1600 |
QTextBlock block = doc.firstBlock(); |
|
1601 |
QTextBlock::Iterator iter = block.begin(); |
|
1602 |
QTextFragment f = iter.fragment(); |
|
1603 |
QVERIFY(f.isValid()); |
|
1604 |
QCOMPARE(f.position(), 0); |
|
1605 |
QCOMPARE(f.length(), 5); |
|
1606 |
//qDebug() << block.text().mid(f.position(), f.length()); |
|
1607 |
||
1608 |
iter++; |
|
1609 |
f = iter.fragment(); |
|
1610 |
QVERIFY(f.isValid()); |
|
1611 |
QCOMPARE(f.position(), 5); |
|
1612 |
QCOMPARE(f.length(), 1); |
|
1613 |
//qDebug() << block.text().mid(f.position(), f.length()); |
|
1614 |
||
1615 |
iter++; |
|
1616 |
f = iter.fragment(); |
|
1617 |
//qDebug() << block.text().mid(f.position(), f.length()); |
|
1618 |
QVERIFY(f.isValid()); |
|
1619 |
QCOMPARE(f.position(), 6); |
|
1620 |
QCOMPARE(f.length(), 5); // 1 space should be preserved. |
|
1621 |
QCOMPARE(block.text().mid(f.position(), f.length()), QString(" text")); |
|
1622 |
||
1623 |
doc.setHtml("<table><tr><td> foo</td></tr></table> text"); // 4 spaces before the second 'text' |
|
1624 |
block = doc.firstBlock().next(); |
|
1625 |
//qDebug() << block.text(); |
|
1626 |
QCOMPARE(block.text(), QString("foo")); |
|
1627 |
||
1628 |
block = block.next(); |
|
1629 |
//qDebug() << block.text(); |
|
1630 |
QCOMPARE(block.text(), QString("text")); |
|
1631 |
} |
|
1632 |
||
1633 |
void tst_QTextDocument::setFragmentMarkersInHtmlExport() |
|
1634 |
{ |
|
1635 |
{ |
|
1636 |
CREATE_DOC_AND_CURSOR(); |
|
1637 |
||
1638 |
cursor.insertText("Leadin"); |
|
1639 |
const int startPos = cursor.position(); |
|
1640 |
||
1641 |
cursor.insertText("Test"); |
|
1642 |
QTextCharFormat fmt; |
|
1643 |
fmt.setForeground(QColor("#00ff00")); |
|
1644 |
cursor.insertText("Blah", fmt); |
|
1645 |
||
1646 |
const int endPos = cursor.position(); |
|
1647 |
cursor.insertText("Leadout", QTextCharFormat()); |
|
1648 |
||
1649 |
cursor.setPosition(startPos); |
|
1650 |
cursor.setPosition(endPos, QTextCursor::KeepAnchor); |
|
1651 |
QTextDocumentFragment fragment(cursor); |
|
1652 |
||
1653 |
QString expected = htmlHead; |
|
1654 |
expected.replace(QRegExp("<body.*>"), QString("<body>")); |
|
1655 |
expected += QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><!--StartFragment-->Test<span style=\" color:#00ff00;\">Blah</span><!--EndFragment--></p>") + htmlTail; |
|
1656 |
QCOMPARE(fragment.toHtml(), expected); |
|
1657 |
} |
|
1658 |
{ |
|
1659 |
CREATE_DOC_AND_CURSOR(); |
|
1660 |
||
1661 |
cursor.insertText("Leadin"); |
|
1662 |
const int startPos = cursor.position(); |
|
1663 |
||
1664 |
cursor.insertText("Test"); |
|
1665 |
||
1666 |
const int endPos = cursor.position(); |
|
1667 |
cursor.insertText("Leadout", QTextCharFormat()); |
|
1668 |
||
1669 |
cursor.setPosition(startPos); |
|
1670 |
cursor.setPosition(endPos, QTextCursor::KeepAnchor); |
|
1671 |
QTextDocumentFragment fragment(cursor); |
|
1672 |
||
1673 |
QString expected = htmlHead; |
|
1674 |
expected.replace(QRegExp("<body.*>"), QString("<body>")); |
|
1675 |
expected += QString("<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><!--StartFragment-->Test<!--EndFragment--></p>") + htmlTail; |
|
1676 |
QCOMPARE(fragment.toHtml(), expected); |
|
1677 |
} |
|
1678 |
} |
|
1679 |
||
1680 |
void tst_QTextDocument::toHtmlBodyBgColor() |
|
1681 |
{ |
|
1682 |
CREATE_DOC_AND_CURSOR(); |
|
1683 |
||
1684 |
cursor.insertText("Blah"); |
|
1685 |
||
1686 |
QTextFrameFormat fmt = doc.rootFrame()->frameFormat(); |
|
1687 |
fmt.setBackground(QColor("#0000ff")); |
|
1688 |
doc.rootFrame()->setFrameFormat(fmt); |
|
1689 |
||
1690 |
QString expectedHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" " |
|
1691 |
"\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" |
|
1692 |
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" |
|
1693 |
"p, li { white-space: pre-wrap; }\n" |
|
1694 |
"</style></head>" |
|
1695 |
"<body style=\" font-family:'%1'; font-size:%2pt; font-weight:%3; font-style:%4;\"" |
|
1696 |
" bgcolor=\"#0000ff\">\n" |
|
1697 |
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Blah</p>" |
|
1698 |
"</body></html>"); |
|
1699 |
||
1700 |
expectedHtml = expectedHtml.arg(defaultFont.family()).arg(defaultFont.pointSizeF()).arg(defaultFont.weight() * 8).arg((defaultFont.italic() ? "italic" : "normal")); |
|
1701 |
||
1702 |
QCOMPARE(doc.toHtml(), expectedHtml); |
|
1703 |
} |
|
1704 |
||
1705 |
void tst_QTextDocument::toHtmlRootFrameProperties() |
|
1706 |
{ |
|
1707 |
CREATE_DOC_AND_CURSOR(); |
|
1708 |
||
1709 |
QTextFrameFormat fmt = doc.rootFrame()->frameFormat(); |
|
1710 |
fmt.setTopMargin(10); |
|
1711 |
fmt.setLeftMargin(10); |
|
1712 |
fmt.setBorder(2); |
|
1713 |
doc.rootFrame()->setFrameFormat(fmt); |
|
1714 |
||
1715 |
cursor.insertText("Blah"); |
|
1716 |
||
1717 |
QString expectedOutput("<table border=\"2\" style=\"-qt-table-type: root; margin-top:10px; " |
|
1718 |
"margin-bottom:4px; margin-left:10px; margin-right:4px;\">\n" |
|
1719 |
"<tr>\n<td style=\"border: none;\">\n" |
|
1720 |
"<p DEFAULTBLOCKSTYLE>Blah</p></td></tr></table>"); |
|
1721 |
||
1722 |
expectedOutput.prepend(htmlHead); |
|
1723 |
expectedOutput.replace("DEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\""); |
|
1724 |
expectedOutput.append(htmlTail); |
|
1725 |
||
1726 |
QCOMPARE(doc.toHtml(), expectedOutput); |
|
1727 |
} |
|
1728 |
||
1729 |
void tst_QTextDocument::capitalizationHtmlInExport() |
|
1730 |
{ |
|
1731 |
doc->setPlainText("Test"); |
|
1732 |
||
1733 |
QRegExp re(".*span style=\"(.*)\">Test.*"); |
|
1734 |
QVERIFY(re.exactMatch(doc->toHtml()) == false); // no span |
|
1735 |
||
1736 |
QTextCursor cursor(doc); |
|
1737 |
cursor.setPosition(4, QTextCursor::KeepAnchor); |
|
1738 |
QTextCharFormat cf; |
|
1739 |
cf.setFontCapitalization(QFont::SmallCaps); |
|
1740 |
cursor.mergeCharFormat(cf); |
|
1741 |
||
1742 |
const QString smallcaps = doc->toHtml(); |
|
1743 |
QVERIFY(re.exactMatch(doc->toHtml())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1744 |
QCOMPARE(re.captureCount(), 1); |
0 | 1745 |
QCOMPARE(re.cap(1).trimmed(), QString("font-variant:small-caps;")); |
1746 |
||
1747 |
cf.setFontCapitalization(QFont::AllUppercase); |
|
1748 |
cursor.mergeCharFormat(cf); |
|
1749 |
const QString uppercase = doc->toHtml(); |
|
1750 |
QVERIFY(re.exactMatch(doc->toHtml())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1751 |
QCOMPARE(re.captureCount(), 1); |
0 | 1752 |
QCOMPARE(re.cap(1).trimmed(), QString("text-transform:uppercase;")); |
1753 |
||
1754 |
cf.setFontCapitalization(QFont::AllLowercase); |
|
1755 |
cursor.mergeCharFormat(cf); |
|
1756 |
const QString lowercase = doc->toHtml(); |
|
1757 |
QVERIFY(re.exactMatch(doc->toHtml())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1758 |
QCOMPARE(re.captureCount(), 1); |
0 | 1759 |
QCOMPARE(re.cap(1).trimmed(), QString("text-transform:lowercase;")); |
1760 |
||
1761 |
doc->setHtml(smallcaps); |
|
1762 |
cursor.setPosition(1); |
|
1763 |
QCOMPARE(cursor.charFormat().fontCapitalization(), QFont::SmallCaps); |
|
1764 |
doc->setHtml(uppercase); |
|
1765 |
QCOMPARE(cursor.charFormat().fontCapitalization(), QFont::AllUppercase); |
|
1766 |
doc->setHtml(lowercase); |
|
1767 |
QCOMPARE(cursor.charFormat().fontCapitalization(), QFont::AllLowercase); |
|
1768 |
} |
|
1769 |
||
1770 |
void tst_QTextDocument::wordspacingHtmlExport() |
|
1771 |
{ |
|
1772 |
doc->setPlainText("Test"); |
|
1773 |
||
1774 |
QRegExp re(".*span style=\"(.*)\">Test.*"); |
|
1775 |
QVERIFY(re.exactMatch(doc->toHtml()) == false); // no span |
|
1776 |
||
1777 |
QTextCursor cursor(doc); |
|
1778 |
cursor.setPosition(4, QTextCursor::KeepAnchor); |
|
1779 |
QTextCharFormat cf; |
|
1780 |
cf.setFontWordSpacing(4); |
|
1781 |
cursor.mergeCharFormat(cf); |
|
1782 |
||
1783 |
QVERIFY(re.exactMatch(doc->toHtml())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1784 |
QCOMPARE(re.captureCount(), 1); |
0 | 1785 |
QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:4px;")); |
1786 |
||
1787 |
cf.setFontWordSpacing(-8.5); |
|
1788 |
cursor.mergeCharFormat(cf); |
|
1789 |
||
1790 |
QVERIFY(re.exactMatch(doc->toHtml())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1791 |
QCOMPARE(re.captureCount(), 1); |
0 | 1792 |
QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:-8.5px;")); |
1793 |
} |
|
1794 |
||
1795 |
class CursorPosSignalSpy : public QObject |
|
1796 |
{ |
|
1797 |
Q_OBJECT |
|
1798 |
public: |
|
1799 |
CursorPosSignalSpy(QTextDocument *doc) |
|
1800 |
{ |
|
1801 |
calls = 0; |
|
1802 |
connect(doc, SIGNAL(cursorPositionChanged(const QTextCursor &)), |
|
1803 |
this, SLOT(cursorPositionChanged(const QTextCursor &))); |
|
1804 |
} |
|
1805 |
||
1806 |
int calls; |
|
1807 |
||
1808 |
private slots: |
|
1809 |
void cursorPositionChanged(const QTextCursor &) |
|
1810 |
{ |
|
1811 |
++calls; |
|
1812 |
} |
|
1813 |
}; |
|
1814 |
||
1815 |
void tst_QTextDocument::cursorPositionChanged() |
|
1816 |
{ |
|
1817 |
CursorPosSignalSpy spy(doc); |
|
1818 |
||
1819 |
cursor.insertText("Test"); |
|
1820 |
QCOMPARE(spy.calls, 1); |
|
1821 |
||
1822 |
spy.calls = 0; |
|
1823 |
QTextCursor unrelatedCursor(doc); |
|
1824 |
unrelatedCursor.insertText("Blah"); |
|
1825 |
QCOMPARE(spy.calls, 2); |
|
1826 |
||
1827 |
spy.calls = 0; |
|
1828 |
cursor.insertText("Blah"); |
|
1829 |
QCOMPARE(spy.calls, 1); |
|
1830 |
||
1831 |
spy.calls = 0; |
|
1832 |
cursor.movePosition(QTextCursor::PreviousCharacter); |
|
1833 |
QCOMPARE(spy.calls, 0); |
|
1834 |
} |
|
1835 |
||
1836 |
void tst_QTextDocument::cursorPositionChangedOnSetText() |
|
1837 |
{ |
|
1838 |
CursorPosSignalSpy spy(doc); |
|
1839 |
||
1840 |
// doc has one QTextCursor stored in the |
|
1841 |
// cursor member variable, thus the signal |
|
1842 |
// gets emitted once. |
|
1843 |
||
1844 |
doc->setPlainText("Foo\nBar\nBaz\nBlub\nBlah"); |
|
1845 |
||
1846 |
QCOMPARE(spy.calls, 1); |
|
1847 |
||
1848 |
spy.calls = 0; |
|
1849 |
doc->setHtml("<p>Foo<p>Bar<p>Baz<p>Blah"); |
|
1850 |
||
1851 |
QCOMPARE(spy.calls, 1); |
|
1852 |
} |
|
1853 |
||
1854 |
void tst_QTextDocument::textFrameIterator() |
|
1855 |
{ |
|
1856 |
cursor.insertTable(1, 1); |
|
1857 |
||
1858 |
int blockCount = 0; |
|
1859 |
int frameCount = 0; |
|
1860 |
||
1861 |
for (QTextFrame::Iterator frameIt = doc->rootFrame()->begin(); |
|
1862 |
!frameIt.atEnd(); ++frameIt) { |
|
1863 |
if (frameIt.currentFrame()) |
|
1864 |
++frameCount; |
|
1865 |
else if (frameIt.currentBlock().isValid()) |
|
1866 |
++blockCount; |
|
1867 |
||
1868 |
} |
|
1869 |
||
1870 |
QEXPECT_FAIL("", "This is currently worked around in the html export but needs fixing!", Continue); |
|
1871 |
QCOMPARE(blockCount, 0); |
|
1872 |
QCOMPARE(frameCount, 1); |
|
1873 |
} |
|
1874 |
||
1875 |
void tst_QTextDocument::codecForHtml() |
|
1876 |
{ |
|
1877 |
const QByteArray header("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;charset=utf-16\">"); |
|
1878 |
QTextCodec *c = Qt::codecForHtml(header); |
|
1879 |
QVERIFY(c); |
|
1880 |
QCOMPARE(c->name(), QByteArray("UTF-16")); |
|
1881 |
} |
|
1882 |
||
1883 |
class TestSyntaxHighlighter : public QObject |
|
1884 |
{ |
|
1885 |
Q_OBJECT |
|
1886 |
public: |
|
1887 |
inline TestSyntaxHighlighter(QTextDocument *doc) : QObject(doc), ok(false) {} |
|
1888 |
||
1889 |
bool ok; |
|
1890 |
||
1891 |
private slots: |
|
1892 |
inline void markBlockDirty(int from, int charsRemoved, int charsAdded) |
|
1893 |
{ |
|
1894 |
Q_UNUSED(charsRemoved); |
|
1895 |
Q_UNUSED(charsAdded); |
|
1896 |
QTextDocument *doc = static_cast<QTextDocument *>(parent()); |
|
1897 |
QTextBlock block = doc->findBlock(from); |
|
1898 |
||
1899 |
QTestDocumentLayout *lout = qobject_cast<QTestDocumentLayout *>(doc->documentLayout()); |
|
1900 |
lout->called = false; |
|
1901 |
||
1902 |
doc->markContentsDirty(block.position(), block.length()); |
|
1903 |
||
1904 |
ok = (lout->called == false); |
|
1905 |
} |
|
1906 |
||
1907 |
inline void modifyBlockAgain(int from, int charsRemoved, int charsAdded) |
|
1908 |
{ |
|
1909 |
Q_UNUSED(charsRemoved); |
|
1910 |
Q_UNUSED(charsAdded); |
|
1911 |
QTextDocument *doc = static_cast<QTextDocument *>(parent()); |
|
1912 |
QTextBlock block = doc->findBlock(from); |
|
1913 |
QTextCursor cursor(block); |
|
1914 |
||
1915 |
QTestDocumentLayout *lout = qobject_cast<QTestDocumentLayout *>(doc->documentLayout()); |
|
1916 |
lout->called = false; |
|
1917 |
||
1918 |
cursor.insertText("Foo"); |
|
1919 |
||
1920 |
ok = (lout->called == true); |
|
1921 |
} |
|
1922 |
}; |
|
1923 |
||
1924 |
void tst_QTextDocument::markContentsDirty() |
|
1925 |
{ |
|
1926 |
QTestDocumentLayout *lout = new QTestDocumentLayout(doc); |
|
1927 |
doc->setDocumentLayout(lout); |
|
1928 |
TestSyntaxHighlighter *highlighter = new TestSyntaxHighlighter(doc); |
|
1929 |
connect(doc, SIGNAL(contentsChange(int, int, int)), |
|
1930 |
highlighter, SLOT(markBlockDirty(int, int, int))); |
|
1931 |
||
1932 |
highlighter->ok = false; |
|
1933 |
cursor.insertText("Some dummy text blah blah"); |
|
1934 |
QVERIFY(highlighter->ok); |
|
1935 |
||
1936 |
disconnect(doc, SIGNAL(contentsChange(int, int, int)), |
|
1937 |
highlighter, SLOT(markBlockDirty(int, int, int))); |
|
1938 |
connect(doc, SIGNAL(contentsChange(int, int, int)), |
|
1939 |
highlighter, SLOT(modifyBlockAgain(int, int, int))); |
|
1940 |
highlighter->ok = false; |
|
1941 |
cursor.insertText("FooBar"); |
|
1942 |
QVERIFY(highlighter->ok); |
|
1943 |
||
1944 |
lout->called = false; |
|
1945 |
||
1946 |
doc->markContentsDirty(1, 4); |
|
1947 |
||
1948 |
QVERIFY(lout->called); |
|
1949 |
} |
|
1950 |
||
1951 |
void tst_QTextDocument::clonePreservesMetaInformation() |
|
1952 |
{ |
|
1953 |
const QString title("Foobar"); |
|
1954 |
const QString url("about:blank"); |
|
1955 |
doc->setHtml("<html><head><title>" + title + "</title></head><body>Hrm</body></html>"); |
|
1956 |
doc->setMetaInformation(QTextDocument::DocumentUrl, url); |
|
1957 |
QCOMPARE(doc->metaInformation(QTextDocument::DocumentTitle), title); |
|
1958 |
QCOMPARE(doc->metaInformation(QTextDocument::DocumentUrl), url); |
|
1959 |
||
1960 |
QTextDocument *clone = doc->clone(); |
|
1961 |
QCOMPARE(clone->metaInformation(QTextDocument::DocumentTitle), title); |
|
1962 |
QCOMPARE(clone->metaInformation(QTextDocument::DocumentUrl), url); |
|
1963 |
delete clone; |
|
1964 |
} |
|
1965 |
||
1966 |
void tst_QTextDocument::clonePreservesPageSize() |
|
1967 |
{ |
|
1968 |
QSizeF sz(100., 100.); |
|
1969 |
doc->setPageSize(sz); |
|
1970 |
QTextDocument *clone = doc->clone(); |
|
1971 |
QCOMPARE(clone->pageSize(), sz); |
|
1972 |
delete clone; |
|
1973 |
} |
|
1974 |
||
1975 |
void tst_QTextDocument::clonePreservesPageBreakPolicies() |
|
1976 |
{ |
|
1977 |
QTextTableFormat tableFmt; |
|
1978 |
tableFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter); |
|
1979 |
||
1980 |
QTextBlockFormat blockFmt; |
|
1981 |
blockFmt.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore); |
|
1982 |
||
1983 |
QTextCursor cursor(doc); |
|
1984 |
||
1985 |
cursor.setBlockFormat(blockFmt); |
|
1986 |
cursor.insertText("foo"); |
|
1987 |
cursor.insertTable(2, 2, tableFmt); |
|
1988 |
||
1989 |
QTextDocument *clone = doc->clone(); |
|
1990 |
QCOMPARE(clone->begin().blockFormat().pageBreakPolicy(), QTextFormat::PageBreak_AlwaysBefore); |
|
1991 |
QVERIFY(!clone->rootFrame()->childFrames().isEmpty()); |
|
1992 |
QCOMPARE(clone->rootFrame()->childFrames().first()->frameFormat().pageBreakPolicy(), QTextFormat::PageBreak_AlwaysAfter); |
|
1993 |
delete clone; |
|
1994 |
} |
|
1995 |
||
1996 |
void tst_QTextDocument::clonePreservesDefaultFont() |
|
1997 |
{ |
|
1998 |
QFont f = doc->defaultFont(); |
|
1999 |
QVERIFY(f.pointSize() != 100); |
|
2000 |
f.setPointSize(100); |
|
2001 |
doc->setDefaultFont(f); |
|
2002 |
QTextDocument *clone = doc->clone(); |
|
2003 |
QCOMPARE(clone->defaultFont(), f); |
|
2004 |
delete clone; |
|
2005 |
} |
|
2006 |
||
2007 |
void tst_QTextDocument::clonePreservesResources() |
|
2008 |
{ |
|
2009 |
QUrl testUrl(":/foobar"); |
|
2010 |
QVariant testResource("hello world"); |
|
2011 |
||
2012 |
doc->addResource(QTextDocument::ImageResource, testUrl, testResource); |
|
2013 |
QTextDocument *clone = doc->clone(); |
|
2014 |
QVERIFY(clone->resource(QTextDocument::ImageResource, testUrl) == testResource); |
|
2015 |
delete clone; |
|
2016 |
} |
|
2017 |
||
2018 |
void tst_QTextDocument::clonePreservesUserStates() |
|
2019 |
{ |
|
2020 |
QTextCursor cursor(doc); |
|
2021 |
cursor.insertText("bla bla bla"); |
|
2022 |
cursor.block().setUserState(1); |
|
2023 |
cursor.insertBlock(); |
|
2024 |
cursor.insertText("foo bar"); |
|
2025 |
cursor.block().setUserState(2); |
|
2026 |
cursor.insertBlock(); |
|
2027 |
cursor.insertText("no user state"); |
|
2028 |
||
2029 |
QTextDocument *clone = doc->clone(); |
|
2030 |
QTextBlock b1 = doc->begin(), b2 = clone->begin(); |
|
2031 |
while (b1 != doc->end()) { |
|
2032 |
b1 = b1.next(); |
|
2033 |
b2 = b2.next(); |
|
2034 |
QCOMPARE(b1.userState(), b2.userState()); |
|
2035 |
} |
|
2036 |
QVERIFY(b2 == clone->end()); |
|
2037 |
delete clone; |
|
2038 |
} |
|
2039 |
||
2040 |
void tst_QTextDocument::clonePreservesRootFrameFormat() |
|
2041 |
{ |
|
2042 |
doc->setPlainText("Hello"); |
|
2043 |
QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); |
|
2044 |
fmt.setMargin(200); |
|
2045 |
doc->rootFrame()->setFrameFormat(fmt); |
|
2046 |
QCOMPARE(doc->rootFrame()->frameFormat().margin(), qreal(200)); |
|
2047 |
QTextDocument *copy = doc->clone(); |
|
2048 |
QCOMPARE(copy->rootFrame()->frameFormat().margin(), qreal(200)); |
|
2049 |
delete copy; |
|
2050 |
} |
|
2051 |
||
2052 |
void tst_QTextDocument::clonePreservesIndentWidth() |
|
2053 |
{ |
|
2054 |
doc->setIndentWidth(42); |
|
2055 |
QTextDocument *clone = doc->clone(); |
|
2056 |
QCOMPARE(clone->indentWidth(), qreal(42)); |
|
2057 |
} |
|
2058 |
||
2059 |
void tst_QTextDocument::blockCount() |
|
2060 |
{ |
|
2061 |
QCOMPARE(doc->blockCount(), 1); |
|
2062 |
cursor.insertBlock(); |
|
2063 |
QCOMPARE(doc->blockCount(), 2); |
|
2064 |
cursor.insertBlock(); |
|
2065 |
QCOMPARE(doc->blockCount(), 3); |
|
2066 |
cursor.insertText("blah blah"); |
|
2067 |
QCOMPARE(doc->blockCount(), 3); |
|
2068 |
doc->undo(); |
|
2069 |
doc->undo(); |
|
2070 |
QCOMPARE(doc->blockCount(), 2); |
|
2071 |
doc->undo(); |
|
2072 |
QCOMPARE(doc->blockCount(), 1); |
|
2073 |
} |
|
2074 |
||
2075 |
void tst_QTextDocument::resolvedFontInEmptyFormat() |
|
2076 |
{ |
|
2077 |
QFont font; |
|
2078 |
font.setPointSize(42); |
|
2079 |
doc->setDefaultFont(font); |
|
2080 |
QTextCharFormat fmt = doc->begin().charFormat(); |
|
2081 |
QVERIFY(fmt.properties().isEmpty()); |
|
2082 |
QVERIFY(fmt.font() == font); |
|
2083 |
} |
|
2084 |
||
2085 |
void tst_QTextDocument::defaultRootFrameMargin() |
|
2086 |
{ |
|
2087 |
QCOMPARE(doc->rootFrame()->frameFormat().margin(), 4.0); |
|
2088 |
} |
|
2089 |
||
2090 |
class TestDocument : public QTextDocument |
|
2091 |
{ |
|
2092 |
public: |
|
2093 |
inline TestDocument(const QUrl &testUrl, const QString &testString) |
|
2094 |
: url(testUrl), string(testString), resourceLoaded(false) {} |
|
2095 |
||
2096 |
bool hasResourceCached(); |
|
2097 |
||
2098 |
protected: |
|
2099 |
virtual QVariant loadResource(int type, const QUrl &name); |
|
2100 |
||
2101 |
private: |
|
2102 |
QUrl url; |
|
2103 |
QString string; |
|
2104 |
bool resourceLoaded; |
|
2105 |
}; |
|
2106 |
||
2107 |
bool TestDocument::hasResourceCached() |
|
2108 |
{ |
|
2109 |
resourceLoaded = false; |
|
2110 |
resource(QTextDocument::ImageResource, url); |
|
2111 |
return !resourceLoaded; |
|
2112 |
} |
|
2113 |
||
2114 |
QVariant TestDocument::loadResource(int type, const QUrl &name) |
|
2115 |
{ |
|
2116 |
if (type == QTextDocument::ImageResource |
|
2117 |
&& name == url) { |
|
2118 |
resourceLoaded = true; |
|
2119 |
return string; |
|
2120 |
} |
|
2121 |
return QTextDocument::loadResource(type, name); |
|
2122 |
} |
|
2123 |
||
2124 |
void tst_QTextDocument::clearResources() |
|
2125 |
{ |
|
2126 |
// regular resource for QTextDocument |
|
2127 |
QUrl testUrl(":/foobar"); |
|
2128 |
QVariant testResource("hello world"); |
|
2129 |
||
2130 |
// implicitly cached resource, initially loaded through TestDocument::loadResource() |
|
2131 |
QUrl cacheUrl(":/blub"); |
|
2132 |
QString cacheResource("mah"); |
|
2133 |
||
2134 |
TestDocument doc(cacheUrl, cacheResource); |
|
2135 |
doc.addResource(QTextDocument::ImageResource, testUrl, testResource); |
|
2136 |
||
2137 |
QVERIFY(doc.resource(QTextDocument::ImageResource, testUrl) == testResource); |
|
2138 |
||
2139 |
doc.setPlainText("Hah"); |
|
2140 |
QVERIFY(doc.resource(QTextDocument::ImageResource, testUrl) == testResource); |
|
2141 |
||
2142 |
doc.setHtml("<b>Mooo</b><img src=\":/blub\"/>"); |
|
2143 |
QVERIFY(doc.resource(QTextDocument::ImageResource, testUrl) == testResource); |
|
2144 |
QVERIFY(doc.resource(QTextDocument::ImageResource, cacheUrl) == cacheResource); |
|
2145 |
||
2146 |
doc.clear(); |
|
2147 |
QVERIFY(!doc.resource(QTextDocument::ImageResource, testUrl).isValid()); |
|
2148 |
QVERIFY(!doc.hasResourceCached()); |
|
2149 |
doc.clear(); |
|
2150 |
||
2151 |
doc.setHtml("<b>Mooo</b><img src=\":/blub\"/>"); |
|
2152 |
QVERIFY(doc.resource(QTextDocument::ImageResource, cacheUrl) == cacheResource); |
|
2153 |
||
2154 |
doc.setPlainText("Foob"); |
|
2155 |
QVERIFY(!doc.hasResourceCached()); |
|
2156 |
} |
|
2157 |
||
2158 |
void tst_QTextDocument::setPlainText() |
|
2159 |
{ |
|
2160 |
doc->setPlainText("Hello World"); |
|
2161 |
QString s(""); |
|
2162 |
doc->setPlainText(s); |
|
2163 |
QCOMPARE(doc->toPlainText(), s); |
|
2164 |
} |
|
2165 |
||
2166 |
void tst_QTextDocument::toPlainText() |
|
2167 |
{ |
|
2168 |
doc->setHtml("Hello World"); |
|
2169 |
QCOMPARE(doc->toPlainText(), QLatin1String("Hello World")); |
|
2170 |
} |
|
2171 |
||
2172 |
void tst_QTextDocument::deleteTextObjectsOnClear() |
|
2173 |
{ |
|
2174 |
QPointer<QTextTable> table = cursor.insertTable(2, 2); |
|
2175 |
QVERIFY(!table.isNull()); |
|
2176 |
doc->clear(); |
|
2177 |
QVERIFY(table.isNull()); |
|
2178 |
} |
|
2179 |
||
2180 |
void tst_QTextDocument::defaultStyleSheet() |
|
2181 |
{ |
|
2182 |
const QString sheet("p { background-color: green; }"); |
|
2183 |
QVERIFY(doc->defaultStyleSheet().isEmpty()); |
|
2184 |
doc->setDefaultStyleSheet(sheet); |
|
2185 |
QCOMPARE(doc->defaultStyleSheet(), sheet); |
|
2186 |
||
2187 |
cursor.insertHtml("<p>test"); |
|
2188 |
QTextBlockFormat fmt = doc->begin().blockFormat(); |
|
2189 |
QVERIFY(fmt.background().color() == QColor("green")); |
|
2190 |
||
2191 |
doc->clear(); |
|
2192 |
cursor.insertHtml("<p>test"); |
|
2193 |
fmt = doc->begin().blockFormat(); |
|
2194 |
QVERIFY(fmt.background().color() == QColor("green")); |
|
2195 |
||
2196 |
QTextDocument *clone = doc->clone(); |
|
2197 |
QCOMPARE(clone->defaultStyleSheet(), sheet); |
|
2198 |
cursor = QTextCursor(clone); |
|
2199 |
cursor.insertHtml("<p>test"); |
|
2200 |
fmt = clone->begin().blockFormat(); |
|
2201 |
QVERIFY(fmt.background().color() == QColor("green")); |
|
2202 |
delete clone; |
|
2203 |
||
2204 |
cursor = QTextCursor(doc); |
|
2205 |
cursor.insertHtml("<p>test"); |
|
2206 |
fmt = doc->begin().blockFormat(); |
|
2207 |
QVERIFY(fmt.background().color() == QColor("green")); |
|
2208 |
||
2209 |
doc->clear(); |
|
2210 |
cursor.insertHtml("<style>p { background-color: red; }</style><p>test"); |
|
2211 |
fmt = doc->begin().blockFormat(); |
|
2212 |
QVERIFY(fmt.background().color() == QColor("red")); |
|
2213 |
||
2214 |
doc->clear(); |
|
2215 |
doc->setDefaultStyleSheet("invalid style sheet...."); |
|
2216 |
cursor.insertHtml("<p>test"); |
|
2217 |
fmt = doc->begin().blockFormat(); |
|
2218 |
QVERIFY(fmt.background().color() != QColor("green")); |
|
2219 |
} |
|
2220 |
||
2221 |
void tst_QTextDocument::maximumBlockCount() |
|
2222 |
{ |
|
2223 |
QCOMPARE(doc->maximumBlockCount(), 0); |
|
2224 |
QVERIFY(doc->isUndoRedoEnabled()); |
|
2225 |
||
2226 |
cursor.insertBlock(); |
|
2227 |
cursor.insertText("Blah"); |
|
2228 |
cursor.insertBlock(); |
|
2229 |
cursor.insertText("Foo"); |
|
2230 |
QCOMPARE(doc->blockCount(), 3); |
|
2231 |
QCOMPARE(doc->toPlainText(), QString("\nBlah\nFoo")); |
|
2232 |
||
2233 |
doc->setMaximumBlockCount(1); |
|
2234 |
QVERIFY(!doc->isUndoRedoEnabled()); |
|
2235 |
||
2236 |
QCOMPARE(doc->blockCount(), 1); |
|
2237 |
QCOMPARE(doc->toPlainText(), QString("Foo")); |
|
2238 |
||
2239 |
cursor.insertBlock(); |
|
2240 |
cursor.insertText("Hello"); |
|
2241 |
doc->setMaximumBlockCount(1); |
|
2242 |
QCOMPARE(doc->blockCount(), 1); |
|
2243 |
QCOMPARE(doc->toPlainText(), QString("Hello")); |
|
2244 |
||
2245 |
doc->setMaximumBlockCount(100); |
|
2246 |
for (int i = 0; i < 1000; ++i) { |
|
2247 |
cursor.insertBlock(); |
|
2248 |
cursor.insertText("Blah)"); |
|
2249 |
QVERIFY(doc->blockCount() <= 100); |
|
2250 |
} |
|
2251 |
||
2252 |
cursor.movePosition(QTextCursor::End); |
|
2253 |
QCOMPARE(cursor.blockNumber(), 99); |
|
2254 |
QTextCharFormat fmt; |
|
2255 |
fmt.setFontItalic(true); |
|
2256 |
cursor.setBlockCharFormat(fmt); |
|
2257 |
cursor.movePosition(QTextCursor::Start); |
|
2258 |
QVERIFY(!cursor.blockCharFormat().fontItalic()); |
|
2259 |
||
2260 |
doc->setMaximumBlockCount(1); |
|
2261 |
QVERIFY(cursor.blockCharFormat().fontItalic()); |
|
2262 |
||
2263 |
cursor.insertTable(2, 2); |
|
2264 |
QCOMPARE(doc->blockCount(), 6); |
|
2265 |
cursor.insertBlock(); |
|
2266 |
QCOMPARE(doc->blockCount(), 1); |
|
2267 |
} |
|
2268 |
||
2269 |
void tst_QTextDocument::adjustSize() |
|
2270 |
{ |
|
2271 |
// avoid ugly tooltips like in task 125583 |
|
2272 |
QString text("Test Text"); |
|
2273 |
doc->setPlainText(text); |
|
2274 |
doc->rootFrame()->setFrameFormat(QTextFrameFormat()); |
|
2275 |
doc->adjustSize(); |
|
2276 |
QCOMPARE(doc->size().width(), doc->idealWidth()); |
|
2277 |
} |
|
2278 |
||
2279 |
void tst_QTextDocument::initialUserData() |
|
2280 |
{ |
|
2281 |
doc->setPlainText("Hello"); |
|
2282 |
QTextBlock block = doc->begin(); |
|
2283 |
block.setUserData(new QTextBlockUserData); |
|
2284 |
QVERIFY(block.userData()); |
|
2285 |
doc->documentLayout(); |
|
2286 |
QVERIFY(block.userData()); |
|
2287 |
doc->setDocumentLayout(new QTestDocumentLayout(doc)); |
|
2288 |
QVERIFY(!block.userData()); |
|
2289 |
} |
|
2290 |
||
2291 |
void tst_QTextDocument::html_defaultFont() |
|
2292 |
{ |
|
2293 |
QFont f; |
|
2294 |
f.setItalic(true); |
|
2295 |
f.setWeight(QFont::Bold); |
|
2296 |
doc->setDefaultFont(f); |
|
2297 |
doc->setPlainText("Test"); |
|
2298 |
||
2299 |
QString bodyPart = QString::fromLatin1("<body style=\" font-family:'%1'; font-size:%2pt; font-weight:%3; font-style:italic;\">") |
|
2300 |
.arg(f.family()).arg(f.pointSizeF()).arg(f.weight() * 8); |
|
2301 |
||
2302 |
QString html = doc->toHtml(); |
|
2303 |
if (!html.contains(bodyPart)) { |
|
2304 |
qDebug() << "html:" << html; |
|
2305 |
qDebug() << "expected body:" << bodyPart; |
|
2306 |
QVERIFY(html.contains(bodyPart)); |
|
2307 |
} |
|
2308 |
||
2309 |
if (html.contains("span")) |
|
2310 |
qDebug() << "html:" << html; |
|
2311 |
QVERIFY(!html.contains("<span style")); |
|
2312 |
} |
|
2313 |
||
2314 |
void tst_QTextDocument::blockCountChanged() |
|
2315 |
{ |
|
2316 |
QSignalSpy spy(doc, SIGNAL(blockCountChanged(int))); |
|
2317 |
||
2318 |
doc->setPlainText("Foo"); |
|
2319 |
||
2320 |
QCOMPARE(doc->blockCount(), 1); |
|
2321 |
QCOMPARE(spy.count(), 0); |
|
2322 |
||
2323 |
spy.clear(); |
|
2324 |
||
2325 |
doc->setPlainText("Foo\nBar"); |
|
2326 |
QCOMPARE(doc->blockCount(), 2); |
|
2327 |
QCOMPARE(spy.count(), 1); |
|
2328 |
QCOMPARE(spy.at(0).value(0).toInt(), 2); |
|
2329 |
||
2330 |
spy.clear(); |
|
2331 |
||
2332 |
cursor.movePosition(QTextCursor::End); |
|
2333 |
cursor.insertText("Blahblah"); |
|
2334 |
||
2335 |
QCOMPARE(spy.count(), 0); |
|
2336 |
||
2337 |
cursor.insertBlock(); |
|
2338 |
QCOMPARE(spy.count(), 1); |
|
2339 |
QCOMPARE(spy.at(0).value(0).toInt(), 3); |
|
2340 |
||
2341 |
spy.clear(); |
|
2342 |
doc->undo(); |
|
2343 |
||
2344 |
QCOMPARE(spy.count(), 1); |
|
2345 |
QCOMPARE(spy.at(0).value(0).toInt(), 2); |
|
2346 |
} |
|
2347 |
||
2348 |
void tst_QTextDocument::nonZeroDocumentLengthOnClear() |
|
2349 |
{ |
|
2350 |
QTestDocumentLayout *lout = new QTestDocumentLayout(doc); |
|
2351 |
doc->setDocumentLayout(lout); |
|
2352 |
||
2353 |
doc->clear(); |
|
2354 |
QVERIFY(lout->called); |
|
2355 |
QVERIFY(!lout->lastDocumentLengths.contains(0)); |
|
2356 |
} |
|
2357 |
||
2358 |
void tst_QTextDocument::setTextPreservesUndoRedoEnabled() |
|
2359 |
{ |
|
2360 |
QVERIFY(doc->isUndoRedoEnabled()); |
|
2361 |
||
2362 |
doc->setPlainText("Test"); |
|
2363 |
||
2364 |
QVERIFY(doc->isUndoRedoEnabled()); |
|
2365 |
||
2366 |
doc->setUndoRedoEnabled(false); |
|
2367 |
QVERIFY(!doc->isUndoRedoEnabled()); |
|
2368 |
doc->setPlainText("Test2"); |
|
2369 |
QVERIFY(!doc->isUndoRedoEnabled()); |
|
2370 |
||
2371 |
doc->setHtml("<p>hello"); |
|
2372 |
QVERIFY(!doc->isUndoRedoEnabled()); |
|
2373 |
} |
|
2374 |
||
2375 |
void tst_QTextDocument::firstLast() |
|
2376 |
{ |
|
2377 |
QCOMPARE(doc->blockCount(), 1); |
|
2378 |
QVERIFY(doc->firstBlock() == doc->lastBlock()); |
|
2379 |
||
2380 |
doc->setPlainText("Hello\nTest\nWorld"); |
|
2381 |
||
2382 |
QCOMPARE(doc->blockCount(), 3); |
|
2383 |
QVERIFY(doc->firstBlock() != doc->lastBlock()); |
|
2384 |
||
2385 |
QCOMPARE(doc->firstBlock().text(), QString("Hello")); |
|
2386 |
QCOMPARE(doc->lastBlock().text(), QString("World")); |
|
2387 |
||
2388 |
// manual forward loop |
|
2389 |
QTextBlock block = doc->firstBlock(); |
|
2390 |
||
2391 |
QVERIFY(block.isValid()); |
|
2392 |
QCOMPARE(block.text(), QString("Hello")); |
|
2393 |
||
2394 |
block = block.next(); |
|
2395 |
||
2396 |
QVERIFY(block.isValid()); |
|
2397 |
QCOMPARE(block.text(), QString("Test")); |
|
2398 |
||
2399 |
block = block.next(); |
|
2400 |
||
2401 |
QVERIFY(block.isValid()); |
|
2402 |
QCOMPARE(block.text(), QString("World")); |
|
2403 |
||
2404 |
block = block.next(); |
|
2405 |
QVERIFY(!block.isValid()); |
|
2406 |
||
2407 |
// manual backward loop |
|
2408 |
block = doc->lastBlock(); |
|
2409 |
||
2410 |
QVERIFY(block.isValid()); |
|
2411 |
QCOMPARE(block.text(), QString("World")); |
|
2412 |
||
2413 |
block = block.previous(); |
|
2414 |
||
2415 |
QVERIFY(block.isValid()); |
|
2416 |
QCOMPARE(block.text(), QString("Test")); |
|
2417 |
||
2418 |
block = block.previous(); |
|
2419 |
||
2420 |
QVERIFY(block.isValid()); |
|
2421 |
QCOMPARE(block.text(), QString("Hello")); |
|
2422 |
||
2423 |
block = block.previous(); |
|
2424 |
QVERIFY(!block.isValid()); |
|
2425 |
} |
|
2426 |
||
2427 |
const QString backgroundImage_html("<body><table><tr><td background=\"foo.png\">Blah</td></tr></table></body>"); |
|
2428 |
||
2429 |
void tst_QTextDocument::backgroundImage_checkExpectedHtml(const QTextDocument &doc) |
|
2430 |
{ |
|
2431 |
QString expectedHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" " |
|
2432 |
"\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" |
|
2433 |
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" |
|
2434 |
"p, li { white-space: pre-wrap; }\n" |
|
2435 |
"</style></head>" |
|
2436 |
"<body style=\" font-family:'%1'; font-size:%2pt; font-weight:%3; font-style:%4;\">\n" |
|
2437 |
"<table border=\"0\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;\" cellspacing=\"2\" cellpadding=\"0\">" |
|
2438 |
"\n<tr>\n<td background=\"foo.png\">" |
|
2439 |
"\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Blah</p>" |
|
2440 |
"</td></tr></table></body></html>"); |
|
2441 |
||
2442 |
expectedHtml = expectedHtml.arg(defaultFont.family()).arg(defaultFont.pointSizeF()).arg(defaultFont.weight() * 8).arg((defaultFont.italic() ? "italic" : "normal")); |
|
2443 |
||
2444 |
QCOMPARE(doc.toHtml(), expectedHtml); |
|
2445 |
} |
|
2446 |
||
2447 |
void tst_QTextDocument::backgroundImage_toHtml() |
|
2448 |
{ |
|
2449 |
CREATE_DOC_AND_CURSOR(); |
|
2450 |
||
2451 |
doc.setHtml(backgroundImage_html); |
|
2452 |
backgroundImage_checkExpectedHtml(doc); |
|
2453 |
} |
|
2454 |
||
2455 |
void tst_QTextDocument::backgroundImage_toHtml2() |
|
2456 |
{ |
|
2457 |
CREATE_DOC_AND_CURSOR(); |
|
2458 |
||
2459 |
cursor.insertHtml(backgroundImage_html); |
|
2460 |
backgroundImage_checkExpectedHtml(doc); |
|
2461 |
} |
|
2462 |
||
2463 |
void tst_QTextDocument::backgroundImage_clone() |
|
2464 |
{ |
|
2465 |
CREATE_DOC_AND_CURSOR(); |
|
2466 |
||
2467 |
doc.setHtml(backgroundImage_html); |
|
2468 |
QTextDocument *clone = doc.clone(); |
|
2469 |
backgroundImage_checkExpectedHtml(*clone); |
|
2470 |
delete clone; |
|
2471 |
} |
|
2472 |
||
2473 |
void tst_QTextDocument::backgroundImage_copy() |
|
2474 |
{ |
|
2475 |
CREATE_DOC_AND_CURSOR(); |
|
2476 |
||
2477 |
doc.setHtml(backgroundImage_html); |
|
2478 |
QTextDocumentFragment fragment(&doc); |
|
2479 |
||
2480 |
{ |
|
2481 |
CREATE_DOC_AND_CURSOR(); |
|
2482 |
||
2483 |
cursor.insertFragment(fragment); |
|
2484 |
backgroundImage_checkExpectedHtml(doc); |
|
2485 |
} |
|
2486 |
} |
|
2487 |
||
2488 |
void tst_QTextDocument::documentCleanup() |
|
2489 |
{ |
|
2490 |
QTextDocument doc; |
|
2491 |
QTextCursor cursor(&doc); |
|
2492 |
cursor.insertText("d\nfoo\nbar\n"); |
|
2493 |
doc.documentLayout(); // forces relayout |
|
2494 |
||
2495 |
// remove char 1 |
|
2496 |
cursor.setPosition(0); |
|
2497 |
QSizeF size = doc.documentLayout()->documentSize(); |
|
2498 |
cursor.deleteChar(); |
|
2499 |
// the size should be unchanged. |
|
2500 |
QCOMPARE(doc.documentLayout()->documentSize(), size); |
|
2501 |
} |
|
2502 |
||
2503 |
void tst_QTextDocument::characterAt() |
|
2504 |
{ |
|
2505 |
QTextDocument doc; |
|
2506 |
QTextCursor cursor(&doc); |
|
2507 |
QString text("12345\n67890"); |
|
2508 |
cursor.insertText(text); |
|
2509 |
int length = doc.characterCount(); |
|
2510 |
QCOMPARE(length, text.length() + 1); |
|
2511 |
QCOMPARE(doc.characterAt(length-1), QChar(QChar::ParagraphSeparator)); |
|
2512 |
QCOMPARE(doc.characterAt(-1), QChar()); |
|
2513 |
QCOMPARE(doc.characterAt(length), QChar()); |
|
2514 |
QCOMPARE(doc.characterAt(length + 1), QChar()); |
|
2515 |
for (int i = 0; i < text.length(); ++i) { |
|
2516 |
QChar c = text.at(i); |
|
2517 |
if (c == QLatin1Char('\n')) |
|
2518 |
c = QChar(QChar::ParagraphSeparator); |
|
2519 |
QCOMPARE(doc.characterAt(i), c); |
|
2520 |
} |
|
2521 |
} |
|
2522 |
||
2523 |
void tst_QTextDocument::revisions() |
|
2524 |
{ |
|
2525 |
QTextDocument doc; |
|
2526 |
QTextCursor cursor(&doc); |
|
2527 |
QString text("Hello World"); |
|
2528 |
QCOMPARE(doc.firstBlock().revision(), 0); |
|
2529 |
cursor.insertText(text); |
|
2530 |
QCOMPARE(doc.firstBlock().revision(), 1); |
|
2531 |
cursor.setPosition(6); |
|
2532 |
cursor.insertBlock(); |
|
2533 |
QCOMPARE(cursor.block().previous().revision(), 2); |
|
2534 |
QCOMPARE(cursor.block().revision(), 2); |
|
2535 |
cursor.insertText("candle"); |
|
2536 |
QCOMPARE(cursor.block().revision(), 3); |
|
2537 |
cursor.movePosition(QTextCursor::EndOfBlock); |
|
2538 |
cursor.insertBlock(); // we are at the block end |
|
2539 |
QCOMPARE(cursor.block().previous().revision(), 3); |
|
2540 |
QCOMPARE(cursor.block().revision(), 4); |
|
2541 |
cursor.insertText("lightbulb"); |
|
2542 |
QCOMPARE(cursor.block().revision(), 5); |
|
2543 |
cursor.movePosition(QTextCursor::StartOfBlock); |
|
2544 |
cursor.insertBlock(); // we are the block start |
|
2545 |
QCOMPARE(cursor.block().previous().revision(), 6); |
|
2546 |
QCOMPARE(cursor.block().revision(), 5); |
|
2547 |
} |
|
2548 |
||
2549 |
void tst_QTextDocument::revisionWithUndoCompressionAndUndo() |
|
2550 |
{ |
|
2551 |
QTextDocument doc; |
|
2552 |
QTextCursor cursor(&doc); |
|
2553 |
cursor.insertText("This is the beginning of it all."); |
|
2554 |
QCOMPARE(doc.firstBlock().revision(), 1); |
|
2555 |
QCOMPARE(doc.revision(), 1); |
|
2556 |
cursor.insertBlock(); |
|
2557 |
QCOMPARE(doc.revision(), 2); |
|
2558 |
cursor.insertText("this"); |
|
2559 |
QCOMPARE(doc.revision(), 3); |
|
2560 |
cursor.insertText("is"); |
|
2561 |
QCOMPARE(doc.revision(), 4); |
|
2562 |
cursor.insertText("compressed"); |
|
2563 |
QCOMPARE(doc.revision(), 5); |
|
2564 |
doc.undo(); |
|
2565 |
QCOMPARE(doc.revision(), 6); |
|
2566 |
QCOMPARE(doc.toPlainText(), QString("This is the beginning of it all.\n")) ; |
|
2567 |
cursor.setPosition(0); |
|
2568 |
QCOMPARE(doc.firstBlock().revision(), 1); |
|
2569 |
cursor.insertText("Very beginnig"); |
|
2570 |
QCOMPARE(doc.firstBlock().revision(), 7); |
|
2571 |
doc.undo(); |
|
2572 |
QCOMPARE(doc.revision(), 8); |
|
2573 |
QCOMPARE(doc.firstBlock().revision(), 1); |
|
2574 |
||
2575 |
cursor.beginEditBlock(); |
|
2576 |
cursor.insertText("Hello"); |
|
2577 |
cursor.insertBlock(); |
|
2578 |
cursor.insertText("world"); |
|
2579 |
cursor.endEditBlock(); |
|
2580 |
QCOMPARE(doc.revision(), 9); |
|
2581 |
doc.undo(); |
|
2582 |
QCOMPARE(doc.revision(), 10); |
|
2583 |
||
2584 |
||
2585 |
} |
|
2586 |
||
2587 |
void tst_QTextDocument::testUndoCommandAdded() |
|
2588 |
{ |
|
2589 |
QVERIFY(doc); |
|
2590 |
QSignalSpy spy(doc, SIGNAL(undoCommandAdded())); |
|
2591 |
QVERIFY(spy.isValid()); |
|
2592 |
QVERIFY(spy.isEmpty()); |
|
2593 |
||
2594 |
cursor.insertText("a"); |
|
2595 |
QCOMPARE(spy.count(), 1); |
|
2596 |
cursor.insertText("b"); // should be merged |
|
2597 |
QCOMPARE(spy.count(), 1); |
|
2598 |
cursor.insertText("c"); // should be merged |
|
2599 |
QCOMPARE(spy.count(), 1); |
|
2600 |
QCOMPARE(doc->toPlainText(), QString("abc")); |
|
2601 |
doc->undo(); |
|
2602 |
QCOMPARE(doc->toPlainText(), QString("")); |
|
2603 |
||
2604 |
doc->clear(); |
|
2605 |
spy.clear(); |
|
2606 |
cursor.insertText("aaa"); |
|
2607 |
QCOMPARE(spy.count(), 1); |
|
2608 |
||
2609 |
spy.clear(); |
|
2610 |
cursor.insertText("aaaa\nbcd"); |
|
2611 |
QCOMPARE(spy.count(), 1); |
|
2612 |
||
2613 |
spy.clear(); |
|
2614 |
cursor.beginEditBlock(); |
|
2615 |
cursor.insertText("aa"); |
|
2616 |
cursor.insertText("bbb\n"); |
|
2617 |
cursor.setCharFormat(QTextCharFormat()); |
|
2618 |
cursor.insertText("\nccc"); |
|
2619 |
QVERIFY(spy.isEmpty()); |
|
2620 |
cursor.endEditBlock(); |
|
2621 |
QCOMPARE(spy.count(), 1); |
|
2622 |
||
2623 |
spy.clear(); |
|
2624 |
cursor.insertBlock(); |
|
2625 |
QCOMPARE(spy.count(), 1); |
|
2626 |
||
2627 |
spy.clear(); |
|
2628 |
cursor.setPosition(5); |
|
2629 |
QVERIFY(spy.isEmpty()); |
|
2630 |
cursor.setCharFormat(QTextCharFormat()); |
|
2631 |
QVERIFY(spy.isEmpty()); |
|
2632 |
cursor.setPosition(10, QTextCursor::KeepAnchor); |
|
2633 |
QVERIFY(spy.isEmpty()); |
|
2634 |
QTextCharFormat cf; |
|
2635 |
cf.setFontItalic(true); |
|
2636 |
cursor.mergeCharFormat(cf); |
|
2637 |
QCOMPARE(spy.count(), 1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2638 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2639 |
spy.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2640 |
doc->undo(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2641 |
QCOMPARE(spy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2642 |
doc->undo(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2643 |
QCOMPARE(spy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2644 |
spy.clear(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2645 |
doc->redo(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2646 |
QCOMPARE(spy.count(), 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2647 |
doc->redo(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2648 |
QCOMPARE(spy.count(), 0); |
0 | 2649 |
} |
2650 |
||
2651 |
void tst_QTextDocument::testUndoBlocks() |
|
2652 |
{ |
|
2653 |
QVERIFY(doc); |
|
2654 |
cursor.insertText("Hello World"); |
|
2655 |
cursor.insertText("period"); |
|
2656 |
doc->undo(); |
|
2657 |
QCOMPARE(doc->toPlainText(), QString("")); |
|
2658 |
cursor.insertText("Hello World"); |
|
2659 |
cursor.insertText("One\nTwo\nThree"); |
|
2660 |
QCOMPARE(doc->toPlainText(), QString("Hello WorldOne\nTwo\nThree")); |
|
2661 |
doc->undo(); |
|
2662 |
QCOMPARE(doc->toPlainText(), QString("Hello World")); |
|
2663 |
cursor.insertText("One\nTwo\nThree"); |
|
2664 |
cursor.insertText("Trailing text"); |
|
2665 |
doc->undo(); |
|
2666 |
QCOMPARE(doc->toPlainText(), QString("Hello WorldOne\nTwo\nThree")); |
|
2667 |
doc->undo(); |
|
2668 |
QCOMPARE(doc->toPlainText(), QString("Hello World")); |
|
2669 |
doc->undo(); |
|
2670 |
QCOMPARE(doc->toPlainText(), QString("")); |
|
2671 |
||
2672 |
cursor.insertText("quod"); |
|
2673 |
cursor.beginEditBlock(); |
|
2674 |
cursor.insertText(" erat"); |
|
2675 |
cursor.endEditBlock(); |
|
2676 |
cursor.insertText(" demonstrandum"); |
|
2677 |
QCOMPARE(doc->toPlainText(), QString("quod erat demonstrandum")); |
|
2678 |
doc->undo(); |
|
2679 |
QCOMPARE(doc->toPlainText(), QString("quod erat")); |
|
2680 |
doc->undo(); |
|
2681 |
QCOMPARE(doc->toPlainText(), QString("quod")); |
|
2682 |
doc->undo(); |
|
2683 |
QCOMPARE(doc->toPlainText(), QString("")); |
|
2684 |
} |
|
2685 |
||
2686 |
class Receiver : public QObject |
|
2687 |
{ |
|
2688 |
Q_OBJECT |
|
2689 |
public: |
|
2690 |
QString first; |
|
2691 |
public slots: |
|
2692 |
void cursorPositionChanged() { |
|
2693 |
if (first.isEmpty()) |
|
2694 |
first = QLatin1String("cursorPositionChanged"); |
|
2695 |
} |
|
2696 |
||
2697 |
void contentsChange() { |
|
2698 |
if (first.isEmpty()) |
|
2699 |
first = QLatin1String("contentsChanged"); |
|
2700 |
} |
|
2701 |
}; |
|
2702 |
||
2703 |
void tst_QTextDocument::receiveCursorPositionChangedAfterContentsChange() |
|
2704 |
{ |
|
2705 |
QVERIFY(doc); |
|
2706 |
doc->setDocumentLayout(new MyAbstractTextDocumentLayout(doc)); |
|
2707 |
Receiver rec; |
|
2708 |
connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), |
|
2709 |
&rec, SLOT(cursorPositionChanged())); |
|
2710 |
connect(doc, SIGNAL(contentsChange(int,int,int)), |
|
2711 |
&rec, SLOT(contentsChange())); |
|
2712 |
cursor.insertText("Hello World"); |
|
2713 |
QCOMPARE(rec.first, QString("contentsChanged")); |
|
2714 |
} |
|
2715 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2716 |
void tst_QTextDocument::escape_data() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2717 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2718 |
QTest::addColumn<QString>("original"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2719 |
QTest::addColumn<QString>("expected"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2720 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2721 |
QTest::newRow("1") << "Hello World\n" << "Hello World\n"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2722 |
QTest::newRow("2") << "#include <QtCore>" << "#include <QtCore>"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2723 |
QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&bar=foo\">plop --> </a></p>" |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2724 |
<< "<p class="cool"><a href="http://example.com/?foo=bar&amp;bar=foo">plop --&gt; </a></p>"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2725 |
QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("<\320\222\321\201>"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2726 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2727 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2728 |
void tst_QTextDocument::escape() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2729 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2730 |
QFETCH(QString, original); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2731 |
QFETCH(QString, expected); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2732 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2733 |
QCOMPARE(Qt::escape(original), expected); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2734 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2735 |
|
0 | 2736 |
QTEST_MAIN(tst_QTextDocument) |
2737 |
#include "tst_qtextdocument.moc" |