Gittyup/test/merge.cpp

252 lines
6.7 KiB
C++
Raw Normal View History

2018-12-13 02:22:04 +03:00
//
// Copyright (c) 2016, Scientific Toolworks, Inc.
//
// This software is licensed under the MIT License. The LICENSE.md file
// describes the conditions under which this software may be distributed.
//
// Author: Shane Gramlich
//
#include "qtsupport.h"
2018-12-13 02:22:04 +03:00
#include "Test.h"
#include "ui/MainWindow.h"
#include "ui/DetailView.h"
2020-04-23 14:30:54 +03:00
#include "ui/DiffView/DiffView.h"
#include "ui/DoubleTreeWidget.h"
2018-12-13 02:22:04 +03:00
#include "ui/RepoView.h"
#include "ui/TreeView.h"
2018-12-13 02:22:04 +03:00
#include <QFile>
#include <QPushButton>
#include <QTextEdit>
#include <QToolButton>
using namespace Test;
using namespace QTest;
2022-05-01 10:51:34 +03:00
class TestMerge : public QObject {
2018-12-13 02:22:04 +03:00
Q_OBJECT
private slots:
void initTestCase();
void firstCommit();
void secondCommit();
void thirdCommit();
void mergeConflict();
void resolve();
void cleanupTestCase();
private:
int inputDelay = 0;
int closeDelay = 0;
ScratchRepository mRepo;
MainWindow *mWindow = nullptr;
QString mMainBranch;
2018-12-13 02:22:04 +03:00
};
2022-05-01 10:51:34 +03:00
void TestMerge::initTestCase() {
mMainBranch = mRepo->unbornHeadName();
2018-12-13 02:22:04 +03:00
mWindow = new MainWindow(mRepo);
mWindow->show();
QVERIFY(qWaitForWindowExposed(mWindow));
2018-12-13 02:22:04 +03:00
}
2022-05-01 10:51:34 +03:00
void TestMerge::firstCommit() {
2018-12-13 02:22:04 +03:00
// Add file and refresh.
QFile file(mRepo->workdir().filePath("test"));
QVERIFY(file.open(QFile::WriteOnly));
QTextStream(&file) << "This will be a test." << Qt::endl;
2018-12-13 02:22:04 +03:00
RepoView *view = mWindow->currentView();
refresh(view);
auto doubleTree = view->findChild<DoubleTreeWidget *>();
QVERIFY(doubleTree);
auto files = doubleTree->findChild<TreeView *>("Unstaged");
2018-12-13 02:22:04 +03:00
QVERIFY(files);
QAbstractItemModel *model = files->model();
QCOMPARE(model->rowCount(), 1);
// Click on the check box.
QModelIndex index = model->index(0, 0);
2022-05-01 10:51:34 +03:00
mouseClick(files->viewport(), Qt::LeftButton, Qt::KeyboardModifiers(),
files->checkRect(index).center());
2018-12-13 02:22:04 +03:00
// Commit and refresh.
QTextEdit *editor = view->findChild<QTextEdit *>("MessageEditor");
QVERIFY(editor);
editor->setText("base commit");
view->commit();
refresh(view, false);
}
2022-05-01 10:51:34 +03:00
void TestMerge::secondCommit() {
2018-12-13 02:22:04 +03:00
RepoView *view = mWindow->currentView();
git::Branch branch = mRepo->createBranch("branch2", mRepo->head().target());
QVERIFY(branch.isValid());
view->checkout(branch);
QCOMPARE(mRepo->head().name(), QString("branch2"));
QFile file(mRepo->workdir().filePath("test"));
QVERIFY(file.open(QFile::WriteOnly));
QTextStream(&file) << "This is a conflict." << Qt::endl;
2018-12-13 02:22:04 +03:00
refresh(view);
auto doubleTree = view->findChild<DoubleTreeWidget *>();
QVERIFY(doubleTree);
auto files = doubleTree->findChild<TreeView *>("Unstaged");
2018-12-13 02:22:04 +03:00
QVERIFY(files);
QAbstractItemModel *model = files->model();
QCOMPARE(model->rowCount(), 1);
// Click on the check box.
QModelIndex index = model->index(0, 0);
2022-05-01 10:51:34 +03:00
mouseClick(files->viewport(), Qt::LeftButton, Qt::KeyboardModifiers(),
files->checkRect(index).center());
2018-12-13 02:22:04 +03:00
// Commit and refresh.
QTextEdit *editor = view->findChild<QTextEdit *>("MessageEditor");
QVERIFY(editor);
editor->setText("conflicting commit b");
view->commit();
refresh(view, false);
}
2022-05-01 10:51:34 +03:00
void TestMerge::thirdCommit() {
2018-12-13 02:22:04 +03:00
RepoView *view = mWindow->currentView();
2022-05-01 10:51:34 +03:00
git::Reference ref =
mRepo->lookupRef(QString("refs/heads/%1").arg(mMainBranch));
2018-12-13 02:22:04 +03:00
QVERIFY(ref);
view->checkout(ref);
QCOMPARE(mRepo->head().name(), mMainBranch);
2018-12-13 02:22:04 +03:00
QFile file(mRepo->workdir().filePath("test"));
QVERIFY(file.open(QFile::WriteOnly));
QTextStream(&file) << "This is a test." << Qt::endl;
2018-12-13 02:22:04 +03:00
refresh(view);
auto doubleTree = view->findChild<DoubleTreeWidget *>();
QVERIFY(doubleTree);
auto files = doubleTree->findChild<TreeView *>("Unstaged");
2018-12-13 02:22:04 +03:00
QVERIFY(files);
QAbstractItemModel *model = files->model();
QCOMPARE(model->rowCount(), 1);
// Click on the check box.
QModelIndex index = model->index(0, 0);
2022-05-01 10:51:34 +03:00
mouseClick(files->viewport(), Qt::LeftButton, Qt::KeyboardModifiers(),
files->checkRect(index).center());
2018-12-13 02:22:04 +03:00
// Commit and refresh.
QTextEdit *editor = view->findChild<QTextEdit *>("MessageEditor");
QVERIFY(editor);
editor->setText("conflicting commit a");
view->commit();
refresh(view, false);
}
2022-05-01 10:51:34 +03:00
void TestMerge::mergeConflict() {
2018-12-13 02:22:04 +03:00
RepoView *view = mWindow->currentView();
2022-05-01 10:51:34 +03:00
git::Reference master =
mRepo->lookupRef(QString("refs/heads/%1").arg(mMainBranch));
2018-12-13 02:22:04 +03:00
QVERIFY(master);
git::Reference branch2 = mRepo->lookupRef("refs/heads/branch2");
QVERIFY(branch2);
QCOMPARE(mRepo->head().name(), mMainBranch);
2018-12-13 02:22:04 +03:00
view->merge(RepoView::Merge, branch2);
// Diff is in a conflicted state
git::Diff diff = mRepo->diffIndexToWorkdir();
QVERIFY(diff.isConflicted());
}
2022-05-01 10:51:34 +03:00
void TestMerge::resolve() {
2018-12-13 02:22:04 +03:00
RepoView *view = mWindow->currentView();
DiffView *diffView = view->findChild<DiffView *>();
auto doubleTree = view->findChild<DoubleTreeWidget *>();
QVERIFY(doubleTree);
auto files = doubleTree->findChild<TreeView *>("Unstaged");
QVERIFY(files);
// Wait for refresh
QAbstractItemModel *model = files->model();
qWait(1000); // Because before the merge, there is already an item in the
// unstaged model
2022-05-01 10:51:34 +03:00
while (model->rowCount() < 1)
qWait(300);
2022-05-01 10:51:34 +03:00
files->selectionModel()->select(files->model()->index(0, 0),
QItemSelectionModel::Select);
QToolButton *theirs = diffView->findChild<QToolButton *>("ConflictTheirs");
QVERIFY(theirs);
2022-05-01 10:51:34 +03:00
mouseClick(theirs, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(),
inputDelay);
2018-12-13 02:22:04 +03:00
2022-05-01 10:51:34 +03:00
QToolButton *undo =
diffView->widget()->findChild<QToolButton *>("ConflictUndo");
QVERIFY(undo);
2022-05-01 10:51:34 +03:00
mouseClick(undo, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(),
inputDelay);
2018-12-13 02:22:04 +03:00
2022-05-01 10:51:34 +03:00
QToolButton *ours =
diffView->widget()->findChild<QToolButton *>("ConflictOurs");
QVERIFY(ours);
2022-05-01 10:51:34 +03:00
mouseClick(ours, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(),
inputDelay);
2018-12-13 02:22:04 +03:00
2022-05-01 10:51:34 +03:00
QToolButton *save =
diffView->widget()->findChild<QToolButton *>("ConflictSave");
QVERIFY(save);
2022-05-01 10:51:34 +03:00
mouseClick(save, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(),
inputDelay);
2018-12-13 02:22:04 +03:00
DetailView *detailView = view->findChild<DetailView *>();
QPushButton *stageAll = nullptr;
while (stageAll == nullptr) {
stageAll = detailView->findChild<QPushButton *>("StageAll");
qWait(100);
}
2022-05-01 10:51:34 +03:00
mouseClick(stageAll, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(),
inputDelay);
2018-12-13 02:22:04 +03:00
// Commit and refresh.
QTextEdit *editor = view->findChild<QTextEdit *>("MessageEditor");
QVERIFY(editor);
editor->setText("conflicts resolved");
view->commit();
refresh(view, false);
// Diff is not in a conflicted state
git::Diff diff = mRepo->diffIndexToWorkdir();
QVERIFY(!diff.isConflicted());
}
2022-05-01 10:51:34 +03:00
void TestMerge::cleanupTestCase() {
2018-12-13 02:22:04 +03:00
qWait(closeDelay);
mWindow->close();
}
TEST_MAIN(TestMerge)
#include "merge.moc"