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
|
|
|
|
//
|
|
|
|
|
2023-08-22 05:03:47 +03:00
|
|
|
#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"
|
2021-11-26 17:25:46 +03:00
|
|
|
#include "ui/DoubleTreeWidget.h"
|
2018-12-13 02:22:04 +03:00
|
|
|
#include "ui/RepoView.h"
|
2021-11-26 17:25:46 +03:00
|
|
|
#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;
|
2021-11-26 17:25:46 +03:00
|
|
|
QString mMainBranch;
|
2018-12-13 02:22:04 +03:00
|
|
|
};
|
|
|
|
|
2022-05-01 10:51:34 +03:00
|
|
|
void TestMerge::initTestCase() {
|
2021-11-26 17:25:46 +03:00
|
|
|
mMainBranch = mRepo->unbornHeadName();
|
2018-12-13 02:22:04 +03:00
|
|
|
mWindow = new MainWindow(mRepo);
|
|
|
|
mWindow->show();
|
2022-05-05 23:32:45 +03:00
|
|
|
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));
|
2023-08-16 08:50:58 +03:00
|
|
|
QTextStream(&file) << "This will be a test." << Qt::endl;
|
2018-12-13 02:22:04 +03:00
|
|
|
|
|
|
|
RepoView *view = mWindow->currentView();
|
|
|
|
refresh(view);
|
|
|
|
|
2021-11-26 17:25:46 +03:00
|
|
|
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));
|
2023-08-16 08:50:58 +03:00
|
|
|
QTextStream(&file) << "This is a conflict." << Qt::endl;
|
2018-12-13 02:22:04 +03:00
|
|
|
|
|
|
|
refresh(view);
|
|
|
|
|
2021-11-26 17:25:46 +03:00
|
|
|
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);
|
2021-11-26 17:25:46 +03:00
|
|
|
QCOMPARE(mRepo->head().name(), mMainBranch);
|
2018-12-13 02:22:04 +03:00
|
|
|
|
|
|
|
QFile file(mRepo->workdir().filePath("test"));
|
|
|
|
QVERIFY(file.open(QFile::WriteOnly));
|
2023-08-16 08:50:58 +03:00
|
|
|
QTextStream(&file) << "This is a test." << Qt::endl;
|
2018-12-13 02:22:04 +03:00
|
|
|
|
|
|
|
refresh(view);
|
|
|
|
|
2021-11-26 17:25:46 +03:00
|
|
|
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);
|
|
|
|
|
2021-11-26 17:25:46 +03:00
|
|
|
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 *>();
|
|
|
|
|
2021-11-26 17:25:46 +03:00
|
|
|
auto doubleTree = view->findChild<DoubleTreeWidget *>();
|
|
|
|
QVERIFY(doubleTree);
|
|
|
|
|
2022-11-17 18:14:59 +03:00
|
|
|
auto files = doubleTree->findChild<TreeView *>("Unstaged");
|
2021-11-26 17:25:46 +03:00
|
|
|
QVERIFY(files);
|
|
|
|
|
|
|
|
// Wait for refresh
|
|
|
|
QAbstractItemModel *model = files->model();
|
2022-11-17 18:14:59 +03:00
|
|
|
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)
|
2021-11-26 17:25:46 +03:00
|
|
|
qWait(300);
|
|
|
|
|
2022-05-01 10:51:34 +03:00
|
|
|
files->selectionModel()->select(files->model()->index(0, 0),
|
|
|
|
QItemSelectionModel::Select);
|
2021-11-26 17:25:46 +03:00
|
|
|
|
|
|
|
QToolButton *theirs = diffView->findChild<QToolButton *>("ConflictTheirs");
|
2022-11-17 18:14:59 +03:00
|
|
|
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");
|
2022-11-17 18:14:59 +03:00
|
|
|
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");
|
2022-11-17 18:14:59 +03:00
|
|
|
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");
|
2022-11-17 18:14:59 +03:00
|
|
|
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"
|