~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/WebKit/qt/tests/qwebelement/tst_qwebelement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Library General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
 
 
21
#include <QtTest/QtTest>
 
22
 
 
23
#include <qwebpage.h>
 
24
#include <qwidget.h>
 
25
#include <qwebview.h>
 
26
#include <qwebframe.h>
 
27
#include <qwebelement.h>
 
28
//TESTED_CLASS=
 
29
//TESTED_FILES=
 
30
 
 
31
/**
 
32
 * Starts an event loop that runs until the given signal is received.
 
33
 Optionally the event loop
 
34
 * can return earlier on a timeout.
 
35
 *
 
36
 * \return \p true if the requested signal was received
 
37
 *         \p false on timeout
 
38
 */
 
39
static bool waitForSignal(QObject* obj, const char* signal, int timeout = 0)
 
40
{
 
41
    QEventLoop loop;
 
42
    QObject::connect(obj, signal, &loop, SLOT(quit()));
 
43
    QTimer timer;
 
44
    QSignalSpy timeoutSpy(&timer, SIGNAL(timeout()));
 
45
    if (timeout > 0) {
 
46
        QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
 
47
        timer.setSingleShot(true);
 
48
        timer.start(timeout);
 
49
    }
 
50
    loop.exec();
 
51
    return timeoutSpy.isEmpty();
 
52
}
 
53
 
 
54
class tst_QWebElement : public QObject
 
55
{
 
56
    Q_OBJECT
 
57
 
 
58
public:
 
59
    tst_QWebElement();
 
60
    virtual ~tst_QWebElement();
 
61
 
 
62
public slots:
 
63
    void init();
 
64
    void cleanup();
 
65
 
 
66
private slots:
 
67
    void textHtml();
 
68
    void simpleCollection();
 
69
    void attributes();
 
70
    void attributesNS();
 
71
    void classes();
 
72
    void namespaceURI();
 
73
    void foreachManipulation();
 
74
    void evaluateJavaScript();
 
75
    void documentElement();
 
76
    void frame();
 
77
    void style();
 
78
    void computedStyle();
 
79
    void appendAndPrepend();
 
80
    void insertBeforeAndAfter();
 
81
    void remove();
 
82
    void clear();
 
83
    void replaceWith();
 
84
    void encloseWith();
 
85
    void encloseContentsWith();
 
86
    void nullSelect();
 
87
    void firstChildNextSibling();
 
88
    void lastChildPreviousSibling();
 
89
    void hasSetFocus();
 
90
 
 
91
private:
 
92
    QWebView* m_view;
 
93
    QWebPage* m_page;
 
94
    QWebFrame* m_mainFrame;
 
95
};
 
96
 
 
97
tst_QWebElement::tst_QWebElement()
 
98
{
 
99
}
 
100
 
 
101
tst_QWebElement::~tst_QWebElement()
 
102
{
 
103
}
 
104
 
 
105
void tst_QWebElement::init()
 
106
{
 
107
    m_view = new QWebView();
 
108
    m_page = m_view->page();
 
109
    m_mainFrame = m_page->mainFrame();
 
110
}
 
111
 
 
112
void tst_QWebElement::cleanup()
 
113
{
 
114
    delete m_view;
 
115
}
 
116
 
 
117
void tst_QWebElement::textHtml()
 
118
{
 
119
    QString html = "<head></head><body><p>test</p></body>";
 
120
    m_mainFrame->setHtml(html);
 
121
    QWebElement body = m_mainFrame->documentElement();
 
122
    QVERIFY(!body.isNull());
 
123
 
 
124
    QCOMPARE(body.toPlainText(), QString("test"));
 
125
    QCOMPARE(body.toPlainText(), m_mainFrame->toPlainText());
 
126
 
 
127
    QCOMPARE(body.toInnerXml(), html);
 
128
}
 
129
 
 
130
void tst_QWebElement::simpleCollection()
 
131
{
 
132
    QString html = "<body><p>first para</p><p>second para</p></body>";
 
133
    m_mainFrame->setHtml(html);
 
134
    QWebElement body = m_mainFrame->documentElement();
 
135
 
 
136
    QList<QWebElement> list = body.findAll("p");
 
137
    QCOMPARE(list.count(), 2);
 
138
    QCOMPARE(list.at(0).toPlainText(), QString("first para"));
 
139
    QCOMPARE(list.at(1).toPlainText(), QString("second para"));
 
140
}
 
141
 
 
142
void tst_QWebElement::attributes()
 
