~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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright 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 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
 * version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Tim Penhey  <tim.penhey@canonical.com>
 *              Marco Trevisan <marco.trevisan@canonical.com>
 */
#ifndef TESTS_MOCK_APPLICATION_H
#define TESTS_MOCK_APPLICATION_H

#include <unordered_map>
#include <gmock/gmock.h>
#include <gio/gdesktopappinfo.h>
#include <UnityCore/GLibWrapper.h>

#include "unity-shared/ApplicationManager.h"
#include "unity-shared/WindowManager.h"

using namespace testing;

namespace testmocks
{
struct MockApplicationWindow : unity::ApplicationWindow
{
  typedef std::shared_ptr<MockApplicationWindow> Ptr;
  typedef NiceMock<MockApplicationWindow> Nice;

  MockApplicationWindow(Window xid)
    : xid_(xid)
    , monitor_(0)
    , type_(unity::WindowType::MOCK)
    , title_("MockApplicationWindow "+std::to_string(xid_))
    , visible_(true)
    , active_(false)
    , urgent_(false)
  {
    monitor.SetGetterFunction([this] { return monitor_; });
    visible.SetGetterFunction([this] { return visible_; });
    active.SetGetterFunction([this] { return active_; });
    urgent.SetGetterFunction([this] { return urgent_; });
    title.SetGetterFunction([this] { return title_; });
    icon.SetGetterFunction([this] { return icon_; });

    ON_CALL(*this, type()).WillByDefault(Invoke([this] { return type_; }));
    ON_CALL(*this, window_id()).WillByDefault(Invoke([this] { return xid_; }));
    ON_CALL(*this, Focus()).WillByDefault(Invoke([this] { return LocalFocus(); }));
    ON_CALL(*this, application()).WillByDefault(Return(unity::ApplicationPtr()));
  }

  Window xid_;
  int monitor_;
  unity::WindowType type_;
  std::string title_;
  std::string icon_;

  bool visible_;
  bool active_;
  bool urgent_;

  MOCK_CONST_METHOD0(type, unity::WindowType());
  MOCK_CONST_METHOD0(window_id, Window());
  MOCK_CONST_METHOD0(application, unity::ApplicationPtr());
  MOCK_CONST_METHOD0(Focus, bool());
  MOCK_CONST_METHOD0(Quit, void());

  bool LocalFocus()
  {
    auto& wm = unity::WindowManager::Default();
    wm.Raise(xid_);
    wm.Activate(xid_);
    active_ = true;
    active.changed.emit(active_);
    return true;
  }

  void SetTitle(std::string const& new_title)
  {
    if (new_title == title())
      return;

    title_ = new_title;
    title.changed(title_);
  }

  void SetIcon(std::string const& new_icon)
  {
    if (new_icon == icon())
      return;

    icon_ = new_icon;
    icon.changed(icon_);
  }
};

struct MockApplication : unity::Application
{
  typedef std::shared_ptr<MockApplication> Ptr;
  typedef NiceMock<MockApplication> Nice;

  MockApplication()
    : MockApplication("")
  {}

  MockApplication(std::string const& desktop_file_path,
                  std::string const& icon_name = "",
                  std::string const& title_str = "")
    : desktop_file_(desktop_file_path)
    , icon_(icon_name)
    , title_(title_str)
    , seen_(false)
    , sticky_(false)
    , visible_(false)
    , active_(false)
    , running_(false)
    , urgent_(false)
    , type_(unity::AppType::MOCK)
    {
      seen.SetSetterFunction(sigc::mem_fun(this, &MockApplication::SetSeen));
      sticky.SetSetterFunction(sigc::mem_fun(this, &MockApplication::SetSticky));

      seen.SetGetterFunction([this] { return seen_; });
      sticky.SetGetterFunction([this] { return sticky_; });
      visible.SetGetterFunction([this] { return visible_; });
      active.SetGetterFunction([this] { return active_; });
      running.SetGetterFunction([this] { return running_; });
      urgent.SetGetterFunction([this] { return urgent_; });
      desktop_file.SetGetterFunction([this] { return desktop_file_; });
      title.SetGetterFunction([this] { return title_; });
      icon.SetGetterFunction([this] { return icon_; });

      ON_CALL(*this, type()).WillByDefault(Invoke([this] { return type_; }));
      ON_CALL(*this, desktop_id()).WillByDefault(Invoke([this] { return desktop_file_; }));
      ON_CALL(*this, repr()).WillByDefault(Return("MockApplication"));
      ON_CALL(*this, GetWindows()).WillByDefault(Invoke([this] () -> unity::WindowList const& { return windows_; }));
      ON_CALL(*this, GetSupportedMimeTypes()).WillByDefault(Return(std::vector<std::string>()));
      ON_CALL(*this, GetFocusableWindow()).WillByDefault(Return(unity::ApplicationWindowPtr()));
      ON_CALL(*this, OwnsWindow(_)).WillByDefault(Invoke(this, &MockApplication::LocalOwnsWindow));
      ON_CALL(*this, LogEvent(_,_)).WillByDefault(Invoke(this, &MockApplication::LocalLogEvent));
    }

