~ken-vandine/content-hub/fix_pending_check

« back to all changes in this revision

Viewing changes to tests/acceptance-tests/app_hub_communication_stores.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, stores_are_reported_correctly_to_clients)
 
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 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
            
 
110
            auto system_wide_pictures_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::pictures());
 
111
            auto system_wide_music_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::music());
 
112
            auto system_wide_documents_store = hub->store_for_scope_and_type(cuc::Scope::system, cuc::Type::Known::documents());
 
113
            
 
114
            auto users_pictures_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::pictures());
 
115
            auto users_music_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::music());
 
116
            auto users_documents_store = hub->store_for_scope_and_type(cuc::Scope::user, cuc::Type::Known::documents());
 
117
            
 
118
            auto apps_pictures_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::pictures());
 
119
            auto apps_music_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::music());
 
120
            auto apps_documents_store = hub->store_for_scope_and_type(cuc::Scope::app, cuc::Type::Known::documents());
 
121
            
 
122
            EXPECT_EQ(system_wide_pictures_store->uri(), "/content/Pictures");
 
123
            EXPECT_EQ(system_wide_music_store->uri(), "/content/Music");
 
124
            EXPECT_EQ(system_wide_documents_store->uri(), "/content/Documents");
 
125
            
 
126
            auto ups = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
 
127
            auto ums = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
 
128
            auto uds = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
 
129
            EXPECT_EQ(users_pictures_store->uri(), ups);
 
130
            EXPECT_EQ(users_music_store->uri(), ums);
 
131
            EXPECT_EQ(users_documents_store->uri(), uds);
 
132
            
 
133
            auto aps = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Pictures";
 
134
            auto ams = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Music";
 
135
            auto ads = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/Documents";
 
136
            EXPECT_EQ(apps_pictures_store->uri(), aps);
 
137
            EXPECT_EQ(apps_music_store->uri(), ams);
 
138
            EXPECT_EQ(apps_documents_store->uri(), ads);
 
139
 
 
140
            hub->quit();
 
141
        });
 
142
        EXPECT_EQ(0, QTest::qExec(std::addressof(harness)));
 
143
    };
 
144
    
 
145
    EXPECT_EQ(EXIT_SUCCESS, test::fork_and_run(child, parent));
 
146
}