void PowerManagementTest::testIntervalStartStop()
{
    UNBLANK_SCREEN;

    AccelerometerSensorChannelInterface* accOne = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accOne != NULL && accOne->isValid(), accOne ? accOne->errorString().toLatin1() : "Null pointer");

    // Screen orientation keeps acc open, the 'default' may change.
    // Thus dynamic values.
    unsigned int originalInterval = readPollInterval(accPollFile);
    unsigned int testInterval = originalInterval > 1 ? originalInterval / 2 : 100;

    accOne->setInterval(testInterval);

    QVERIFY(readPollInterval(accPollFile) == originalInterval);

    accOne->start();
    QVERIFY(readPollInterval(accPollFile) == testInterval);

    accOne->stop();

    QVERIFY(readPollInterval(accPollFile) == originalInterval);

    delete accOne;
}
/**
 * Verify stopping sensor while screen is blanked.
 */
void PowerManagementTest::testScreenBlank2()
{
    // Unblank to make sure we notice going into blank
    UNBLANK_SCREEN;

    AccelerometerSensorChannelInterface* accOne = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accOne != NULL && accOne->isValid(), accOne ? accOne->errorString().toLatin1() : "Null pointer");

    connect(accOne, SIGNAL(dataAvailable(const XYZ&)), &helper, SLOT(dataAvailable(const XYZ&)));

    accOne->start();

    QTest::qWait(1000);
    QVERIFY2(helper.m_valueCount > 0, "No samples received.");

    // Blank screen
    BLANK_SCREEN;
    QTest::qWait(500);
    helper.reset(); // Clear the buffer
    QTest::qWait(1000);

    // Stop the sensor
    accOne->stop();

    // Verify that values have not come through
    QVERIFY2(helper.m_valueCount == 0, "Samples leaking through");

    // Unblank
    UNBLANK_SCREEN;
    QTest::qWait(500);

    // Reset - in testing the old value might stay in the fakepipe.
    helper.reset();

    // Verity that sensor was not incorrectly resumed
    QTest::qWait(1000);
    QVERIFY(helper.m_valueCount == 0);

    delete accOne;
}
/**
 * Verify screen blank override behavior.
 */
void PowerManagementTest::testScreenBlank4()
{
    // Unblank to make sure we notice going into blank
    UNBLANK_SCREEN;
    QTest::qWait(1000);

    AccelerometerSensorChannelInterface* accOne = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accOne != NULL && accOne->isValid(), accOne ? accOne->errorString().toLatin1() : "Null pointer");

    connect(accOne, SIGNAL(dataAvailable(const XYZ&)), &helper, SLOT(dataAvailable(const XYZ&)));

    accOne->setStandbyOverride(true);
    accOne->start();

    QTest::qWait(1000);
    QVERIFY2(helper.m_valueCount > 0, "No samples received.");

    // Blank screen
    BLANK_SCREEN;
    QTest::qWait(1000);
    helper.reset(); // Clear the buffer
    QTest::qWait(1000);

    // Verify that values have not come through
    QVERIFY2(helper.m_valueCount >  0, "Adaptor apparently went to standby against rules");

    // Unblank
    UNBLANK_SCREEN;
    QTest::qWait(1000);

    // Reset - in testing the old value might stay in the fakepipe.
    helper.reset();

    // Verity that values comes through again
    QTest::qWait(1000);
    QVERIFY2(helper.m_valueCount > 0, "Not getting values after resume.");

    accOne->stop();

    delete accOne;
}
void PowerManagementTest::testIntervalRace()
{
    UNBLANK_SCREEN;

    AccelerometerSensorChannelInterface* accOne = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accOne != NULL && accOne->isValid(), accOne ? accOne->errorString().toLatin1() : "Null pointer");

    AccelerometerSensorChannelInterface* accTwo = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accTwo != NULL && accTwo->isValid(), accTwo ? accTwo->errorString().toLatin1() : "Null pointer");

    // Screen orientation keeps acc open, the 'default' may change.
    // Thus dynamic values.
    unsigned int originalInterval = readPollInterval(accPollFile);
    qDebug() << "original interval:" << originalInterval;
    QVERIFY2((originalInterval == 0 || originalInterval > 3), "Can't run the test with current poll value.");

    unsigned int testIntervalOne = 100; // Faster than original
    unsigned int testIntervalTwo = 50; // Faster than previous
    unsigned int testIntervalThree = 200; // Slower than previous

    accOne->setInterval(testIntervalOne);
    accOne->start();

    QVERIFY(readPollInterval(accPollFile) == testIntervalOne);

    accTwo->setInterval(testIntervalTwo);
    accTwo->start();

    QVERIFY(readPollInterval(accPollFile) == testIntervalTwo);

    accTwo->stop();

    QVERIFY(readPollInterval(accPollFile) == testIntervalOne);

    accTwo->setInterval(testIntervalThree);
    accTwo->start();

    QVERIFY(readPollInterval(accPollFile) == testIntervalOne);

    accOne->stop();

    QVERIFY(readPollInterval(accPollFile) == testIntervalThree);

    accTwo->stop();

    QVERIFY(readPollInterval(accPollFile) == originalInterval);

    delete accOne;
    delete accTwo;
}
/**
 * Verify standby override for screen blank and unblank.
 */