143
{
 
144
    m_mainFrame->setHtml("<body><p>Test");
 
145
    QWebElement body = m_mainFrame->documentElement();
 
146
 
 
147
    QVERIFY(!body.hasAttribute("title"));
 
148
    QVERIFY(!body.hasAttributes());
 
149
 
 
150
    body.setAttribute("title", "test title");
 
151
 
 
152
    QVERIFY(body.hasAttributes());
 
153
    QVERIFY(body.hasAttribute("title"));
 
154
 
 
155
    QCOMPARE(body.attribute("title"), QString("test title"));
 
156
 
 
157
    body.removeAttribute("title");
 
158
 
 
159
    QVERIFY(!body.hasAttribute("title"));
 
160
    QVERIFY(!body.hasAttributes());
 
161
 
 
162
    QCOMPARE(body.attribute("does-not-exist", "testvalue"), QString("testvalue"));
 
163
}
 
164
 
 
165
void tst_QWebElement::attributesNS()
 
166
{
 
167
    QString content = "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
 
168
                      "xmlns:svg=\"http://www.w3.org/2000/svg\">"
 
169
                      "<body><svg:svg id=\"foobar\" width=\"400px\" height=\"300px\">"
 
170
                      "</svg:svg></body></html>";
 
171
 
 
172
    m_mainFrame->setContent(content.toUtf8(), "application/xhtml+xml");
 
173
 
 
174
    QWebElement svg = m_mainFrame->findFirstElement("svg");
 
175
    QVERIFY(!svg.isNull());
 
176
 
 
177
    QVERIFY(!svg.hasAttributeNS("http://www.w3.org/2000/svg", "foobar"));
 
178
    QCOMPARE(svg.attributeNS("http://www.w3.org/2000/svg", "foobar", "defaultblah"), QString("defaultblah"));
 
179
    svg.setAttributeNS("http://www.w3.org/2000/svg", "svg:foobar", "true");
 
180
    QVERIFY(svg.hasAttributeNS("http://www.w3.org/2000/svg", "foobar"));
 
181
    QCOMPARE(svg.attributeNS("http://www.w3.org/2000/svg", "foobar", "defaultblah"), QString("true"));
 
182
}
 
183
 
 
184
void tst_QWebElement::classes()
 
185
{
 
186
    m_mainFrame->setHtml("<body><p class=\"a b c d a c\">Test");
 
187
 
 
188
    QWebElement body = m_mainFrame->documentElement();
 
189
    QCOMPARE(body.classes().count(), 0);
 
190
 
 
191
    QWebElement p = m_mainFrame->documentElement().findAll("p").at(0);
 
192
    QStringList classes = p.classes();
 
193
    QCOMPARE(classes.count(), 4);
 
194
    QCOMPARE(classes[0], QLatin1String("a"));
 
195
    QCOMPARE(classes[1], QLatin1String("b"));
 
196
    QCOMPARE(classes[2], QLatin1String("c"));
 
197
    QCOMPARE(classes[3], QLatin1String("d"));
 
198
    QVERIFY(p.hasClass("a"));
 
199
    QVERIFY(p.hasClass("b"));
 
200
    QVERIFY(p.hasClass("c"));
 
201
    QVERIFY(p.hasClass("d"));
 
202
    QVERIFY(!p.hasClass("e"));
 
203
 
 
204
    p.addClass("f");
 
205
    QVERIFY(p.hasClass("f"));
 
206
    p.addClass("a");
 
207
    QCOMPARE(p.classes().count(), 5);
 
208
    QVERIFY(p.hasClass("a"));
 
209
    QVERIFY(p.hasClass("b"));
 
210
    QVERIFY(p.hasClass("c"));
 
211
    QVERIFY(p.hasClass("d"));
 
212
 
 
213
    p.toggleClass("a");
 
214
    QVERIFY(!p.hasClass("a"));
 
215
    QVERIFY(p.hasClass("b"));
 
216
    QVERIFY(p.hasClass("c"));
 
217
    QVERIFY(p.hasClass("d"));
 
218
    QVERIFY(p.hasClass("f"));
 
219
    QCOMPARE(p.classes().count(), 4);
 
220
    p.toggleClass("f");
 
221
    QVERIFY(!p.hasClass("f"));
 
222
    QCOMPARE(p.classes().count(), 3);
 
223
    p.toggleClass("a");
 
224
    p.toggleClass("f");
 
225
    QVERIFY(p.hasClass("a"));
 
226
    QVERIFY(p.hasClass("f"));
 
227
    QCOMPARE(p.classes().count(), 5);
 
228
 
 
229
    p.removeClass("f");
 
230
    QVERIFY(!p.hasClass("f"));
 
231
    QCOMPARE(p.classes().count(), 4);
 
232
    p.removeClass("d");
 
233
    QVERIFY(!p.hasClass("d"));
 
234
    QCOMPARE(p.classes().count(), 3);
 
235
    p.removeClass("not-exist");
 
236
    QCOMPARE(p.classes().count(), 3);
 
237
    p.removeClass("c");
 
238
    QVERIFY(!p.hasClass("c"));
 
239
    QCOMPARE(p.classes().count(), 2);
 
240
    p.removeClass("b");
 
241
    QVERIFY(!p.hasClass("b"));
 
242
    QCOMPARE(p.classes().count(), 1);
 
243
    p.removeClass("a");
 
244
    QVERIFY(!p.hasClass("a"));
 
245
    QCOMPARE(p.classes().count(), 0);
 
246
    p.removeClass("foobar");
 
247
    QCOMPARE(p.classes().count(), 0);
 
248
}
 
