~ken-vandine/content-hub/fix_pending_check

« back to all changes in this revision

Viewing changes to tests/acceptance-tests/app_hub_communication_transfer.cpp

  • Committer: Thomas Voß
  • Date: 2013-07-15 11:07:50 UTC
  • Revision ID: thomas.voss@canonical.com-20130715110750-l9395s2sdimj0ccw
Initial checkin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include "test_harness.h"
 
20
#include "../cross_process_sync.h"
 
21
#include "../fork_and_run.h"
 
22
 
 
23
#include <com/ubuntu/content/hub.h>
 
24
#include <com/ubuntu/content/peer.h>
 
25
#include <com/ubuntu/content/scope.h>
 
26
#include <com/ubuntu/content/store.h>
 
27
#include <com/ubuntu/content/type.h>
 
28
 
 
29
#include "com/ubuntu/content/peer_registry.h"
 
30
#include "com/ubuntu/content/service.h"
 
31
#include "com/ubuntu/content/serviceadaptor.h"
 
32
 
 
33
#include <gmock/gmock.h>
 
34
#include <gtest/gtest.h>
 
35
 
 
36
#include <QCoreApplication>
 
37
#include <QtDBus/QDBusConnection>
 
38
#include <QStandardPaths>
 
39
#include <QtTest/QTest>
 
40
 
 
41
#include <thread>
 
42
 
 
43
namespace cuc = com::ubuntu::content;
 
44
namespace cucd = com::ubuntu::content::detail;
 
45
 
 
46
void PrintTo(const QString& s, ::std::ostream* os) {
 
47
    *os << std::string(qPrintable(s));
 
48
}
 
49
 
 
50
namespace
 
51
{
 
52
QString service_name{"com.ubuntu.content.Service"};
 
53
 
 
54
struct MockedPeerRegistry : public cucd::PeerRegistry
 
55
{
 
56
    MockedPeerRegistry() : cucd::PeerRegistry()
 
57
    {
 
58
        using namespace ::testing;
 
59
 
 
60
        ON_CALL(*this, default_peer_for_type(_)).WillByDefault(Return(cuc::Peer::unknown()));
 
61
        ON_CALL(*this, install_default_peer_for_type(_,_)).WillByDefault(Return(false));
 
62
        ON_CALL(*this, install_peer_for_type(_,_)).WillByDefault(Return(false));
 
63
    }
 
64
 
 
65
    MOCK_METHOD1(default_peer_for_type, cuc::Peer(cuc::Type t));
 
66
    MOCK_METHOD2(enumerate_known_peers_for_type, void(cuc::Type, const std::function<void(const cuc::Peer&)>&));
 
67
    
 
68
    MOCK_METHOD2(install_default_peer_for_type, bool(cuc::Type, cuc::Peer));
 
69
    MOCK_METHOD2(install_peer_for_type, bool(cuc::Type, cuc::Peer));
 
70
};
 
71
}
 
72
 
 
73
TEST(Hub, transfer_creation_and_states_work)
 
74
{
 
75
    test::CrossProcessSync sync;
 
76
    
 
77
    auto parent = [&sync]()
 
78
    {
 
79
        int argc = 0;
 
80
        QCoreApplication app{argc, nullptr};
 
81
 
 
82
        QDBusConnection connection = QDBusConnection::sessionBus();        
 
83
 
 
84
        QSharedPointer<cucd::PeerRegistry> registry{new ::testing::NiceMock<MockedPeerRegistry>{}};
 
85
        auto implementation = new cucd::Service(registry, &app);
 
86
        new ServiceAdaptor(implementation);
 
87
 
 
88
        ASSERT_TRUE(connection.registerService(service_name));
 
89
        ASSERT_TRUE(connection.registerObject("/", implementation));
 
90
 
 
91
        sync.signal_ready();
 
92
 
 
93
        app.exec();
 
94
 
 
95
        connection.unregisterObject("/");
 
96
        connection.unregisterService(service_name);
 
97
 
 
98
        delete implementation;
 
99
    };
 
100
 
 
101
    auto child = [&sync]()
 
102
    {        
 
103
        sync.wait_for_signal_ready();
 
104
        
 
105
        test::TestHarness harness;
 
106
        harness.add_test_case([]()
 
107
        {
 
108
            auto hub = cuc::Hub::Client::instance();
 
109
            auto transfer = hub->create_import_for_type_from_peer(
 
110
                cuc::Type::Known::pictures(),
 
111
                hub->default_peer_for_type(cuc::Type::Known::pictures()));
 
112
            EXPECT_TRUE(transfer != nullptr);
 
113
            hub->quit();
 
114
        });
 
115
        EXPECT_EQ(0, QTest::qExec(std::addressof(harness)));
 
116
    };
 
117
    
 
118
    EXPECT_EQ(EXIT_SUCCESS, test::fork_and_run(child, parent));
 
119
}