  std::string desktop_file_;
  std::string icon_;
  std::string title_;
  bool seen_;
  bool sticky_;
  bool visible_;
  bool active_;
  bool running_;
  bool urgent_;
  unity::WindowList windows_;
  unity::AppType type_;
  std::vector<std::pair<unity::ApplicationEventType, unity::ApplicationSubjectPtr>> actions_log_;

  MOCK_CONST_METHOD0(type, unity::AppType());
  MOCK_CONST_METHOD0(repr, std::string());
  MOCK_CONST_METHOD0(desktop_id, std::string());
  MOCK_CONST_METHOD0(GetWindows, unity::WindowList const&());
  MOCK_CONST_METHOD1(OwnsWindow, bool(Window));
  MOCK_CONST_METHOD0(GetSupportedMimeTypes, std::vector<std::string>());
  MOCK_CONST_METHOD0(GetFocusableWindow, unity::ApplicationWindowPtr());
  MOCK_CONST_METHOD0(CreateLocalDesktopFile, bool());
  MOCK_CONST_METHOD2(LogEvent, void(unity::ApplicationEventType, unity::ApplicationSubjectPtr const&));
  MOCK_CONST_METHOD2(Focus, void(bool, int));
  MOCK_CONST_METHOD0(Quit, void());

  bool LocalOwnsWindow(Window window_id) const {
    auto end = std::end(windows_);
    return std::find_if(std::begin(windows_), end, [window_id] (unity::ApplicationWindowPtr window) {
      return window->window_id() == window_id;
    }) != end;
  }

  void LocalLogEvent(unity::ApplicationEventType type, unity::ApplicationSubjectPtr const& subject)
  {
    if (subject)
      actions_log_.push_back(std::make_pair(type, subject));
  }

  bool HasLoggedEvent(unity::ApplicationEventType type, unity::ApplicationSubjectPtr const& subject)
  {
    if (!subject)
      return false;

    for (auto const& pair : actions_log_)
    {
      if (pair.first == type && *pair.second == *subject)
        return true;
    }

    return false;
  }

  void SetRunState(bool state) {
    if (running_ == state)
      return;

    running_ = state;
    running.changed.emit(running_);
  }

  void SetVisibility(bool state) {
    if (visible_ == state)
      return;

    visible_ = state;
    visible.changed.emit(visible_);
  }

  bool SetSeen(bool const& param) {
    if (param != seen_) {
      seen_ = param;
      return true;
    }
    return false;
  }

  bool SetSticky(bool const& param) {
    if (param != sticky_) {
      sticky_ = param;
      return true;
    }
    return false;
  }

  void SetActiveState(bool state)
  {
    if (active_ == state)
      return;
    active_ = state;
    active.changed.emit(state);
  }

  void SetTitle(std::string const& new_title)
  {
    if (new_title == title())
      return;

    title_ = new_title;
    title.changed(title_);
  }

  void SetIcon(std::string const& new_icon)
  {
    if (new_icon == icon())
      return;

    icon_ = new_icon;
    icon.changed(icon_);
  }
};

struct MockApplicationSubject : unity::ApplicationSubject
{
  MockApplicationSubject()
  {
    uri.SetSetterFunction([this] (std::string const& val) { if (val == uri_) return false; uri_ = val; return true; });
    uri.SetGetterFunction([this] { return uri_; });
    origin.SetSetterFunction([this] (std::string const& val) { if (val == origin_) return false; origin_ = val; return true; });
    origin.SetGetterFunction([this] { return origin_; });
    text.SetSetterFunction([this] (std::string const& val) { if (val == text_) return false; text_ = val; return true; });
    text.SetGetterFunction([this] { return text_; });
    storage.SetSetterFunction([this] (std::string const& val) { if (val == storage_) return false; storage_ = val; return true; });
    storage.SetGetterFunction([this] { return storage_; });
    current_uri.SetSetterFunction([this] (std::string const& val) { if (val == current_uri_) return false; current_uri_ = val; return true; });
    current_uri.SetGetterFunction([this] { return current_uri_; });
    current_origin.SetSetterFunction([this] (std::string const& val) { if (val == current_origin_) return false; current_origin_ = val; return true; });
    current_origin.SetGetterFunction([this] { return current_origin_; });
    mimetype.SetSetterFunction([this] (std::string const& val) { if (val == mimetype_) return false; mimetype_ = val; return true; });
    mimetype.SetGetterFunction([this] { return mimetype_; });
    interpretation.SetSetterFunction([this] (std::string const& val) { if (val == interpretation_) return false; interpretation_ = val; return true; });
    interpretation.SetGetterFunction([this] { return interpretation_; });
    manifestation.SetSetterFunction([this] (std::string const& val) { if (val == manifestation_) return false; manifestation_ = val; return true; });
    manifestation.SetGetterFunction([this] { return manifestation_; });
  }