249
 
 
250
void tst_QWebElement::namespaceURI()
 
251
{
 
252
    QString content = "<html xmlns=\"http://www.w3.org/1999/xhtml\" "
 
253
                      "xmlns:svg=\"http://www.w3.org/2000/svg\">"
 
254
                      "<body><svg:svg id=\"foobar\" width=\"400px\" height=\"300px\">"
 
255
                      "</svg:svg></body></html>";
 
256
 
 
257
    m_mainFrame->setContent(content.toUtf8(), "application/xhtml+xml");
 
258
    QWebElement body = m_mainFrame->documentElement();
 
259
    QCOMPARE(body.namespaceUri(), QLatin1String("http://www.w3.org/1999/xhtml"));
 
260
 
 
261
    QWebElement svg = body.findAll("*#foobar").at(0);
 
262
    QCOMPARE(svg.prefix(), QLatin1String("svg"));
 
263
    QCOMPARE(svg.localName(), QLatin1String("svg"));
 
264
    QCOMPARE(svg.tagName(), QLatin1String("svg:svg"));
 
265
    QCOMPARE(svg.namespaceUri(), QLatin1String("http://www.w3.org/2000/svg"));
 
266
 
 
267
}
 
268
 
 
269
void tst_QWebElement::foreachManipulation()
 
270
{
 
271
    QString html = "<body><p>first para</p><p>second para</p></body>";
 
272
    m_mainFrame->setHtml(html);
 
273
    QWebElement body = m_mainFrame->documentElement();
 
274
 
 
275
    foreach(QWebElement p, body.findAll("p")) {
 
276
        p.setInnerXml("<div>foo</div><div>bar</div>");
 
277
    }
 
278
 
 
279
    QCOMPARE(body.findAll("div").count(), 4);
 
280
}
 
281
 
 
282
void tst_QWebElement::evaluateJavaScript()
 
283
{
 
284
    QVariant result;
 
285
    m_mainFrame->setHtml("<body><p>test");
 
286
    QWebElement para = m_mainFrame->findFirstElement("p");
 
287
 
 
288
    result = para.evaluateJavaScript("this.tagName");
 
289
    QVERIFY(result.isValid());
 
290
    QVERIFY(result.type() == QVariant::String);
 
291
    QCOMPARE(result.toString(), QLatin1String("P"));
 
292
 
 
293
    result = para.evaluateJavaScript("this.hasAttributes()");
 
294
    QVERIFY(result.isValid());
 
295
    QVERIFY(result.type() == QVariant::Bool);
 
296
    QVERIFY(!result.toBool());
 
297
 
 
298
    para.evaluateJavaScript("this.setAttribute('align', 'left');");
 
299
    QCOMPARE(para.attribute("align"), QLatin1String("left"));
 
300
 
 
301
    result = para.evaluateJavaScript("this.hasAttributes()");
 
302
    QVERIFY(result.isValid());
 
303
    QVERIFY(result.type() == QVariant::Bool);
 
304
    QVERIFY(result.toBool());
 
305
}
 
306
 
 
307
void tst_QWebElement::documentElement()
 
308
{
 
309
    m_mainFrame->setHtml("<body><p>Test");
 
310
 
 
311
    QWebElement para = m_mainFrame->documentElement().findAll("p").at(0);
 
312
    QVERIFY(para.parent().parent() == m_mainFrame->documentElement());
 
313
    QVERIFY(para.document() == m_mainFrame->documentElement());
 
314
}
 
