File: app_hub_communication_transfer.cpp

package info (click to toggle)
content-hub 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,856 kB
  • sloc: cpp: 7,324; xml: 307; sh: 69; makefile: 35; ansic: 19
file content (234 lines) | stat: -rw-r--r-- 10,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include "app_manager_mock.h"
#include "test_harness.h"
#include "../cross_process_sync.h"
#include "../fork_and_run.h"

#include <com/lomiri/content/hub.h>
#include <com/lomiri/content/item.h>
#include <com/lomiri/content/peer.h>
#include <com/lomiri/content/scope.h>
#include <com/lomiri/content/store.h>
#include <com/lomiri/content/transfer.h>
#include <com/lomiri/content/type.h>

#include "com/lomiri/content/detail/peer_registry.h"
#include "com/lomiri/content/detail/service.h"
#include "com/lomiri/content/serviceadaptor.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <QCoreApplication>
#include <QtDBus/QDBusConnection>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QtTest/QTest>

#include <thread>

namespace cua = com::lomiri::ApplicationManager;
namespace cuc = com::lomiri::content;
namespace cucd = com::lomiri::content::detail;

void PrintTo(const QString& s, ::std::ostream* os) {
    *os << std::string(qPrintable(s));
}

namespace
{
QString service_name{"com.lomiri.content.dbus.Service"};

struct MockedPeerRegistry : public cucd::PeerRegistry
{
    MockedPeerRegistry() : cucd::PeerRegistry()
    {
        using namespace ::testing;

        ON_CALL(*this, default_source_for_type(_)).WillByDefault(Return(cuc::Peer::unknown()));
        ON_CALL(*this, install_default_source_for_type(_,_)).WillByDefault(Return(false));
        ON_CALL(*this, install_source_for_type(_,_)).WillByDefault(Return(false));
    }

    MOCK_METHOD1(default_source_for_type, cuc::Peer(cuc::Type t));
    MOCK_METHOD1(enumerate_known_peers, void(const std::function<void(const cuc::Peer&)>&));
    MOCK_METHOD2(enumerate_known_sources_for_type, void(cuc::Type, const std::function<void(const cuc::Peer&)>&));
    MOCK_METHOD2(enumerate_known_destinations_for_type, void(cuc::Type, const std::function<void(const cuc::Peer&)>&));
    MOCK_METHOD2(enumerate_known_shares_for_type, void(cuc::Type, const std::function<void(const cuc::Peer&)>&));
    MOCK_METHOD2(install_default_source_for_type, bool(cuc::Type, cuc::Peer));
    MOCK_METHOD2(install_source_for_type, bool(cuc::Type, cuc::Peer));
    MOCK_METHOD2(install_destination_for_type, bool(cuc::Type, cuc::Peer));
    MOCK_METHOD2(install_share_for_type, bool(cuc::Type, cuc::Peer));
    MOCK_METHOD1(remove_peer, bool(cuc::Peer));
    MOCK_METHOD1(peer_is_legacy, bool(QString));
};
}

