Skip to content

Commit

Permalink
Android: Add test cases of editable QtAbstractItemModels
Browse files Browse the repository at this point in the history
Task-number: QTBUG-129387
Task-number: QTBUG-130253
Change-Id: I92c5fd57b6fab6b7e50c76c995dacf1976a3332d
Reviewed-by: Assam Boudjelthia <[email protected]>
  • Loading branch information
Soarmin committed Nov 8, 2024
1 parent a065c6b commit 8ec25c9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
import org.qtproject.qt.android.QtAbstractItemModel;
import org.qtproject.qt.android.QtModelIndex;

public class TestQtAbstractItemModel extends QtAbstractItemModel
public class TestQtAbstractItemModel
extends QtAbstractItemModel implements QtAbstractItemModel.OnDataChangedListener
{
int m_rows = 0;
int m_cols = 0;
int m_dataChangedCount = 0;

public TestQtAbstractItemModel()
{
setOnDataChangedListener(this);
}

@Override
public int columnCount(QtModelIndex parent)
Expand Down Expand Up @@ -92,6 +99,18 @@ public void fetchMore(QtModelIndex parent)
endInsertRows();
}

@Override
public boolean setData(QtModelIndex index, Object value, int role)
{
dataChanged(index, index , new int[]{role});
return true;
}

@Override
public void onDataChanged(QtModelIndex topLeft, QtModelIndex bottomRight, int[] roles) {
m_dataChangedCount++;
}

public void addRow()
{
beginInsertRows(new QtModelIndex(), m_rows, m_rows);
Expand Down Expand Up @@ -130,5 +149,6 @@ public void reset()
m_rows = 0;
m_cols = 0;
endResetModel();
m_dataChangedCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
import org.qtproject.qt.android.QtAbstractListModel;
import org.qtproject.qt.android.QtModelIndex;

public class TestQtAbstractListModel extends QtAbstractListModel
public class TestQtAbstractListModel
extends QtAbstractListModel implements QtAbstractListModel.OnDataChangedListener
{
int m_rows = 0;
int m_dataChangedCount = 0;

public TestQtAbstractListModel()
{
setOnDataChangedListener(this);
}

@Override public Object data(QtModelIndex index, int role)
{
Expand Down Expand Up @@ -65,6 +72,18 @@ public class TestQtAbstractListModel extends QtAbstractListModel
endInsertRows();
}

@Override
public boolean setData(QtModelIndex index, Object value, int role)
{
dataChanged(index, index , new int[]{role});
return true;
}

@Override
public void onDataChanged(QtModelIndex topLeft, QtModelIndex bottomRight, int[] roles) {
m_dataChangedCount++;
}

public void addRow()
{
beginInsertRows(new QtModelIndex(), m_rows, m_rows);
Expand All @@ -86,5 +105,6 @@ public void reset()
beginResetModel();
m_rows = 0;
endResetModel();
m_dataChangedCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QtCore/qjniobject.h>
#include <QtCore/qjnitypes.h>
#include <QtCore/qstring.h>
#include <QSignalSpy>

using namespace Qt::Literals;

Expand Down Expand Up @@ -39,6 +40,8 @@ private slots:
void fetchMore();
void hasIndex();
void data();
void setData_data();
void setData();
};

void tst_AndroidItemModel::initTestCase_data()
Expand Down Expand Up @@ -204,6 +207,55 @@ void tst_AndroidItemModel::data()
}
}

void tst_AndroidItemModel::setData_data()
{
QTest::addColumn<int>("row");
QTest::addColumn<int>("column");
QTest::addColumn<int>("role");

QTest::newRow("role0") << 0 << 0 << 0;
QTest::newRow("role1") << 0 << 0 << 1;
QTest::newRow("role2") << 0 << 0 << 2;
}

void tst_AndroidItemModel::setData()
{
QFETCH_GLOBAL(bool, isList);
QFETCH(int, row);
QFETCH(int, column);
QFETCH(int, role);

jboolean success = false;
QSignalSpy spy(
qProxy,
SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &, const QList<int> &)));

jModel.callMethod<void>("addRow");
if (!isList)
jModel.callMethod<void>("addCol");

QCOMPARE_EQ(qProxy->rowCount(), 1);
QCOMPARE_EQ(qProxy->columnCount(), 1);

JQtModelIndex index = jModel.callMethod<JQtModelIndex>("index", row, column, JQtModelIndex());
QVERIFY(jModel.callMethod<jboolean>("setData", index, QJniObject(Void()), role));
QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 1, 50);

const QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(arguments.size(), 3);

const auto topLeft = qvariant_cast<QModelIndex>(arguments.at(0));
const auto bottomRight = qvariant_cast<QModelIndex>(arguments.at(1));
const auto roles = qvariant_cast<QList<int>>(arguments.at(2));

QCOMPARE(topLeft, qProxy->index(row, column, QModelIndex()));
QCOMPARE(bottomRight, qProxy->index(row, column, QModelIndex()));
QCOMPARE(roles, QList<int>{role});

jint jDataChangedCount = jModel.getField<jint>("m_dataChangedCount");
QTRY_COMPARE_WITH_TIMEOUT(jDataChangedCount, 1, 50);
}

void tst_AndroidItemModel::resetModel()
{
jModel.callMethod<void>("reset");
Expand Down

0 comments on commit 8ec25c9

Please sign in to comment.