File: list-apps.cpp

package info (click to toggle)
lomiri-app-launch 0.1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,924 kB
  • sloc: cpp: 14,112; ansic: 1,769; makefile: 209; sh: 114; xml: 48; python: 40
file content (309 lines) | stat: -rw-r--r-- 12,226 bytes parent folder | download | duplicates (2)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright © 2016 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 warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

#include <gio/gio.h>
#include <glib/gstdio.h>
#include <gtest/gtest.h>
#include <numeric>

#include "eventually-fixture.h"
#include "libertine-service.h"

#include "app-store-click.h"
#include "app-store-legacy.h"
#ifdef HAVE_LIBERTINE
#include "app-store-libertine.h"
#endif
#include "app-store-snap.h"
#include "application-impl-snap.h"
#include "application.h"
#include "registry.h"

#include "snapd-mock.h"
#define SNAPD_LIST_APPS_SOCKET SNAPD_TEST_SOCKET "-list-apps"

class ListApps : public EventuallyFixture
{
protected:
    DbusTestService* service = nullptr;
    GDBusConnection* bus = nullptr;

#ifdef HAVE_LIBERTINE
    std::shared_ptr<LibertineService> libertine;
#endif

    virtual void SetUp()
    {
        /* Ensure it is cleared */
        g_unlink(SNAPD_LIST_APPS_SOCKET);

        /* Click DB test mode */
        g_setenv("TEST_CLICK_DB", CMAKE_BINARY_DIR "/click-db-dir", TRUE);
        g_setenv("TEST_CLICK_USER", "test-user", TRUE);

        gchar* linkfarmpath = g_build_filename(CMAKE_SOURCE_DIR, "link-farm", nullptr);
        g_setenv("LOMIRI_APP_LAUNCH_LINK_FARM", linkfarmpath, TRUE);
        g_free(linkfarmpath);

        g_setenv("XDG_DATA_DIRS", CMAKE_SOURCE_DIR, TRUE);
        g_setenv("XDG_CACHE_HOME", CMAKE_SOURCE_DIR "/libertine-data", TRUE);
        g_setenv("XDG_DATA_HOME", CMAKE_SOURCE_DIR "/libertine-home", TRUE);

        g_setenv("LOMIRI_APP_LAUNCH_SNAPD_SOCKET", SNAPD_LIST_APPS_SOCKET, TRUE);
        g_setenv("LOMIRI_APP_LAUNCH_SNAP_BASEDIR", SNAP_BASEDIR, TRUE);
        g_setenv("LOMIRI_APP_LAUNCH_DISABLE_SNAPD_TIMEOUT", "You betcha!", TRUE);

        service = dbus_test_service_new(nullptr);

#ifdef HAVE_LIBERTINE
        libertine = std::make_shared<LibertineService>();
        dbus_test_service_add_task(service, *libertine);
#endif // HAVE_LIBERTINE

        dbus_test_service_start_tasks(service);

        bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);
        g_dbus_connection_set_exit_on_close(bus, FALSE);
        g_object_add_weak_pointer(G_OBJECT(bus), (gpointer*)&bus);

#ifdef HAVE_LIBERTINE
        ASSERT_EVENTUALLY_FUNC_EQ(false, std::function<bool()>{[&] { return libertine->getUniqueName().empty(); }});
#endif // HAVE_LIBERTINE
    }

    virtual void TearDown()
    {
        g_unlink(SNAPD_LIST_APPS_SOCKET);
#ifdef HAVE_LIBERTINE
        libertine.reset();
#endif // HAVE_LIBERTINE
        g_clear_object(&service);

        g_object_unref(bus);
        ASSERT_EVENTUALLY_EQ(nullptr, bus);
    }

    bool findApp(const std::list<std::shared_ptr<lomiri::app_launch::Application>>& apps, const std::string& appid)
    {
        return findApp(apps, lomiri::app_launch::AppID::parse(appid));
    }

    bool findApp(const std::list<std::shared_ptr<lomiri::app_launch::Application>>& apps,
                 const lomiri::app_launch::AppID& appId)
    {
        try
        {
            getApp(apps, appId);
            return true;
        }
        catch (std::runtime_error& e)
        {
            return false;
        }
    }

    std::shared_ptr<lomiri::app_launch::Application> getApp(
        const std::list<std::shared_ptr<lomiri::app_launch::Application>>& apps, const std::string& appid)
    {
        return getApp(apps, lomiri::app_launch::AppID::parse(appid));
    }

    std::shared_ptr<lomiri::app_launch::Application> getApp(
        const std::list<std::shared_ptr<lomiri::app_launch::Application>>& apps, const lomiri::app_launch::AppID& appId)
    {
        for (auto app : apps)
        {
            if (app->appId() == appId)
            {
                return app;
            }
        }

        throw std::runtime_error{"Unable to find app: " + std::string{appId}};
    }

    void printApps(const std::list<std::shared_ptr<lomiri::app_launch::Application>>& apps)
    {
        g_debug("Got apps: %s",
                std::accumulate(apps.begin(), apps.end(), std::string{},
                                [](const std::string& prev, std::shared_ptr<lomiri::app_launch::Application> app) {
                                    if (prev.empty())
                                    {
                                        return std::string(app->appId());
                                    }
                                    else
                                    {
                                        return prev + ", " + std::string(app->appId());
                                    }
                                })
                    .c_str());
    }
};

