void tst_QBluetoothDeviceDiscoveryAgent::tst_deviceDiscovery()
{
    {
        QFETCH(QBluetoothDeviceDiscoveryAgent::InquiryType, inquiryType);

        QBluetoothDeviceDiscoveryAgent discoveryAgent;
        QVERIFY(discoveryAgent.error() == discoveryAgent.NoError);
        QVERIFY(discoveryAgent.errorString().isEmpty());
        QVERIFY(!discoveryAgent.isActive());

        QVERIFY(discoveryAgent.discoveredDevices().isEmpty());

        QSignalSpy finishedSpy(&discoveryAgent, SIGNAL(finished()));
        QSignalSpy errorSpy(&discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)));
        QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)));
//        connect(&discoveryAgent, SIGNAL(finished()), this, SLOT(finished()));
//        connect(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
//                this, SLOT(deviceDiscoveryDebug(const QBluetoothDeviceInfo&)));

        discoveryAgent.setInquiryType(inquiryType);
        discoveryAgent.start();

        QVERIFY(discoveryAgent.isActive());

        // Wait for up to MaxScanTime for the scan to finish
        int scanTime = MaxScanTime;
        while (finishedSpy.count() == 0 && scanTime > 0) {
            QTest::qWait(15000);
            scanTime -= 15000;
        }
        qDebug() << scanTime << MaxScanTime;
        // verify that we are finished
        QVERIFY(!discoveryAgent.isActive());
        // stop
        discoveryAgent.stop();
        QVERIFY(!discoveryAgent.isActive());
        qDebug() << "Scan time left:" << scanTime;
        // Expect finished signal with no error
        QVERIFY(finishedSpy.count() == 1);
        QVERIFY(errorSpy.isEmpty());
        QVERIFY(discoveryAgent.error() == discoveryAgent.NoError);
        QVERIFY(discoveryAgent.errorString().isEmpty());

        // verify that the list is as big as the signals received.
        QVERIFY(discoveredSpy.count() == discoveryAgent.discoveredDevices().length());
        // verify that there really was some devices in the array
        QVERIFY(discoveredSpy.count() > 0);

        // All returned QBluetoothDeviceInfo should be valid.
        while (!discoveredSpy.isEmpty()) {
            const QBluetoothDeviceInfo info =
                qvariant_cast<QBluetoothDeviceInfo>(discoveredSpy.takeFirst().at(0));
            QVERIFY(info.isValid());
        }
    }
}
void tst_QBluetoothDeviceDiscoveryAgent::tst_startStopDeviceDiscoveries()
{
    {
        QBluetoothDeviceDiscoveryAgent::InquiryType inquiryType = QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry;
        QBluetoothDeviceDiscoveryAgent discoveryAgent;

        QVERIFY(discoveryAgent.error() == discoveryAgent.NoError);
        QVERIFY(discoveryAgent.errorString().isEmpty());
        QVERIFY(!discoveryAgent.isActive());
        QVERIFY(discoveryAgent.discoveredDevices().isEmpty());

        QSignalSpy finishedSpy(&discoveryAgent, SIGNAL(finished()));
        QSignalSpy cancelSpy(&discoveryAgent, SIGNAL(canceled()));
        QSignalSpy errorSpy(&discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)));
        QSignalSpy discoveredSpy(&discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)));

        // Starting case 1: start-stop, expecting cancel signal
        discoveryAgent.setInquiryType(inquiryType);
        // we should have no errors at this point.
        QVERIFY(errorSpy.isEmpty());

        discoveryAgent.start();
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // cancel current request.
        discoveryAgent.stop();

        // Wait for up to MaxWaitTime for the cancel to finish
        int waitTime = MaxWaitTime;
        while (cancelSpy.count() == 0 && waitTime > 0) {
            QTest::qWait(100);
            waitTime-=100;
        }

        // we should not be active anymore
        QVERIFY(!discoveryAgent.isActive());        
        QVERIFY(errorSpy.isEmpty());
        QVERIFY(cancelSpy.count() == 1);
        cancelSpy.clear();
        // Starting case 2: start-start-stop, expecting cancel signal
        discoveryAgent.start();
        // we should be active now
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // start again. should this be error?
        discoveryAgent.start();
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // stop
        discoveryAgent.stop();               

        // Wait for up to MaxWaitTime for the cancel to finish
        waitTime = MaxWaitTime;
        while (cancelSpy.count() == 0 && waitTime > 0) {
            QTest::qWait(100);
            waitTime-=100;
        }

        // we should not be active anymore
        QVERIFY(!discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        QVERIFY(cancelSpy.count() == 1);
        cancelSpy.clear();

        //  Starting case 3: stop
        discoveryAgent.stop();
        QVERIFY(!discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());

        // Expect finished signal with no error
        QVERIFY(finishedSpy.count() == 0);
        QVERIFY(discoveryAgent.error() == discoveryAgent.NoError);
        QVERIFY(discoveryAgent.errorString().isEmpty());


        // Starting case 4: start-stop-start-stop, expecting only 1 cancel signal
        discoveryAgent.start();
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // cancel current request.
        discoveryAgent.stop();
        // start a new one
        discoveryAgent.start();
        // we should be active now
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // stop
        discoveryAgent.stop();

        // Wait for up to MaxWaitTime for the cancel to finish
        waitTime = MaxWaitTime;
        while (waitTime > 0) {
            QTest::qWait(100);
            waitTime-=100;
        }

        // we should not be active anymore
        QVERIFY(!discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // should only have 1 cancel
        QVERIFY(cancelSpy.count() == 1);
        cancelSpy.clear();

        // Starting case 5: start-stop-start: expecting finished signal & no cancel
        discoveryAgent.start();
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // cancel current request.
        discoveryAgent.stop();
        // start a new one
        discoveryAgent.start();
        // we should be active now
        QVERIFY(discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());

        // Wait for up to MaxScanTime for the cancel to finish
        waitTime = MaxScanTime;
        while (finishedSpy.count() == 0 && waitTime > 0) {
            QTest::qWait(1000);
            waitTime-=1000;
        }

        // we should not be active anymore
        QVERIFY(!discoveryAgent.isActive());
        QVERIFY(errorSpy.isEmpty());
        // should only have 1 cancel
        QVERIFY(finishedSpy.count() == 1);
        QVERIFY(cancelSpy.isEmpty());
    }
}