~sethj/ubuntu/wily/unity/fix-for-1445595

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// -*- 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/Indicators.h>
#include <UnityCore/AppmenuIndicator.h>
#include "mock_indicators.h"

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

namespace
{

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

  SigReceiver(Indicators const& const_indicators)
  {
    auto& indicators = const_cast<Indicators&>(const_indicators);
    indicators.on_object_added.connect(sigc::mem_fun(this, &SigReceiver::OnObjectAdded));
    indicators.on_object_removed.connect(sigc::mem_fun(this, &SigReceiver::OnObjectRemoved));
    indicators.on_entry_activate_request.connect(sigc::mem_fun(this, &SigReceiver::OnEntryActivateRequest));
    indicators.on_entry_activated.connect(sigc::mem_fun(this, &SigReceiver::OnEntryActivated));
    indicators.on_entry_show_menu.connect(sigc::mem_fun(this, &SigReceiver::OnEntryShowMenu));
  }

  MOCK_CONST_METHOD1(OnObjectAdded, void(Indicator::Ptr const&));
  MOCK_CONST_METHOD1(OnObjectRemoved, void(Indicator::Ptr const&));
  MOCK_CONST_METHOD1(OnEntryActivateRequest, void(std::string const&));
  MOCK_CONST_METHOD3(OnEntryActivated, void(std::string const&, std::string const&, nux::Rect const&));
  MOCK_CONST_METHOD5(OnEntryShowMenu, void(std::string const&, unsigned, int, int, unsigned));
};

struct TestIndicators : Test
{
  // Utility function used to fill the class with test indicators with entries
  void SetupTestChildren()
  {
    // Adding an indicator filled with entries into the TestMockIndicators
    Indicator::Entries sync_data;
    Entry* entry;

    Indicator::Ptr test_indicator_1 = indicators.AddIndicator("indicator-test-1");

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

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

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

    // Sync the indicator, adding 3 entries
    test_indicator_1->Sync(sync_data);
    EXPECT_EQ(test_indicator_1->GetEntries().size(), 3);


    // Adding another indicator filled with entries into the TestMockIndicators
    Indicator::Ptr test_indicator_2 = indicators.AddIndicator("indicator-test-2");
    sync_data.clear();

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

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

    // Sync the indicator, adding 2 entries
    test_indicator_2->Sync(sync_data);
    EXPECT_EQ(test_indicator_2->GetEntries().size(), 2);

    ASSERT_THAT(indicators.GetIndicators().size(), 2);
  }

  MockIndicators::Nice indicators;
};

TEST_F(TestIndicators, Construction)
{
  EXPECT_TRUE(indicators.GetIndicators().empty());
}

TEST_F(TestIndicators, GetInvalidIndicator)
{
  ASSERT_THAT(indicators.GetIndicator("no-available-indicator"), IsNull());
}

TEST_F(TestIndicators, IndicatorsFactory)
{
  Indicator::Ptr standard_indicator = indicators.AddIndicator("libapplication.so");
  EXPECT_EQ(standard_indicator->name(), "libapplication.so");
  EXPECT_FALSE(standard_indicator->IsAppmenu());

  Indicator::Ptr appmenu_indicator = indicators.AddIndicator("libappmenu.so");
  EXPECT_EQ(appmenu_indicator->name(), "libappmenu.so");
  EXPECT_TRUE(appmenu_indicator->IsAppmenu());
}