TEST_F(ListApps, ListClick)
{
    auto registry = std::make_shared<lomiri::app_launch::Registry>();
    lomiri::app_launch::app_store::Click store(registry->impl);
    auto apps = store.list();

    printApps(apps);

    EXPECT_EQ(11, int(apps.size()));

    EXPECT_TRUE(findApp(apps, "chatter.robert-ancell_chatter_2"));
    EXPECT_TRUE(findApp(apps, "com.test.bad-version_application_4.5.6"));
    EXPECT_TRUE(findApp(apps, "com.test.good_application_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.mir_mir_1"));
    EXPECT_TRUE(findApp(apps, "com.test.mir_nomir_1"));
    EXPECT_TRUE(findApp(apps, "com.test.multiple_first_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.multiple_second_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.multiple_third_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.multiple_fourth_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.multiple_fifth_1.2.3"));
    EXPECT_TRUE(findApp(apps, "com.test.no-app_no-application_1.2.3"));

    EXPECT_FALSE(findApp(apps, "com.test.no-hooks_application_1.2.3"));
    EXPECT_FALSE(findApp(apps, "com.test.no-json_application_1.2.3"));
    EXPECT_FALSE(findApp(apps, "com.test.no-object_application_1.2.3"));
    EXPECT_FALSE(findApp(apps, "com.test.no-version_application_1.2.3"));
}

TEST_F(ListApps, ListLegacy)
{
    auto registry = std::make_shared<lomiri::app_launch::Registry>();
    lomiri::app_launch::app_store::Legacy store(registry->impl);
    auto apps = store.list();

    printApps(apps);

    EXPECT_EQ(1, int(apps.size()));

    EXPECT_TRUE(findApp(apps, lomiri::app_launch::AppID(lomiri::app_launch::AppID::Package::from_raw({}),
                                                        lomiri::app_launch::AppID::AppName::from_raw("no-exec"),
                                                        lomiri::app_launch::AppID::Version::from_raw({}))));
}

#ifdef HAVE_LIBERTINE
TEST_F(ListApps, ListLibertine)
{
    auto registry = std::make_shared<lomiri::app_launch::Registry>();
    lomiri::app_launch::app_store::Libertine store(registry->impl);
    auto apps = store.list();

    printApps(apps);

    EXPECT_EQ(3, int(apps.size()));

    EXPECT_TRUE(findApp(apps, "container-name_test_0.0"));
    EXPECT_TRUE(findApp(apps, "container-name_user-app_0.0"));
}
#endif // HAVE_LIBERTINE

static std::pair<std::string, std::string> interfaces{
    "GET /v2/interfaces HTTP/1.1\r\nHost: snapd\r\nAccept: */*\r\n\r\n",
    SnapdMock::httpJsonResponse(
        SnapdMock::snapdOkay(SnapdMock::interfacesJson({{"lomiri", "lomiri-package", {"foo", "bar"}},
                                                        {"mir", "lomiri-package", {"qmlapp"}},
                                                        {"unity7", "lomiri-package", {"foo"}},
                                                        {"unity7", "unity7-package", {"single", "multiple"}},
                                                        {"x11", "x11-package", {"multiple", "hidden"}}

        })))};
static std::pair<std::string, std::string> u8Package{
    "GET /v2/snaps/lomiri-package HTTP/1.1\r\nHost: snapd\r\nAccept: */*\r\n\r\n",
    SnapdMock::httpJsonResponse(SnapdMock::snapdOkay(
        SnapdMock::packageJson("lomiri-package", "active", "app", "1.2.3.4", "x123", {"foo", "bar", "qmlapp"})))};
