~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
/*
 * 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: Andrea Azzarone <andrea.azzarone@canonical.com>
 *
 */

#include <gio/gio.h>
#include <gmock/gmock.h>

#include "UnitySettings.h"
#include "test_utils.h"

#include <NuxCore/Logger.h>
#include <UnityCore/GLibWrapper.h>

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

  SigReceiver(std::shared_ptr<unity::Settings> const& settings)
  {
   settings->form_factor.changed.connect(sigc::mem_fun(this, &SigReceiver::FormFactorChanged));
  }

  MOCK_CONST_METHOD1(FormFactorChanged, void(unity::FormFactor));
};

struct TestUnitySettings : testing::Test
{
  unity::glib::Object<GSettings> gsettings;
  std::shared_ptr<unity::Settings> unity_settings;
  SigReceiver::Nice sig_receiver;

  TestUnitySettings()
   : gsettings(g_settings_new("com.canonical.Unity"))
   , unity_settings(std::make_shared<unity::Settings>())
   , sig_receiver(unity_settings)
  {
    g_settings_set_enum(gsettings, "form-factor", static_cast<int>(unity::FormFactor::DESKTOP));
  }

  ~TestUnitySettings()
  {
    sig_receiver.notify_callbacks();
    g_settings_reset(gsettings, "form-factor");
  }
};

TEST_F(TestUnitySettings, SetFormFactor)
{
  unity_settings->form_factor = unity::FormFactor::NETBOOK;

  int raw_form_factor = g_settings_get_enum(gsettings, "form-factor");
  EXPECT_EQ(raw_form_factor, static_cast<int>(unity::FormFactor::NETBOOK));
}

TEST_F(TestUnitySettings, GetFormFactor)
{
  ASSERT_NE(unity_settings->form_factor(), unity::FormFactor::NETBOOK);

  g_settings_set_enum(gsettings, "form-factor", static_cast<int>(unity::FormFactor::NETBOOK));
  EXPECT_EQ(unity_settings->form_factor(), unity::FormFactor::NETBOOK);
}

TEST_F(TestUnitySettings, FormFactorChangedSignal_Extern)
{
  EXPECT_CALL(sig_receiver, FormFactorChanged(unity::FormFactor::NETBOOK));

  g_settings_set_enum(gsettings, "form-factor", static_cast<int>(unity::FormFactor::NETBOOK));
}

TEST_F(TestUnitySettings, FormFactorChangedSignal_Extern_OtherKeys)
{
  EXPECT_CALL(sig_receiver, FormFactorChanged(testing::_)).Times(0);

  g_settings_set_int(gsettings, "minimize-count", 0);
  Utils::WaitForTimeoutMSec(100);
}

TEST_F(TestUnitySettings, FormFactorChangedSignal_Inter)
{
  EXPECT_CALL(sig_receiver, FormFactorChanged(unity::FormFactor::NETBOOK));

  unity_settings->form_factor = unity::FormFactor::NETBOOK;
}

}