void StandbyOverrideTest::testStandbyOverride()
{

    UNBLANK_SCREEN;

    AccelerometerSensorChannelInterface* accOne = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accOne != NULL && accOne->isValid(), accOne ? accOne->errorString().toLatin1() : "Null pointer");

    AccelerometerSensorChannelInterface* accTwo = AccelerometerSensorChannelInterface::interface("accelerometersensor");
    QVERIFY2(accTwo != NULL && accTwo->isValid(), accTwo ? accTwo->errorString().toLatin1() : "Null pointer");

    connect(accOne, SIGNAL(dataAvailable(const XYZ&)), &helper1, SLOT(dataAvailable(const XYZ&)));
    connect(accTwo, SIGNAL(dataAvailable(const XYZ&)), &helper2, SLOT(dataAvailable(const XYZ&)));

    accOne->start();
    accTwo->start();

    QTest::qWait(500);

    // Test standby override true when screen unblank
    accOne->setStandbyOverride(true);
    accTwo->setStandbyOverride(false);
    QTest::qWait(500);
    helper1.reset();  // Clear the buffer
    helper2.reset();
    QTest::qWait(1500);

    QVERIFY2(helper1.m_valueCount > 0, "No samples received.");
    QVERIFY2(helper2.m_valueCount > 0, "No samples received.");

    accOne->setStandbyOverride(false);
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(1500);
    QVERIFY2(helper1.m_valueCount > 0, "No samples received.");
    QVERIFY2(helper2.m_valueCount > 0, "No samples received.");

    // Test standby override when screen blank
    BLANK_SCREEN;
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(1500);

    QVERIFY2(helper1.m_valueCount == 0, "Samples leaking through.");
    QVERIFY2(helper2.m_valueCount == 0, "Samples leaking through.");

    accOne->setStandbyOverride(true);
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(1500);

    QVERIFY2(helper1.m_valueCount > 0, "No samples received.");
    QVERIFY2(helper2.m_valueCount > 0, "No samples received.");

    accOne->setStandbyOverride(false);
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(500);
    QVERIFY2(helper1.m_valueCount == 0, "Samples leaking through.");
    QVERIFY2(helper2.m_valueCount == 0, "Samples leaking through.");

    accOne->setStandbyOverride(true);
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(1500);
    QVERIFY2(helper1.m_valueCount > 0, "No samples received.");
    QVERIFY2(helper2.m_valueCount > 0, "No samples received.");

    accOne->stop();
    QTest::qWait(500);
    helper1.reset();
    helper2.reset();
    QTest::qWait(1500);
    QVERIFY2(helper1.m_valueCount == 0, "Samples leaking through.");
    QVERIFY2(helper2.m_valueCount == 0, "Samples leaking through.");

    accTwo->stop();

    delete accOne;
    delete accTwo;
}