315
 
 
316
void tst_QWebElement::frame()
 
317
{
 
318
    m_mainFrame->setHtml("<body><p>test");
 
319
 
 
320
    QWebElement doc = m_mainFrame->documentElement();
 
321
    QVERIFY(doc.webFrame() == m_mainFrame);
 
322
 
 
323
    m_view->setHtml(QString("data:text/html,<frameset cols=\"25%,75%\"><frame src=\"data:text/html,"
 
324
                            "<p>frame1\">"
 
325
                            "<frame src=\"data:text/html,<p>frame2\"></frameset>"), QUrl());
 
326
 
 
327
    waitForSignal(m_page, SIGNAL(loadFinished(bool)));
 
328
 
 
329
    QCOMPARE(m_mainFrame->childFrames().count(), 2);
 
330
 
 
331
    QWebFrame* firstFrame = m_mainFrame->childFrames().at(0);
 
332
    QWebFrame* secondFrame = m_mainFrame->childFrames().at(1);
 
333
 
 
334
    QCOMPARE(firstFrame->toPlainText(), QString("frame1"));
 
335
    QCOMPARE(secondFrame->toPlainText(), QString("frame2"));
 
336
 
 
337
    QWebElement firstPara = firstFrame->documentElement().findAll("p").at(0);
 
338
    QWebElement secondPara = secondFrame->documentElement().findAll("p").at(0);
 
339
 
 
340
    QVERIFY(firstPara.webFrame() == firstFrame);
 
341
    QVERIFY(secondPara.webFrame() == secondFrame);
 
342
}
 
343
 
 
344
void tst_QWebElement::style()
 
345
{
 
346
    QString html = "<head>"
 
347
        "<style type='text/css'>"
 
348
            "p { color: green !important }"
 
349
            "#idP { color: red }"
 
350
            ".classP { color : yellow ! important }"
 
351
        "</style>"
 
352
    "</head>"
 
353
    "<body>"
 
354
        "<p id='idP' class='classP' style='color: blue;'>some text</p>"
 
355
    "</body>";
 
356
 
 
357
    m_mainFrame->setHtml(html);
 
358
 
 
359
    QWebElement p = m_mainFrame->documentElement().findAll("p").at(0);
 
360
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("blue"));
 
361
    QVERIFY(p.styleProperty("cursor", QWebElement::InlineStyle).isEmpty());
 
362
 
 
363
    p.setStyleProperty("color", "red");
 
364
    p.setStyleProperty("cursor", "auto");
 
365
 
 
366
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("red"));
 
367
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("yellow"));
 
368
    QCOMPARE(p.styleProperty("cursor", QWebElement::InlineStyle), QLatin1String("auto"));
 
369
 
 
370
    p.setStyleProperty("color", "green !important");
 
371
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("green"));
 
372
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("green"));
 
373
 
 
374
    p.setStyleProperty("color", "blue");
 
375
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("green"));
 
376
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("green"));
 
377
 
 
378
    p.setStyleProperty("color", "blue !important");
 
379
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("blue"));
 
380
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("blue"));
 
381
 
 
382
    QString html2 = "<head>"
 
383
        "<style type='text/css'>"
 
384
            "p { color: green }"
 
385
            "#idP { color: red }"
 
386
            ".classP { color: yellow }"
 
387
        "</style>"
 
388
    "</head>"
 
389
    "<body>"
 
390
        "<p id='idP' class='classP' style='color: blue;'>some text</p>"
 
391
    "</body>";
 
392
 
 
393
    m_mainFrame->setHtml(html2);
 
394
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
395
 
 
396
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("blue"));
 
397
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("blue"));
 
398
 
 
399
    QString html3 = "<head>"
 
400
        "<style type='text/css'>"
 
401
            "p { color: green !important }"
 
402
            "#idP { color: red !important}"
 
403
            ".classP { color: yellow !important}"
 
404
        "</style>"
 
405
    "</head>"
 
406
    "<body>"
 
407
        "<p id='idP' class='classP' style='color: blue !important;'>some text</p>"
 
408
    "</body>";
 
409
 
 
410
    m_mainFrame->setHtml(html3);
 
411
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
412
 
 
413
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("blue"));
 
414
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("blue"));
 
415
 
 
416
    QString html5 = "<head>"
 
417
        "<style type='text/css'>"
 
418
            "p { color: green }"
 
419
            "#idP { color: red }"
 