TEST_F(TestIndicators, IndicatorsHandling)
{
  SigReceiver::Nice sig_receiver(indicators);
  Indicators::IndicatorsList indicators_list;

  // Adding some indicators...
  EXPECT_CALL(sig_receiver, OnObjectAdded(_));
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  Indicator::Ptr test_indicator_1(indicators.AddIndicator("indicator-test-1"));
  EXPECT_EQ(indicators.GetIndicator("indicator-test-1"), test_indicator_1);

  indicators_list = indicators.GetIndicators();
  EXPECT_EQ(indicators_list.size(), 1);
  EXPECT_NE(std::find(indicators_list.begin(), indicators_list.end(), test_indicator_1), indicators_list.end());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_));
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  Indicator::Ptr test_indicator_2(indicators.AddIndicator("indicator-test-2"));
  EXPECT_EQ(indicators.GetIndicator("indicator-test-2"), test_indicator_2);

  indicators_list = indicators.GetIndicators();
  EXPECT_EQ(indicators_list.size(), 2);
  EXPECT_NE(std::find(indicators_list.begin(), indicators_list.end(), test_indicator_2), indicators_list.end());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_));
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  Indicator::Ptr test_indicator_3(indicators.AddIndicator("indicator-test-3"));
  EXPECT_EQ(indicators.GetIndicator("indicator-test-3"), test_indicator_3);

  indicators_list = indicators.GetIndicators();
  EXPECT_EQ(indicators_list.size(), 3);
  EXPECT_NE(std::find(indicators_list.begin(), indicators_list.end(), test_indicator_3), indicators_list.end());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  ASSERT_THAT(indicators.GetIndicator("invalid-indicator-test-4"), IsNull());
  EXPECT_EQ(indicators.GetIndicators().size(), 3);

  // Readding an indicator already there should do nothing
  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  Indicator::Ptr test_indicator_3_duplicate(indicators.AddIndicator("indicator-test-3"));
  EXPECT_EQ(indicators.GetIndicator("indicator-test-3"), test_indicator_3);
  EXPECT_EQ(indicators.GetIndicators().size(), 3);
  EXPECT_EQ(test_indicator_3, test_indicator_3_duplicate);

  // Removing the indicators...
  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(test_indicator_2));

  indicators.RemoveIndicator("indicator-test-2");
  ASSERT_THAT(indicators.GetIndicator("indicator-test-2"), IsNull());

  indicators_list = indicators.GetIndicators();
  EXPECT_EQ(indicators_list.size(), 2);
  EXPECT_EQ(std::find(indicators_list.begin(), indicators_list.end(), test_indicator_2), indicators_list.end());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(test_indicator_1));

  indicators.RemoveIndicator("indicator-test-1");
  ASSERT_THAT(indicators.GetIndicator("indicator-test-1"), IsNull());

  indicators_list = indicators.GetIndicators();
  EXPECT_EQ(indicators_list.size(), 1);
  EXPECT_EQ(std::find(indicators_list.begin(), indicators_list.end(), test_indicator_1), indicators_list.end());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(test_indicator_3));

  indicators.RemoveIndicator("indicator-test-3");
  ASSERT_THAT(indicators.GetIndicator("indicator-test-3"), IsNull());

  indicators_list = indicators.GetIndicators();
  EXPECT_TRUE(indicators_list.empty());

  EXPECT_CALL(sig_receiver, OnObjectAdded(_)).Times(0);
  EXPECT_CALL(sig_receiver, OnObjectRemoved(_)).Times(0);

  indicators.RemoveIndicator("invalid-indicator-test-4");
}

TEST_F(TestIndicators, ActivateEntry)
{
  SetupTestChildren();
  SigReceiver::Nice sig_receiver(indicators);

  // Activating Entries from the Indicators class to see if they get updated
  ASSERT_THAT(indicators.GetIndicator("indicator-test-1"), NotNull());
  ASSERT_THAT(indicators.GetActiveEntry(), IsNull());

  Entry::Ptr entry12(indicators.GetIndicator("indicator-test-1")->GetEntry("indicator-test-1|entry-2"));
  ASSERT_THAT(entry12, NotNull());
  ASSERT_THAT(entry12->active(), false);
  ASSERT_THAT(entry12->geometry(), nux::Rect());

  EXPECT_CALL(sig_receiver, OnEntryActivated("panel1", entry12->id(), nux::Rect(1, 2, 3, 4)));
  indicators.ActivateEntry("panel1", "indicator-test-1|entry-2", nux::Rect(1, 2, 3, 4));

  EXPECT_EQ(entry12->active(), true);
  EXPECT_EQ(entry12->geometry(), nux::Rect(1, 2, 3, 4));
  EXPECT_EQ(indicators.GetActiveEntry(), entry12);
}

TEST_F(TestIndicators, ActivateEntryShouldDisactivatePrevious)
{
  SetupTestChildren();
  SigReceiver::Nice sig_receiver(indicators);

  ASSERT_THAT(indicators.GetIndicator("indicator-test-2"), NotNull());

  Entry::Ptr entry22(indicators.GetIndicator("indicator-test-2")->GetEntry("indicator-test-2|entry-2"));
  ASSERT_THAT(entry22, NotNull());

  indicators.ActivateEntry("panel0", "indicator-test-2|entry-2", nux::Rect(1, 2, 3, 4));
  ASSERT_THAT(entry22->active(), true);
  ASSERT_THAT(entry22->geometry(), nux::Rect(1, 2, 3, 4));

  // Activating another entry, the previously selected one should be disactivate
  Entry::Ptr entry21(indicators.GetIndicator("indicator-test-2")->GetEntry("indicator-test-2|entry-1"));
  ASSERT_THAT(entry21, NotNull());

  ASSERT_THAT(entry21->active(), false);
  ASSERT_THAT(entry21->geometry(), nux::Rect());

  EXPECT_CALL(sig_receiver, OnEntryActivated("panel1", entry21->id(), nux::Rect(4, 3, 2, 1)));
  indicators.ActivateEntry("panel1", "indicator-test-2|entry-1", nux::Rect(4, 3, 2, 1));

  EXPECT_EQ(entry22->active(), false);
  EXPECT_EQ(entry22->geometry(), nux::Rect());

  EXPECT_EQ(entry21->active(), true);
  EXPECT_EQ(entry21->geometry(), nux::Rect(4, 3, 2, 1));
}

