void tst_QServiceFilter::versionMatching()
{
    QFETCH(QString, version);
    QFETCH(int, majorV);
    QFETCH(int, minorV);
    QFETCH(int, rule);

    QServiceFilter filter;
    QCOMPARE(filter.majorVersion(), -1);
    QCOMPARE(filter.minorVersion(), -1);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);

    filter.setInterface("com.nokia.qt.test",version, (QServiceFilter::VersionMatchRule)rule);
    QCOMPARE(filter.majorVersion(), majorV);
    QCOMPARE(filter.minorVersion(), minorV);
    QCOMPARE(filter.versionMatchRule(), (QServiceFilter::VersionMatchRule)rule);
}
void tst_QServiceFilter::streamTest()
{
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    buffer.open(QIODevice::ReadWrite);
    QDataStream stream(&buffer);

    QServiceFilter emptyFilter;
    QCOMPARE(emptyFilter.majorVersion(), -1);
    QCOMPARE(emptyFilter.minorVersion(), -1);
    QCOMPARE(emptyFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(emptyFilter.serviceName(), QString(""));
    QCOMPARE(emptyFilter.interfaceName(), QString(""));
    QCOMPARE(emptyFilter.customAttribute("key1"), QString());
    QCOMPARE(emptyFilter.capabilities(), QStringList());
    QCOMPARE(emptyFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);

    buffer.seek(0);
    stream << emptyFilter;
    
    QServiceFilter tempFilter;
    QCOMPARE(tempFilter.majorVersion(), -1);
    QCOMPARE(tempFilter.minorVersion(), -1);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString(""));
    QCOMPARE(tempFilter.interfaceName(), QString(""));
    QCOMPARE(tempFilter.customAttribute("key1"), QString());
    QCOMPARE(tempFilter.capabilities(), QStringList());
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);

    buffer.seek(0);
    stream >> tempFilter;
    
    QCOMPARE(tempFilter.majorVersion(), -1);
    QCOMPARE(tempFilter.minorVersion(), -1);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString(""));
    QCOMPARE(tempFilter.interfaceName(), QString(""));
    QCOMPARE(tempFilter.customAttribute("key1"), QString());
    QCOMPARE(tempFilter.capabilities(), QStringList());
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);
   
    //assign filter to new filter via constructor
    tempFilter.setInterface("com.nokia.qt.valid", "4.77", QServiceFilter::ExactVersionMatch);
    tempFilter.setServiceName("ServiceName");
    tempFilter.setCustomAttribute("key1", "value1");
    tempFilter.setCapabilities(QServiceFilter::MatchLoadable, QStringList() << "execute" << "delete");
    QCOMPARE(tempFilter.majorVersion(), 4);
    QCOMPARE(tempFilter.minorVersion(), 77);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString("ServiceName"));
    QCOMPARE(tempFilter.interfaceName(), QString("com.nokia.qt.valid"));
    QCOMPARE(tempFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(tempFilter.capabilities(), (QStringList()<<"execute" << "delete"));
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchLoadable);
    buffer.seek(0);
    stream << tempFilter;

    QServiceFilter constructFilter;
    buffer.seek(0);
    stream >> constructFilter;
    QCOMPARE(constructFilter.majorVersion(), 4);
    QCOMPARE(constructFilter.minorVersion(), 77);
    QCOMPARE(constructFilter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(constructFilter.serviceName(), QString("ServiceName"));
    QCOMPARE(constructFilter.interfaceName(), QString("com.nokia.qt.valid"));
    QCOMPARE(constructFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(constructFilter.capabilities(), (QStringList()<<"execute" << "delete"));
    QCOMPARE(constructFilter.capabilityMatchRule(), QServiceFilter::MatchLoadable);

    //assign empty filter to filter with values

    buffer.seek(0);
    stream << emptyFilter;
    buffer.seek(0);
    stream >> constructFilter;
    QCOMPARE(constructFilter.majorVersion(), -1);
    QCOMPARE(constructFilter.minorVersion(), -1);
    QCOMPARE(constructFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(constructFilter.serviceName(), QString(""));
    QCOMPARE(constructFilter.interfaceName(), QString(""));
    QCOMPARE(constructFilter.customAttribute("key1"), QString());
    QCOMPARE(constructFilter.capabilities(), QStringList());
    QCOMPARE(constructFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);
}
void tst_QServiceFilter::testAssignmentOperator()
{
    QServiceFilter emptyFilter;
    QServiceFilter tempFilter(emptyFilter);
   
    //assign empty filter to empty filter 
    QCOMPARE(emptyFilter.majorVersion(), -1);
    QCOMPARE(emptyFilter.minorVersion(), -1);
    QCOMPARE(emptyFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(emptyFilter.serviceName(), QString(""));
    QCOMPARE(emptyFilter.interfaceName(), QString(""));
    QCOMPARE(emptyFilter.customAttribute("key1"), QString());
    QCOMPARE(emptyFilter.capabilities(), QStringList());
    QCOMPARE(emptyFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);

    QCOMPARE(tempFilter.majorVersion(), -1);
    QCOMPARE(tempFilter.minorVersion(), -1);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString(""));
    QCOMPARE(tempFilter.interfaceName(), QString(""));
    QCOMPARE(tempFilter.customAttribute("key1"), QString());
    QCOMPARE(tempFilter.capabilities(), QStringList());
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);

    tempFilter = emptyFilter;
    
    QCOMPARE(tempFilter.majorVersion(), -1);
    QCOMPARE(tempFilter.minorVersion(), -1);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString(""));
    QCOMPARE(tempFilter.interfaceName(), QString(""));
    QCOMPARE(tempFilter.customAttribute("key1"), QString());
    QCOMPARE(tempFilter.capabilities(), QStringList());
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);
   
    //assign filter to new filter via constructor
    tempFilter.setInterface("com.nokia.qt.valid", "4.77", QServiceFilter::ExactVersionMatch);
    tempFilter.setServiceName("ServiceName");
    tempFilter.setCustomAttribute("key1", "value1");
    tempFilter.setCapabilities(QServiceFilter::MatchLoadable, QStringList() << "read" << "write");
    QCOMPARE(tempFilter.majorVersion(), 4);
    QCOMPARE(tempFilter.minorVersion(), 77);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString("ServiceName"));
    QCOMPARE(tempFilter.interfaceName(), QString("com.nokia.qt.valid"));
    QCOMPARE(tempFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(tempFilter.capabilities(), (QStringList() << "read" << "write"));
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchLoadable);

    QServiceFilter constructFilter(tempFilter);
    QCOMPARE(constructFilter.majorVersion(), 4);
    QCOMPARE(constructFilter.minorVersion(), 77);
    QCOMPARE(constructFilter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(constructFilter.serviceName(), QString("ServiceName"));
    QCOMPARE(constructFilter.interfaceName(), QString("com.nokia.qt.valid"));
    QCOMPARE(constructFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(constructFilter.capabilities(), (QStringList() << "read" << "write"));
    QCOMPARE(constructFilter.capabilityMatchRule(), QServiceFilter::MatchLoadable);

    //ensure that we don't have any potential references between tempFilter and 
    //constructedFilter
    tempFilter.setServiceName("NewServiceName");
    tempFilter.setInterface("com.nokia.qt.valid2", "5.88", QServiceFilter::MinimumVersionMatch);
    tempFilter.setCustomAttribute("key2", "value2");
    tempFilter.setCapabilities(QServiceFilter::MatchMinimum,QStringList() << "execute");
    QCOMPARE(tempFilter.majorVersion(), 5);
    QCOMPARE(tempFilter.minorVersion(), 88);
    QCOMPARE(tempFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(tempFilter.serviceName(), QString("NewServiceName"));
    QCOMPARE(tempFilter.interfaceName(), QString("com.nokia.qt.valid2"));
    QCOMPARE(tempFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(tempFilter.customAttribute("key2"), QString("value2"));
    QCOMPARE(tempFilter.capabilities(), (QStringList() << "execute"));
    QCOMPARE(tempFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);
    QCOMPARE(constructFilter.majorVersion(), 4);
    QCOMPARE(constructFilter.minorVersion(), 77);
    QCOMPARE(constructFilter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(constructFilter.serviceName(), QString("ServiceName"));
    QCOMPARE(constructFilter.interfaceName(), QString("com.nokia.qt.valid"));
    QCOMPARE(constructFilter.customAttribute("key1"), QString("value1"));
    QCOMPARE(constructFilter.customAttribute("key2"), QString());
    QCOMPARE(constructFilter.capabilities(), (QStringList() << "read" << "write"));
    QCOMPARE(constructFilter.capabilityMatchRule(), QServiceFilter::MatchLoadable);

    //assign empty filter to filter with values
    constructFilter = emptyFilter;
    QCOMPARE(constructFilter.majorVersion(), -1);
    QCOMPARE(constructFilter.minorVersion(), -1);
    QCOMPARE(constructFilter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(constructFilter.serviceName(), QString(""));
    QCOMPARE(constructFilter.interfaceName(), QString(""));
    QCOMPARE(constructFilter.customAttribute("key1"), QString());
    QCOMPARE(constructFilter.customAttribute("key2"), QString());
    QCOMPARE(constructFilter.capabilities(), QStringList());
    QCOMPARE(constructFilter.capabilityMatchRule(), QServiceFilter::MatchMinimum);
}
void tst_QServiceFilter::setInterface()
{
    //don't separate this out into test_data() function as we test the behavior
    //on the same filter object
    QServiceFilter filter;
    QCOMPARE(filter.majorVersion(), -1);
    QCOMPARE(filter.minorVersion(), -1);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString());

    filter.setInterface("com.nokia.qt.text", "1.0", QServiceFilter::ExactVersionMatch);

    QCOMPARE(filter.majorVersion(), 1);
    QCOMPARE(filter.minorVersion(), 0);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.text"));

    filter.setInterface("com.nokia.qt.text", "1.5", QServiceFilter::MinimumVersionMatch);

    QCOMPARE(filter.majorVersion(), 1);
    QCOMPARE(filter.minorVersion(), 5);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.text"));

    //invalid version tag -> ignore the call
    filter.setInterface("com.nokia.qt.label", "f.0", QServiceFilter::ExactVersionMatch);

    QCOMPARE(filter.majorVersion(), 1);
    QCOMPARE(filter.minorVersion(), 5);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::MinimumVersionMatch); //default
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.text"));

    //empty version tag -> reset version
    filter.setInterface("com.nokia.qt.label", "", QServiceFilter::ExactVersionMatch);

    QCOMPARE(filter.majorVersion(), -1);
    QCOMPARE(filter.minorVersion(), -1);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::ExactVersionMatch); //default
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.label"));

    //empty.interfaceName() tag -> ignore the call
    filter.setInterface("", "4.5", QServiceFilter::MinimumVersionMatch);

    QCOMPARE(filter.majorVersion(), -1);
    QCOMPARE(filter.minorVersion(), -1);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::ExactVersionMatch); //default
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.label"));
    
    //set a valid 
    filter.setInterface("com.nokia.qt.valid", "4.77", QServiceFilter::ExactVersionMatch);

    QCOMPARE(filter.majorVersion(), 4);
    QCOMPARE(filter.minorVersion(), 77);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(filter.serviceName(), QString());
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.valid"));

    filter.setServiceName("myService");
    QCOMPARE(filter.majorVersion(), 4);
    QCOMPARE(filter.minorVersion(), 77);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::ExactVersionMatch);
    QCOMPARE(filter.serviceName(), QString("myService"));
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.valid"));

    //test default constructed version and matching rule
    filter.setInterface("com.nokia.qt.valid2");
    QCOMPARE(filter.majorVersion(), -1);
    QCOMPARE(filter.minorVersion(), -1);
    QCOMPARE(filter.versionMatchRule(), QServiceFilter::MinimumVersionMatch);
    QCOMPARE(filter.serviceName(), QString("myService"));
    QCOMPARE(filter.interfaceName(), QString("com.nokia.qt.valid2"));
}