420
            ".classP { color: yellow }"
 
421
        "</style>"
 
422
    "</head>"
 
423
    "<body>"
 
424
        "<p id='idP' class='classP'>some text</p>"
 
425
    "</body>";
 
426
 
 
427
    m_mainFrame->setHtml(html5);
 
428
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
429
 
 
430
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String(""));
 
431
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("red"));
 
432
 
 
433
    QString html6 = "<head>"
 
434
        "<link rel='stylesheet' href='qrc:/style.css' type='text/css' />"
 
435
        "<style type='text/css'>"
 
436
            "p { color: green }"
 
437
            "#idP { color: red }"
 
438
            ".classP { color: yellow ! important}"
 
439
        "</style>"
 
440
    "</head>"
 
441
    "<body>"
 
442
        "<p id='idP' class='classP' style='color: blue;'>some text</p>"
 
443
    "</body>";
 
444
 
 
445
    // in few seconds, the CSS should be completey loaded
 
446
    QSignalSpy spy(m_page, SIGNAL(loadFinished(bool)));
 
447
    m_mainFrame->setHtml(html6);
 
448
    QTest::qWait(200);
 
449
 
 
450
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
451
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("blue"));
 
452
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("black"));
 
453
 
 
454
    QString html7 = "<head>"
 
455
        "<style type='text/css'>"
 
456
            "@import url(qrc:/style2.css);"
 
457
        "</style>"
 
458
        "<link rel='stylesheet' href='qrc:/style.css' type='text/css' />"
 
459
    "</head>"
 
460
    "<body>"
 
461
        "<p id='idP' style='color: blue;'>some text</p>"
 
462
    "</body>";
 
463
 
 
464
    // in few seconds, the style should be completey loaded
 
465
    m_mainFrame->setHtml(html7);
 
466
    QTest::qWait(200);
 
467
 
 
468
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
469
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("black"));
 
470
 
 
471
    QString html8 = "<body><p>some text</p></body>";
 
472
 
 
473
    m_mainFrame->setHtml(html8);
 
474
    p = m_mainFrame->documentElement().findAll("p").at(0);
 
475
 
 
476
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String(""));
 
477
    QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String(""));
 
478
}
 
479
 
 
480
void tst_QWebElement::computedStyle()
 
481
{
 
482
    QString html = "<body><p>some text</p></body>";
 
483
    m_mainFrame->setHtml(html);
 
484
 
 
485
    QWebElement p = m_mainFrame->documentElement().findAll("p").at(0);
 
486
    QCOMPARE(p.styleProperty("cursor", QWebElement::ComputedStyle), QLatin1String("auto"));
 
487
    QVERIFY(!p.styleProperty("cursor", QWebElement::ComputedStyle).isEmpty());
 
488
    QVERIFY(p.styleProperty("cursor", QWebElement::InlineStyle).isEmpty());
 
489
 
 
490
    p.setStyleProperty("cursor", "text");
 
491
    p.setStyleProperty("color", "red");
 
492
 
 
493
    QCOMPARE(p.styleProperty("cursor", QWebElement::ComputedStyle), QLatin1String("text"));
 
494
    QCOMPARE(p.styleProperty("color", QWebElement::ComputedStyle), QLatin1String("rgb(255, 0, 0)"));
 
495
    QCOMPARE(p.styleProperty("color", QWebElement::InlineStyle), QLatin1String("red"));
 
496
}
 
497
 
 
498
void tst_QWebElement::appendAndPrepend()
 
499
{
 
500
    QString html = "<body>"
 
501
        "<p>"
 
502
            "foo"
 
503
        "</p>"
 
504
        "<p>"
 
505
            "bar"
 
506
        "</p>"
 
507
    "</body>";
 
508
 
 
509
    m_mainFrame->setHtml(html);
 
510
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
511
 
 
512
    QCOMPARE(body.findAll("p").count(), 2);
 
513
    body.appendInside(body.findFirst("p"));
 
514
    QCOMPARE(body.findAll("p").count(), 2);
 
515
    QCOMPARE(body.findFirst("p").toPlainText(), QString("bar"));
 
516
    QCOMPARE(body.findAll("p").last().toPlainText(), QString("foo"));
 
517
 
 
518
    body.appendInside(body.findFirst("p").clone());
 
519
    QCOMPARE(body.findAll("p").count(), 3);
 
520
    QCOMPARE(body.findFirst("p").toPlainText(), QString("bar"));
 
521
    QCOMPARE(body.findAll("p").last().toPlainText(), QString("bar"));
 
522
 
 
523
    body.prependInside(body.findAll("p").at(1).clone());
 
524
    QCOMPARE(body.findAll("p").count(), 4);
 
525
    QCOMPARE(body.findFirst("p").toPlainText(), QString("foo"));
 
526
 
 
527
    body.findFirst("p").appendInside("<div>booyakasha</div>");
 
528
    QCOMPARE(body.findAll("p div").count(), 1);
 
529
    QCOMPARE(body.findFirst("p div").toPlainText(), QString("booyakasha"));
 
530
 
 
531
    body.findFirst("div").prependInside("<code>yepp</code>");
 
532
    QCOMPARE(body.findAll("p div code").count(), 1);
 
533
    QCOMPARE(body.findFirst("p div code").toPlainText(), QString("yepp"));
 
534
}
 