TEST_F(TestIndicators, ActivateEntryInvalidEmitsNullSignal)
{
  SetupTestChildren();
  SigReceiver::Nice sig_receiver(indicators);

  ASSERT_THAT(indicators.GetIndicator("indicator-test-1"), NotNull());

  Entry::Ptr entry13(indicators.GetIndicator("indicator-test-1")->GetEntry("indicator-test-1|entry-3"));
  ASSERT_THAT(entry13, NotNull());

  EXPECT_CALL(sig_receiver, OnEntryActivated("panel0", entry13->id(), nux::Rect(4, 2, 3, 4)));
  indicators.ActivateEntry("panel0", "indicator-test-1|entry-3", nux::Rect(4, 2, 3, 4));

  // Activating invalid entry, the previously selected one should be disactivate

  EXPECT_CALL(sig_receiver, OnEntryActivated("", "", nux::Rect()));
  indicators.ActivateEntry("panel1", "indicator-entry-invalid", nux::Rect(5, 5, 5, 5));

  EXPECT_EQ(entry13->active(), false);
  EXPECT_EQ(entry13->geometry(), nux::Rect());
}

TEST_F(TestIndicators, SetEntryShowNow)
{
  SetupTestChildren();

  ASSERT_THAT(indicators.GetIndicator("indicator-test-2"), NotNull());
  Entry::Ptr entry22(indicators.GetIndicator("indicator-test-2")->GetEntry("indicator-test-2|entry-2"));

  ASSERT_THAT(entry22, NotNull());
  ASSERT_THAT(entry22->show_now(), false);

  indicators.SetEntryShowNow("indicator-test-2|entry-2", true);
  EXPECT_EQ(entry22->show_now(), true);

  indicators.SetEntryShowNow("indicator-test-2|entry-2", false);
  EXPECT_EQ(entry22->show_now(), false);
}

TEST_F(TestIndicators, EntryShowMenu)
{
  SetupTestChildren();

  // See if the indicators class get notified on entries actions
  ASSERT_THAT(indicators.GetIndicator("indicator-test-1"), NotNull());

  Entry::Ptr entry13(indicators.GetIndicator("indicator-test-1")->GetEntry("indicator-test-1|entry-3"));
  ASSERT_THAT(entry13, NotNull());

  EXPECT_CALL(indicators, OnEntryShowMenu(entry13->id(), 465789, 35, 53, 2));
  entry13->ShowMenu(465789, 35, 53, 2);

  EXPECT_CALL(indicators, OnEntryShowMenu(entry13->id(), 0, 55, 68, 3));
  entry13->ShowMenu(55, 68, 3);
}

TEST_F(TestIndicators, EntryScroll)
{
  SetupTestChildren();

  // See if the indicators class get notified on entries actions
  ASSERT_THAT(indicators.GetIndicator("indicator-test-1"), NotNull());

  Entry::Ptr entry11(indicators.GetIndicator("indicator-test-1")->GetEntry("indicator-test-1|entry-1"));
  ASSERT_THAT(entry11, NotNull());

  EXPECT_CALL(indicators, OnEntryScroll(entry11->id(), 80));
  entry11->Scroll(80);
}

TEST_F(TestIndicators, EntrySecondaryActivate)
{
  SetupTestChildren();

  // See if the indicators class get notified on entries actions
  ASSERT_THAT(indicators.GetIndicator("indicator-test-2"), NotNull());

  Entry::Ptr entry22(indicators.GetIndicator("indicator-test-2")->GetEntry("indicator-test-2|entry-1"));
  ASSERT_THAT(entry22, NotNull());

  EXPECT_CALL(indicators, OnEntrySecondaryActivate(entry22->id()));
  entry22->SecondaryActivate();
}

TEST_F(TestIndicators, ShowAppMenu)
{
  {
    Indicator::Ptr appmenu_indicator = indicators.AddIndicator("libappmenu.so");
    ASSERT_TRUE(appmenu_indicator->IsAppmenu());
  }

  ASSERT_EQ(indicators.GetIndicators().size(), 1);

  {
    Indicator::Ptr indicator = indicators.GetIndicator("libappmenu.so");
    ASSERT_THAT(indicator, NotNull());
    auto appmenu_indicator = dynamic_cast<AppmenuIndicator*>(indicator.get());
    ASSERT_THAT(appmenu_indicator, NotNull());

    EXPECT_CALL(indicators, OnShowAppMenu(4356789, 54, 13));
    appmenu_indicator->ShowAppmenu(4356789, 54, 13);
  }
}

}