1 /***************************************************************************
2      testqgsappbrowserproviders.cpp
3      --------------------------------------
4     Date                 : October 30 2018
5     Copyright            : (C) 2018 Nyall Dawson
6     Email                : nyall dot dawson at gmail 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 #include "qgstest.h"
16 
17 #include <QObject>
18 #include <QString>
19 #include <QStringList>
20 
21 //qgis includes...
22 #include "qgsdataitem.h"
23 #include "qgsapplication.h"
24 #include "qgslogger.h"
25 #include "qgsdataitemprovider.h"
26 #include "qgsdataitemproviderregistry.h"
27 #include "qgssettings.h"
28 #include "qgsappbrowserproviders.h"
29 
30 class TestQgsAppBrowserProviders : public QObject
31 {
32     Q_OBJECT
33 
34   public:
35     TestQgsAppBrowserProviders();
36 
37   private slots:
38     void initTestCase();// will be called before the first testfunction is executed.
39     void cleanupTestCase();// will be called after the last testfunction was executed.
init()40     void init() {} // will be called before each testfunction is executed.
cleanup()41     void cleanup() {} // will be called after every testfunction.
42 
43     void testProjectItemCreation();
44 
45   private:
46     QgsDirectoryItem *mDirItem = nullptr;
47     QString mScanItemsSetting;
48     QString mTestDataDir;
49 };
50 
51 TestQgsAppBrowserProviders::TestQgsAppBrowserProviders() = default;
52 
initTestCase()53 void TestQgsAppBrowserProviders::initTestCase()
54 {
55   //
56   // Runs once before any tests are run
57   //
58   // init QGIS's paths - true means that all path will be inited from prefix
59   QgsApplication::init();
60   QgsApplication::initQgis();
61   QgsApplication::showSettings();
62 
63   QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
64   mTestDataDir = dataDir + '/';
65 
66   // Set up the QgsSettings environment
67   QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
68   QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
69   QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
70   // save current scanItemsSetting value
71   QgsSettings settings;
72   mScanItemsSetting = settings.value( QStringLiteral( "/qgis/scanItemsInBrowser2" ), QVariant( "" ) ).toString();
73 
74   //create a directory item that will be used in all tests...
75   mDirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), TEST_DATA_DIR );
76 }
77 
cleanupTestCase()78 void TestQgsAppBrowserProviders::cleanupTestCase()
79 {
80   // restore scanItemsSetting
81   QgsSettings settings;
82   settings.setValue( QStringLiteral( "/qgis/scanItemsInBrowser2" ), mScanItemsSetting );
83   if ( mDirItem )
84     delete mDirItem;
85 
86   QgsApplication::exitQgis();
87 }
88 
89 
testProjectItemCreation()90 void TestQgsAppBrowserProviders::testProjectItemCreation()
91 {
92   QgsDirectoryItem *dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), mTestDataDir + QStringLiteral( "qgis_server/" ) );
93   QVector<QgsDataItem *> children = dirItem->createChildren();
94 
95   // now, add a specific provider which handles project files
96   QgsApplication::dataItemProviderRegistry()->addProvider( new QgsProjectDataItemProvider() );
97 
98   dirItem = new QgsDirectoryItem( nullptr, QStringLiteral( "Test" ), mTestDataDir + QStringLiteral( "qgis_server/" ) );
99   children = dirItem->createChildren();
100 
101   for ( QgsDataItem *child : children )
102   {
103     if ( child->type() == QgsDataItem::Project && child->path() == mTestDataDir + QStringLiteral( "qgis_server/test_project.qgs" ) )
104     {
105       child->populate( true );
106 
107       QCOMPARE( child->children().count(), 7 );
108       QVERIFY( dynamic_cast< QgsProjectLayerTreeGroupItem * >( child->children().at( 2 ) ) );
109       QCOMPARE( child->children().at( 2 )->name(), QStringLiteral( "groupwithoutshortname" ) );
110 
111       QCOMPARE( child->children().at( 2 )->children().count(), 1 );
112       QVERIFY( dynamic_cast< QgsLayerItem * >( child->children().at( 2 )->children().at( 0 ) ) );
113       QCOMPARE( child->children().at( 2 )->children().at( 0 )->name(), QStringLiteral( "testlayer3" ) );
114 
115       QVERIFY( dynamic_cast< QgsProjectLayerTreeGroupItem * >( child->children().at( 3 ) ) );
116       QCOMPARE( child->children().at( 3 )->name(), QStringLiteral( "groupwithshortname" ) );
117 
118       QCOMPARE( child->children().at( 3 )->children().count(), 1 );
119       QVERIFY( dynamic_cast< QgsLayerItem * >( child->children().at( 3 )->children().at( 0 ) ) );
120       QCOMPARE( child->children().at( 3 )->children().at( 0 )->name(), QStringLiteral( "testlayer2" ) );
121 
122       QVERIFY( dynamic_cast< QgsLayerItem * >( child->children().at( 5 ) ) );
123       QCOMPARE( child->children().at( 5 )->name(), QStringLiteral( "testlayer" ) );
124 
125       QVERIFY( dynamic_cast< QgsLayerItem * >( child->children().at( 6 ) ) );
126       QCOMPARE( child->children().at( 6 )->name(), QStringLiteral( u"testlayer \u00E8\u00E9" ) );
127 
128       delete dirItem;
129       return;
130     }
131   }
132   delete dirItem;
133   QVERIFY( false ); // should not be reached
134 }
135 
136 QGSTEST_MAIN( TestQgsAppBrowserProviders )
137 #include "testqgsappbrowserproviders.moc"
138