535
 
 
536
void tst_QWebElement::insertBeforeAndAfter()
 
537
{
 
538
    QString html = "<body>"
 
539
        "<p>"
 
540
            "foo"
 
541
        "</p>"
 
542
        "<div>"
 
543
            "yeah"
 
544
        "</div>"
 
545
        "<p>"
 
546
            "bar"
 
547
        "</p>"
 
548
    "</body>";
 
549
 
 
550
    m_mainFrame->setHtml(html);
 
551
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
552
    QWebElement div = body.findFirst("div");
 
553
 
 
554
    QCOMPARE(body.findAll("p").count(), 2);
 
555
    QCOMPARE(body.findAll("div").count(), 1);
 
556
 
 
557
    div.prependOutside(body.findAll("p").last().clone());
 
558
    QCOMPARE(body.findAll("p").count(), 3);
 
559
    QCOMPARE(body.findAll("p").at(0).toPlainText(), QString("foo"));
 
560
    QCOMPARE(body.findAll("p").at(1).toPlainText(), QString("bar"));
 
561
    QCOMPARE(body.findAll("p").at(2).toPlainText(), QString("bar"));
 
562
 
 
563
    div.appendOutside(body.findFirst("p").clone());
 
564
    QCOMPARE(body.findAll("p").count(), 4);
 
565
    QCOMPARE(body.findAll("p").at(0).toPlainText(), QString("foo"));
 
566
    QCOMPARE(body.findAll("p").at(1).toPlainText(), QString("bar"));
 
567
    QCOMPARE(body.findAll("p").at(2).toPlainText(), QString("foo"));
 
568
    QCOMPARE(body.findAll("p").at(3).toPlainText(), QString("bar"));
 
569
 
 
570
    div.prependOutside("<span>hey</span>");
 
571
    QCOMPARE(body.findAll("span").count(), 1);
 
572
 
 
573
    div.appendOutside("<span>there</span>");
 
574
    QCOMPARE(body.findAll("span").count(), 2);
 
575
    QCOMPARE(body.findAll("span").at(0).toPlainText(), QString("hey"));
 
576
    QCOMPARE(body.findAll("span").at(1).toPlainText(), QString("there"));
 
577
}
 
578
 
 
579
void tst_QWebElement::remove()
 
580
{
 
581
    QString html = "<body>"
 
582
        "<p>"
 
583
            "foo"
 
584
        "</p>"
 
585
        "<div>"
 
586
            "<p>yeah</p>"
 
587
        "</div>"
 
588
        "<p>"
 
589
            "bar"
 
590
        "</p>"
 
591
    "</body>";
 
592
 
 
593
    m_mainFrame->setHtml(html);
 
594
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
595
 
 
596
    QCOMPARE(body.findAll("div").count(), 1);
 
597
    QCOMPARE(body.findAll("p").count(), 3);
 
598
 
 
599
    QWebElement div = body.findFirst("div");
 
600
    div.takeFromDocument();
 
601
 
 
602
    QCOMPARE(div.isNull(), false);
 
603
    QCOMPARE(body.findAll("div").count(), 0);
 
604
    QCOMPARE(body.findAll("p").count(), 2);
 
605
 
 
606
    body.appendInside(div);
 
607
 
 
608
    QCOMPARE(body.findAll("div").count(), 1);
 
609
    QCOMPARE(body.findAll("p").count(), 3);
 
610
}
 
611
 
 
612
void tst_QWebElement::clear()
 
