Skip to content

Commit

Permalink
Android: Add tests for QtAbstractListModel
Browse files Browse the repository at this point in the history
Adds test data and conditions for QtAbstractListModel interface

Pick-to: 6.8 6.8.0
Task-number: QTBUG-125974
Change-Id: I168a5cfabfc191c7ee50aa917d0fd0f9fabc1703
Reviewed-by: Assam Boudjelthia <[email protected]>
  • Loading branch information
Soarmin committed Sep 18, 2024
1 parent b6d7b9e commit ca102dc
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.qtproject.qt.android.QtAbstractItemModel;
import org.qtproject.qt.android.QtModelIndex;

public class TestModel extends QtAbstractItemModel
public class TestQtAbstractItemModel extends QtAbstractItemModel
{
int m_rows = 0;
int m_cols = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

package org.qtproject.qt.android.tests;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.qtproject.qt.android.QtAbstractListModel;
import org.qtproject.qt.android.QtModelIndex;

public class TestQtAbstractListModel extends QtAbstractListModel
{
int m_rows = 0;

@Override public Object data(QtModelIndex index, int role)
{
int r = index.row();
int c = index.column();
if (r < 0 || c < 0 || c > 1 || r > m_rows)
return null;

switch (role) {
case 0:
return String.format("r%d/c%d", r, c);
case 1:
return new Boolean(((r + c) % 2) == 0);
case 2:
return new Integer((c << 8) + r);
case 3:
return new Double((r + 1.0) / (c + 1.0));
case 4:
return new Long((c << 8) * (r << 8));
default:
return null;
}
}

@Override public int rowCount(QtModelIndex parent) { return parent.isValid() ? 0 : m_rows; }

@Override public HashMap<Integer, String> roleNames()
{
final HashMap<Integer, String> roles = new HashMap<Integer, String>();
roles.put(0, "stringRole");
roles.put(1, "booleanRole");
roles.put(2, "integerRole");
roles.put(3, "doubleRole");
roles.put(4, "longRole");
return roles;
}

@Override public boolean canFetchMore(QtModelIndex parent)
{
return !parent.isValid() && (m_rows < 30);
}

@Override public void fetchMore(QtModelIndex parent)
{
if (!canFetchMore(parent))
return;
int toAdd = Math.min(10, 30 - rowCount(parent));
beginInsertRows(new QtModelIndex(), m_rows, m_rows + toAdd - 1);
m_rows += toAdd;
endInsertRows();
}

public void addRow()
{
beginInsertRows(new QtModelIndex(), m_rows, m_rows);
m_rows++;
endInsertRows();
}

public void removeRow()
{
if (m_rows == 0)
return;
beginRemoveRows(new QtModelIndex(), 0, 0);
m_rows--;
endRemoveRows();
}

public void reset()
{
beginResetModel();
m_rows = 0;
endResetModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@

using namespace Qt::Literals;

Q_DECLARE_JNI_CLASS(JTestModel, "org/qtproject/qt/android/tests/TestModel")
Q_DECLARE_JNI_CLASS(TestQtAbstractItemModel,
"org/qtproject/qt/android/tests/TestQtAbstractItemModel")
Q_DECLARE_JNI_CLASS(TestQtAbstractListModel,
"org/qtproject/qt/android/tests/TestQtAbstractListModel")

class tst_AndroidItemModel : public QObject
{
Q_OBJECT
JTestModel jModel;
QJniObject jModel;
QAbstractItemModel *qProxy;
void resetModel();

private slots:
void initTestCase();
void initTestCase_data();
void init();
void cleanup();
void addRow();
void addColumn();
Expand All @@ -37,8 +41,21 @@ private slots:
void data();
};

void tst_AndroidItemModel::initTestCase()
void tst_AndroidItemModel::initTestCase_data()
{
QTest::addColumn<QJniObject>("JavaModel");
QTest::addColumn<int>("columnCount");
QTest::addColumn<bool>("isList");
QTest::newRow("TestItemModel")
<< QJniObject::construct<QtJniTypes::TestQtAbstractItemModel>() << 3 << false;
QTest::newRow("TestListModel")
<< QJniObject::construct<QtJniTypes::TestQtAbstractListModel>() << 1 << true;
}

void tst_AndroidItemModel::init()
{
QFETCH_GLOBAL(QJniObject, JavaModel);
jModel = JavaModel;
QVERIFY(jModel.isValid());
qProxy = QAndroidItemModelProxy::createNativeProxy(jModel);
QVERIFY(qProxy);
Expand All @@ -58,6 +75,10 @@ void tst_AndroidItemModel::addRow()

void tst_AndroidItemModel::addColumn()
{
QFETCH_GLOBAL(bool, isList);
if (isList)
QSKIP("This test function requires a two-dimensional model.");

const int columnsBefore = qProxy->columnCount();
jModel.callMethod<void>("addCol");
QCOMPARE_EQ(qProxy->columnCount(), columnsBefore + 1);
Expand All @@ -76,6 +97,10 @@ void tst_AndroidItemModel::removeRow()

void tst_AndroidItemModel::removeColumn()
{
QFETCH_GLOBAL(bool, isList);
if (isList)
QSKIP("This test function requires a two-dimensional model.");

jModel.callMethod<void>("addCol");
jModel.callMethod<void>("addCol");
QCOMPARE_EQ(qProxy->columnCount(), 2);
Expand Down Expand Up @@ -115,13 +140,17 @@ void tst_AndroidItemModel::fetchMore()

void tst_AndroidItemModel::hasIndex()
{
// fetchMore() adds 10 rows
qProxy->fetchMore(QModelIndex());
jModel.callMethod<void>("addCol");
jModel.callMethod<void>("addCol");
QFETCH_GLOBAL(int, columnCount);
QFETCH_GLOBAL(bool, isList);

if (!isList) {
for (int i = 0; i < columnCount; ++i)
jModel.callMethod<void>("addCol");
}

qProxy->fetchMore(QModelIndex());
for (int r = 0; r < 10; ++r) {
for (int c = 0; c < 2; ++c) {
for (int c = 0; c < columnCount; ++c) {
QVERIFY(qProxy->hasIndex(r, c));
}
}
Expand All @@ -134,15 +163,20 @@ void tst_AndroidItemModel::data()
{ 2, QMetaType::Int },
{ 3, QMetaType::Double },
{ 4, QMetaType::Long } };
QFETCH_GLOBAL(int, columnCount);
QFETCH_GLOBAL(bool, isList);

if (!isList) {
for (int i = 0; i < columnCount; ++i)
jModel.callMethod<void>("addCol");
}

QVERIFY(qProxy->canFetchMore(QModelIndex()));
qProxy->fetchMore(QModelIndex());
QCOMPARE_EQ(qProxy->rowCount(), 10);
jModel.callMethod<void>("addCol");
jModel.callMethod<void>("addCol");
jModel.callMethod<void>("addCol");

for (int r = 0; r < 10; ++r) {
for (int c = 0; c < 3; ++c) {
for (int c = 0; c < columnCount; ++c) {
QModelIndex index = qProxy->index(r, c);
for (int role : roleToType.keys()) {
const QVariant data = qProxy->data(index, role);
Expand Down Expand Up @@ -174,7 +208,6 @@ void tst_AndroidItemModel::resetModel()
{
jModel.callMethod<void>("reset");
QCOMPARE_EQ(qProxy->rowCount(), 0);
QCOMPARE_EQ(qProxy->columnCount(), 0);
}

#include "tst_androiditemmodel.moc"
Expand Down

0 comments on commit ca102dc

Please sign in to comment.