Changeset View
Changeset View
Standalone View
Standalone View
autotests/qml/qmlbookmarksapitest.cpp
- This file was added.
1 | /* ============================================================ | ||||
---|---|---|---|---|---|
2 | * Falkon - Qt web browser | ||||
3 | * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com> | ||||
4 | * | ||||
5 | * This program is free software: you can redistribute it and/or modify | ||||
6 | * it under the terms of the GNU General Public License as published by | ||||
7 | * the Free Software Foundation, either version 3 of the License, or | ||||
8 | * (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
17 | * ============================================================ */ | ||||
18 | #include "qmlbookmarksapitest.h" | ||||
19 | #include "autotests.h" | ||||
20 | #include "mainapplication.h" | ||||
21 | #include "bookmarks.h" | ||||
22 | #include "bookmarkitem.h" | ||||
23 | #include "qml/api/bookmarks/qmlbookmarktreenode.h" | ||||
24 | | ||||
25 | void QmlBookmarksApiTest::initTestCase() | ||||
26 | { | ||||
27 | } | ||||
28 | | ||||
29 | void QmlBookmarksApiTest::cleanupTestCase() | ||||
30 | { | ||||
31 | } | ||||
32 | | ||||
33 | void QmlBookmarksApiTest::testBookmarkTreeNodeType() | ||||
34 | { | ||||
35 | auto type = BookmarkItem::Type(m_testHelper.evaluate("Falkon.Bookmarks.rootItem().type").toInt()); | ||||
36 | QCOMPARE(mApp->bookmarks()->rootItem()->type(), type); | ||||
37 | | ||||
38 | type = BookmarkItem::Type(m_testHelper.evaluate("Falkon.Bookmarks.toolbarFolder().type").toInt()); | ||||
39 | QCOMPARE(mApp->bookmarks()->toolbarFolder()->type(), type); | ||||
40 | } | ||||
41 | | ||||
42 | void QmlBookmarksApiTest::testBookmarkTreeNode() | ||||
43 | { | ||||
44 | QObject *bookmark = m_testHelper.evaluateQObject("Falkon.Bookmarks.toolbarFolder()"); | ||||
45 | QVERIFY(bookmark); | ||||
46 | auto toolbarFolder = mApp->bookmarks()->toolbarFolder(); | ||||
47 | | ||||
48 | QCOMPARE(toolbarFolder->title(), bookmark->property("title").toString()); | ||||
49 | QCOMPARE(toolbarFolder->urlString(), bookmark->property("url").toString()); | ||||
50 | QCOMPARE(toolbarFolder->description(), bookmark->property("description").toString()); | ||||
51 | QCOMPARE(!mApp->bookmarks()->canBeModified(toolbarFolder), bookmark->property("unmodifiable").toBool()); | ||||
52 | QObject* parent = qvariant_cast<QObject*>(bookmark->property("parent")); | ||||
53 | QVERIFY(parent); | ||||
54 | QCOMPARE(mApp->bookmarks()->rootItem()->title(), parent->property("title").toString()); | ||||
55 | } | ||||
56 | | ||||
57 | void QmlBookmarksApiTest::testBookmarksCreation() | ||||
58 | { | ||||
59 | auto item = new BookmarkItem(BookmarkItem::Url); | ||||
60 | item->setTitle("Example Domain"); | ||||
61 | item->setUrl(QUrl("https://example.com/")); | ||||
62 | item->setDescription("Testing bookmark description"); | ||||
63 | | ||||
64 | QObject *qmlBookmarks = m_testHelper.evaluateQObject("Falkon.Bookmarks"); | ||||
65 | QVERIFY(qmlBookmarks); | ||||
66 | | ||||
67 | QSignalSpy qmlBookmarksSpy(qmlBookmarks, SIGNAL(created(QmlBookmarkTreeNode*))); | ||||
68 | mApp->bookmarks()->addBookmark(mApp->bookmarks()->rootItem(), item); | ||||
69 | | ||||
70 | QCOMPARE(qmlBookmarksSpy.count(), 1); | ||||
71 | | ||||
72 | QObject *created = qvariant_cast<QObject*>(qmlBookmarksSpy.at(0).at(0)); | ||||
73 | QVERIFY(created); | ||||
74 | QCOMPARE(item->title(), created->property("title").toString()); | ||||
75 | | ||||
76 | qRegisterMetaType<BookmarkItem*>(); | ||||
77 | QSignalSpy bookmarksSpy(mApp->bookmarks(), &Bookmarks::bookmarkAdded); | ||||
78 | | ||||
79 | auto out = m_testHelper.evaluate("Falkon.Bookmarks.create({" | ||||
80 | " parent: Falkon.Bookmarks.toolbarFolder()," | ||||
81 | " title: 'Example Plugin'," | ||||
82 | " url: 'https://another-example.com'" | ||||
83 | "});"); | ||||
84 | QVERIFY(out.toBool()); | ||||
85 | | ||||
86 | QCOMPARE(bookmarksSpy.count(), 1); | ||||
87 | BookmarkItem* createdItem = qvariant_cast<BookmarkItem*>(bookmarksSpy.at(0).at(0)); | ||||
88 | QVERIFY(createdItem); | ||||
89 | QCOMPARE(createdItem->title(), QString("Example Plugin")); | ||||
90 | } | ||||
91 | | ||||
92 | void QmlBookmarksApiTest::testBookmarksExistence() | ||||
93 | { | ||||
94 | // in continuation from testBookmarksCreation | ||||
95 | | ||||
96 | auto result = m_testHelper.evaluate("Falkon.Bookmarks.isBookmarked('https://example.com/')").toBool(); | ||||
97 | QVERIFY(result); | ||||
98 | QCOMPARE(mApp->bookmarks()->isBookmarked(QUrl("https://example.com/")), result); | ||||
99 | } | ||||
100 | | ||||
101 | void QmlBookmarksApiTest::testBookmarksModification() | ||||
102 | { | ||||
103 | // in continuation from testBookmarksExistence | ||||
104 | | ||||
105 | QObject *qmlBookmarks = m_testHelper.evaluateQObject("Falkon.Bookmarks"); | ||||
106 | QVERIFY(qmlBookmarks); | ||||
107 | | ||||
108 | QSignalSpy qmlBookmarksSpy(qmlBookmarks, SIGNAL(changed(QmlBookmarkTreeNode*))); | ||||
109 | BookmarkItem* item = mApp->bookmarks()->searchBookmarks("https://example.com/").at(0); | ||||
110 | item->setTitle("Modified Example Domain"); | ||||
111 | mApp->bookmarks()->changeBookmark(item); | ||||
112 | | ||||
113 | QCOMPARE(qmlBookmarksSpy.count(), 1); | ||||
114 | | ||||
115 | QObject *modified = qvariant_cast<QObject*>(qmlBookmarksSpy.at(0).at(0)); | ||||
116 | QVERIFY(modified); | ||||
117 | QCOMPARE(modified->property("title").toString(), QString("Modified Example Domain")); | ||||
118 | | ||||
119 | qRegisterMetaType<BookmarkItem*>(); | ||||
120 | QSignalSpy bookmarksSpy(mApp->bookmarks(), &Bookmarks::bookmarkChanged); | ||||
121 | | ||||
122 | auto out = m_testHelper.evaluate("Falkon.Bookmarks.update(Falkon.Bookmarks.get('https://another-example.com'),{" | ||||
123 | " title: 'Modified Example Plugin'" | ||||
124 | "})"); | ||||
125 | QVERIFY(out.toBool()); | ||||
126 | | ||||
127 | QCOMPARE(bookmarksSpy.count(), 1); | ||||
128 | BookmarkItem* modifiedItem = qvariant_cast<BookmarkItem*>(bookmarksSpy.at(0).at(0)); | ||||
129 | QVERIFY(modifiedItem); | ||||
130 | QCOMPARE(modifiedItem->title(), QString("Modified Example Plugin")); | ||||
131 | } | ||||
132 | | ||||
133 | void QmlBookmarksApiTest::testBookmarksRemoval() | ||||
134 | { | ||||
135 | // in continuation from testBookmarksModification | ||||
136 | | ||||
137 | QObject *qmlBookmarks = m_testHelper.evaluateQObject("Falkon.Bookmarks"); | ||||
138 | QVERIFY(qmlBookmarks); | ||||
139 | | ||||
140 | QSignalSpy qmlBookmarksSpy(qmlBookmarks, SIGNAL(removed(QmlBookmarkTreeNode*))); | ||||
141 | BookmarkItem* item = mApp->bookmarks()->searchBookmarks("https://example.com/").at(0); | ||||
142 | mApp->bookmarks()->removeBookmark(item); | ||||
143 | | ||||
144 | QCOMPARE(qmlBookmarksSpy.count(), 1); | ||||
145 | | ||||
146 | QObject *removed = qvariant_cast<QObject*>(qmlBookmarksSpy.at(0).at(0)); | ||||
147 | QVERIFY(removed); | ||||
148 | QCOMPARE(removed->property("title").toString(), QString("Modified Example Domain")); | ||||
149 | | ||||
150 | qRegisterMetaType<BookmarkItem*>(); | ||||
151 | QSignalSpy bookmarksSpy(mApp->bookmarks(), &Bookmarks::bookmarkRemoved); | ||||
152 | | ||||
153 | auto out = m_testHelper.evaluate("Falkon.Bookmarks.remove(Falkon.Bookmarks.get('https://another-example.com'))"); | ||||
154 | QVERIFY(out.toBool()); | ||||
155 | | ||||
156 | QCOMPARE(bookmarksSpy.count(), 1); | ||||
157 | BookmarkItem* removedItem = qvariant_cast<BookmarkItem*>(bookmarksSpy.at(0).at(0)); | ||||
158 | QVERIFY(removedItem); | ||||
159 | QCOMPARE(removedItem->title(), QString("Modified Example Plugin")); | ||||
160 | } | ||||
161 | | ||||
162 | FALKONTEST_MAIN(QmlBookmarksApiTest) |