613
{
 
614
    QString html = "<body>"
 
615
        "<p>"
 
616
            "foo"
 
617
        "</p>"
 
618
        "<div>"
 
619
            "<p>yeah</p>"
 
620
        "</div>"
 
621
        "<p>"
 
622
            "bar"
 
623
        "</p>"
 
624
    "</body>";
 
625
 
 
626
    m_mainFrame->setHtml(html);
 
627
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
628
 
 
629
    QCOMPARE(body.findAll("div").count(), 1);
 
630
    QCOMPARE(body.findAll("p").count(), 3);
 
631
    body.findFirst("div").removeChildren();
 
632
    QCOMPARE(body.findAll("div").count(), 1);
 
633
    QCOMPARE(body.findAll("p").count(), 2);
 
634
}
 
635
 
 
636
 
 
637
void tst_QWebElement::replaceWith()
 
638
{
 
639
    QString html = "<body>"
 
640
        "<p>"
 
641
            "foo"
 
642
        "</p>"
 
643
        "<div>"
 
644
            "yeah"
 
645
        "</div>"
 
646
        "<p>"
 
647
            "<span>haba</span>"
 
648
        "</p>"
 
649
    "</body>";
 
650
 
 
651
    m_mainFrame->setHtml(html);
 
652
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
653
 
 
654
    QCOMPARE(body.findAll("div").count(), 1);
 
655
    QCOMPARE(body.findAll("span").count(), 1);
 
656
    body.findFirst("div").replace(body.findFirst("span").clone());
 
657
    QCOMPARE(body.findAll("div").count(), 0);
 
658
    QCOMPARE(body.findAll("span").count(), 2);
 
659
    QCOMPARE(body.findAll("p").count(), 2);
 
660
 
 
661
    body.findFirst("span").replace("<p><code>wow</code></p>");
 
662
    QCOMPARE(body.findAll("p").count(), 3);
 
663
    QCOMPARE(body.findAll("p code").count(), 1);
 
664
    QCOMPARE(body.findFirst("p code").toPlainText(), QString("wow"));
 
665
}
 
666
 
 
667
void tst_QWebElement::encloseContentsWith()
 
668
{
 
669
    QString html = "<body>"
 
670
        "<div>"
 
671
            "<i>"
 
672
                "yeah"
 
673
            "</i>"
 
674
            "<i>"
 
675
                "hello"
 
676
            "</i>"
 
677
        "</div>"
 
678
        "<p>"
 
679
            "<span>foo</span>"
 
680
            "<span>bar</span>"
 
681
        "</p>"
 
682
        "<u></u>"
 
683
        "<b></b>"
 
684
        "<em>hey</em>"
 
685
    "</body>";
 
686
 
 
687
    m_mainFrame->setHtml(html);
 
688
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
689
 
 
690
    body.findFirst("p").encloseContentsWith(body.findFirst("b"));
 
691
    QCOMPARE(body.findAll("p b span").count(), 2);
 
692
    QCOMPARE(body.findFirst("p b span").toPlainText(), QString("foo"));
 
693
 
 
694
    body.findFirst("u").encloseContentsWith("<i></i>");
 
695
    QCOMPARE(body.findAll("u i").count(), 1);
 
696
    QCOMPARE(body.findFirst("u i").toPlainText(), QString());
 
697
 
 
698
    body.findFirst("div").encloseContentsWith("<span></span>");
 
699
    QCOMPARE(body.findAll("div span i").count(), 2);
 
700
    QCOMPARE(body.findFirst("div span i").toPlainText(), QString("yeah"));
 
701
 
 
702
    QString snippet = ""
 
703
        "<table>"
 
704
            "<tbody>"
 
705
                "<tr>"
 
706
                    "<td></td>"
 
707
                    "<td></td>"
 
708
                "</tr>"
 
709
                "<tr>"
 
710
                    "<td></td>"
 
711
                    "<td></td>"
 
712
                "<tr>"
 
713
            "</tbody>"
 
714
        "</table>";
 
715
 
 
716
    body.findFirst("em").encloseContentsWith(snippet);
 
717
    QCOMPARE(body.findFirst("em table tbody tr td").toPlainText(), QString("hey"));
 
718
}
 
719
 
 
720
void tst_QWebElement::encloseWith()
 
