AutoTest: Create new tree items only if necessary...

...otherwise update existing. This reduces the number of items
that would get created or destroyed especially while modifying
code or just open/close test related documents.

Change-Id: Ia6a03ec33550c8c28e5120422e0f68fa428c1020
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com>
This commit is contained in:
Christian Stenger
2016-02-08 15:56:07 +01:00
parent 7138d90377
commit ac659dd25a
4 changed files with 309 additions and 254 deletions

View File

@@ -57,28 +57,6 @@ TestTreeItem::TestTreeItem(const QString &name, const QString &filePath, Type ty
} }
} }
TestTreeItem::~TestTreeItem()
{
removeChildren();
}
TestTreeItem::TestTreeItem(const TestTreeItem &other)
: TreeItem( { other.m_name } ),
m_name(other.m_name),
m_filePath(other.m_filePath),
m_checked(other.m_checked),
m_type(other.m_type),
m_line(other.m_line),
m_column(other.m_column),
m_mainFile(other.m_mainFile),
m_referencingFile(other.m_referencingFile),
m_state(other.m_state),
m_markedForRemoval(other.m_markedForRemoval)
{
for (int row = 0, count = other.childCount(); row < count; ++row)
appendChild(new TestTreeItem(*other.childItem(row)));
}
static QIcon testTreeIcon(TestTreeItem::Type type) static QIcon testTreeIcon(TestTreeItem::Type type)
{ {
static QIcon icons[] = { static QIcon icons[] = {
@@ -169,39 +147,56 @@ bool TestTreeItem::setData(int /*column*/, const QVariant &data, int role)
return false; return false;
} }
bool TestTreeItem::modifyContent(const TestTreeItem *modified) bool TestTreeItem::modifyTestCaseContent(const QString &name, unsigned line, unsigned column)
{
bool hasBeenModified = modifyName(name);
hasBeenModified |= modifyLineAndColumn(line, column);
return hasBeenModified;
}
bool TestTreeItem::modifyTestFunctionContent(const TestCodeLocationAndType &location)
{
bool hasBeenModified = modifyFilePath(location.m_name);
hasBeenModified |= modifyLineAndColumn(location.m_line, location.m_column);
return hasBeenModified;
}
bool TestTreeItem::modifyDataTagContent(const QString &fileName,
const TestCodeLocationAndType &location)
{
bool hasBeenModified = modifyFilePath(fileName);
hasBeenModified |= modifyName(location.m_name);
hasBeenModified |= modifyLineAndColumn(location.m_line, location.m_column);
return hasBeenModified;
}
bool TestTreeItem::modifyGTestSetContent(const QString &fileName, const QString &referencingFile,
const TestCodeLocationAndType &location)
{
bool hasBeenModified = modifyFilePath(fileName);
if (m_referencingFile != referencingFile) {
m_referencingFile = referencingFile;
hasBeenModified = true;
}
hasBeenModified |= modifyLineAndColumn(location.m_line, location.m_column);
if (m_state != location.m_state) {
m_state = location.m_state;
hasBeenModified = true;
}
return hasBeenModified;
}
bool TestTreeItem::modifyLineAndColumn(unsigned line, unsigned column)
{ {
bool hasBeenModified = false; bool hasBeenModified = false;
if (m_filePath != modified->m_filePath) { if (m_line != line) {
m_filePath = modified->m_filePath; m_line = line;
hasBeenModified = true; hasBeenModified = true;
} }
if (m_name != modified->m_name) { if (m_column != column) {
m_name = modified->m_name; m_column = column;
hasBeenModified = true; hasBeenModified = true;
} }
if (m_line != modified->m_line) {
m_line = modified->m_line;
hasBeenModified = true;
}
if (m_mainFile != modified->m_mainFile) {
m_mainFile = modified->m_mainFile;
hasBeenModified = true;
}
if (m_referencingFile != modified->m_referencingFile) {
m_referencingFile = modified->m_referencingFile;
hasBeenModified = true;
}
if (m_type != modified->m_type) {
m_type = modified->m_type;
hasBeenModified = true;
}
if (m_state != modified->m_state) {
m_state = modified->m_state;
hasBeenModified = true;
}
if (m_markedForRemoval != modified->m_markedForRemoval)
m_markedForRemoval = modified->m_markedForRemoval;
return hasBeenModified; return hasBeenModified;
} }
@@ -268,6 +263,38 @@ TestTreeItem *TestTreeItem::childItem(int row) const
return static_cast<TestTreeItem *>(child(row)); return static_cast<TestTreeItem *>(child(row));
} }
TestTreeItem *TestTreeItem::findChildByName(const QString &name)
{
return findChildBy([name](const TestTreeItem *other) -> bool {
return other->name() == name;
});
}
TestTreeItem *TestTreeItem::findChildByFiles(const QString &filePath,
const QString &referencingFile)
{
return findChildBy([filePath, referencingFile](const TestTreeItem *other) -> bool {
return other->filePath() == filePath && other->referencingFile() == referencingFile;
});
}
TestTreeItem *TestTreeItem::findChildByNameAndFile(const QString &name, const QString &filePath)
{
return findChildBy([name, filePath](const TestTreeItem *other) -> bool {
return other->filePath() == filePath && other->name() == name;
});
}
TestTreeItem *TestTreeItem::findChildByNameTypeAndFile(const QString &name, TestTreeItem::Type type,
const QString &referencingFile)
{
return findChildBy([name, type, referencingFile](const TestTreeItem *other) -> bool {
return other->referencingFile() == referencingFile
&& other->name() == name
&& other->type() == type;
});
}
void TestTreeItem::revalidateCheckState() void TestTreeItem::revalidateCheckState()
{ {
if (childCount() == 0) if (childCount() == 0)
@@ -294,5 +321,33 @@ void TestTreeItem::revalidateCheckState()
m_checked = (foundUnchecked ? Qt::Unchecked : Qt::Checked); m_checked = (foundUnchecked ? Qt::Unchecked : Qt::Checked);
} }
inline bool TestTreeItem::modifyFilePath(const QString &filePath)
{
if (m_filePath != filePath) {
m_filePath = filePath;
return true;
}
return false;
}
inline bool TestTreeItem::modifyName(const QString &name)
{
if (m_name != name) {
m_name = name;
return true;
}
return false;
}
TestTreeItem *TestTreeItem::findChildBy(CompareFunction compare)
{
for (int row = 0, count = childCount(); row < count; ++row) {
TestTreeItem *child = childItem(row);
if (compare(child))
return child;
}
return 0;
}
} // namespace Internal } // namespace Internal
} // namespace Autotest } // namespace Autotest

