~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
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Andrea Azzarone <azzaronea@gmail.com>
 *
 */

#include <gmock/gmock.h>
using namespace testing;
#include <Nux/NuxTimerTickSource.h>
#include <NuxCore/AnimationController.h>
#include "HudController.h"
#include "mock-base-window.h"
#include "unity-shared/DashStyle.h"
#include "unity-shared/PanelStyle.h"
#include "unity-shared/WindowManager.h"
#include "test_utils.h"
using namespace unity;

namespace
{

const unsigned ANIMATION_DURATION = 90 * 1000; // in microseconds
const unsigned TICK_DURATION = 10 * 1000;

class MockHudView : public hud::AbstractView
{
public:
  typedef nux::ObjectPtr<MockHudView> Ptr;

  MOCK_METHOD0(AboutToShow, void());
  MOCK_METHOD0(AboutToHide, void());
  MOCK_METHOD0(Relayout, void());
  MOCK_METHOD0(ResetToDefault, void());
  MOCK_METHOD0(SearchFinished, void());
  MOCK_METHOD4(SetIcon, void(std::string const&, unsigned int tile_size, unsigned int size, unsigned int padding));
  MOCK_METHOD1(SetQueries, void(hud::Hud::Queries queries));
  MOCK_METHOD2(SetMonitorOffset, void(int x, int y));
  MOCK_METHOD1(ShowEmbeddedIcon, void(bool show));
  MOCK_CONST_METHOD0(default_focus, nux::View*());
  MOCK_CONST_METHOD0(GetName, std::string());
  MOCK_METHOD1(AddProperties, void(debug::IntrospectionData&));
  MOCK_METHOD2(Draw, void(nux::GraphicsEngine&, bool));
  nux::Geometry GetContentGeometry()
  {
    return nux::Geometry();
  }
};


struct TestHudController : Test
{
  TestHudController()
    : view_(new NiceMock<MockHudView>)
    , base_window_(new testmocks::MockBaseWindow::Nice())
    , controller_(std::make_shared<hud::Controller>([this] { return view_.GetPointer(); },
                                                    [this] { return base_window_.GetPointer(); }))
  {}

protected:
  // required to create hidden secret global variables
  dash::Style dash_style_;
  panel::Style panel_style_;

  MockHudView::Ptr view_;
  testmocks::MockBaseWindow::Ptr base_window_;
  hud::Controller::Ptr controller_;
};

TEST_F(TestHudController, Construction)
{
  EXPECT_CALL(*base_window_, SetOpacity(0.0f));
  controller_ = std::make_shared<hud::Controller>([this] { return view_.GetPointer(); },
                                                  [this] { return base_window_.GetPointer(); });
}

TEST_F(TestHudController, TestShowAndHideHud)
{
  long long t;
  long long global_tick = 0;
  nux::NuxTimerTickSource tick_source;
  nux::animation::AnimationController animation_controller(tick_source);

  // Verify initial conditions
  EXPECT_EQ(base_window_->GetOpacity(), 0.0);

  // Set expectations for showing the HUD
  EXPECT_CALL(*view_, AboutToShow()).Times(1);
  EXPECT_CALL(*view_, ResetToDefault()).Times(1);
  {
    InSequence showing;
    EXPECT_CALL(*base_window_, SetOpacity(Eq(0.0f))).Times(AtLeast(1));
    EXPECT_CALL(*base_window_, SetOpacity(AllOf(Gt(0.0f), Lt(1.0f))))
      .Times(AtLeast(ANIMATION_DURATION/TICK_DURATION-1));
    EXPECT_CALL(*base_window_, SetOpacity(Eq(1.0f))).Times(AtLeast(1));
  }

  controller_->ShowHud();
  for (t = global_tick; t < global_tick + ANIMATION_DURATION+1; t += TICK_DURATION)
    tick_source.tick(t);
  global_tick += t;

  EXPECT_EQ(base_window_->GetOpacity(), 1.0);

  Mock::VerifyAndClearExpectations(view_.GetPointer());
  Mock::VerifyAndClearExpectations(base_window_.GetPointer());

  // Set expectations for hiding the HUD
  EXPECT_CALL(*view_, AboutToHide()).Times(1);
  {
    InSequence hiding;
    EXPECT_CALL(*base_window_, SetOpacity(Eq(1.0f))).Times(AtLeast(1));
    EXPECT_CALL(*base_window_, SetOpacity(AllOf(Lt(1.0f), Gt(0.0f))))
      .Times(AtLeast(ANIMATION_DURATION/TICK_DURATION-1));
    EXPECT_CALL(*base_window_, SetOpacity(Eq(0.0f))).Times(AtLeast(1));
  }

  controller_->HideHud();
  for (t = global_tick; t < global_tick + ANIMATION_DURATION+1; t += TICK_DURATION)
    tick_source.tick(t);
  global_tick += t;

  EXPECT_EQ(base_window_->GetOpacity(), 0.0);
}

TEST_F(TestHudController, DisconnectWMSignalsOnDestruction)
{
  auto& signal = WindowManager::Default().initiate_spread;
  size_t before = signal.size();
  {
    auto controller = std::make_shared<hud::Controller>([this] { return view_.GetPointer(); },
                                                        [this] { return base_window_.GetPointer(); });
  }
  ASSERT_EQ(before, signal.size());
  signal.emit();
}

}