721
{
 
722
    QString html = "<body>"
 
723
        "<p>"
 
724
            "foo"
 
725
        "</p>"
 
726
        "<div>"
 
727
            "yeah"
 
728
        "</div>"
 
729
        "<p>"
 
730
            "<span>bar</span>"
 
731
        "</p>"
 
732
        "<em>hey</em>"
 
733
        "<h1>hello</h1>"
 
734
    "</body>";
 
735
 
 
736
    m_mainFrame->setHtml(html);
 
737
    QWebElement body = m_mainFrame->documentElement().findFirst("body");
 
738
 
 
739
    body.findFirst("p").encloseWith("<br>");
 
740
    QCOMPARE(body.findAll("br").count(), 0);
 
741
 
 
742
    QCOMPARE(body.findAll("div").count(), 1);
 
743
    body.findFirst("div").encloseWith(body.findFirst("span").clone());
 
744
    QCOMPARE(body.findAll("div").count(), 1);
 
745
    QCOMPARE(body.findAll("span").count(), 2);
 
746
    QCOMPARE(body.findAll("p").count(), 2);
 
747
 
 
748
    body.findFirst("div").encloseWith("<code></code>");
 
749
    QCOMPARE(body.findAll("code").count(), 1);
 
750
    QCOMPARE(body.findAll("code div").count(), 1);
 
751
    QCOMPARE(body.findFirst("code div").toPlainText(), QString("yeah"));
 
752
 
 
753
    QString snippet = ""
 
754
        "<table>"
 
755
            "<tbody>"
 
756
                "<tr>"
 
757
                    "<td></td>"
 
758
                    "<td></td>"
 
759
                "</tr>"
 
760
                "<tr>"
 
761
                    "<td></td>"
 
762
                    "<td></td>"
 
763
                "<tr>"
 
764
            "</tbody>"
 
765
        "</table>";
 
766
 
 
767
    body.findFirst("em").encloseWith(snippet);
 
768
    QCOMPARE(body.findFirst("table tbody tr td em").toPlainText(), QString("hey"));
 
769
}
 
770
 
 
771
void tst_QWebElement::nullSelect()
 
772
{
 
773
    m_mainFrame->setHtml("<body><p>Test");
 
774
 
 
775
    QList<QWebElement> collection = m_mainFrame->findAllElements("invalid{syn(tax;;%#$f223e>>");
 
776
    QVERIFY(collection.count() == 0);
 
777
}
 
778
 
 
779
void tst_QWebElement::firstChildNextSibling()
 
780
{
 
781
    m_mainFrame->setHtml("<body><!--comment--><p>Test</p><!--another commend><table>");
 
782
 
 
783
    QWebElement body = m_mainFrame->findFirstElement("body");
 
784
    QVERIFY(!body.isNull());
 
785
    QWebElement p = body.firstChild();
 
786
    QVERIFY(!p.isNull());
 
787
    QCOMPARE(p.tagName(), QString("P"));
 
788
    QWebElement table = p.nextSibling();
 
789
    QVERIFY(!table.isNull());
 
790
    QCOMPARE(table.tagName(), QString("TABLE"));
 
791
    QVERIFY(table.nextSibling().isNull());
 
792
}
 
793
 
 
794
void tst_QWebElement::lastChildPreviousSibling()
 
795
{
 
796
    m_mainFrame->setHtml("<body><!--comment--><p>Test</p><!--another commend><table>");
 
797
 
 
798
    QWebElement body = m_mainFrame->findFirstElement("body");
 
799
    QVERIFY(!body.isNull());
 
800
    QWebElement table = body.lastChild();
 
801
    QVERIFY(!table.isNull());
 
802
    QCOMPARE(table.tagName(), QString("TABLE"));
 
803
    QWebElement p = table.previousSibling();
 
804
    QVERIFY(!p.isNull());
 
805
    QCOMPARE(p.tagName(), QString("P"));
 
806
    QVERIFY(p.previousSibling().isNull());
 
807
}
 
808
 
 
809
void tst_QWebElement::hasSetFocus()
 
810
{
 
811
    m_mainFrame->setHtml("<html><body>" \
 
812
                            "<input type='text' id='input1'/>" \
 
813
                            "<br>"\
 
814
                            "<input type='text' id='input2'/>" \
 
815
                            "</body></html>");
 
816
 
 
817
    QList<QWebElement> inputs = m_mainFrame->documentElement().findAll("input");
 
818
    QWebElement input1 = inputs.at(0);
 
819
    input1.setFocus();
 
820
    QVERIFY(input1.hasFocus());
 
821
 
 
822
    QWebElement input2 = inputs.at(1);
 
823
    input2.setFocus();
 
824
    QVERIFY(!input1.hasFocus());
 
825
    QVERIFY(input2.hasFocus());
 
826
}
 
827
 
 
828
QTEST_MAIN(tst_QWebElement)
 
829
#include "tst_qwebelement.moc"