2
* Copyright 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include <transfer/actions-live.h>
21
#include <transfer/transfer-mock.h>
23
#include "glib-fixture.h"
27
using namespace unity::indicator::transfer;
28
using namespace std::placeholders;
30
class ActionsFixture: public GlibFixture
32
typedef GlibFixture super;
36
const std::vector<MockTransfer::Action> empty_history;
38
std::shared_ptr<MockTransfer> m_transfer_a;
39
std::shared_ptr<MockTransfer> m_transfer_b;
40
std::shared_ptr<Transfers> m_transfers;
41
std::shared_ptr<LiveActions> m_live_actions;
42
std::shared_ptr<Actions> m_actions;
48
m_transfer_a.reset(new MockTransfer{"id-a", "/usr/share/icons/ubuntu-mobile/status/scalable/search.svg"});
49
m_transfer_b.reset(new MockTransfer{"id-b", "/usr/share/icons/ubuntu-mobile/status/scalable/syncing.svg"});
50
m_transfers.reset(new Transfers({std::dynamic_pointer_cast<Transfer>(m_transfer_a),
51
std::dynamic_pointer_cast<Transfer>(m_transfer_b)}));
52
m_live_actions.reset(new LiveActions{m_transfers});
53
m_actions = std::dynamic_pointer_cast<Actions>(m_live_actions);
56
virtual void TearDown()
59
m_live_actions.reset();
67
void TestIdFunc(std::function<void(const Transfer::Id&)> callme, MockTransfer::Action expected_action)
69
const std::vector<MockTransfer::Action> expected_history{expected_action};
71
EXPECT_EQ(empty_history, m_transfer_a->history());
72
EXPECT_EQ(empty_history, m_transfer_b->history());
74
callme(m_transfer_a->id());
75
EXPECT_EQ(expected_history, m_transfer_a->history());
76
EXPECT_EQ(empty_history, m_transfer_b->history());
78
callme(m_transfer_b->id());
79
EXPECT_EQ(expected_history, m_transfer_a->history());
80
EXPECT_EQ(expected_history, m_transfer_b->history());
83
void TestBatchFunc(std::function<void()> callme, MockTransfer::Action expected_action)
85
EXPECT_EQ(empty_history, m_transfer_a->history());
86
EXPECT_EQ(empty_history, m_transfer_b->history());
90
const std::vector<MockTransfer::Action> expected_history({expected_action});
91
EXPECT_EQ(expected_history, m_transfer_a->history());
92
EXPECT_EQ(expected_history, m_transfer_b->history());
100
TEST_F(ActionsFixture, Pause)
102
TestIdFunc(std::bind(&LiveActions::pause, *m_live_actions, _1),
103
MockTransfer::Pause);
106
TEST_F(ActionsFixture, Cancel)
108
TestIdFunc(std::bind(&LiveActions::cancel, *m_live_actions, _1),
109
MockTransfer::Cancel);
112
TEST_F(ActionsFixture, Resume)
114
TestIdFunc(std::bind(&LiveActions::resume, *m_live_actions, _1),
115
MockTransfer::Resume);
118
TEST_F(ActionsFixture, PauseAll)
120
TestBatchFunc(std::bind(&LiveActions::pause_all, *m_live_actions),
121
MockTransfer::Pause);
124
TEST_F(ActionsFixture, ResumeAll)
126
TestBatchFunc(std::bind(&LiveActions::resume_all, *m_live_actions),
127
MockTransfer::Resume);
130
TEST_F(ActionsFixture, ClearAll)
132
TestBatchFunc(std::bind(&LiveActions::clear_all, *m_live_actions),
133
MockTransfer::Clear);
136
TEST_F(ActionsFixture, Activate)
138
std::vector<MockTransfer::Action> expected_history;
139
EXPECT_EQ(expected_history, m_transfer_a->history());
141
m_live_actions->activate(m_transfer_a->id());
142
expected_history.push_back(MockTransfer::Pause);
143
EXPECT_EQ(expected_history, m_transfer_a->history());
145
m_live_actions->activate(m_transfer_a->id());
146
expected_history.push_back(MockTransfer::Resume);
147
EXPECT_EQ(expected_history, m_transfer_a->history());
149
m_transfer_a->state().set(Transfer::DONE);
150
m_live_actions->activate(m_transfer_a->id());
151
expected_history.push_back(MockTransfer::Open);
152
EXPECT_EQ(expected_history, m_transfer_a->history());
154
m_transfer_a->state().set(Transfer::CANCELING);
155
m_live_actions->activate(m_transfer_a->id());
156
expected_history.push_back(MockTransfer::Clear);
157
EXPECT_EQ(expected_history, m_transfer_a->history());
160
TEST_F(ActionsFixture, InvalidId)
162
EXPECT_EQ(empty_history, m_transfer_a->history());
163
EXPECT_EQ(empty_history, m_transfer_b->history());
165
m_live_actions->pause("unknown-id");
166
increment_expected_errors(G_LOG_LEVEL_CRITICAL);
167
EXPECT_EQ(empty_history, m_transfer_a->history());
168
EXPECT_EQ(empty_history, m_transfer_b->history());
170
m_live_actions->resume("unknown-id");
171
increment_expected_errors(G_LOG_LEVEL_CRITICAL);
172
EXPECT_EQ(empty_history, m_transfer_a->history());
173
EXPECT_EQ(empty_history, m_transfer_b->history());
175
m_live_actions->cancel("unknown-id");
176
increment_expected_errors(G_LOG_LEVEL_CRITICAL);
177
EXPECT_EQ(empty_history, m_transfer_a->history());
178
EXPECT_EQ(empty_history, m_transfer_b->history());
180
m_live_actions->resume("unknown-id");
181
increment_expected_errors(G_LOG_LEVEL_CRITICAL);
182
EXPECT_EQ(empty_history, m_transfer_a->history());
183
EXPECT_EQ(empty_history, m_transfer_b->history());