~townsend/unity/fix-lp1301394-trusty

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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2012 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 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/>.
 *
 * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
 */

#include <gmock/gmock.h>
#include <UnityCore/Indicator.h>

using namespace std;
using namespace unity;
using namespace indicator;
using namespace testing;

namespace
{
struct SigReceiver : sigc::trackable
{
  typedef NiceMock<SigReceiver> Nice;

  SigReceiver(Indicator const& const_indicator)
  {
    auto& indicator = const_cast<Indicator&>(const_indicator);
    indicator.updated.connect(sigc::mem_fun(this, &SigReceiver::Updated));
    indicator.on_entry_added.connect(sigc::mem_fun(this, &SigReceiver::EntryAdded));
    indicator.on_entry_removed.connect(sigc::mem_fun(this, &SigReceiver::EntryRemoved));
    indicator.on_show_menu.connect(sigc::mem_fun(this, &SigReceiver::ShowMenu));
    indicator.on_secondary_activate.connect(sigc::mem_fun(this, &SigReceiver::SecondaryActivate));
    indicator.on_scroll.connect(sigc::mem_fun(this, &SigReceiver::Scroll));
  }

  MOCK_CONST_METHOD0(Updated, void());
  MOCK_CONST_METHOD1(EntryAdded, void(Entry::Ptr const&));
  MOCK_CONST_METHOD1(EntryRemoved, void(std::string const&));
  MOCK_CONST_METHOD5(ShowMenu, void(std::string const&, unsigned, int, int, unsigned));
  MOCK_CONST_METHOD1(SecondaryActivate, void(std::string const&));
  MOCK_CONST_METHOD2(Scroll, void(std::string const&, int));
};

TEST(TestIndicator, Construction)
{
  Indicator indicator("indicator-test");

  EXPECT_EQ(indicator.name(), "indicator-test");
  EXPECT_FALSE(indicator.IsAppmenu());
  EXPECT_EQ(indicator.GetEntry("test-entry"), nullptr);
  EXPECT_EQ(indicator.EntryIndex("test-entry"), -1);
  EXPECT_TRUE(indicator.GetEntries().empty());
}

TEST(TestIndicator, Syncing)
{
  Indicator::Entries sync_data;
  Entry* entry;

  Indicator indicator("indicator-test");
  SigReceiver::Nice sig_receiver(indicator);

  entry = new Entry("test-entry-1", "name-hint", "label", true, true, 0, "icon",
                    true, true, -1);
  Entry::Ptr entry1(entry);
  sync_data.push_back(entry1);

  entry = new Entry("test-entry-2", "name-hint", "label", true, true, 0, "icon",
                    true, true, -1);
  Entry::Ptr entry2(entry);
  sync_data.push_back(entry2);

  entry = new Entry("test-entry-3", "name-hint", "label", true, true, 0, "icon",
                    true, true, -1);
  Entry::Ptr entry3(entry);
  sync_data.push_back(entry3);

  // Sync the indicator, adding 3 entries
  {
    testing::InSequence s;
    EXPECT_CALL(sig_receiver, EntryAdded(entry1));
    EXPECT_CALL(sig_receiver, EntryAdded(entry2));
    EXPECT_CALL(sig_receiver, EntryAdded(entry3));
    EXPECT_CALL(sig_receiver, EntryRemoved(_)).Times(0);
    EXPECT_CALL(sig_receiver, Updated());
  }

  indicator.Sync(sync_data);
  EXPECT_EQ(indicator.GetEntries().size(), 3);
  EXPECT_EQ(indicator.GetEntry("test-entry-2"), entry2);
  EXPECT_EQ(indicator.EntryIndex("test-entry-2"), 1);
  // Mock::VerifyAndClearExpectations(&sig_receiver);

  // Sync the indicator removing an entry
  sync_data.remove(entry2);
  EXPECT_EQ(sync_data.size(), 2);
  EXPECT_CALL(sig_receiver, Updated());
  EXPECT_CALL(sig_receiver, EntryAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, EntryRemoved(entry2->id()));

  indicator.Sync(sync_data);
  EXPECT_EQ(indicator.GetEntries().size(), 2);
  EXPECT_EQ(indicator.GetEntry("test-entry-2"), nullptr);
  EXPECT_EQ(indicator.EntryIndex("test-entry-2"), -1);

  // Sync the indicator removing an entry and adding a new one
  entry = new Entry("test-entry-4", "name-hint", "label", true, true, 0, "icon",
                    true, true, -1);
  Entry::Ptr entry4(entry);
  sync_data.push_back(entry4);
  sync_data.remove(entry3);
  EXPECT_EQ(sync_data.size(), 2);

  EXPECT_CALL(sig_receiver, EntryAdded(entry4));
  EXPECT_CALL(sig_receiver, EntryRemoved(entry3->id()));
  EXPECT_CALL(sig_receiver, Updated());
  indicator.Sync(sync_data);
  EXPECT_EQ(indicator.GetEntries().size(), 2);

  // Remove all the indicators

  EXPECT_CALL(sig_receiver, EntryAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, EntryRemoved(entry1->id()));
  EXPECT_CALL(sig_receiver, EntryRemoved(entry4->id()));
  EXPECT_CALL(sig_receiver, Updated());

  sync_data.clear();
  indicator.Sync(sync_data);
  EXPECT_EQ(indicator.GetEntries().size(), 0);
}

TEST(TestIndicator, Updated)
{
  Indicator indicator("indicator-test");
  SigReceiver::Nice sig_receiver(indicator);
  Indicator::Entries sync_data;

  auto entry1 = std::make_shared<Entry>("test-entry-1", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  sync_data.push_back(entry1);

  auto entry2 = std::make_shared<Entry>("test-entry-2", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  sync_data.push_back(entry2);

  auto entry3 = std::make_shared<Entry>("test-entry-3", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  sync_data.push_back(entry3);

  EXPECT_CALL(sig_receiver, Updated());

  // Sync the indicator, adding 3 entries
  indicator.Sync(sync_data);

  // Readding the same entries, nothing is emitted
  EXPECT_CALL(sig_receiver, Updated()).Times(0);
  indicator.Sync(sync_data);

  sync_data.remove(entry3);
  EXPECT_CALL(sig_receiver, Updated());
  indicator.Sync(sync_data);

  sync_data.push_back(entry3);
  EXPECT_CALL(sig_receiver, Updated());
  indicator.Sync(sync_data);
}

TEST(TestIndicator, ChildrenSignalShowMenu)
{
  Indicator indicator("indicator-test");
  SigReceiver::Nice sig_receiver(indicator);

  auto entry = std::make_shared<Entry>("test-entry-1", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  indicator.Sync({entry});

  EXPECT_CALL(sig_receiver, ShowMenu(entry->id(), 123456789, 50, 100, 2));
  entry->ShowMenu(123456789, 50, 100, 2);

  // Check if a removed entry still emits a signal to the old indicator
  indicator.Sync({});

  entry->ShowMenu(123456789, 50, 100, 2);
  EXPECT_CALL(sig_receiver, ShowMenu(_, _, _, _, _)).Times(0);
}

TEST(TestIndicator, ChildrenSignalSecondaryActivate)
{
  Indicator indicator("indicator-test");
  SigReceiver::Nice sig_receiver(indicator);

  auto entry = std::make_shared<Entry>("test-entry-2", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  indicator.Sync({entry});

  EXPECT_CALL(sig_receiver, SecondaryActivate(entry->id()));
  entry->SecondaryActivate();
}

TEST(TestIndicator, ChildrenSignalScroll)
{
  Indicator indicator("indicator-test");
  SigReceiver::Nice sig_receiver(indicator);

  auto entry = std::make_shared<Entry>("test-entry-2", "name-hint", "label", true, true, 0, "icon", true, true, -1);
  indicator.Sync({entry});

  EXPECT_CALL(sig_receiver, Scroll(entry->id(), -5));
  entry->Scroll(-5);
}

}