Android: Make use of ItemDataRole in QtAIM tests

Previously we were using raw zero-based integer values
for roles in QtAbstractItemModel tests.

Roles starting from 0x0 to 0xFF are reserved for pre-defined roles
defined in Qt::ItemDataRole enum.

This change will change the base role value to Qt::UserRole.
It will later help us to use QAbstractItemModelTester as this
tester class expects certain types from pre-defined role values.

Task-number: QTBUG-132880
Change-Id: Ie19739cd7bf49829fe58f5ad53078942d47a433b
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 967d9b8cf200163b25a3c1a3b3d79a107f9073b2)
This commit is contained in:
Soheil Armin 2025-01-20 12:09:27 +02:00
parent abbbfa27bd
commit d25e08afc2
3 changed files with 60 additions and 38 deletions

View File

@ -13,6 +13,13 @@ import org.qtproject.qt.android.QtModelIndex;
public class TestQtAbstractItemModel public class TestQtAbstractItemModel
extends QtAbstractItemModel implements QtAbstractItemModel.OnDataChangedListener extends QtAbstractItemModel implements QtAbstractItemModel.OnDataChangedListener
{ {
static final int QT_USER_ROLE = 0x100;
static final int ROLE_STRING = QT_USER_ROLE;
static final int ROLE_BOOLEAN = QT_USER_ROLE + 1;
static final int ROLE_INTEGER = QT_USER_ROLE + 2;
static final int ROLE_DOUBLE = QT_USER_ROLE + 3;
static final int ROLE_LONG = QT_USER_ROLE + 4;
int m_rows = 0; int m_rows = 0;
int m_cols = 0; int m_cols = 0;
int m_dataChangedCount = 0; int m_dataChangedCount = 0;
@ -37,15 +44,15 @@ public class TestQtAbstractItemModel
return null; return null;
switch (role) { switch (role) {
case 0: case ROLE_STRING:
return String.format("r%d/c%d", r, c); return String.format("r%d/c%d", r, c);
case 1: case ROLE_BOOLEAN:
return new Boolean(((r + c) % 2) == 0); return new Boolean(((r + c) % 2) == 0);
case 2: case ROLE_INTEGER:
return new Integer((c << 8) + r); return new Integer((c << 8) + r);
case 3: case ROLE_DOUBLE:
return new Double((r + 1.0) / (c + 1.0)); return new Double((r + 1.0) / (c + 1.0));
case 4: case ROLE_LONG:
return new Long((c << 8) * (r << 8)); return new Long((c << 8) * (r << 8));
default: default:
return null; return null;
@ -74,11 +81,11 @@ public class TestQtAbstractItemModel
public HashMap<Integer, String> roleNames() public HashMap<Integer, String> roleNames()
{ {
final HashMap<Integer, String> roles = new HashMap<Integer, String>(); final HashMap<Integer, String> roles = new HashMap<Integer, String>();
roles.put(0, "stringRole"); roles.put(ROLE_STRING, "stringRole");
roles.put(1, "booleanRole"); roles.put(ROLE_BOOLEAN, "booleanRole");
roles.put(2, "integerRole"); roles.put(ROLE_INTEGER, "integerRole");
roles.put(3, "doubleRole"); roles.put(ROLE_DOUBLE, "doubleRole");
roles.put(4, "longRole"); roles.put(ROLE_LONG, "longRole");
return roles; return roles;
} }

View File

@ -13,6 +13,13 @@ import org.qtproject.qt.android.QtModelIndex;
public class TestQtAbstractListModel public class TestQtAbstractListModel
extends QtAbstractListModel implements QtAbstractListModel.OnDataChangedListener extends QtAbstractListModel implements QtAbstractListModel.OnDataChangedListener
{ {
static final int QT_USER_ROLE = 0x100;
static final int ROLE_STRING = QT_USER_ROLE;
static final int ROLE_BOOLEAN = QT_USER_ROLE + 1;
static final int ROLE_INTEGER = QT_USER_ROLE + 2;
static final int ROLE_DOUBLE = QT_USER_ROLE + 3;
static final int ROLE_LONG = QT_USER_ROLE + 4;
int m_rows = 0; int m_rows = 0;
int m_dataChangedCount = 0; int m_dataChangedCount = 0;
@ -29,15 +36,15 @@ public class TestQtAbstractListModel
return null; return null;
switch (role) { switch (role) {
case 0: case ROLE_STRING:
return String.format("r%d/c%d", r, c); return String.format("r%d/c%d", r, c);
case 1: case ROLE_BOOLEAN:
return new Boolean(((r + c) % 2) == 0); return new Boolean(((r + c) % 2) == 0);
case 2: case ROLE_INTEGER:
return new Integer((c << 8) + r); return new Integer((c << 8) + r);
case 3: case ROLE_DOUBLE:
return new Double((r + 1.0) / (c + 1.0)); return new Double((r + 1.0) / (c + 1.0));
case 4: case ROLE_LONG:
return new Long((c << 8) * (r << 8)); return new Long((c << 8) * (r << 8));
default: default:
return null; return null;
@ -49,11 +56,11 @@ public class TestQtAbstractListModel
@Override public HashMap<Integer, String> roleNames() @Override public HashMap<Integer, String> roleNames()
{ {
final HashMap<Integer, String> roles = new HashMap<Integer, String>(); final HashMap<Integer, String> roles = new HashMap<Integer, String>();
roles.put(0, "stringRole"); roles.put(ROLE_STRING, "stringRole");
roles.put(1, "booleanRole"); roles.put(ROLE_BOOLEAN, "booleanRole");
roles.put(2, "integerRole"); roles.put(ROLE_INTEGER, "integerRole");
roles.put(3, "doubleRole"); roles.put(ROLE_DOUBLE, "doubleRole");
roles.put(4, "longRole"); roles.put(ROLE_LONG, "longRole");
return roles; return roles;
} }

View File

@ -28,6 +28,14 @@ class tst_AndroidItemModel : public QObject
QAbstractItemModel *qProxy; QAbstractItemModel *qProxy;
void resetModel(); void resetModel();
enum DataRole{
ROLE_STRING = Qt::UserRole,
ROLE_BOOLEAN,
ROLE_INTEGER,
ROLE_DOUBLE,
ROLE_LONG
};
private slots: private slots:
void initTestCase_data(); void initTestCase_data();
void init(); void init();
@ -115,11 +123,11 @@ void tst_AndroidItemModel::removeColumn()
void tst_AndroidItemModel::roleNames() void tst_AndroidItemModel::roleNames()
{ {
const static QHash<int, QByteArray> expectedRoles = { { 0, "stringRole" }, const static QHash<int, QByteArray> expectedRoles = { { ROLE_STRING, "stringRole" },
{ 1, "booleanRole" }, { ROLE_BOOLEAN, "booleanRole" },
{ 2, "integerRole" }, { ROLE_INTEGER, "integerRole" },
{ 3, "doubleRole" }, { ROLE_DOUBLE, "doubleRole" },
{ 4, "longRole" } }; { ROLE_LONG, "longRole" } };
QCOMPARE(qProxy->roleNames(), expectedRoles); QCOMPARE(qProxy->roleNames(), expectedRoles);
} }
@ -161,11 +169,11 @@ void tst_AndroidItemModel::hasIndex()
void tst_AndroidItemModel::data() void tst_AndroidItemModel::data()
{ {
const static QHash<int, QMetaType::Type> roleToType = { { 0, QMetaType::QString }, const static QHash<int, QMetaType::Type> roleToType = { { ROLE_STRING, QMetaType::QString },
{ 1, QMetaType::Bool }, { ROLE_BOOLEAN, QMetaType::Bool },
{ 2, QMetaType::Int }, { ROLE_INTEGER, QMetaType::Int },
{ 3, QMetaType::Double }, { ROLE_DOUBLE, QMetaType::Double },
{ 4, QMetaType::Long } }; { ROLE_LONG, QMetaType::Long } };
QFETCH_GLOBAL(int, columnCount); QFETCH_GLOBAL(int, columnCount);
QFETCH_GLOBAL(bool, isList); QFETCH_GLOBAL(bool, isList);
@ -185,20 +193,20 @@ void tst_AndroidItemModel::data()
const QVariant data = qProxy->data(index, role); const QVariant data = qProxy->data(index, role);
QCOMPARE_EQ(data.typeId(), roleToType[role]); QCOMPARE_EQ(data.typeId(), roleToType[role]);
switch (role) { switch (role) {
case 0: case ROLE_STRING:
QCOMPARE(data.toString(), QCOMPARE(data.toString(),
"r%1/c%2"_L1.arg(QString::number(r), QString::number(c))); "r%1/c%2"_L1.arg(QString::number(r), QString::number(c)));
break; break;
case 1: case ROLE_BOOLEAN:
QCOMPARE(data.toBool(), ((r + c) % 2) == 0); QCOMPARE(data.toBool(), ((r + c) % 2) == 0);
break; break;
case 2: case ROLE_INTEGER:
QCOMPARE(data.toInt(), (c << 8) + r); QCOMPARE(data.toInt(), (c << 8) + r);
break; break;
case 3: case ROLE_DOUBLE:
QVERIFY(qFuzzyCompare(data.toDouble(), (1.0 + r) / (1.0 + c))); QVERIFY(qFuzzyCompare(data.toDouble(), (1.0 + r) / (1.0 + c)));
break; break;
case 4: case ROLE_LONG:
QCOMPARE(data.toULongLong(), ((c << 8) * (r << 8))); QCOMPARE(data.toULongLong(), ((c << 8) * (r << 8)));
break; break;
} }
@ -213,9 +221,9 @@ void tst_AndroidItemModel::setData_data()
QTest::addColumn<int>("column"); QTest::addColumn<int>("column");
QTest::addColumn<int>("role"); QTest::addColumn<int>("role");
QTest::newRow("role0") << 0 << 0 << 0; QTest::newRow("role0") << 0 << 0 << int(ROLE_STRING);
QTest::newRow("role1") << 0 << 0 << 1; QTest::newRow("role1") << 0 << 0 << int(ROLE_BOOLEAN);
QTest::newRow("role2") << 0 << 0 << 2; QTest::newRow("role2") << 0 << 0 << int(ROLE_INTEGER);
} }
void tst_AndroidItemModel::setData() void tst_AndroidItemModel::setData()