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:
parent
abbbfa27bd
commit
d25e08afc2
@ -13,6 +13,13 @@ import org.qtproject.qt.android.QtModelIndex;
|
||||
public class TestQtAbstractItemModel
|
||||
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_cols = 0;
|
||||
int m_dataChangedCount = 0;
|
||||
@ -37,15 +44,15 @@ public class TestQtAbstractItemModel
|
||||
return null;
|
||||
|
||||
switch (role) {
|
||||
case 0:
|
||||
case ROLE_STRING:
|
||||
return String.format("r%d/c%d", r, c);
|
||||
case 1:
|
||||
case ROLE_BOOLEAN:
|
||||
return new Boolean(((r + c) % 2) == 0);
|
||||
case 2:
|
||||
case ROLE_INTEGER:
|
||||
return new Integer((c << 8) + r);
|
||||
case 3:
|
||||
case ROLE_DOUBLE:
|
||||
return new Double((r + 1.0) / (c + 1.0));
|
||||
case 4:
|
||||
case ROLE_LONG:
|
||||
return new Long((c << 8) * (r << 8));
|
||||
default:
|
||||
return null;
|
||||
@ -74,11 +81,11 @@ public class TestQtAbstractItemModel
|
||||
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");
|
||||
roles.put(ROLE_STRING, "stringRole");
|
||||
roles.put(ROLE_BOOLEAN, "booleanRole");
|
||||
roles.put(ROLE_INTEGER, "integerRole");
|
||||
roles.put(ROLE_DOUBLE, "doubleRole");
|
||||
roles.put(ROLE_LONG, "longRole");
|
||||
return roles;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,13 @@ import org.qtproject.qt.android.QtModelIndex;
|
||||
public class TestQtAbstractListModel
|
||||
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_dataChangedCount = 0;
|
||||
|
||||
@ -29,15 +36,15 @@ public class TestQtAbstractListModel
|
||||
return null;
|
||||
|
||||
switch (role) {
|
||||
case 0:
|
||||
case ROLE_STRING:
|
||||
return String.format("r%d/c%d", r, c);
|
||||
case 1:
|
||||
case ROLE_BOOLEAN:
|
||||
return new Boolean(((r + c) % 2) == 0);
|
||||
case 2:
|
||||
case ROLE_INTEGER:
|
||||
return new Integer((c << 8) + r);
|
||||
case 3:
|
||||
case ROLE_DOUBLE:
|
||||
return new Double((r + 1.0) / (c + 1.0));
|
||||
case 4:
|
||||
case ROLE_LONG:
|
||||
return new Long((c << 8) * (r << 8));
|
||||
default:
|
||||
return null;
|
||||
@ -49,11 +56,11 @@ public class TestQtAbstractListModel
|
||||
@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");
|
||||
roles.put(ROLE_STRING, "stringRole");
|
||||
roles.put(ROLE_BOOLEAN, "booleanRole");
|
||||
roles.put(ROLE_INTEGER, "integerRole");
|
||||
roles.put(ROLE_DOUBLE, "doubleRole");
|
||||
roles.put(ROLE_LONG, "longRole");
|
||||
return roles;
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,14 @@ class tst_AndroidItemModel : public QObject
|
||||
QAbstractItemModel *qProxy;
|
||||
void resetModel();
|
||||
|
||||
enum DataRole{
|
||||
ROLE_STRING = Qt::UserRole,
|
||||
ROLE_BOOLEAN,
|
||||
ROLE_INTEGER,
|
||||
ROLE_DOUBLE,
|
||||
ROLE_LONG
|
||||
};
|
||||
|
||||
private slots:
|
||||
void initTestCase_data();
|
||||
void init();
|
||||
@ -115,11 +123,11 @@ void tst_AndroidItemModel::removeColumn()
|
||||
|
||||
void tst_AndroidItemModel::roleNames()
|
||||
{
|
||||
const static QHash<int, QByteArray> expectedRoles = { { 0, "stringRole" },
|
||||
{ 1, "booleanRole" },
|
||||
{ 2, "integerRole" },
|
||||
{ 3, "doubleRole" },
|
||||
{ 4, "longRole" } };
|
||||
const static QHash<int, QByteArray> expectedRoles = { { ROLE_STRING, "stringRole" },
|
||||
{ ROLE_BOOLEAN, "booleanRole" },
|
||||
{ ROLE_INTEGER, "integerRole" },
|
||||
{ ROLE_DOUBLE, "doubleRole" },
|
||||
{ ROLE_LONG, "longRole" } };
|
||||
QCOMPARE(qProxy->roleNames(), expectedRoles);
|
||||
}
|
||||
|
||||
@ -161,11 +169,11 @@ void tst_AndroidItemModel::hasIndex()
|
||||
|
||||
void tst_AndroidItemModel::data()
|
||||
{
|
||||
const static QHash<int, QMetaType::Type> roleToType = { { 0, QMetaType::QString },
|
||||
{ 1, QMetaType::Bool },
|
||||
{ 2, QMetaType::Int },
|
||||
{ 3, QMetaType::Double },
|
||||
{ 4, QMetaType::Long } };
|
||||
const static QHash<int, QMetaType::Type> roleToType = { { ROLE_STRING, QMetaType::QString },
|
||||
{ ROLE_BOOLEAN, QMetaType::Bool },
|
||||
{ ROLE_INTEGER, QMetaType::Int },
|
||||
{ ROLE_DOUBLE, QMetaType::Double },
|
||||
{ ROLE_LONG, QMetaType::Long } };
|
||||
QFETCH_GLOBAL(int, columnCount);
|
||||
QFETCH_GLOBAL(bool, isList);
|
||||
|
||||
@ -185,20 +193,20 @@ void tst_AndroidItemModel::data()
|
||||
const QVariant data = qProxy->data(index, role);
|
||||
QCOMPARE_EQ(data.typeId(), roleToType[role]);
|
||||
switch (role) {
|
||||
case 0:
|
||||
case ROLE_STRING:
|
||||
QCOMPARE(data.toString(),
|
||||
"r%1/c%2"_L1.arg(QString::number(r), QString::number(c)));
|
||||
break;
|
||||
case 1:
|
||||
case ROLE_BOOLEAN:
|
||||
QCOMPARE(data.toBool(), ((r + c) % 2) == 0);
|
||||
break;
|
||||
case 2:
|
||||
case ROLE_INTEGER:
|
||||
QCOMPARE(data.toInt(), (c << 8) + r);
|
||||
break;
|
||||
case 3:
|
||||
case ROLE_DOUBLE:
|
||||
QVERIFY(qFuzzyCompare(data.toDouble(), (1.0 + r) / (1.0 + c)));
|
||||
break;
|
||||
case 4:
|
||||
case ROLE_LONG:
|
||||
QCOMPARE(data.toULongLong(), ((c << 8) * (r << 8)));
|
||||
break;
|
||||
}
|
||||
@ -213,9 +221,9 @@ void tst_AndroidItemModel::setData_data()
|
||||
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;
|
||||
QTest::newRow("role0") << 0 << 0 << int(ROLE_STRING);
|
||||
QTest::newRow("role1") << 0 << 0 << int(ROLE_BOOLEAN);
|
||||
QTest::newRow("role2") << 0 << 0 << int(ROLE_INTEGER);
|
||||
}
|
||||
|
||||
void tst_AndroidItemModel::setData()
|
||||
|
Loading…
x
Reference in New Issue
Block a user