~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
/*
 * 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: Marco Trevisan (TreviƱo) <marco@ubuntu.com>
 *
 */

#include <gmock/gmock.h>

#include <Nux/Nux.h>
#include "PanelStyle.h"
#include "test_standalone_wm.h"
#include "unity-shared/WindowButtons.h"
#include "unity-shared/WindowButtonPriv.h"

namespace unity
{
namespace
{

struct TestWindowButtons : public testing::Test
{
  StandaloneWindow::Ptr AddFakeWindowToWM(Window xid)
  {
    auto fake_window = std::make_shared<StandaloneWindow>(xid);
    wm->AddStandaloneWindow(fake_window);

    return fake_window;
  }

  internal::WindowButton* GetWindowButtonByType(panel::WindowButtonType type)
  {
    for (auto* area : wbuttons.GetChildren())
    {
      auto button = dynamic_cast<internal::WindowButton*>(area);

      if (button && button->GetType() == type)
        return button;
    }

    return nullptr;
  }

  struct MockWindowButtons : WindowButtons
  {
    MOCK_METHOD0(QueueDraw, void());
  };

  panel::Style panel_style;
  testing::NiceMock<MockWindowButtons> wbuttons;
  testwrapper::StandaloneWM wm;
};

TEST_F(TestWindowButtons, Construction)
{
  EXPECT_EQ(wbuttons.monitor(), 0);
  EXPECT_EQ(wbuttons.controlled_window(), 0);
  EXPECT_EQ(wbuttons.opacity(), 1.0f);
  EXPECT_EQ(wbuttons.focused(), true);

  ASSERT_NE(GetWindowButtonByType(panel::WindowButtonType::CLOSE), nullptr);
  ASSERT_NE(GetWindowButtonByType(panel::WindowButtonType::MINIMIZE), nullptr);
  ASSERT_NE(GetWindowButtonByType(panel::WindowButtonType::MAXIMIZE), nullptr);
  ASSERT_NE(GetWindowButtonByType(panel::WindowButtonType::UNMAXIMIZE), nullptr);

  ASSERT_EQ(GetWindowButtonByType(panel::WindowButtonType::CLOSE)->GetParentObject(), &wbuttons);
  ASSERT_EQ(GetWindowButtonByType(panel::WindowButtonType::MINIMIZE)->GetParentObject(), &wbuttons);
  ASSERT_EQ(GetWindowButtonByType(panel::WindowButtonType::MAXIMIZE)->GetParentObject(), &wbuttons);
  ASSERT_EQ(GetWindowButtonByType(panel::WindowButtonType::UNMAXIMIZE)->GetParentObject(), &wbuttons);
}

TEST_F(TestWindowButtons, OpacitySet)
{
  wbuttons.opacity = 0.555f;
  EXPECT_EQ(wbuttons.opacity(), 0.555f);

  wbuttons.opacity = -0.355f;
  EXPECT_EQ(wbuttons.opacity(), 0.0f);

  wbuttons.opacity = 5.355f;
  EXPECT_EQ(wbuttons.opacity(), 1.0f);
}

TEST_F(TestWindowButtons, ChangingOpacityQueuesDraw)
{
  EXPECT_CALL(wbuttons, QueueDraw()).Times(1);
  wbuttons.opacity = 0.555f;

  EXPECT_CALL(wbuttons, QueueDraw()).Times(0);
  wbuttons.opacity = 0.555f;
}

TEST_F(TestWindowButtons, ChangingFocusedQueuesDraw)
{
  EXPECT_CALL(wbuttons, QueueDraw()).Times(1);
  wbuttons.focused = false;

  EXPECT_CALL(wbuttons, QueueDraw()).Times(0);
  wbuttons.focused = false;

  EXPECT_CALL(wbuttons, QueueDraw()).Times(1);
  wbuttons.focused = true;
}

TEST_F(TestWindowButtons, ChangingControlledWindowUpdatesCloseButton)
{
  Window xid = 12345;
  auto fake_win = AddFakeWindowToWM(xid);
  ASSERT_TRUE(fake_win->closable);
  wbuttons.controlled_window = xid;
  EXPECT_TRUE(GetWindowButtonByType(panel::WindowButtonType::CLOSE)->enabled());

  xid = 54321;
  fake_win = AddFakeWindowToWM(xid);
  fake_win->closable = false;
  wbuttons.controlled_window = xid;
  EXPECT_FALSE(GetWindowButtonByType(panel::WindowButtonType::CLOSE)->enabled());
}

TEST_F(TestWindowButtons, ChangingControlledWindowUpdatesMinimizeButton)
{
  Window xid = 12345;
  auto fake_win = AddFakeWindowToWM(xid);
  ASSERT_TRUE(fake_win->minimizable);
  wbuttons.controlled_window = xid;
  EXPECT_TRUE(GetWindowButtonByType(panel::WindowButtonType::MINIMIZE)->enabled());

  xid = 54321;
  fake_win = AddFakeWindowToWM(xid);
  fake_win->minimizable = false;
  wbuttons.controlled_window = xid;
  EXPECT_FALSE(GetWindowButtonByType(panel::WindowButtonType::MINIMIZE)->enabled());
}


struct TestWindowButton : public testing::Test
{
  TestWindowButton()
    : button(panel::WindowButtonType::CLOSE)
  {}

  struct MockWindowButton : internal::WindowButton
  {
    MockWindowButton(panel::WindowButtonType type)
      : internal::WindowButton(type)
    {}

    MOCK_METHOD0(QueueDraw, void());
  };

  panel::Style panel_style;
  testing::NiceMock<MockWindowButton> button;
};

TEST_F(TestWindowButton, Construction)
{
  EXPECT_EQ(button.GetType(), panel::WindowButtonType::CLOSE);
  EXPECT_TRUE(button.enabled());
  EXPECT_FALSE(button.overlay_mode());
}

TEST_F(TestWindowButton, ChangingOverlayModeQueueDraws)
{
  EXPECT_CALL(button, QueueDraw()).Times(1);
  button.overlay_mode = true;
}

TEST_F(TestWindowButton, EnableProperty)
{
  ASSERT_TRUE(button.enabled());
  EXPECT_EQ(button.enabled(), button.IsViewEnabled());

  EXPECT_CALL(button, QueueDraw()).Times(1);
  button.enabled = false;
  ASSERT_FALSE(button.enabled());
  EXPECT_EQ(button.enabled(), button.IsViewEnabled());

  EXPECT_CALL(button, QueueDraw()).Times(0);
  button.enabled = false;
  ASSERT_FALSE(button.enabled());
  EXPECT_EQ(button.enabled(), button.IsViewEnabled());
}
}
}