Permalink
Browse files

Add more tests for HIDReportParser

  • Loading branch information...
Izzatbek committed Dec 11, 2016
1 parent 8c86128 commit f1ad59c314e9e366a5496c1af352c16c24ad8fb4
Showing with 86 additions and 8 deletions.
  1. +4 −3 pointing/utils/HIDReportParser.cpp
  2. +5 −1 pointing/utils/HIDReportParser.h
  3. +77 −4 tests/hidreportparser/hidreportparser.h
@@ -165,7 +165,7 @@ namespace pointing
}
}
void HIDReportParser::clearAll()
void HIDReportParser::clearDescriptor()
{
lastUsage = 0;
parentUsage = 0;
@@ -177,6 +177,8 @@ namespace pointing
curRepInfo = &reportMap[0];
dataMap.clear();
usageList.clear();
delete[] report;
report = nullptr;
}
bool HIDReportParser::findCorrectReport()
@@ -217,7 +219,7 @@ namespace pointing
bool HIDReportParser::setDescriptor(const unsigned char *desc, int size)
{
clearAll();
clearDescriptor();
int currentPosition = 0;
while(currentPosition < size) {
@@ -227,7 +229,6 @@ namespace pointing
}
bool result = findCorrectReport();
delete[] report;
int reportLength = getReportLength();
if (!reportLength)
return false; // Should not be zero
@@ -59,7 +59,6 @@ namespace pointing
int debugLevel;
void parseItem(const HIDItem &item);
void clearAll();
// Find the report which contains relative X and Y
bool findCorrectReport();
@@ -73,6 +72,11 @@ namespace pointing
~HIDReportParser();
/**
* @brief Clears all data, so the parser can't parse anymore
*/
void clearDescriptor();
/**
* @brief setDescriptor Sets the HID descriptor, parses it.
* @param desc
@@ -49,21 +49,69 @@ private slots:
QCOMPARE(buttons, 0);
}
void simpleMouseReportsForObject(HIDReportParser *obj)
{
unsigned char report[4] = {0x01, 0x01, 0xFE, 0x00};
obj->setReport(report);
int dx = 0, dy = 0, buttons = 0;
bool result = obj->getReportData(&dx, &dy, &buttons);
int length = obj->getReportLength();
QCOMPARE(length, 4);
QCOMPARE(result, true);
QCOMPARE(dx, 1);
QCOMPARE(dy, -2);
QCOMPARE(buttons, 1);
}
void simpleMouseReports()
{
unsigned char input[] = {
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x03, 0x81, 0x02, 0x75, 0x05, 0x95, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xc0, 0xc0
};
parser->setDescriptor(input, sizeof(input));
simpleMouseReportsForObject(parser);
}
void copyConstructor()
{
// Simple mouse with 2 buttons and wheel, 52 bytes
unsigned char input[] = {
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x03, 0x81, 0x02, 0x75, 0x05, 0x95, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xc0, 0xc0
};
parser->setDescriptor(input, sizeof(input));
HIDReportParser another = *parser;
simpleMouseReportsForObject(&another);
}
void assignmentOperator()
{
HIDReportParser another;
// Simple mouse with 2 buttons and wheel, 52 bytes
unsigned char input[] = {
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x03, 0x81, 0x02, 0x75, 0x05, 0x95, 0x01, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xc0, 0xc0
};
parser->setDescriptor(input, sizeof(input));
another = *parser;
simpleMouseReportsForObject(&another);
}
void cleanDescriptor()
{
// Use the parser
simpleMouseReports();
// And clean it
parser->clearDescriptor();
unsigned char report[4] = {0x01, 0x01, 0xFE, 0x00};
parser->setReport(report);
int dx = 0, dy = 0, buttons = 0;
int dx = 1, dy = 1, buttons = 1;
bool result = parser->getReportData(&dx, &dy, &buttons);
int length = parser->getReportLength();
QCOMPARE(length, 4);
QCOMPARE(result, true);
// Nothing should change and the result should be false
QCOMPARE(result, false);
QCOMPARE(length, 0);
QCOMPARE(dx, 1);
QCOMPARE(dy, -2);
QCOMPARE(dy, 1);
QCOMPARE(buttons, 1);
}
@@ -642,6 +690,31 @@ private slots:
QCOMPARE(buttons, 2);
}
void emptyDescriptor()
{
parser->clearDescriptor();
int length = parser->getReportLength();
QCOMPARE(length, 0);
int dx = 0, dy = 0, buttons = 0;
bool result = parser->getReportData(&dx, &dy, &buttons);
// Should return false since no descriptor was specified
QCOMPARE(result, false);
QCOMPARE(dx, 0);
QCOMPARE(dy, 0);
QCOMPARE(buttons, 0);
unsigned char report[5] = { 0x01, 0x02, 0x03, 0x04, 0x05};
// Setting the report should return true even if the desciptor size = 0
result = parser->setReport(report);
QCOMPARE(result, true);
parser->getReportData(&dx, &dy, &buttons);
// dx, dy, buttons should not change
QCOMPARE(dx, 0);
QCOMPARE(dy, 0);
QCOMPARE(buttons, 0);
}
// At the end
void cleanupTestCase()
{

0 comments on commit f1ad59c

Please sign in to comment.