1 /***************************************************************************
2 testqgshtmlwidgetwrapper.cpp
3 --------------------------------------
4 Date : September 2020
5 Copyright : (C) 2020 Julien Cabieces
6 Email : julien dot cabieces at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16 #ifdef WITH_QTWEBKIT
17 #include <QWebFrame>
18 #endif
19
20 #include "qgstest.h"
21
22 #include "editorwidgets/core/qgseditorwidgetregistry.h"
23 #include "qgsapplication.h"
24 #include "qgshtmlwidgetwrapper.h"
25 #include "qgsgui.h"
26
27 class TestQgsHtmlWidgetWrapper : public QObject
28 {
29 Q_OBJECT
30 public:
31 TestQgsHtmlWidgetWrapper() = default;
32
33 private slots:
34 void initTestCase(); // will be called before the first testfunction is executed.
35 void cleanupTestCase(); // will be called after the last testfunction was executed.
36 void init(); // will be called before each testfunction is executed.
37 void cleanup(); // will be called after every testfunction.
38 void testExpressionEvaluate_data();
39 void testExpressionEvaluate();
40 };
41
initTestCase()42 void TestQgsHtmlWidgetWrapper::initTestCase()
43 {
44 QgsApplication::init();
45 QgsApplication::initQgis();
46 QgsGui::editorWidgetRegistry()->initEditors();
47 }
48
cleanupTestCase()49 void TestQgsHtmlWidgetWrapper::cleanupTestCase()
50 {
51 QgsApplication::exitQgis();
52 }
53
init()54 void TestQgsHtmlWidgetWrapper::init()
55 {
56 }
57
cleanup()58 void TestQgsHtmlWidgetWrapper::cleanup()
59 {
60 }
61
testExpressionEvaluate_data()62 void TestQgsHtmlWidgetWrapper::testExpressionEvaluate_data()
63 {
64 QTest::addColumn<QString>( "expression" );
65 QTest::addColumn<bool>( "needsGeometry" );
66 QTest::addColumn<QString>( "expectedText" );
67
68 QTest::newRow( "with-geometry" ) << "geom_to_wkt($geometry)" << true << "The text is 'Point (0.5 0.5)'";
69 QTest::newRow( "without-geometry" ) << "2+pk" << false << "The text is '3'";
70 }
71
testExpressionEvaluate()72 void TestQgsHtmlWidgetWrapper::testExpressionEvaluate()
73 {
74 QFETCH( QString, expression );
75 QFETCH( bool, needsGeometry );
76 QFETCH( QString, expectedText );
77
78 QgsVectorLayer layer( QStringLiteral( "Point?crs=epsg:4326&field=pk:int" ), QStringLiteral( "layer" ), QStringLiteral( "memory" ) );
79 QgsProject::instance()->addMapLayer( &layer, false, false );
80 QgsFeature f( layer.fields() );
81 f.setAttribute( QStringLiteral( "pk" ), 1 );
82 f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT(0.5 0.5)" ) ) );
83 QVERIFY( f.isValid() );
84 QVERIFY( f.geometry().isGeosValid() );
85 QVERIFY( layer.dataProvider()->addFeature( f ) );
86
87 QgsHtmlWidgetWrapper *htmlWrapper = new QgsHtmlWidgetWrapper( &layer, nullptr, nullptr );
88 htmlWrapper->setHtmlCode( QStringLiteral( "The text is '<script>document.write(expression.evaluate(\"%1\"));</script>'" ).arg( expression ) );
89
90 QgsWebView *webView = qobject_cast<QgsWebView *>( htmlWrapper->widget() );
91 Q_ASSERT( webView );
92 QCOMPARE( htmlWrapper->needsGeometry(), needsGeometry );
93
94 htmlWrapper->setFeature( f );
95
96 #ifdef WITH_QTWEBKIT
97 QCOMPARE( webView->page()->mainFrame()->toPlainText(), expectedText );
98 #endif
99
100 QgsProject::instance()->removeMapLayer( &layer );
101 }
102
103 QGSTEST_MAIN( TestQgsHtmlWidgetWrapper )
104 #include "testqgshtmlwidgetwrapper.moc"
105