1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* This file is part of the KDE project
*
* Copyright (c) 2011 Boudewijn Rempt <boud@kogmbh.com>
* Copyright (c) 2012 C. Boemann <cbo@kogmbh.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "rdf_test.h"
#include <QTest>
#include <QUuid>
#include <QString>
#include <QTextDocument>
#include <QTextTable>
#include <QTextCharFormat>
#include <KoRdfSemanticItem.h>
#include <KoDocumentRdf.h>
#include <KoTextEditor.h>
#include <KoBookmark.h>
#include <KoTextInlineRdf.h>
#include <KoTextDocument.h>
#include <KoTextRangeManager.h>
#include "TestSemanticItem.h"
const QString lorem(
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud"
"exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla"
"pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia"
"deserunt mollit anim id est laborum.\n"
);
QString RdfTest::insertTableWithSemItem(KoTextEditor &editor,
KoDocumentRdf &rdfDoc,
const QString name)
{
editor.insertTable(5,10);
#define TABLESIZE (5*10)
const QTextTable *table = editor.currentTable();
QTextCursor cur(editor.document());
cur.setPosition(table->firstPosition());
KoBookmark *bookmark = new KoBookmark(cur);
bookmark->setPositionOnlyMode(false); // we want it to be several chars long
KoTextInlineRdf *inlineRdf(new KoTextInlineRdf(editor.document(), bookmark));
QString newId = inlineRdf->createXmlId();
inlineRdf->setXmlId(newId);
bookmark->setName(newId);
bookmark->setInlineRdf(inlineRdf);
KoTextDocument(editor.document()).textRangeManager()->insert(bookmark);
editor.setPosition(table->firstPosition());
editor.movePosition(QTextCursor::PreviousCharacter);
hTestSemanticItem testItem(new TestSemanticItem(0, &rdfDoc));
testItem->setName(name);
Soprano::Statement st(
testItem->linkingSubject(), // subject
Soprano::Node::createResourceNode(QUrl("http://docs.oasis-open.org/opendocument/meta/package/common#idref")), // predicate
Soprano::Node::createLiteralNode(newId), // object
rdfDoc.manifestRdfNode()); // manifest datastore
rdfDoc.model()->addStatement(st);
rdfDoc.rememberNewInlineRdfObject(inlineRdf);
Q_ASSERT(rdfDoc.model()->statementCount() > 0);
bookmark->setRangeEnd(table->lastPosition());
return newId;
}
void RdfTest::testCreateMarkers()
{
QObject parent;
// the rdf storage. In calligra, it's part of the KoDocument.
KoDocumentRdf rdfDoc;
// create a document
QTextDocument doc;
KoTextRangeManager rangeManager(&parent);
KoTextDocument textDoc(&doc);
textDoc.setTextRangeManager(&rangeManager);
KoTextEditor editor(&doc);
textDoc.setTextEditor(&editor);
// insert some lorem ipsum
editor.insertText(lorem);
// insert a table and set a bookmark on it with semantics
QString newId = insertTableWithSemItem(editor, rdfDoc, "test item1");
// insert some more lorem before the table
editor.insertText(lorem);
// verify that the bookmark marks the table and only that
QPair<int,int> position = rdfDoc.findExtent(newId);
QCOMPARE(position.first, 2*(lorem.length()+1));
QCOMPARE(position.second, 2*(lorem.length()+1)+TABLESIZE-1);
editor.setPosition(position.first + 1);
QPair<int,int> position2 = rdfDoc.findExtent(&editor);
qDebug()<<position<<position2;
QCOMPARE(position, position2);
// check that the id is like we expext
QCOMPARE(rdfDoc.findXmlId(&editor), newId);
}
void RdfTest::testFindMarkers()
{
QObject parent;
// the rdf storage. In calligra, it's part of the KoDocument.
KoDocumentRdf rdfDoc;
// create a document
QTextDocument doc;
KoTextRangeManager rangeManager(&parent);
KoTextDocument textDoc(&doc);
textDoc.setTextRangeManager(&rangeManager);
KoTextEditor editor(&doc);
textDoc.setTextEditor(&editor);
// insert some lorem ipsum
editor.insertText(lorem);
// insert a table and set a bookmark on it
QStringList idList;
QString newId = insertTableWithSemItem(editor, rdfDoc, "test item1");
idList << newId;
editor.setPosition(0);
// now use soprano to find the tables
QList<hTestSemanticItem> semItems = TestSemanticItem::allObjects(&rdfDoc);
Q_ASSERT(semItems.length() == 1);
foreach(hTestSemanticItem semItem, semItems) {
QStringList xmlidlist = semItem->xmlIdList();
Q_ASSERT(xmlidlist.length() == 1);
foreach(const QString xmlid, xmlidlist) {
Q_ASSERT(idList.contains(xmlid));
QPair<int, int> position = rdfDoc.findExtent(xmlid);
QCOMPARE(position.first, lorem.length() + 1);
QCOMPARE(position.second, lorem.length() + 1 + TABLESIZE - 1);
editor.setPosition(position.first + 2);
const QTextTable *table = editor.currentTable();
QVERIFY(table);
Q_UNUSED(table);
}
}
/*
// add an extra semitem
editor.insertText(lorem);
editor.movePosition(QTextCursor::End);
QString newId2 = insertTableWithSemItem(editor, rdfDoc, "test item2");
idList << newId2;
editor.insertText(lorem);
// get all semantic items and verify they are correct
semItems = TestSemanticItem::allObjects(&rdfDoc);
QCOMPARE(semItems.length(), 2);
// find the extents from the xml-id's in the semantic items
QCOMPARE(semItems[0]->xmlIdList().length(), 1);
QPair<int, int> position = rdfDoc.findExtent(semItems[0]->xmlIdList()[0]);
QCOMPARE(position.first, 942);
QCOMPARE(position.second, 995);
editor.setPosition(position.first + 2);
const QTextTable *table = editor.currentTable();
Q_ASSERT(table);
Q_UNUSED(table);
QCOMPARE(semItems[1]->xmlIdList().length(), 1);
position = rdfDoc.findExtent(semItems[1]->xmlIdList()[0]);
QCOMPARE(position.first, 444);
QCOMPARE(position.second, 497);
editor.setPosition(position.first + 2);
table = editor.currentTable();
Q_ASSERT(table);
*/}
void RdfTest::testFindByName()
{
/*
QObject parent;
// the rdf storage. In calligra, it's part of the KoDocument.
KoDocumentRdf rdfDoc;
// create a document
QTextDocument doc;
KoInlineTextObjectManager inlineObjectManager(&parent);
KoTextDocument textDoc(&doc);
textDoc.setInlineTextObjectManager(&inlineObjectManager);
KoTextEditor editor(&doc);
textDoc.setTextEditor(&editor);
// enter some lorem ipsum
editor.insertText(lorem);
// enter a bit of marked text
QStringList idList;
QString newId = insertSemItem(editor, rdfDoc, parent, "test item1");
idList << newId;
idList << insertSemItem(editor, rdfDoc, parent, "test item2")
<< insertSemItem(editor, rdfDoc, parent, "test item3")
<< insertSemItem(editor, rdfDoc, parent, "test item4");
QList<hTestSemanticItem> results = TestSemanticItem::findItemsByName("test item1",
&rdfDoc);
Q_ASSERT(results.size() == 1);
QStringList xmlids = results[0]->xmlIdList();
Q_ASSERT(xmlids.size() == 1);
Q_ASSERT(xmlids[0] == newId);
*/
}
void RdfTest::testEditAndFindMarkers()
{
/*
QObject parent;
// the rdf storage. In calligra, it's part of the KoDocument.
KoDocumentRdf rdfDoc;
// create a document
QTextDocument doc;
KoInlineTextObjectManager inlineObjectManager(&parent);
KoTextDocument textDoc(&doc);
textDoc.setInlineTextObjectManager(&inlineObjectManager);
KoTextEditor editor(&doc);
textDoc.setTextEditor(&editor);
// enter some lorem ipsum
editor.insertText(lorem);
// enter a bit of marked text
QStringList idList;
QString newId = insertSemItem(editor, rdfDoc, parent, "test item1");
idList << newId;
// XXX: finish test!
*/
}
void RdfTest::testRemoveMarkers()
{
/*
QObject parent;
// the rdf storage. In calligra, it's part of the KoDocument.
KoDocumentRdf rdfDoc;
// create a document
QTextDocument doc;
KoInlineTextObjectManager inlineObjectManager(&parent);
KoTextDocument textDoc(&doc);
textDoc.setInlineTextObjectManager(&inlineObjectManager);
KoTextEditor editor(&doc);
textDoc.setTextEditor(&editor);
// enter some lorem ipsum
editor.insertText(lorem);
// enter a bit of marked text
QStringList idList;
idList << insertSemItem(editor, rdfDoc, parent, "test item1");
editor.insertText(lorem);
idList << insertSemItem(editor, rdfDoc, parent, "test item2");
editor.insertText(lorem);
QString newId = insertSemItem(editor, rdfDoc, parent, "test item3");
idList << newId;
editor.insertText(lorem);
idList << insertSemItem(editor, rdfDoc, parent, "test item4");
editor.insertText(lorem);
QList<hTestSemanticItem> results = TestSemanticItem::findItemsByName("test item3", &rdfDoc);
Q_ASSERT(results.size() == 1);
QStringList xmlids = results[0]->xmlIdList();
Q_ASSERT(xmlids.size() == 1);
Q_ASSERT(xmlids[0] == newId);
QPair<int, int> pos = rdfDoc.findExtent(newId);
// move around the markers for this item.
editor.setPosition(pos.first - 10, QTextCursor::MoveAnchor);
editor.setPosition(pos.second + 10, QTextCursor::KeepAnchor);
Q_ASSERT(editor.hasSelection());
// remove the table + the markers from the document
editor.deleteChar();
results = TestSemanticItem::allObjects(&rdfDoc);
// we haven't rebuild the rdf database yet -- and we cannot do that from KoTextEditor,
// so there still are 4 items
Q_ASSERT(results.count() == 4);
// now redo the database
rdfDoc.updateInlineRdfStatements(&doc);
// and now there should be three
results = TestSemanticItem::allObjects(&rdfDoc);
qDebug() << "we have" << results.count() << "items";
foreach(hTestSemanticItem item, results) {
Q_ASSERT(item->xmlIdList().length() == 1);
QPair<int,int> pos = rdfDoc.findExtent(item->xmlIdList().first());
qDebug() << item->name() << "pos:" << pos;
editor.setPosition(pos.first + 1);
qDebug() << "points to table" << editor.currentTable();
}
*/
}
QTEST_MAIN(RdfTest)
#include "rdf_test.moc"
|