1 /*
2  * Copyright (C) 2009 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 Lesser 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  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19 
20 #include "widget.h"
21 
22 #include "qwebelement.h"
23 #include "qwebframe.h"
24 #include "ui_widget.h"
25 #include <QPainter>
26 #include <QtTest/QtTest>
27 
Widget(QWidget * parent)28 Widget::Widget(QWidget* parent) :
29     QWidget(parent),
30     ui(new Ui::Widget)
31 {
32     ui->setupUi(this);
33 }
34 
refreshJS()35 void Widget::refreshJS()
36 {
37     ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("myWidget", this);
38 }
start()39 void Widget::start()
40 {
41     ui->webView->load(QUrl("qrc:///test.html"));
42     connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(refreshJS()));
43     ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("myWidget", this);
44 }
45 
completeTest()46 void Widget::completeTest()
47 {
48     QCOMPARE(ui->lbl1->pixmap()->size(), ui->lbl2->size());
49     QCOMPARE(ui->lbl3->size(), ui->lbl4->pixmap()->size());
50     QCOMPARE(ui->lbl2->size().width(), ui->webView->page()->mainFrame()->findFirstElement("#img1").evaluateJavaScript("this.width").toInt());
51     QCOMPARE(ui->lbl3->size().width(), ui->webView->page()->mainFrame()->findFirstElement("#img2").evaluateJavaScript("this.width").toInt());
52     emit testComplete();
53 }
54 
setPixmap(const QPixmap & p)55 void Widget::setPixmap(const QPixmap& p)
56 {
57     ui->lbl1->setPixmap(p);
58 }
pixmap() const59 QPixmap Widget::pixmap() const
60 {
61     QPixmap px(ui->lbl3->size());
62     {
63         QPainter p(&px);
64         ui->lbl3->render(&p);
65     }
66     return px;
67 }
setImage(const QImage & img)68 void Widget::setImage(const QImage& img)
69 {
70     ui->lbl4->setPixmap(QPixmap::fromImage(img));
71 }
72 
image() const73 QImage Widget::image() const
74 {
75     QImage img(ui->lbl2->size(), QImage::Format_ARGB32);
76     {
77         QPainter p(&img);
78         ui->lbl2->render(&p);
79     }
80     return img;
81 }
82 
~Widget()83 Widget::~Widget()
84 {
85     delete ui;
86 }
87 
changeEvent(QEvent * e)88 void Widget::changeEvent(QEvent* e)
89 {
90     QWidget::changeEvent(e);
91     switch (e->type()) {
92     case QEvent::LanguageChange:
93         ui->retranslateUi(this);
94         break;
95     default:
96         break;
97     }
98 }
compare(const QVariant & a,const QVariant & b)99 void Widget::compare(const QVariant& a, const QVariant& b)
100 {
101     QCOMPARE(a, b);
102 }
103 
imageSlot(const QImage & img)104 void Widget::imageSlot(const QImage& img)
105 {
106     QCOMPARE(img.size(), ui->lbl3->size());
107     emit pixmapSignal(QPixmap::fromImage(img));
108 }
109 
pixmapSlot(const QPixmap & pxm)110 void Widget::pixmapSlot(const QPixmap& pxm)
111 {
112     QCOMPARE(pxm.size(), ui->lbl2->size());
113     emit imageSignal(ui->lbl4->pixmap()->toImage());
114 }
115 
randomSlot(const QPixmap & pxm)116 void Widget::randomSlot(const QPixmap& pxm)
117 {
118     QVERIFY(pxm.isNull());
119 }
120