~gordallott/unity/fade-dash-icons

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
#include <gtest/gtest.h>
#include <glib-object.h>
#include <dee.h>
#include <string>
#include <iostream>
#include <stdexcept>
#include <map>
#include <memory>
#include <sigc++/signal.h>
#include <sigc++/trackable.h>

#include <UnityCore/GLibWrapper.h>
#include <UnityCore/Variant.h>
#include <UnityCore/HomeLens.h>
#include <UnityCore/Lens.h>
#include <UnityCore/Lenses.h>

#include "test_utils.h"

using namespace std;
using namespace unity::dash;

namespace
{

/*
 * FORWARDS
 */

class StaticTestLens;

typedef struct {
  StaticTestLens* lens;
  gchar*          search_string;
} LensSearchClosure;

static gboolean dispatch_global_search(gpointer userdata);


/*
 * Mock Lens instance that does not use DBus. The default search does like this:
 * For input "bar" output:
 *
 * i = 0
 * for letter in "bar":
 *   put result row [ "uri+$letter+$lens_id", "icon+$letter+$lens_id", i % 3, "mime+$letter+$lens_id", ...]
 *   i++
 *
 * The mock lens has 3 categories:
 *
 *  0) "cat0+$lens_id"
 *  1) "cat1+$lens_id"
 *  2) "Shared cat"
 */
class StaticTestLens : public Lens
{
public:
  typedef std::shared_ptr<StaticTestLens> Ptr;

  StaticTestLens(string const& id, string const& name, string const& description, string const& search_hint)
    : Lens(id, "", "", name, "lens-icon.png",
           description, search_hint, true, "",
           ModelType::LOCAL)
  {
    search_in_global(true);

    DeeModel* cats = categories()->model();
    DeeModel* results = global_results()->model();
    DeeModel* flters = filters()->model();

    // Set model schemas
    dee_model_set_schema(cats, "s", "s", "s", "a{sv}", NULL);
    dee_model_set_schema(results, "s", "s", "u", "s", "s", "s", "s", NULL);
    dee_model_set_schema(flters, "s", "s", "s", "s", "a{sv}", "b", "b", "b", NULL);

    // Populate categories model
    ostringstream cat0, cat1;
    cat0 << "cat0+" << id;
    cat1 << "cat1+" << id;
    GVariantBuilder b;
    g_variant_builder_init(&b, G_VARIANT_TYPE_VARDICT);
    GVariant *asv = g_variant_builder_end(&b);

    dee_model_append(cats, cat0.str().c_str(), "icon.png", "tile-vertical", asv);
    dee_model_append(cats, cat1.str().c_str(), "icon.png", "tile-vertical", asv);
    dee_model_append(cats, "Shared cat", "icon.png", "tile-vertical", asv);
  }

  virtual ~StaticTestLens() {}

  virtual void DoGlobalSearch(string const& search_string)
  {
    DeeModel* model = global_results()->model();
    GVariant** row_buf = g_new(GVariant*, 8);

    row_buf[1] = g_variant_new_string("");
    row_buf[3] = g_variant_new_string("");
    row_buf[4] = g_variant_new_string("");
    row_buf[5] = g_variant_new_string("");
    row_buf[6] = g_variant_new_string("");
    row_buf[7] = NULL;

    unsigned int i;
    for (i = 0; i < search_string.size(); i++)
    {
      ostringstream uri;
      uri << "uri+" << search_string.at(i) << "+" << id();
      row_buf[0] = g_variant_new_string(uri.str().c_str());
      row_buf[2] = g_variant_new_uint32(i % 3);

      dee_model_append_row(model, row_buf);
    }

    g_free(row_buf);
  }

  void GlobalSearch(string const& search_string)
  {
    /* Dispatch search async, because that's */
    LensSearchClosure* closure = g_new0(LensSearchClosure, 1);
    closure->lens = this;
    closure->search_string = g_strdup(search_string.c_str());
    g_idle_add(dispatch_global_search, closure);
  }

  void Search(string const& search_string)
  {

  }

  void Activate(string const& uri)
  {

  }

