<commit_before>#include <tests/lib/test.h>

#define TP_QT_ENABLE_LOWLEVEL_API

#include <TelepathyQt/BaseConnectionManager>
#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/ConnectionManagerLowlevel>
#include <TelepathyQt/DBusError>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingConnection>

using namespace Tp;

class TestBaseCM : public Test
{
    Q_OBJECT
public:
    TestBaseCM(QObject *parent = 0)
        : Test(parent)
    { }

private Q_SLOTS:
    void initTestCase();
    void init();

    void testNoProtocols();

    void cleanup();
    void cleanupTestCase();

};

void TestBaseCM::initTestCase()
{
    initTestCaseImpl();
}

void TestBaseCM::init()
{
    initImpl();
}

void TestBaseCM::testNoProtocols()
{
    qDebug() << "Introspecting non-existing CM";

    ConnectionManagerPtr cliCM = ConnectionManager::create(
            QLatin1String("testcm"));
    PendingReady *pr = cliCM->becomeReady(ConnectionManager::FeatureCore);
    connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectFailure(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);

    qDebug() << "Creating CM";

    BaseConnectionManagerPtr cm = BaseConnectionManager::create(
            QLatin1String("testcm"));
    Tp::DBusError err;
    QVERIFY(cm->registerObject(&err));
    QVERIFY(!err.isValid());

    qDebug() << "Introspecting new CM";

    cliCM = ConnectionManager::create(QLatin1String("testcm"));
    pr = cliCM->becomeReady(ConnectionManager::FeatureCore);
    connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectSuccessfulCall(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(cliCM->supportedProtocols().size(), 0);

    qDebug() << "Requesting connection";

    PendingConnection *pc = cliCM->lowlevel()->requestConnection(
            QLatin1String("jabber"), QVariantMap());
    connect(pc, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectFailure(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);
}

void TestBaseCM::cleanup()
{
    cleanupImpl();
}

void TestBaseCM::cleanupTestCase()
{
    cleanupTestCaseImpl();<commit_msg>test-base-cm: Add test for the protocol-related methods of BaseCM<commit_after>#include <tests/lib/test.h>

#define TP_QT_ENABLE_LOWLEVEL_API

#include <TelepathyQt/BaseConnectionManager>
#include <TelepathyQt/BaseProtocol>
#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/ConnectionManagerLowlevel>
#include <TelepathyQt/DBusError>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingConnection>

using namespace Tp;

class TestBaseCM : public Test
{
    Q_OBJECT
public:
    TestBaseCM(QObject *parent = 0)
        : Test(parent)
    { }

private Q_SLOTS:
    void initTestCase();
    void init();

    void testNoProtocols();
    void testProtocols();

    void cleanup();
    void cleanupTestCase();

};

void TestBaseCM::initTestCase()
{
    initTestCaseImpl();
}

void TestBaseCM::init()
{
    initImpl();
}

void TestBaseCM::testNoProtocols()
{
    qDebug() << "Introspecting non-existing CM";

    ConnectionManagerPtr cliCM = ConnectionManager::create(
            QLatin1String("testcm"));
    PendingReady *pr = cliCM->becomeReady(ConnectionManager::FeatureCore);
    connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectFailure(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);

    qDebug() << "Creating CM";

    BaseConnectionManagerPtr cm = BaseConnectionManager::create(
            QLatin1String("testcm"));
    Tp::DBusError err;
    QVERIFY(cm->registerObject(&err));
    QVERIFY(!err.isValid());

    QCOMPARE(cm->protocols().size(), 0);

    qDebug() << "Introspecting new CM";

    cliCM = ConnectionManager::create(QLatin1String("testcm"));
    pr = cliCM->becomeReady(ConnectionManager::FeatureCore);
    connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectSuccessfulCall(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(cliCM->supportedProtocols().size(), 0);

    qDebug() << "Requesting connection";

    PendingConnection *pc = cliCM->lowlevel()->requestConnection(
            QLatin1String("jabber"), QVariantMap());
    connect(pc, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectFailure(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mLastError, TP_QT_ERROR_NOT_IMPLEMENTED);
}

void TestBaseCM::testProtocols()
{
    qDebug() << "Creating CM";

    BaseConnectionManagerPtr cm = BaseConnectionManager::create(QLatin1String("testcm"));

    BaseProtocolPtr protocol = BaseProtocol::create(QLatin1String("myprotocol"));
    QVERIFY(!protocol.isNull());
    QVERIFY(cm->addProtocol(protocol));

    QVERIFY(cm->hasProtocol(QLatin1String("myprotocol")));
    QCOMPARE(cm->protocol(QLatin1String("myprotocol")), protocol);
    QCOMPARE(cm->protocols().size(), 1);

    QVERIFY(!cm->hasProtocol(QLatin1String("otherprotocol")));
    QVERIFY(cm->protocol(QLatin1String("otherprotocol")).isNull());

    //can't add the same protocol twice
    QVERIFY(!cm->addProtocol(protocol));

    Tp::DBusError err;
    QVERIFY(cm->registerObject(&err));
    QVERIFY(!err.isValid());

    //can't add another protocol after registerObject()
    protocol = BaseProtocol::create(QLatin1String("otherprotocol"));
    QVERIFY(!protocol.isNull());
    QVERIFY(!cm->addProtocol(protocol));
    QCOMPARE(cm->protocols().size(), 1);
    protocol.reset();

    QVariantMap props = cm->immutableProperties();
    QVERIFY(props.contains(TP_QT_IFACE_CONNECTION_MANAGER + QLatin1String(".Protocols")));

    ProtocolPropertiesMap protocols = qvariant_cast<Tp::ProtocolPropertiesMap>(
            props[TP_QT_IFACE_CONNECTION_MANAGER + QLatin1String(".Protocols")]);
    QVERIFY(protocols.contains(QLatin1String("myprotocol")));
    QVERIFY(!protocols.contains(QLatin1String("otherprotocol")));

    qDebug() << "Introspecting CM";

    ConnectionManagerPtr cliCM = ConnectionManager::create(
            QLatin1String("testcm"));
    PendingReady *pr = cliCM->becomeReady(ConnectionManager::FeatureCore);
    connect(pr, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectSuccessfulCall(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);

    QCOMPARE(cliCM->supportedProtocols().size(), 1);
    QVERIFY(cliCM->hasProtocol(QLatin1String("myprotocol")));

    PendingConnection *pc = cliCM->lowlevel()->requestConnection(
            QLatin1String("myprotocol"), QVariantMap());
    connect(pc, SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(expectFailure(Tp::PendingOperation*)));
    QCOMPARE(mLoop->exec(), 0);
    QCOMPARE(mLastError, TP_QT_ERROR_NOT_IMPLEMENTED);
}

void TestBaseCM::cleanup()
{
    cleanupImpl();
}

void TestBaseCM::cleanupTestCase()
{
    cleanupTestCaseImpl();
{'fraction_non_alphanumeric': 0.14744247398505056, 'fraction_numerical': 0.005569397625677854, 'mean_word_length': 4.867583834909716, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3182678', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2103, 'n_words': 321}