Merge lp:~azzar1/unity/lp-1155659 into lp:unity

Proposed by Andrea Azzarone
Status: Merged
Approved by: Marco Trevisan (Treviño)
Approved revision: no longer in the source branch.
Merged at revision: 3220
Proposed branch: lp:~azzar1/unity/lp-1155659
Merge into: lp:unity
Diff against target: 334 lines (+115/-112)
4 files modified
launcher/Launcher.cpp (+1/-2)
launcher/TooltipManager.cpp (+39/-73)
launcher/TooltipManager.h (+10/-11)
tests/test_tooltip_manager.cpp (+65/-26)
To merge this branch: bzr merge lp:~azzar1/unity/lp-1155659
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Marco Trevisan (Treviño) Approve
Review via email: mp+153577@code.launchpad.net

Commit message

Add more unit tests for TooltipManager. Don't reset time on every mouse movement.
Also reduce tooltip timeout lenght.

Description of the change

== Problem ==
Tooltip timeout should not be resetted on each mouse movement.

== Test ==
unit tests added.

To post a comment you must log in.
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Works good, tests pass! :)

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'launcher/Launcher.cpp'
2--- launcher/Launcher.cpp 2013-03-14 20:28:48 +0000
3+++ launcher/Launcher.cpp 2013-03-15 15:34:25 +0000
4@@ -303,7 +303,6 @@
5 icon->mouse_enter.emit(monitor);
6
7 _icon_under_mouse = icon;
8- tooltip_manager_.SetIcon(icon);
9 }
10
11 bool Launcher::MouseBeyondDragThreshold() const
12@@ -2224,7 +2223,7 @@
13 void Launcher::RecvMouseMove(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
14 {
15 SetMousePosition(x, y);
16- tooltip_manager_.MouseMoved();
17+ tooltip_manager_.MouseMoved(_icon_under_mouse);
18
19 if (!_hidden)
20 UpdateChangeInMousePosition(dx, dy);
21
22=== modified file 'launcher/TooltipManager.cpp'
23--- launcher/TooltipManager.cpp 2013-03-15 12:34:14 +0000
24+++ launcher/TooltipManager.cpp 2013-03-15 15:34:25 +0000
25@@ -15,6 +15,7 @@
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 * Authored by: Jacob Edwards <j.johan.edwards@gmail.com>
29+ * Andrea Azzarone <andrea.azzarone@canonical.com>
30 */
31
32 #include "TooltipManager.h"
33@@ -23,104 +24,69 @@
34 {
35 namespace launcher
36 {
37-
38 namespace
39 {
40-const unsigned int TOOLTIPS_SHOW_TIMEOUT_LENGTH = 1000;
41+const unsigned int TOOLTIPS_SHOW_TIMEOUT_LENGTH = 500;
42 }
43
44 TooltipManager::TooltipManager()
45- : show_tooltips_(false)
46- , hovered_(false)
47- , timer_locked_(false)
48+ : skip_timeout_(false)
49 {}
50
51-void TooltipManager::SetIcon(AbstractLauncherIcon::Ptr const& new_icon)
52-{
53- if (icon_ == new_icon)
54- return;
55-
56- // Unlock hover timer, in case the previous icon had no valid tooltip
57- timer_locked_ = false;
58-
59- if (show_tooltips_)
60- {
61- // Show new tooltip, get rid of the old olne
62- if (icon_)
63- icon_->HideTooltip();
64- if (new_icon)
65- new_icon->ShowTooltip();
66- }
67- else if (!new_icon)
68- {
69- // Stop the hover timer for null launcher space
70- StopTimer();
71- }
72- else
73- {
74- AbstractLauncherIcon::IconType type = new_icon->GetIconType();
75- if ((type == AbstractLauncherIcon::IconType::HOME ||
76- type == AbstractLauncherIcon::IconType::HUD) &&
77- new_icon->GetQuirk(AbstractLauncherIcon::Quirk::ACTIVE))
78- {
79- // Lock the hover timer for no valid tooltip cases
80- timer_locked_ = true;
81- StopTimer();
82- }
83- }
84-
85- icon_ = new_icon;
86-}
87-
88-void TooltipManager::SetHover(bool on_launcher)
89-{
90- if (hovered_ == on_launcher)
91- return;
92- hovered_ = on_launcher;
93-
94- if (show_tooltips_ && !hovered_)
95- {
96- show_tooltips_ = false;
97- if (icon_)
98- icon_->HideTooltip();
99- }
100-}
101-
102-void TooltipManager::MouseMoved()
103+void TooltipManager::MouseMoved(AbstractLauncherIcon::Ptr const& icon_under_mouse)
104 {
105- if (!icon_ || show_tooltips_)
106+ if (icon_ == icon_under_mouse)
107 return;
108
109- ResetTimer();
110+ StopTimer();
111+ if (icon_)
112+ icon_->HideTooltip();
113+
114+ icon_ = icon_under_mouse;
115+
116+ if (icon_ && !skip_timeout_)
117+ ResetTimer(icon_);
118+ else if (icon_ && skip_timeout_)
119+ icon_->ShowTooltip();
120 }
121
122 void TooltipManager::IconClicked()
123 {
124+ if (icon_)
125+ icon_->HideTooltip();
126+}
127+
128+void TooltipManager::SetHover(bool hovered)
129+{
130+ if (!hovered)
131+ Reset();
132+}
133+
134+void TooltipManager::Reset()
135+{
136 StopTimer();
137- if (show_tooltips_ && icon_)
138+
139+ if (icon_)
140 icon_->HideTooltip();
141
142- show_tooltips_ = false;
143- timer_locked_ = true;
144+ icon_ = AbstractLauncherIcon::Ptr();
145+ skip_timeout_ = false;
146 }
147
148-void TooltipManager::ResetTimer()
149+void TooltipManager::ResetTimer(AbstractLauncherIcon::Ptr const& icon_under_mouse)
150 {
151- if (timer_locked_)
152- return;
153-
154 hover_timer_.reset(new glib::Timeout(TOOLTIPS_SHOW_TIMEOUT_LENGTH));
155 hover_timer_->Run([&] () {
156- show_tooltips_ = true;
157- icon_->ShowTooltip();
158+ skip_timeout_ = true;
159+ icon_under_mouse->ShowTooltip();
160 return false;
161 });
162-}
163-
164+ }
165+
166 void TooltipManager::StopTimer()
167 {
168- hover_timer_.reset();
169+ hover_timer_.reset();
170 }
171
172-} // namespace launcher
173-} // namespace unity
174+}
175+}
176
177=== modified file 'launcher/TooltipManager.h'
178--- launcher/TooltipManager.h 2013-03-15 12:34:14 +0000
179+++ launcher/TooltipManager.h 2013-03-15 15:34:25 +0000
180@@ -15,10 +15,11 @@
181 * along with this program. If not, see <http://www.gnu.org/licenses/>.
182 *
183 * Authored by: Jacob Edwards <j.johan.edwards@gmail.com>
184+ * Andrea Azzarone <andrea.azzarone@canonical.com>
185 */
186
187-#ifndef TOOLTIPMANAGER
188-#define TOOLTIPMANAGER
189+#ifndef LAUNCHER_TOOLTIP_MANAGER_H
190+#define LAUNCHER_TOOLTIP_MANAGER_H
191
192 #include <boost/noncopyable.hpp>
193 #include <UnityCore/GLibSource.h>
194@@ -35,23 +36,21 @@
195 public:
196 TooltipManager();
197
198- void SetHover(bool on_launcher);
199- void SetIcon(AbstractLauncherIcon::Ptr const& new_icon);
200- void MouseMoved();
201+ void SetHover(bool hovered);
202+ void MouseMoved(AbstractLauncherIcon::Ptr const& icon_under_mouse);
203 void IconClicked();
204
205 private:
206- void ResetTimer();
207+ void Reset();
208+ void ResetTimer(AbstractLauncherIcon::Ptr const& icon_under_mouse);
209 void StopTimer();
210
211- bool show_tooltips_;
212- bool hovered_;
213+ bool skip_timeout_;
214 AbstractLauncherIcon::Ptr icon_;
215 glib::Source::UniquePtr hover_timer_;
216- bool timer_locked_;
217 };
218
219-} // namespace launcher
220-} // namespace unity
221+}
222+}
223
224 #endif
225
226=== modified file 'tests/test_tooltip_manager.cpp'
227--- tests/test_tooltip_manager.cpp 2013-03-05 18:51:05 +0000
228+++ tests/test_tooltip_manager.cpp 2013-03-15 15:34:25 +0000
229@@ -15,40 +15,79 @@
230 * along with this program. If not, see <http://www.gnu.org/licenses/>.
231 *
232 * Authored by: Jacob Edwards <j.johan.edwards@gmail.com>
233+ * Andrea Azzarone <andrea.azzarone@gmail.com>
234 */
235
236-#include <gtest/gtest.h>
237+#include <gmock/gmock.h>
238 using namespace testing;
239
240 #include "launcher/TooltipManager.h"
241 #include "launcher/MockLauncherIcon.h"
242+using unity::launcher::MockLauncherIcon;
243+using unity::launcher::TooltipManager;
244+
245 #include "test_utils.h"
246
247-namespace unity
248-{
249-namespace launcher
250-{
251-
252 namespace
253 {
254
255-TEST(TestTooltipManager, TestHideAndShowTooltip)
256-{
257- // Makes sure that TooltipManager calls icon->ShowTooltip() when the mouse
258- // hovers it, and icon->HideTooltip() after the mouse dehovers it.
259- TooltipManager tm;
260- MockLauncherIcon* icon = new MockLauncherIcon();
261-
262- tm.SetIcon(AbstractLauncherIcon::Ptr(icon));
263- tm.MouseMoved();
264- Utils::WaitForTimeoutMSec(1050);
265-
266- EXPECT_TRUE(icon->IsTooltipVisible());
267- tm.SetIcon(AbstractLauncherIcon::Ptr());
268- EXPECT_FALSE(icon->IsTooltipVisible());
269-}
270-
271-}
272-
273-} // launcher
274-} // unity
275+bool CheckIsTooltipVisible(nux::ObjectPtr<MockLauncherIcon> const& icon, bool value) {
276+ return icon->IsTooltipVisible() == value;
277+}
278+
279+struct TestTooltipManager : public Test {
280+ TooltipManager tooltip_manager;
281+};
282+
283+TEST_F(TestTooltipManager, MouseMoved)
284+{
285+ nux::ObjectPtr<MockLauncherIcon> icon1(new MockLauncherIcon());
286+ nux::ObjectPtr<MockLauncherIcon> icon2(new MockLauncherIcon());
287+
288+ tooltip_manager.MouseMoved(icon1);
289+ ASSERT_FALSE(icon1->IsTooltipVisible()); // don't skip the timeout!
290+ Utils::WaitUntil(std::bind(CheckIsTooltipVisible, icon1, true));
291+ Utils::WaitUntil(std::bind(CheckIsTooltipVisible, icon2, false));
292+
293+ tooltip_manager.MouseMoved(icon2);
294+ ASSERT_FALSE(icon1->IsTooltipVisible());
295+ ASSERT_TRUE(icon2->IsTooltipVisible());
296+
297+ tooltip_manager.MouseMoved(nux::ObjectPtr<MockLauncherIcon>());
298+ ASSERT_FALSE(icon1->IsTooltipVisible());
299+ ASSERT_FALSE(icon2->IsTooltipVisible());
300+
301+ tooltip_manager.MouseMoved(icon1);
302+ ASSERT_TRUE(icon1->IsTooltipVisible());
303+ ASSERT_FALSE(icon2->IsTooltipVisible());
304+}
305+
306+TEST_F(TestTooltipManager, IconClicked)
307+{
308+ nux::ObjectPtr<MockLauncherIcon> icon(new MockLauncherIcon());
309+
310+ tooltip_manager.MouseMoved(icon);
311+ ASSERT_FALSE(icon->IsTooltipVisible()); // don't skip the timeout!
312+ Utils::WaitUntil(std::bind(CheckIsTooltipVisible, icon, true));
313+
314+ tooltip_manager.IconClicked();
315+ ASSERT_FALSE(icon->IsTooltipVisible());
316+}
317+
318+TEST_F(TestTooltipManager, SetHover)
319+{
320+ nux::ObjectPtr<MockLauncherIcon> icon(new MockLauncherIcon());
321+
322+ tooltip_manager.MouseMoved(icon);
323+ ASSERT_FALSE(icon->IsTooltipVisible()); // don't skip the timeout!
324+ Utils::WaitUntil(std::bind(CheckIsTooltipVisible, icon, true));
325+
326+ tooltip_manager.SetHover(false);
327+ ASSERT_FALSE(icon->IsTooltipVisible());
328+
329+ tooltip_manager.MouseMoved(icon);
330+ ASSERT_FALSE(icon->IsTooltipVisible()); // don't skip the timeout!
331+ Utils::WaitUntil(std::bind(CheckIsTooltipVisible, icon, true));
332+}
333+
334+}