2011-03-10 Alexis Menard <alexis.menard@openbossa.org>
[WebKit-https.git] / Source / WebKit / qt / tests / qdeclarativewebview / tst_qdeclarativewebview.cpp
1 #include "../util.h"
2 #include <QColor>
3 #include <QDebug>
4 #include <QDeclarativeComponent>
5 #include <QDeclarativeEngine>
6 #include <QDeclarativeProperty>
7 #include <QDeclarativeView>
8 #include <QDir>
9 #include <QGraphicsWebView>
10 #include <QTest>
11 #include <QWebFrame>
12
13 QT_BEGIN_NAMESPACE
14
15 class tst_QDeclarativeWebView : public QObject {
16     Q_OBJECT
17
18 public:
19     tst_QDeclarativeWebView();
20
21 private slots:
22     void preferredWidthTest();
23     void preferredHeightTest();
24     void preferredWidthDefaultTest();
25     void preferredHeightDefaultTest();
26 #if QT_VERSION >= 0x040702
27     void backgroundColor();
28 #endif
29
30 private:
31     void checkNoErrors(const QDeclarativeComponent&);
32 };
33
34 tst_QDeclarativeWebView::tst_QDeclarativeWebView()
35 {
36     Q_UNUSED(waitForSignal)
37 }
38
39 void tst_QDeclarativeWebView::preferredWidthTest()
40 {
41     QDeclarativeEngine engine;
42     QDeclarativeComponent component(&engine, QUrl("qrc:///resources/webviewtest.qml"));
43     checkNoErrors(component);
44     QObject* wv = component.create();
45     QVERIFY(wv);
46     wv->setProperty("testUrl", QUrl("qrc:///resources/sample.html"));
47     QCOMPARE(wv->property("prefWidth").toInt(), 600);
48 }
49
50 void tst_QDeclarativeWebView::preferredHeightTest()
51 {
52     QDeclarativeEngine engine;
53     QDeclarativeComponent component(&engine, QUrl("qrc:///resources/webviewtest.qml"));
54     checkNoErrors(component);
55     QObject* wv = component.create();
56     QVERIFY(wv);
57     wv->setProperty("testUrl", QUrl("qrc:///resources/sample.html"));
58     QCOMPARE(wv->property("prefHeight").toInt(), 500);
59 }
60
61 void tst_QDeclarativeWebView::preferredWidthDefaultTest()
62 {
63     QGraphicsWebView view;
64     view.load(QUrl("qrc:///resources/sample.html"));
65
66     QDeclarativeEngine engine;
67     QDeclarativeComponent component(&engine, QUrl("qrc:///resources/webviewtestdefault.qml"));
68     checkNoErrors(component);
69     QObject* wv = component.create();
70     QVERIFY(wv);
71     wv->setProperty("testUrl", QUrl("qrc:///resources/sample.html"));
72     QCOMPARE(wv->property("prefWidth").toDouble(), view.preferredWidth());
73 }
74
75 void tst_QDeclarativeWebView::preferredHeightDefaultTest()
76 {
77     QGraphicsWebView view;
78     view.load(QUrl("qrc:///resources/sample.html"));
79
80     QDeclarativeEngine engine;
81     QDeclarativeComponent component(&engine, QUrl("qrc:///resources/webviewtestdefault.qml"));
82     checkNoErrors(component);
83     QObject* wv = component.create();
84     QVERIFY(wv);
85     wv->setProperty("testUrl", QUrl("qrc:///resources/sample.html"));
86     QCOMPARE(wv->property("prefHeight").toDouble(), view.preferredHeight());
87 }
88
89 #if QT_VERSION >= 0x040702
90 void tst_QDeclarativeWebView::backgroundColor()
91 {
92     // We test here the rendering of the background.
93     QDeclarativeEngine engine;
94     QDeclarativeComponent component(&engine, QUrl("qrc:///resources/webviewbackgroundcolor.qml"));
95     checkNoErrors(component);
96     QObject* wv = component.create();
97     QVERIFY(wv);
98     QCOMPARE(wv->property("backgroundColor").value<QColor>(), QColor(Qt::red));
99     QDeclarativeView view;
100     view.setSource(QUrl("qrc:///resources/webviewbackgroundcolor.qml"));
101     view.show();
102     QTest::qWaitForWindowShown(&view);
103     QPixmap result(view.width(), view.height());
104     QPainter painter(&result);
105     view.render(&painter);
106     QPixmap reference(view.width(), view.height());
107     reference.fill(Qt::red);
108     QCOMPARE(reference, result);
109
110     // We test the emission of the backgroundColorChanged signal.
111     QSignalSpy spyColorChanged(wv, SIGNAL(backgroundColorChanged()));
112     wv->setProperty("backgroundColor", Qt::red);
113     QCOMPARE(spyColorChanged.count(), 0);
114     wv->setProperty("backgroundColor", Qt::green);
115     QCOMPARE(spyColorChanged.count(), 1);
116 }
117 #endif
118
119 void tst_QDeclarativeWebView::checkNoErrors(const QDeclarativeComponent& component)
120 {
121     // Wait until the component is ready
122     QTRY_VERIFY(component.isReady() || component.isError());
123     QVERIFY(!component.isError());
124 }
125
126 QTEST_MAIN(tst_QDeclarativeWebView)
127 #include "tst_qdeclarativewebview.moc"
128
129 QT_END_NAMESPACE