2
* Copyright 2012 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the applicable version of the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of both the GNU Lesser General Public
15
* License version 3 along with this program. If not, see
16
* <http://www.gnu.org/licenses/>
18
* Authored by: Andrea Azzarone <azzaronea@gmail.com>
22
#include <gmock/gmock.h>
23
using namespace testing;
24
#include <Nux/NuxTimerTickSource.h>
25
#include <NuxCore/AnimationController.h>
26
#include "HudController.h"
27
#include "mock-base-window.h"
28
#include "unity-shared/DashStyle.h"
29
#include "unity-shared/PanelStyle.h"
30
#include "unity-shared/UnitySettings.h"
31
#include "test_utils.h"
32
using namespace unity;
37
const unsigned ANIMATION_DURATION = 90 * 1000; // in microseconds
38
const unsigned TICK_DURATION = 10 * 1000;
40
class MockHudView : public hud::AbstractView
43
typedef nux::ObjectPtr<MockHudView> Ptr;
45
MOCK_METHOD0(AboutToShow, void());
46
MOCK_METHOD0(AboutToHide, void());
47
MOCK_METHOD0(Relayout, void());
48
MOCK_METHOD0(ResetToDefault, void());
49
MOCK_METHOD0(SearchFinished, void());
50
MOCK_METHOD4(SetIcon, void(std::string const&, unsigned int tile_size, unsigned int size, unsigned int padding));
51
MOCK_METHOD1(SetQueries, void(hud::Hud::Queries queries));
52
MOCK_METHOD2(SetMonitorOffset, void(int x, int y));
53
MOCK_METHOD1(ShowEmbeddedIcon, void(bool show));
54
MOCK_CONST_METHOD0(default_focus, nux::View*());
55
MOCK_CONST_METHOD0(GetName, std::string());
56
MOCK_METHOD1(AddProperties, void(GVariantBuilder*));
57
MOCK_METHOD2(Draw, void(nux::GraphicsEngine&, bool));
58
nux::Geometry GetContentGeometry()
60
return nux::Geometry();
65
class TestHudController : public Test
69
: view_(new NiceMock<MockHudView>)
70
, base_window_(new NiceMock<testmocks::MockBaseWindow>())
72
ON_CALL(*base_window_, SetOpacity(_))
73
.WillByDefault(Invoke(base_window_.GetPointer(),
74
&testmocks::MockBaseWindow::RealSetOpacity));
76
// Set expectations for creating the controller
77
EXPECT_CALL(*base_window_, SetOpacity(0.0f));
79
controller_.reset(new hud::Controller([&](){ return view_.GetPointer(); },
80
[&](){ return base_window_.GetPointer();}));
84
hud::Controller::Ptr controller_;
85
MockHudView::Ptr view_;
86
testmocks::MockBaseWindow::Ptr base_window_;
88
// required to create hidden secret global variables
89
Settings unity_settings_;
90
dash::Style dash_style_;
91
panel::Style panel_style_;
95
TEST_F(TestHudController, TestShowAndHideHud)
98
long long global_tick = 0;
99
nux::NuxTimerTickSource tick_source;
100
nux::animation::AnimationController animation_controller(tick_source);
102
// Verify initial conditions
103
EXPECT_EQ(base_window_->GetOpacity(), 0.0);
105
// Set expectations for showing the HUD
106
EXPECT_CALL(*view_, AboutToShow()).Times(1);
107
EXPECT_CALL(*view_, ResetToDefault()).Times(1);
110
EXPECT_CALL(*base_window_, SetOpacity(Eq(0.0f))).Times(AtLeast(1));
111
EXPECT_CALL(*base_window_, SetOpacity(AllOf(Gt(0.0f), Lt(1.0f))))
112
.Times(AtLeast(ANIMATION_DURATION/TICK_DURATION-1));
113
EXPECT_CALL(*base_window_, SetOpacity(Eq(1.0f))).Times(AtLeast(1));
116
controller_->ShowHud();
117
for (t = global_tick; t < global_tick + ANIMATION_DURATION+1; t += TICK_DURATION)
121
EXPECT_EQ(base_window_->GetOpacity(), 1.0);
123
Mock::VerifyAndClearExpectations(view_.GetPointer());
124
Mock::VerifyAndClearExpectations(base_window_.GetPointer());
126
// Set expectations for hiding the HUD
127
EXPECT_CALL(*view_, AboutToHide()).Times(1);
130
EXPECT_CALL(*base_window_, SetOpacity(Eq(1.0f))).Times(AtLeast(1));
131
EXPECT_CALL(*base_window_, SetOpacity(AllOf(Lt(1.0f), Gt(0.0f))))
132
.Times(AtLeast(ANIMATION_DURATION/TICK_DURATION-1));
133
EXPECT_CALL(*base_window_, SetOpacity(Eq(0.0f))).Times(AtLeast(1));
136
controller_->HideHud();
137
for (t = global_tick; t < global_tick + ANIMATION_DURATION+1; t += TICK_DURATION)
141
EXPECT_EQ(base_window_->GetOpacity(), 0.0);