TEST(Hub, transfer_creation_and_states_work)
{
    using namespace ::testing;

    test::CrossProcessSync sync;
    
    auto parent = [&sync]()
    {
        int argc = 0;
        QCoreApplication app{argc, nullptr};

        QString default_peer_id{"com.does.not.exist.anywhere.application"};

        QDBusConnection connection = QDBusConnection::sessionBus();        
        
        auto mock = new ::testing::NiceMock<MockedPeerRegistry>{};
        EXPECT_CALL(*mock, default_source_for_type(_)).
        Times(AtLeast(1)).
        WillRepeatedly(Return(cuc::Peer{default_peer_id}));
        
        QSharedPointer<cucd::PeerRegistry> registry{mock};
        auto app_manager = QSharedPointer<cua::ApplicationManager>(new MockedAppManager());
        cucd::Service implementation(connection, registry, app_manager, &app);
        new ServiceAdaptor(std::addressof(implementation));

        connection.registerService(service_name);
        connection.registerObject("/", std::addressof(implementation));

        QObject::connect(&app, &QCoreApplication::aboutToQuit, [&](){
            connection.unregisterObject("/");
            connection.unregisterService(service_name);
        });

        // Make sure we signal readiness after the event loop has started.
        QTimer::singleShot(0 /* msec */, [&sync]() { sync.signal_ready(); });

        app.exec();
    };

    auto child = [&sync]()
    {
        int argc = 0;
        QCoreApplication app(argc, nullptr);
        app.setApplicationName("com.some.test.app");

        sync.wait_for_signal_ready();
        
        test::TestHarness harness;
        harness.add_test_case([]()
        {
            QTemporaryDir store_dir;
            QVector<cuc::Item> source_items;
            source_items << cuc::Item();
            source_items[0].setName("name3");
            source_items[0].setText("data3");
            /* Work around issue of charging the transfer when run under sbuild */
            if (qgetenv("CONTENT_HUB_TESTING").isEmpty()) {
                source_items << cuc::Item(QUrl::fromLocalFile(QFileInfo("file1").absoluteFilePath()));
                source_items[1].setName("name1");
                source_items << cuc::Item(QUrl::fromLocalFile(QFileInfo("file2").absoluteFilePath()));
                source_items[2].setName("name2");
            }
            
            QVector<cuc::Item> expected_items;
            expected_items << cuc::Item();
            expected_items[0].setName("name3");
            expected_items[0].setText("data3");
            /* Work around issue of charging the transfer when run under sbuild */
            if (qgetenv("CONTENT_HUB_TESTING").isEmpty()) {
                expected_items << cuc::Item(QUrl("file://" + store_dir.path() + "/file1"));
                expected_items[1].setName("name1");
                expected_items << cuc::Item(QUrl("file://" + store_dir.path() + "/file2"));
                expected_items[2].setName("name2");
            }

            /** [Importing pictures] */
            auto hub = cuc::Hub::Client::instance();
            auto transfer = hub->create_import_from_peer(
                hub->default_source_for_type(cuc::Type::Known::pictures()));
            ASSERT_TRUE(transfer != nullptr);
            EXPECT_EQ(cuc::Transfer::created, transfer->state());
            ASSERT_FALSE(hub->has_pending(transfer->destination()));
            EXPECT_TRUE(transfer->setSelectionType(cuc::Transfer::SelectionType::multiple));
            ASSERT_EQ(cuc::Transfer::SelectionType::multiple, transfer->selectionType());
            transfer->setStore(new cuc::Store{store_dir.path()});
            EXPECT_TRUE(transfer->start());
            EXPECT_EQ(cuc::Transfer::initiated, transfer->state());
            ASSERT_TRUE(hub->has_pending(transfer->destination()));
            EXPECT_TRUE(transfer->setSelectionType(cuc::Transfer::SelectionType::single));
            ASSERT_EQ(cuc::Transfer::SelectionType::multiple, transfer->selectionType());
            EXPECT_TRUE(transfer->charge(source_items));
            EXPECT_EQ(cuc::Transfer::charged, transfer->state());
            EXPECT_EQ(expected_items, transfer->collect());
            /** [Importing pictures] */

            /** Test that the transfer aborts when destination file exists */
            auto dupe_transfer = hub->create_import_from_peer(
                hub->default_source_for_type(cuc::Type::Known::pictures()));
            ASSERT_TRUE(dupe_transfer != nullptr);
            EXPECT_EQ(cuc::Transfer::created, dupe_transfer->state());
            EXPECT_TRUE(dupe_transfer->setSelectionType(cuc::Transfer::SelectionType::multiple));
            ASSERT_EQ(cuc::Transfer::SelectionType::multiple, dupe_transfer->selectionType());
            dupe_transfer->setStore(new cuc::Store{store_dir.path()});
            EXPECT_TRUE(dupe_transfer->start());
            EXPECT_EQ(cuc::Transfer::initiated, dupe_transfer->state());
            EXPECT_TRUE(dupe_transfer->charge(source_items));
            // FIX ME: Figure out why it does not abort
            //EXPECT_EQ(cuc::Transfer::aborted, dupe_transfer->state());
            /* end dest exists test */

            /* Test that only a single transfer exists for the same peer */
            auto single_transfer = hub->create_import_from_peer(
                hub->default_source_for_type(cuc::Type::Known::pictures()));
            ASSERT_TRUE(single_transfer != nullptr);
            EXPECT_EQ(cuc::Transfer::created, single_transfer->state());
            EXPECT_TRUE(single_transfer->start());
            EXPECT_EQ(cuc::Transfer::initiated, single_transfer->state());

            auto second_transfer = hub->create_import_from_peer(
                hub->default_source_for_type(cuc::Type::Known::pictures()));
            ASSERT_TRUE(second_transfer != nullptr);
            EXPECT_EQ(cuc::Transfer::created, second_transfer->state());
            /* Now that a second transfer was created, the previous
             * transfer should have been aborted */
            EXPECT_EQ(cuc::Transfer::aborted, single_transfer->state());
            /* end single transfer test */

            /* Test create_import_from_peer_for_type */
            auto type_transfer = hub->create_import_from_peer_for_type(
                hub->default_source_for_type(cuc::Type::Known::pictures()),
                cuc::Type::Known::pictures());
            ASSERT_TRUE(type_transfer != nullptr);
            EXPECT_EQ(cuc::Transfer::created, type_transfer->state());
            EXPECT_TRUE(transfer->setSelectionType(cuc::Transfer::SelectionType::multiple));
            ASSERT_EQ(cuc::Transfer::SelectionType::multiple, transfer->selectionType());
            EXPECT_TRUE(type_transfer->start());
            EXPECT_EQ(cuc::Transfer::initiated, type_transfer->state());
            ASSERT_EQ(cuc::Type::Known::pictures().id(), type_transfer->contentType());
            EXPECT_TRUE(type_transfer->abort());
            EXPECT_EQ(cuc::Transfer::aborted, type_transfer->state());
            /* end create_import_from_peer_for_type test */

            hub->quit();
        });
        EXPECT_EQ(0, QTest::qExec(std::addressof(harness)));

        return testing::Test::HasFailure();
    };
    
    EXPECT_EQ(EXIT_SUCCESS, test::fork_and_run(child, parent));
}