View File

@@ -44,6 +44,8 @@ namespace {
namespace Autotest { namespace Autotest {
namespace Internal { namespace Internal {
struct TestCodeLocationAndType;
class TestTreeItem : public Utils::TreeItem class TestTreeItem : public Utils::TreeItem
{ {
@@ -72,15 +74,20 @@ public:
TestTreeItem(const QString &name = QString(), const QString &filePath = QString(), TestTreeItem(const QString &name = QString(), const QString &filePath = QString(),
Type type = Root); Type type = Root);
virtual ~TestTreeItem();
TestTreeItem(const TestTreeItem& other);
virtual QVariant data(int column, int role) const override; virtual QVariant data(int column, int role) const override;
virtual bool setData(int column, const QVariant &data, int role) override; virtual bool setData(int column, const QVariant &data, int role) override;
bool modifyContent(const TestTreeItem *modified); bool modifyTestCaseContent(const QString &name, unsigned line, unsigned column);
bool modifyTestFunctionContent(const TestCodeLocationAndType &location);
bool modifyDataTagContent(const QString &fileName, const TestCodeLocationAndType &location);
bool modifyGTestSetContent(const QString &fileName, const QString &referencingFile,
const TestCodeLocationAndType &location);
bool modifyLineAndColumn(unsigned line, unsigned column);
const QString name() const { return m_name; } const QString name() const { return m_name; }
void setName(const QString &name) { m_name = name; }
const QString filePath() const { return m_filePath; } const QString filePath() const { return m_filePath; }
void setFilePath(const QString &filePath) { m_filePath = filePath; }
void setLine(unsigned line) { m_line = line;} void setLine(unsigned line) { m_line = line;}
unsigned line() const { return m_line; } unsigned line() const { return m_line; }
void setColumn(unsigned column) { m_column = column; } void setColumn(unsigned column) { m_column = column; }
@@ -100,8 +107,19 @@ public:
TestTreeItem *parentItem() const; TestTreeItem *parentItem() const;
TestTreeItem *childItem(int row) const; TestTreeItem *childItem(int row) const;
TestTreeItem *findChildByName(const QString &name);
TestTreeItem *findChildByFiles(const QString &filePath, const QString &referencingFile);
TestTreeItem *findChildByNameAndFile(const QString &name, const QString &filePath);
TestTreeItem *findChildByNameTypeAndFile(const QString &name,
TestTreeItem::Type type, const QString &referencingFile);
private: private:
void revalidateCheckState(); void revalidateCheckState();
bool modifyFilePath(const QString &filePath);
bool modifyName(const QString &name);
typedef std::function<bool(const TestTreeItem *)> CompareFunction;
TestTreeItem *findChildBy(CompareFunction compare);
QString m_name; QString m_name;
QString m_filePath; QString m_filePath;

View File

@@ -655,136 +655,90 @@ QMap<QString, QString> TestTreeModel::referencingFiles() const
return finder.referencingFiles(); return finder.referencingFiles();
} }
TestTreeItem *TestTreeModel::findTestTreeItemByContent(TestTreeItem *item, TestTreeItem *parent, static TestTreeItem *constructDataTagTestTreeItem(const QString &fileName,
Type type) const TestCodeLocationAndType &location)
{ {
for (int row = 0, count = parent->childCount(); row < count; ++row) { TestTreeItem *tagTreeItem = new TestTreeItem(location.m_name, fileName, location.m_type);
TestTreeItem *current = parent->childItem(row); tagTreeItem->setLine(location.m_line);
if (current->name() != item->name()) tagTreeItem->setColumn(location.m_column);
continue; tagTreeItem->setState(location.m_state);
return tagTreeItem;
switch (type) {
case AutoTest:
if (current->filePath() == item->filePath())
return current;
break;
case QuickTest:
if (current->filePath() == item->filePath() && current->mainFile() == item->mainFile())
return current;
break;
case GoogleTest:
if (current->type() == item->type())
return current;
break;
case Invalid:
break;
}
}
return 0;
} }
void TestTreeModel::addTestTreeItem(TestTreeItem *item, Type type) static TestTreeItem *constructUnnamedQuickFunctionTestTreeItem(const QString &functionName,
const QString &referencingFile,
const TestCodeLocationAndType &location)
{ {
TestTreeItem *parent = rootItemForType(type); TestTreeItem *treeItem = new TestTreeItem(functionName, location.m_name, location.m_type);
TestTreeItem *toBeUpdated = findTestTreeItemByContent(item, parent, type); treeItem->setLine(location.m_line);
const int count = item->childCount(); treeItem->setColumn(location.m_column);
if (toBeUpdated) { treeItem->setMainFile(referencingFile); // FIXME: can be handled by referencingFile
if (!toBeUpdated->markedForRemoval()) { treeItem->setReferencingFile(referencingFile);
for (int row = 0; row < count; ++row) return treeItem;
toBeUpdated->appendChild(new TestTreeItem(*item->childItem(row)));
} else {
for (int childRow = count - 1; childRow >= 0; --childRow) {
TestTreeItem *childItem = item->childItem(childRow);
TestTreeItem *origChild = findTestTreeItemByContent(childItem, toBeUpdated, type);
if (origChild) {
QModelIndex toBeModifiedIndex = indexForItem(origChild);
modifyTestSubtree(toBeModifiedIndex, childItem);
} else {
toBeUpdated->insertChild(qMin(count, toBeUpdated->childCount()),
new TestTreeItem(*childItem));
}
}
}
delete item;
} else {
parent->appendChild(item);
}
emit testTreeModelChanged();
} }
static TestTreeItem *constructTestTreeItem(const QString &fileName, static TestTreeItem *constructFunctionTestTreeItem(const QString &funcName,
const QString &referencingFile, const TestCodeLocationAndType &location,
const QString &testCaseName, const TestCodeLocationList &dataTags)
int line, int column, {
const QMap<QString, TestCodeLocationAndType> &functions, TestTreeItem *treeItem = new TestTreeItem(funcName, location.m_name, location.m_type);
const QMap<QString, TestCodeLocationList> dataTags = QMap<QString, TestCodeLocationList>()) treeItem->setLine(location.m_line);
treeItem->setColumn(location.m_column);
treeItem->setState(location.m_state);
// if there are any data tags for this function add them
foreach (const TestCodeLocationAndType &tagLocation, dataTags)
treeItem->appendChild(constructDataTagTestTreeItem(location.m_name, tagLocation));
return treeItem;
}
static TestTreeItem *constructTestTreeItem(const TestParseResult &result)
{ {
TestTreeItem *treeItem; TestTreeItem *treeItem;
if (testCaseName.isEmpty()) { // unnamed Quick Test if (result.testCaseName.isEmpty()) { // unnamed Quick Test
treeItem = new TestTreeItem(QString(), QString(), TestTreeItem::TestClass); treeItem = new TestTreeItem(QString(), QString(), TestTreeItem::TestClass);
foreach (const QString &functionName, functions.keys()) { foreach (const QString &functionName, result.functions.keys()) {
const TestCodeLocationAndType locationAndType = functions.value(functionName); treeItem->appendChild(constructUnnamedQuickFunctionTestTreeItem(
TestTreeItem *testFunction = new TestTreeItem(functionName, locationAndType.m_name, functionName, result.referencingFile, result.functions.value(functionName)));
locationAndType.m_type);
testFunction->setLine(locationAndType.m_line);
testFunction->setColumn(locationAndType.m_column);
testFunction->setMainFile(referencingFile); // FIXME: can be handled by referencingFile
testFunction->setReferencingFile(referencingFile);
treeItem->appendChild(testFunction);
} }
} else { } else {
treeItem = new TestTreeItem(testCaseName, fileName, TestTreeItem::TestClass); treeItem = new TestTreeItem(result.testCaseName, result.fileName, TestTreeItem::TestClass);
treeItem->setMainFile(referencingFile); // FIXME: can be handled by referencingFile treeItem->setMainFile(result.referencingFile); // FIXME: can be handled by referencingFile
treeItem->setReferencingFile(referencingFile); treeItem->setReferencingFile(result.referencingFile);
treeItem->setLine(line); treeItem->setLine(result.line);
treeItem->setColumn(column); treeItem->setColumn(result.column);
foreach (const QString &functionName, functions.keys()) { foreach (const QString &functionName, result.functions.keys()) {
const TestCodeLocationAndType locationAndType = functions.value(functionName); const TestCodeLocationAndType locationAndType = result.functions.value(functionName);
TestTreeItem *treeItemChild = new TestTreeItem(functionName, locationAndType.m_name, const QString qualifiedName = result.testCaseName + QLatin1String("::") + functionName;
locationAndType.m_type); treeItem->appendChild(
treeItemChild->setLine(locationAndType.m_line); constructFunctionTestTreeItem(functionName, locationAndType,
treeItemChild->setColumn(locationAndType.m_column); result.dataTagsOrTestSets.value(qualifiedName)));
treeItemChild->setState(locationAndType.m_state);
// check for data tags and if there are any for this function add them
const QString qualifiedFunctionName = testCaseName + QLatin1String("::") + functionName;
if (dataTags.contains(qualifiedFunctionName)) {
const TestCodeLocationList &tags = dataTags.value(qualifiedFunctionName);
foreach (const TestCodeLocationAndType &tagLocation, tags) {
TestTreeItem *tagTreeItem = new TestTreeItem(tagLocation.m_name,
locationAndType.m_name,
tagLocation.m_type);
tagTreeItem->setLine(tagLocation.m_line);
tagTreeItem->setColumn(tagLocation.m_column);
tagTreeItem->setState(tagLocation.m_state);
treeItemChild->appendChild(tagTreeItem);
}
}
treeItem->appendChild(treeItemChild);
} }
} }
return treeItem; return treeItem;
} }
static TestTreeItem *constructGTestTreeItem(const QString &filePath, const QString &testCaseName, static TestTreeItem *constructGTestSetTreeItem(const QString &filePath,
const bool parameterized, const QString &referencingFile,
const QString &referencingFile, const TestCodeLocationAndType &location)
const TestCodeLocationList &testSets)
{ {
TestTreeItem *item = new TestTreeItem(testCaseName, QString(), TestTreeItem *treeItem = new TestTreeItem(location.m_name, filePath, location.m_type);
parameterized ? TestTreeItem::GTestCaseParameterized treeItem->setState(location.m_state);
: TestTreeItem::GTestCase); treeItem->setLine(location.m_line);
foreach (const TestCodeLocationAndType &locationAndType, testSets) { treeItem->setColumn(location.m_column);
TestTreeItem *treeItemChild = new TestTreeItem(locationAndType.m_name, filePath, treeItem->setMainFile(referencingFile);
locationAndType.m_type); return treeItem;
treeItemChild->setState(locationAndType.m_state); }
treeItemChild->setLine(locationAndType.m_line);
treeItemChild->setColumn(locationAndType.m_column); static TestTreeItem *constructGTestTreeItem(const TestParseResult &result)
treeItemChild->setMainFile(referencingFile); {
item->appendChild(treeItemChild); TestTreeItem *item = new TestTreeItem(result.testCaseName, QString(),
} result.parameterized ? TestTreeItem::GTestCaseParameterized
item->setReferencingFile(referencingFile); : TestTreeItem::GTestCase);
item->setReferencingFile(result.referencingFile);
foreach (const TestCodeLocationAndType &location, result.dataTagsOrTestSets.first())
item->appendChild(constructGTestSetTreeItem(result.fileName, result.referencingFile, location));
return item; return item;
} }
@@ -792,17 +746,17 @@ void TestTreeModel::onParseResultReady(TestParseResult result)
{ {
switch (result.type) { switch (result.type) {
case AutoTest: case AutoTest:
handleParseResult(result);
case QuickTest: case QuickTest:
addTestTreeItem(constructTestTreeItem(result.fileName, result.referencingFile, if (result.testCaseName.isEmpty()) {
result.testCaseName, result.line, result.column, handleUnnamedQuickParseResult(result);
result.functions, result.dataTagsOrTestSets), break;
result.type); }
handleParseResult(result);
break; break;
case GoogleTest: case GoogleTest:
QTC_ASSERT(result.dataTagsOrTestSets.size() == 1, return); QTC_ASSERT(result.dataTagsOrTestSets.size() == 1, return);
addTestTreeItem(constructGTestTreeItem(result.fileName, result.testCaseName, handleGTestParseResult(result);
result.parameterized, result.referencingFile,
result.dataTagsOrTestSets.first()), result.type);
break; break;
case Invalid: case Invalid:
QTC_ASSERT(false, qWarning("TestParseResult of type Invalid unexpected.")); QTC_ASSERT(false, qWarning("TestParseResult of type Invalid unexpected."));
@@ -810,6 +764,114 @@ void TestTreeModel::onParseResultReady(TestParseResult result)
} }
} }
void TestTreeModel::handleParseResult(const TestParseResult &result)
{
TestTreeItem *root;
switch (result.type) {
case AutoTest:
root = m_autoTestRootItem;
break;
case QuickTest:
root = m_quickTestRootItem;
break;
default:
QTC_ASSERT(false, return); // should never happen, just to avoid warning
}
TestTreeItem *toBeModified = root->findChildByFiles(result.fileName, result.referencingFile);
// if there's no matching item, add the new one
if (!toBeModified) {
root->appendChild(constructTestTreeItem(result));
return;
}
// else we have to check level by level.. first the current level...
bool changed = toBeModified->modifyTestCaseContent(result.testCaseName, result.line,
result.column);
toBeModified->markForRemoval(false);
if (changed)
emit dataChanged(indexForItem(toBeModified), indexForItem(toBeModified));
// ...now the functions
foreach (const QString &func, result.functions.keys()) {
TestTreeItem *functionItem = toBeModified->findChildByName(func);
// if there's no function matching, add the new one
if (!functionItem) {
const QString qualifiedName = result.testCaseName + QLatin1String("::") + func;
toBeModified->appendChild(
constructFunctionTestTreeItem(func, result.functions.value(func),
result.dataTagsOrTestSets.value(qualifiedName)));
continue;
}
// else we have to check level by level.. first the current level...
changed = functionItem->modifyTestFunctionContent(result.functions.value(func));
functionItem->markForRemoval(false);
if (changed)
emit dataChanged(indexForItem(functionItem), indexForItem(functionItem));
// ...now the data tags
const QString &funcFileName = result.functions.value(func).m_name;
const QString qualifiedFunctionName = result.testCaseName + QLatin1String("::") + func;
foreach (const TestCodeLocationAndType &location, result.dataTagsOrTestSets.value(qualifiedFunctionName)) {
TestTreeItem *dataTagItem = functionItem->findChildByName(location.m_name);
if (!dataTagItem) {
functionItem->appendChild(constructDataTagTestTreeItem(funcFileName, location));
continue;
}
changed = dataTagItem->modifyDataTagContent(funcFileName, location);
dataTagItem->markForRemoval(false);
if (changed)
emit dataChanged(indexForItem(dataTagItem), indexForItem(dataTagItem));
}
}
}
void TestTreeModel::handleUnnamedQuickParseResult(const TestParseResult &result)
{
TestTreeItem *toBeModified = unnamedQuickTests();
if (!toBeModified) {
m_quickTestRootItem->appendChild(constructTestTreeItem(result));
return;
}
// if we have already Unnamed Quick tests we might update them..
foreach (const QString &func, result.functions.keys()) {
const TestCodeLocationAndType &location = result.functions.value(func);
TestTreeItem *functionItem = toBeModified->findChildByNameAndFile(func, location.m_name);
if (!functionItem) {
toBeModified->appendChild(
constructUnnamedQuickFunctionTestTreeItem(func, result.referencingFile,
location));
continue;
}
functionItem->modifyLineAndColumn(result.line, result.column);
functionItem->markForRemoval(false);
}
}
void TestTreeModel::handleGTestParseResult(const TestParseResult &result)
{
TestTreeItem::Type type = result.parameterized ? TestTreeItem::GTestCaseParameterized
: TestTreeItem::GTestCase;
TestTreeItem *toBeModified = m_googleTestRootItem->findChildByNameTypeAndFile(
result.testCaseName, type, result.referencingFile);
if (!toBeModified) {
m_googleTestRootItem->appendChild(constructGTestTreeItem(result));
return;
}
// if found nothing has to be updated as all relevant members are used to find the item
foreach (const TestCodeLocationAndType &location , result.dataTagsOrTestSets.first()) {
TestTreeItem *testSetItem = toBeModified->findChildByNameAndFile(location.m_name,
result.fileName);
if (!testSetItem) {
toBeModified->appendChild(constructGTestSetTreeItem(result.fileName,
result.referencingFile, location));
continue;
}
bool changed = testSetItem->modifyGTestSetContent(result.fileName,
result.referencingFile, location);
testSetItem->markForRemoval(false);
if (changed)
emit dataChanged(indexForItem(testSetItem), indexForItem(testSetItem));
}
}
void TestTreeModel::removeAllTestItems() void TestTreeModel::removeAllTestItems()
{ {
m_autoTestRootItem->removeChildren(); m_autoTestRootItem->removeChildren();
@@ -833,84 +895,6 @@ TestTreeItem *TestTreeModel::rootItemForType(TestTreeModel::Type type)
QTC_ASSERT(false, return 0); QTC_ASSERT(false, return 0);
} }
void TestTreeModel::modifyTestSubtree(QModelIndex &toBeModifiedIndex, const TestTreeItem *newItem)
{
if (!toBeModifiedIndex.isValid())
return;
TestTreeItem *toBeModifiedItem = static_cast<TestTreeItem *>(itemForIndex(toBeModifiedIndex));
if (toBeModifiedItem->modifyContent(newItem))
emit dataChanged(toBeModifiedIndex, toBeModifiedIndex,
QVector<int>() << Qt::DisplayRole << Qt::ToolTipRole << LinkRole);
// process sub-items as well...
const int childCount = toBeModifiedItem->childCount();
const int newChildCount = newItem->childCount();
// for keeping the CheckState on modifications
// TODO might still fail for duplicate entries
QHash<QString, Qt::CheckState> checkStates;
for (int row = 0; row < childCount; ++row) {
const TestTreeItem *child = toBeModifiedItem->childItem(row);
checkStates.insert(child->name(), child->checked());
}
if (childCount <= newChildCount) {
processChildren(toBeModifiedIndex, newItem, childCount, checkStates);
// add additional items
for (int row = childCount; row < newChildCount; ++row) {
const TestTreeItem *newChild = newItem->childItem(row);
TestTreeItem *toBeAdded = new TestTreeItem(*newChild);
if (checkStates.contains(toBeAdded->name())
&& checkStates.value(toBeAdded->name()) != Qt::Checked)
toBeAdded->setChecked(checkStates.value(toBeAdded->name()));
toBeModifiedItem->appendChild(toBeAdded);
}
} else {
processChildren(toBeModifiedIndex, newItem, newChildCount, checkStates);
// remove rest of the items
for (int row = childCount - 1; row > newChildCount; --row)
delete takeItem(toBeModifiedItem->childItem(row));
}
emit testTreeModelChanged();
}
void TestTreeModel::processChildren(QModelIndex &parentIndex, const TestTreeItem *newItem,
const int upperBound,
const QHash<QString, Qt::CheckState> &checkStates)
{
static QVector<int> modificationRoles = QVector<int>() << Qt::DisplayRole
<< Qt::ToolTipRole
<< LinkRole;
TestTreeItem *toBeModifiedItem = static_cast<TestTreeItem *>(itemForIndex(parentIndex));
for (int row = 0; row < upperBound; ++row) {
QModelIndex child = parentIndex.child(row, 0);
TestTreeItem *toBeModifiedChild = toBeModifiedItem->childItem(row);
const TestTreeItem *modifiedChild = newItem->childItem(row);
if (toBeModifiedChild->modifyContent(modifiedChild))
emit dataChanged(child, child, modificationRoles);
// handle data tags - just remove old and add them
if (modifiedChild->childCount() || toBeModifiedChild->childCount()) {
toBeModifiedChild->removeChildren();
const int count = modifiedChild->childCount();
for (int childRow = 0; childRow < count; ++childRow)
toBeModifiedChild->appendChild(new TestTreeItem(*modifiedChild->childItem(childRow)));
}
if (checkStates.contains(toBeModifiedChild->name())) {
Qt::CheckState state = checkStates.value(toBeModifiedChild->name());
if (state != toBeModifiedChild->checked()) {
toBeModifiedChild->setChecked(state);
emit dataChanged(child, child, QVector<int>() << Qt::CheckStateRole);
}
} else { // newly added (BAD: happens for renaming as well)
toBeModifiedChild->setChecked(Qt::Checked);
emit dataChanged(child, child, QVector<int>() << Qt::CheckStateRole);
}
}
}
#ifdef WITH_TESTS #ifdef WITH_TESTS
int TestTreeModel::autoTestsCount() const int TestTreeModel::autoTestsCount() const
{ {

View File

@@ -89,21 +89,19 @@ signals:
public slots: public slots:
private: private:
void addTestTreeItem(TestTreeItem *item, Type type);
void onParseResultReady(TestParseResult result); void onParseResultReady(TestParseResult result);
void handleParseResult(const TestParseResult &result);
void handleUnnamedQuickParseResult(const TestParseResult &result);
void handleGTestParseResult(const TestParseResult &result);
void removeAllTestItems(); void removeAllTestItems();
void removeFiles(const QStringList &files); void removeFiles(const QStringList &files);
void markForRemoval(const QString &filePath, Type type); void markForRemoval(const QString &filePath, Type type);
bool sweepChildren(TestTreeItem *item); bool sweepChildren(TestTreeItem *item);
TestTreeItem *findTestTreeItemByContent(TestTreeItem *item, TestTreeItem *parent, Type type);
TestTreeItem *unnamedQuickTests() const; TestTreeItem *unnamedQuickTests() const;
TestTreeItem *rootItemForType(Type type); TestTreeItem *rootItemForType(Type type);
explicit TestTreeModel(QObject *parent = 0); explicit TestTreeModel(QObject *parent = 0);
void modifyTestSubtree(QModelIndex &toBeModifiedIndex, const TestTreeItem *newItem);
void processChildren(QModelIndex &parentIndex, const TestTreeItem *newItem,
const int upperBound, const QHash<QString, Qt::CheckState> &checkStates);
void setupParsingConnections(); void setupParsingConnections();
TestTreeItem *m_autoTestRootItem; TestTreeItem *m_autoTestRootItem;