void TestPageCommands::testInsertPageCommand() // move of frames { KWDocument document; KWPageInsertCommand command1(&document, 0); QCOMPARE(document.pageCount(), 0); QCOMPARE(document.frameSetCount(), 0); command1.redo(); QCOMPARE(document.pageCount(), 1); QCOMPARE(document.frameSetCount(), 0); KWFrameSet *fs = new KWFrameSet(); MockShape *shape = new MockShape(); KWFrame *frame = new KWFrame(shape, fs); Q_UNUSED(frame); document.addFrameSet(fs); QPointF startPos = shape->position(); KWPageInsertCommand command2(&document, 0); QCOMPARE(document.pageCount(), 1); QCOMPARE(document.frameSetCount(), 1); command2.redo(); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(command2.page().pageNumber(), 1); QCOMPARE(command1.page().pageNumber(), 2); QPointF newPos = shape->position(); QCOMPARE(newPos, QPointF(0, command2.page().height()) + startPos); // it moved ;) KWPageInsertCommand command3(&document, 2); command3.redo(); QCOMPARE(document.pageCount(), 3); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(newPos, shape->position()); // it has not moved from page 2 command3.undo(); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(newPos, shape->position()); // it has not moved from page 2 QCOMPARE(command2.page().pageNumber(), 1); QCOMPARE(command1.page().pageNumber(), 2); command2.undo(); QCOMPARE(document.pageCount(), 1); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(command1.page().pageNumber(), 1); QCOMPARE(startPos, shape->position()); // it has been moved back command2.redo(); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(command2.page().pageNumber(), 1); QCOMPARE(command1.page().pageNumber(), 2); QCOMPARE(QPointF(0, command2.page().height()) + startPos, newPos); // it moved again ;) }
void TestDocumentLayout::placeAnchoredFrame() { initForNewTest(QString()); MockShape *picture = new MockShape(); picture->setSize(QSizeF(100, 100)); KTextAnchor *anchor = new KTextAnchor(picture); anchor->setOffset(QPointF(23, 45)); QTextCursor cursor(doc); KInlineTextObjectManager *manager = new KInlineTextObjectManager(); layout->setInlineTextObjectManager(manager); MockLayoutState *state = new MockLayoutState(doc); layout->setLayout(state); state->shape = shape1; QCOMPARE(doc->begin().text().length(), 0); manager->insertInlineObject(cursor, anchor); QCOMPARE(doc->begin().text().length(), 1); QCOMPARE(cursor.position(), 1); shape1->setPosition(QPointF(300, 300)); layout->layout(); QCOMPARE(picture->parent(), shape1); QCOMPARE(picture->position(), QPointF(23, 59.4)); cursor.setPosition(0); cursor.insertText("foo"); // moves my anchors slightly to the right/down and gives line height layout->layout(); QCOMPARE(picture->parent(), shape1); QPointF newPos = picture->position(); QVERIFY(newPos.x() > 23); QVERIFY(newPos.y() > 45); // it adds the baseline now cursor.movePosition(QTextCursor::End); cursor.insertText("\nNew Line\nAnd another"); layout->layout(); QCOMPARE(picture->position(), newPos); QTextLayout *firstLineLayout = doc->begin().layout(); QTextOption option = firstLineLayout->textOption(); option.setAlignment(Qt::AlignHCenter); firstLineLayout->setTextOption(option); layout->layout(); QTextLine first = doc->begin().layout()->lineAt(0); QVERIFY(first.isValid()); QVERIFY(first.naturalTextRect().x() > 10); newPos.setX(newPos.x() + first.naturalTextRect().x()); // text is moved due to alignment QCOMPARE(picture->position(), newPos); anchor->setOffset(QPointF()); anchor->setAlignment(KTextAnchor::Left); anchor->setAlignment(KTextAnchor::TopOfParagraph); layout->layout(); // image is 100 wide, now centered in a parent of 200 so X = 50 QCOMPARE(picture->position(), QPointF(50, 0)); }
void TestDocumentLayout::placeAnchoredFrame2() { QFETCH(int, horizontalAlignment); QFETCH(int, verticalAlignment); QFETCH(QPointF, startPosition); QFETCH(QPointF, imagePosition); initForNewTest(QString(loremIpsum)); MockShape *picture = new MockShape(); picture->setSize(QSizeF(198, 400)); KTextAnchor *anchor = new KTextAnchor(picture); anchor->setAlignment(KTextAnchor::AnchorHorizontal(horizontalAlignment)); anchor->setAlignment(KTextAnchor::AnchorVertical(verticalAlignment)); picture->setPosition(startPosition); QTextCursor cursor(doc); KInlineTextObjectManager *manager = new KInlineTextObjectManager(); layout->setInlineTextObjectManager(manager); MockLayoutState *state = new MockLayoutState(doc); layout->setLayout(state); state->shape = shape1; manager->insertInlineObject(cursor, anchor); QCOMPARE(cursor.position(), 1); layout->layout(); QCOMPARE(picture->parent(), shape1); QCOMPARE(picture->position(), imagePosition); // test if rest of text is below picture. QTextLayout *lay = doc->begin().layout(); QVERIFY(lay->lineCount() >= 1); QTextLine line = lay->lineForTextPosition(1); // the first char of real text. QVERIFY(line.isValid()); // qDebug() << line.y() << line.height(); QVERIFY(line.y() + line.height() >= 412); // test that text is below image }
void TestPageCommands::testRemovePageCommand() // move of frames { KWDocument document; KWPageInsertCommand insertCommand(&document, 0); insertCommand.redo(); KWFrameSet *fs = new KWFrameSet(); MockShape *shape = new MockShape(); KWFrame *frame = new KWFrame(shape, fs); Q_UNUSED(frame); document.addFrameSet(fs); KWPageInsertCommand insertCommand2(&document, 1); insertCommand2.redo(); MockShape *shape2 = new MockShape(); QPointF pos = QPointF(20, insertCommand2.page().offsetInDocument() + 10); shape2->setPosition(pos); KWFrame *frame2 = new KWFrame(shape2, fs); Q_UNUSED(frame2); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 2); // remove page2 KWPageRemoveCommand command1(&document, insertCommand2.page()); command1.redo(); QCOMPARE(insertCommand.page().pageNumber(), 1); QCOMPARE(insertCommand2.page().isValid(), false); QCOMPARE(document.pageCount(), 1); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(shape2->position(), pos); // shapes are not deleted, just removed from the document command1.undo(); QCOMPARE(insertCommand.page().pageNumber(), 1); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 2); QCOMPARE(shape2->position(), pos); // not moved. // remove page 1 KWPageRemoveCommand command2(&document, insertCommand.page()); command2.redo(); QCOMPARE(insertCommand.page().isValid(), false); QCOMPARE(document.pageCount(), 1); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 1); QCOMPARE(shape->position(), QPointF(0,0)); QCOMPARE(shape2->position(), QPointF(20, 10)); // moved! command2.undo(); QCOMPARE(document.pageCount(), 2); QCOMPARE(document.frameSetCount(), 1); QCOMPARE(fs->frameCount(), 2); QCOMPARE(shape->position(), QPointF(0,0)); QCOMPARE(shape2->position(), pos); // moved back! }