  std::string uri_;
  std::string origin_;
  std::string text_;
  std::string storage_;
  std::string current_uri_;
  std::string current_origin_;
  std::string mimetype_;
  std::string interpretation_;
  std::string manifestation_;
};

struct MockApplicationManager : public unity::ApplicationManager
{
  typedef NiceMock<MockApplicationManager> Nice;

  MockApplicationManager()
  {
    ON_CALL(*this, GetUnityApplication()).WillByDefault(Invoke(this, &MockApplicationManager::LocalGetUnityApplication));
    ON_CALL(*this, GetApplicationForDesktopFile(_)).WillByDefault(Invoke(this, &MockApplicationManager::LocalGetApplicationForDesktopFile));
    ON_CALL(*this, GetActiveWindow()).WillByDefault(Invoke([this] { return unity::ApplicationWindowPtr(); } ));
    ON_CALL(*this, GetRunningApplications()).WillByDefault(Invoke([this] { return unity::ApplicationList(); } ));
    ON_CALL(*this, GetApplicationForWindow(_)).WillByDefault(Invoke([this] (Window) { return unity::ApplicationPtr(); } ));
    ON_CALL(*this, GetActiveApplication()).WillByDefault(Invoke([this] { return unity::ApplicationPtr(); } ));
    ON_CALL(*this, GetWindowsForMonitor(_)).WillByDefault(Invoke([this] (Window) { return unity::WindowList(); } ));
    ON_CALL(*this, GetWindowForId(_)).WillByDefault(Invoke([this] (int) { return unity::ApplicationWindowPtr(); } ));
  }

  static void StartApp(std::string const& desktop_file)
  {
    // We know the application manager is a mock one so we can cast it.
    auto& app_manager = unity::ApplicationManager::Default();
    auto self = dynamic_cast<MockApplicationManager*>(&app_manager);
    auto app = self->LocalGetApplicationForDesktopFile(desktop_file);
    app_manager.application_started.emit(app);
  }

  MOCK_CONST_METHOD0(GetUnityApplication, unity::ApplicationPtr());
  MOCK_CONST_METHOD0(GetActiveWindow, unity::ApplicationWindowPtr());
  MOCK_CONST_METHOD1(GetApplicationForDesktopFile, unity::ApplicationPtr(std::string const&));
  MOCK_CONST_METHOD0(GetRunningApplications, unity::ApplicationList());
  MOCK_CONST_METHOD1(GetApplicationForWindow, unity::ApplicationPtr(Window));
  MOCK_CONST_METHOD0(GetActiveApplication, unity::ApplicationPtr());
  MOCK_CONST_METHOD1(GetWindowsForMonitor, unity::WindowList(int));
  MOCK_CONST_METHOD1(GetWindowForId, unity::ApplicationWindowPtr(Window));

  unity::ApplicationPtr LocalGetApplicationForDesktopFile(std::string const& desktop_file)
  {
    AppMap::iterator iter = app_map_.find(desktop_file);
    if (iter == app_map_.end())
    {
      std::string title;
      std::string icon;
      std::shared_ptr<GKeyFile> key_file(g_key_file_new(), g_key_file_free);

      if (g_key_file_load_from_file(key_file.get(), desktop_file.c_str(), G_KEY_FILE_NONE, nullptr))
      {
        title = unity::glib::String(g_key_file_get_string(key_file.get(), G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, nullptr)).Str();
        icon = unity::glib::String(g_key_file_get_string(key_file.get(), G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, nullptr)).Str();
      }

      auto app = std::make_shared<MockApplication::Nice>(desktop_file, icon, title);
      app_map_.insert(AppMap::value_type(desktop_file, app));
      return app;
    }
    else
    {
      return iter->second;
    }
  }

  unity::ApplicationPtr LocalGetUnityApplication() const
  {
    static unity::ApplicationPtr unity(new MockApplication::Nice);
    auto unity_mock = std::static_pointer_cast<MockApplication>(unity);
    unity_mock->desktop_file_ = "compiz.desktop";
    unity_mock->title_ = "Unity Desktop";
    unity_mock->running_ = true;

    return unity;
  }

private:
  typedef std::unordered_map<std::string, unity::ApplicationPtr> AppMap;
  AppMap app_map_;
};

struct TestUnityAppBase : testing::Test
{
  TestUnityAppBase()
  {
    auto const& unity_app = unity::ApplicationManager::Default().GetUnityApplication();
    unity_app_ = std::static_pointer_cast<MockApplication>(unity_app);
  }

  ~TestUnityAppBase()
  {
    Mock::VerifyAndClearExpectations(unity_app_.get());
    unity_app_->actions_log_.clear();
  }

  MockApplication::Ptr unity_app_;
};

}

#endif