File: test-multisource.cpp

package info (click to toggle)
lomiri-indicator-transfer 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,112 kB
  • sloc: cpp: 3,155; python: 65; ansic: 46; makefile: 14
file content (166 lines) | stat: -rw-r--r-- 5,618 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
/*
 * Copyright 2015 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:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include "source-mock.h"

#include <gtest/gtest.h>

#include <transfer/controller.h>
#include <transfer/multisource.h>

using ::testing::AtLeast;

using namespace lomiri::indicator::transfer;

class MultiSourceFixture: public ::testing::Test
{
protected:

  struct Event
  {
    typedef enum { ADDED, CHANGED, REMOVED } Type;
    Type type;
    Transfer::Id id;
    bool operator==(const Event& that) const { return type==that.type && id==that.id; }
  };

  bool model_consists_of(const std::shared_ptr<const Model>& model, std::initializer_list<std::shared_ptr<Transfer>> list) const
  {
    // test get_all()
    std::vector<std::shared_ptr<Transfer>> transfers(list);
    std::sort(transfers.begin(), transfers.end());
    g_return_val_if_fail(transfers == model->get_all(), false);

    // test get()
    for(auto& transfer : transfers)
      g_return_val_if_fail(transfer == model->get(transfer->id), false);

    // test get_ids()
    std::set<Transfer::Id> ids;
    for(auto& transfer : transfers)
      ids.insert(transfer->id);
    g_return_val_if_fail(ids == model->get_ids(), false);

    return true;
  }
};

TEST_F(MultiSourceFixture,MultiplexesModels)
{
  // set up the tributary sources, 'a' and 'b'
  auto a = std::make_shared<MockSource>();
  auto b = std::make_shared<MockSource>();

  // set up the multisource and connect it to the tributaries
  MultiSource multisource;
  auto multimodel = multisource.get_model();
  std::vector<Event> events;
  std::vector<Event> expected_events;
  multisource.get_model()->added().connect([&events](const Transfer::Id& id){events.push_back(Event{Event::ADDED,id});});
  multisource.get_model()->changed().connect([&events](const Transfer::Id& id){events.push_back(Event{Event::CHANGED,id});});
  multisource.get_model()->removed().connect([&events](const Transfer::Id& id){events.push_back(Event{Event::REMOVED,id});});
  multisource.add_source(a);
  multisource.add_source(b);

  // add a transfer to the 'a' source...
  const Transfer::Id aid {"aid"};
  auto at = std::make_shared<Transfer>();
  at->id = aid;
  a->m_model->add(at);
  expected_events.push_back(Event{Event::ADDED,aid});

  // confirm that the multimodel sees the new transfer
  EXPECT_EQ(expected_events, events);
  EXPECT_EQ(at, a->get_model()->get(aid));
  EXPECT_TRUE(model_consists_of(multimodel, {at}));

  // add a transfer to the 'b' source...
  const Transfer::Id bid {"bid"};
  auto bt = std::make_shared<Transfer>();
  bt->id = bid;
  b->m_model->add(bt);
  expected_events.push_back(Event{Event::ADDED,bid});

  // confirm that the multimodel sees the new transfer
  EXPECT_EQ(expected_events, events);
  EXPECT_EQ(bt, b->get_model()->get(bid));
  EXPECT_TRUE(model_consists_of(multimodel, {at, bt}));

  // poke transfer 'at'...
  at->progress = 50.0;
  a->m_model->emit_changed(aid);
  expected_events.push_back(Event{Event::CHANGED,aid});
  EXPECT_EQ(expected_events, events);
  EXPECT_TRUE(model_consists_of(multimodel, {at, bt}));

  // remove transfer 'at'...
  a->m_model->remove(aid);
  expected_events.push_back(Event{Event::REMOVED,aid});
  EXPECT_EQ(expected_events, events);
  EXPECT_FALSE(a->get_model()->get(aid));
  EXPECT_TRUE(model_consists_of(multimodel, {bt}));

  // remove transfer 'bt'...
  b->m_model->remove(bid);
  expected_events.push_back(Event{Event::REMOVED,bid});
  EXPECT_EQ(expected_events, events);
  EXPECT_FALSE(b->get_model()->get(aid));
  EXPECT_TRUE(model_consists_of(multimodel, {}));
}

TEST(Multisource,MethodDelegation)
{
  // set up the tributary sources, 'a' and 'b'
  auto a = std::make_shared<MockSource>();
  auto b = std::make_shared<MockSource>();

  // set up the multisource and connect it to the tributaries
  MultiSource multisource;
  multisource.add_source(a);
  multisource.add_source(b);

  // add a transfer to the 'a' source...
  const Transfer::Id aid {"aid"};
  auto at = std::make_shared<Transfer>();
  at->id = aid;
  a->m_model->add(at);

  // add a transfer to the 'b' source...
  const Transfer::Id bid {"bid"};
  auto bt = std::make_shared<Transfer>();
  bt->id = bid;
  b->m_model->add(bt);

  // confirm that multisource method calls are delegated to 'a'
  EXPECT_CALL(*a, open(aid)); multisource.open(aid);
  EXPECT_CALL(*a, start(aid)); multisource.start(aid);
  EXPECT_CALL(*a, pause(aid)); multisource.pause(aid);
  EXPECT_CALL(*a, resume(aid)); multisource.resume(aid);
  EXPECT_CALL(*a, cancel(aid)); multisource.cancel(aid);
  EXPECT_CALL(*a, open_app(aid)); multisource.open_app(aid);

  // confirm that multisource method calls are delegated to 'a'
  EXPECT_CALL(*b, open(bid)); multisource.open(bid);
  EXPECT_CALL(*b, start(bid)); multisource.start(bid);
  EXPECT_CALL(*b, pause(bid)); multisource.pause(bid);
  EXPECT_CALL(*b, resume(bid)); multisource.resume(bid);
  EXPECT_CALL(*b, cancel(bid)); multisource.cancel(bid);
  EXPECT_CALL(*b, open_app(bid)); multisource.open_app(bid);
}