static std::pair<std::string, std::string> u7Package{
    "GET /v2/snaps/unity7-package HTTP/1.1\r\nHost: snapd\r\nAccept: */*\r\n\r\n",
    SnapdMock::httpJsonResponse(SnapdMock::snapdOkay(SnapdMock::packageJson(
        "unity7-package", "active", "app", "1.2.3.4", "x123", {"scope", "single", "multiple"})))};
static std::pair<std::string, std::string> x11Package{
    "GET /v2/snaps/x11-package HTTP/1.1\r\nHost: snapd\r\nAccept: */*\r\n\r\n",
    SnapdMock::httpJsonResponse(SnapdMock::snapdOkay(
        SnapdMock::packageJson("x11-package", "active", "app", "1.2.3.4", "x123", {"multiple", "hidden"})))};

TEST_F(ListApps, ListSnap)
{
    // Dont care about snaps right now
    return;

    SnapdMock mock{SNAPD_LIST_APPS_SOCKET,
                   {interfaces, u8Package,                                             /* lomiri check */
                    interfaces, u8Package, u8Package,                                  /* mir check */
                    interfaces, u8Package, u7Package, u7Package, u7Package, u8Package, /* unity7 check */
                    interfaces, x11Package, x11Package, x11Package}};                  /* x11 check */
    auto registry = std::make_shared<lomiri::app_launch::Registry>();

    lomiri::app_launch::app_store::Snap store(registry->impl);
    auto apps = store.list();

    printApps(apps);

    mock.result();

    EXPECT_EQ(5, int(apps.size()));
    EXPECT_TRUE(findApp(apps, "lomiri-package_foo_x123"));
    EXPECT_TRUE(findApp(apps, "lomiri-package_qmlapp_x123"));
    EXPECT_TRUE(findApp(apps, "unity7-package_single_x123"));
    EXPECT_TRUE(findApp(apps, "unity7-package_multiple_x123"));
    EXPECT_TRUE(findApp(apps, "x11-package_multiple_x123"));

    EXPECT_FALSE(findApp(apps, "lomiri-package_bar_x123"));
    EXPECT_FALSE(findApp(apps, "unity7-package_scope_x123"));
    EXPECT_FALSE(findApp(apps, "x11-package_hidden_x123"));

    EXPECT_TRUE(getApp(apps, "lomiri-package_foo_x123")->info()->supportsLomiriLifecycle());
    EXPECT_FALSE(getApp(apps, "unity7-package_single_x123")->info()->supportsLomiriLifecycle());
    EXPECT_FALSE(getApp(apps, "unity7-package_multiple_x123")->info()->supportsLomiriLifecycle());

    EXPECT_FALSE(std::dynamic_pointer_cast<lomiri::app_launch::app_info::Desktop>(
                    getApp(apps, "x11-package_multiple_x123")->info())
                    ->xMirEnable());
    EXPECT_FALSE(std::dynamic_pointer_cast<lomiri::app_launch::app_info::Desktop>(
                    getApp(apps, "unity7-package_single_x123")->info())
                    ->xMirEnable());
    EXPECT_FALSE(std::dynamic_pointer_cast<lomiri::app_launch::app_info::Desktop>(
                    getApp(apps, "lomiri-package_foo_x123")->info())
                    ->xMirEnable());
    EXPECT_FALSE(std::dynamic_pointer_cast<lomiri::app_launch::app_info::Desktop>(
                     getApp(apps, "lomiri-package_qmlapp_x123")->info())
                     ->xMirEnable());
}

TEST_F(ListApps, ListAll)
{
    SnapdMock mock{SNAPD_LIST_APPS_SOCKET,
                   {interfaces, u8Package,                                             /* lomiri check */
                    interfaces, u8Package, u8Package,                                  /* mir check */
                    interfaces, u8Package, u7Package, u7Package, u7Package, u8Package, /* unity7 check */
                    interfaces, x11Package, x11Package, x11Package}};                  /* x11 check */

    auto registry = std::make_shared<lomiri::app_launch::Registry>();

    /* Get all the apps */
    auto apps = lomiri::app_launch::Registry::installedApps(registry);

    printApps(apps);

#ifdef HAVE_LIBERTINE
    EXPECT_EQ(18u, int(apps.size()));
#else
    EXPECT_EQ(15u, apps.size());
#endif
}