  void Preview(string const& uri)
  {

  }

};

static gboolean dispatch_global_search(gpointer userdata)
{
  LensSearchClosure* closure = (LensSearchClosure*) userdata;

  closure->lens->DoGlobalSearch(closure->search_string);

  g_free(closure->search_string);
  g_free(closure);

  return FALSE;
}

/*
 * Mock Lenses class
 */
class StaticTestLenses : public Lenses
{
public:
  typedef std::shared_ptr<StaticTestLenses> Ptr;

  StaticTestLenses()
  {
    count.SetGetterFunction(sigc::mem_fun(&list_, &Lenses::LensList::size));
  }

  virtual ~StaticTestLenses() {}

  Lenses::LensList GetLenses() const
  {
    return list_;
  }

  Lens::Ptr GetLens(std::string const& lens_id) const
  {
    for (auto lens : list_)
    {
      if (lens->id() == lens_id)
        return lens;
    }
    return Lens::Ptr();
  }

  Lens::Ptr GetLensAtIndex(std::size_t index) const
  {
    return list_.at(index);
  }

protected:
  Lenses::LensList list_;
};

class TwoStaticTestLenses : public StaticTestLenses
{
public:
  TwoStaticTestLenses()
  : lens_1_(new StaticTestLens("first.lens", "First Lens", "The very first lens", "First search hint"))
  , lens_2_(new StaticTestLens("second.lens", "Second Lens", "The second lens", "Second search hint"))
  {
    list_.push_back(lens_1_);
    list_.push_back(lens_2_);
  }

private:
  Lens::Ptr lens_1_;
  Lens::Ptr lens_2_;
};

TEST(TestHomeLens, TestConstruction)
{
  HomeLens home_lens_("name", "description", "searchhint");

  EXPECT_EQ(home_lens_.id(), "home.lens");
  EXPECT_EQ(home_lens_.connected, false);
  EXPECT_EQ(home_lens_.search_in_global, false);
  EXPECT_EQ(home_lens_.name, "name");
  EXPECT_EQ(home_lens_.description, "description");
  EXPECT_EQ(home_lens_.search_hint, "searchhint");
}

TEST(TestHomeLens, TestInitiallyEmpty)
{
  HomeLens home_lens_("name", "description", "searchhint");
  DeeModel* results = home_lens_.results()->model();
  DeeModel* categories = home_lens_.categories()->model();;
  DeeModel* filters = home_lens_.filters()->model();;

  EXPECT_EQ(dee_model_get_n_rows(results), 0);
  EXPECT_EQ(dee_model_get_n_rows(categories), 0);
  EXPECT_EQ(dee_model_get_n_rows(filters), 0);

  EXPECT_EQ(home_lens_.count(), 0);
}

TEST(TestHomeLens, TestTwoStaticLenses)
{
  HomeLens home_lens_("name", "description", "searchhint");
  TwoStaticTestLenses lenses_;

  home_lens_.AddLenses(lenses_);

  EXPECT_EQ(home_lens_.count, (size_t) 2);

  /* Test iteration of registered lensess */
  map<string,string> remaining;
  remaining["first.lens"] = "";
  remaining["second.lens"] = "";
  for (auto lens : home_lens_.GetLenses())
  {
    remaining.erase(lens->id());
  }

  EXPECT_EQ(remaining.size(), 0);

  /* Test sorting and GetAtIndex */
  EXPECT_EQ(home_lens_.GetLensAtIndex(0)->id(), "first.lens");
  EXPECT_EQ(home_lens_.GetLensAtIndex(1)->id(), "second.lens");
}

TEST(TestHomeLens, TestCategoryMerging)
{
  HomeLens home_lens_("name", "description", "searchhint");
  TwoStaticTestLenses lenses_;
  DeeModel* cats = home_lens_.categories()->model();
  DeeModelIter* iter;
  unsigned int cat0_first = 0,
                cat1_first = 1,
                cat_shared = 2,
                cat0_second = 3,
                cat1_second = 4;
  const unsigned int NAME_COLUMN = 0;

  home_lens_.AddLenses(lenses_);

  EXPECT_EQ(dee_model_get_n_rows(cats), 5); // 5 because each lens has 3 cats, but 1 is shared between them

  /* Validate the merged categories */
  iter = dee_model_get_iter_at_row(cats, cat0_first);
  EXPECT_EQ("cat0+first.lens", string(dee_model_get_string(cats, iter, NAME_COLUMN)));

  iter = dee_model_get_iter_at_row(cats, cat1_first);
  EXPECT_EQ("cat1+first.lens", string(dee_model_get_string(cats, iter, NAME_COLUMN)));

  iter = dee_model_get_iter_at_row(cats, cat_shared);
  EXPECT_EQ("Shared cat", string(dee_model_get_string(cats, iter, NAME_COLUMN)));

  iter = dee_model_get_iter_at_row(cats, cat0_second);
  EXPECT_EQ("cat0+second.lens", string(dee_model_get_string(cats, iter, NAME_COLUMN)));

  iter = dee_model_get_iter_at_row(cats, cat1_second);
  EXPECT_EQ("cat1+second.lens", string(dee_model_get_string(cats, iter, NAME_COLUMN)));
}

// It's not that we must not support filters. It is just not implemented yet.
// But we actively test against it to make sure we don't end up with broken
// filters in the UI. When/if we land support for filters on the home screen
// this test should obviously be removed
TEST(TestHomeLens, TestIgnoreFilters)
{
  HomeLens home_lens_("name", "description", "searchhint");
  TwoStaticTestLenses lenses_;
  DeeModel* filters = home_lens_.filters()->model();

  EXPECT_EQ(dee_model_get_n_rows(filters), 0);
}

TEST(TestHomeLens, TestOneSearch)
{
  HomeLens home_lens_("name", "description", "searchhint");
  TwoStaticTestLenses lenses_;
  DeeModel* results = home_lens_.results()->model();
  DeeModel* cats = home_lens_.categories()->model();
  DeeModel* filters = home_lens_.filters()->model();
  DeeModelIter* iter;
  unsigned int cat0_first = 0,
                cat1_first = 1,
                cat_shared = 2,
                cat0_second = 3,
                cat1_second = 4;
  const unsigned int URI_COLUMN = 0;
  const unsigned int CAT_COLUMN = 2;

  home_lens_.AddLenses(lenses_);

  home_lens_.Search("ape");

  Utils::WaitForTimeoutMSec();

  /* Validate counts */
  EXPECT_EQ(dee_model_get_n_rows(results), 6); // 3 hits from each lens
  EXPECT_EQ(dee_model_get_n_rows(cats), 5); // 5 because each lens has 3 cats, but 1 is shared between them
  EXPECT_EQ(dee_model_get_n_rows(filters), 0); // We ignore filters deliberately currently

  /* Validate results. In particular that we get the correct merged
   * category offsets assigned */
  iter = dee_model_get_iter_at_row(results, 0);
  EXPECT_EQ(string("uri+a+first.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat0_first, dee_model_get_uint32(results, iter, CAT_COLUMN));

  iter = dee_model_get_iter_at_row(results, 1);
  EXPECT_EQ(string("uri+p+first.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat1_first, dee_model_get_uint32(results, iter, CAT_COLUMN));

  iter = dee_model_get_iter_at_row(results, 2);
  EXPECT_EQ(string("uri+e+first.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat_shared, dee_model_get_uint32(results, iter, CAT_COLUMN));

  iter = dee_model_get_iter_at_row(results, 3);
  EXPECT_EQ(string("uri+a+second.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat0_second, dee_model_get_uint32(results, iter, CAT_COLUMN));

  iter = dee_model_get_iter_at_row(results, 4);
  EXPECT_EQ(string("uri+p+second.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat1_second, dee_model_get_uint32(results, iter, CAT_COLUMN));

  iter = dee_model_get_iter_at_row(results, 5);
  EXPECT_EQ(string("uri+e+second.lens"), string(dee_model_get_string(results, iter, URI_COLUMN)));
  EXPECT_EQ(cat_shared, dee_model_get_uint32(results, iter, CAT_COLUMN));
}

}