Merge lp:~mandel/unity/fix-static-cairo-text into lp:unity

Proposed by Manuel de la Peña
Status: Merged
Approved by: Nick Dedekind
Approved revision: no longer in the source branch.
Merged at revision: 2971
Proposed branch: lp:~mandel/unity/fix-static-cairo-text
Merge into: lp:unity
Diff against target: 367 lines (+182/-34)
7 files modified
launcher/LauncherIcon.cpp (+3/-3)
launcher/Tooltip.cpp (+19/-14)
launcher/Tooltip.h (+7/-6)
tests/CMakeLists.txt (+1/-0)
tests/test_launcher_tooltip.cpp (+109/-0)
tests/test_static_cairo_text.cpp (+33/-5)
unity-shared/StaticCairoText.cpp (+10/-6)
To merge this branch: bzr merge lp:~mandel/unity/fix-static-cairo-text
Reviewer Review Type Date Requested Status
Marco Trevisan (Treviño) Approve
Nick Dedekind (community) Needs Fixing
Brandon Schaefer (community) Approve
Tim Penhey Pending
PS Jenkins bot continuous-integration Pending
Review via email: mp+138966@code.launchpad.net

This proposal supersedes a proposal from 2012-12-05.

Description of the change

Fixes #1071327 by ensuring that the PreLayoutManagement method does not call SetBaseSize unless is really needed else (if it is called) the method will be called again making the nux layout get into an infinite loop.

Tests can be ran doing ./tests/test-gtest --gtest_filter=*TestStaticCairoText* from the build dir. In order to run the launcher tooltip tests please do ./tests/test-gtest --gtest_filter=*Tooltip* and run the launcher to assert that tooltips work as expected.

To post a comment you must log in.
Revision history for this message
Nick Dedekind (nick-dedekind) wrote : Posted in a previous version of this proposal

LGTM.
Tests provided succeed.

review: Approve
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote : Posted in a previous version of this proposal

Waiting for a proper fix in nux, this is fine.

review: Approve
Revision history for this message
Brandon Schaefer (brandontschaefer) wrote : Posted in a previous version of this proposal

This causes a regression in the tooltips...

http://i.imgur.com/cVZUb.jpg

review: Needs Fixing
Revision history for this message
Tim Penhey (thumper) wrote : Posted in a previous version of this proposal
Revision history for this message
Tim Penhey (thumper) wrote : Posted in a previous version of this proposal

A test should be added to work out why tooltip rendering broke, and make the tests pass.

review: Needs Fixing
Revision history for this message
Manuel de la Peña (mandel) wrote : Posted in a previous version of this proposal

Sorry, I did not check that the launcher would work correctly and I just focused on the Dash. Is a very simple fix will push asap (The min height of the label has to be the one returned by extend).

Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Fix works ok and test passes, but since you're there... Could you please get rid of the _labelText variable (you can just use the StaticCairoText::GetText where needed)? It looks like a duplication to me.

150 + Settings* settings;

No need a pointer here... Just add Settings settings;

Revision history for this message
Brandon Schaefer (brandontschaefer) wrote :

Looks good to me now. Tooltip renders correctly.

review: Approve
Revision history for this message
Nick Dedekind (nick-dedekind) wrote :

57 + text.SetSetterFunction([this](std::string newText)
58 + {
59 + this->_tooltip_text->SetText(newText);
60 + return true;
61 + }
62 + );c

You can't just return true; otherwise the property changed signal gets emitted every time you call set. It should only return true if the value of the text has changed, otherwise false.

48 + _tooltip_text = new nux::StaticCairoText(TEXT("Unity"), NUX_TRACKER_LOCATION);
271 + EXPECT_NE(new_tip, tooltip->_tooltip_text->GetText());

Don't know why tool-tips are being initialised to TEXT("Unity"). They should be blank by default.
And then you can change the test to:
271 + EXPECT_EQ("", tooltip->_tooltip_text->GetText());

Otherwise, LGTM.
Builds OK for me, runs and passes test.

review: Approve
Revision history for this message
Nick Dedekind (nick-dedekind) :
review: Needs Fixing
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Thanks for the property fixes, it looks ok now.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'launcher/LauncherIcon.cpp'
2--- launcher/LauncherIcon.cpp 2012-12-03 15:34:23 +0000
3+++ launcher/LauncherIcon.cpp 2012-12-11 15:48:21 +0000
4@@ -147,7 +147,7 @@
5 _tooltip = new Tooltip();
6 AddChild(_tooltip.GetPointer());
7
8- _tooltip->SetText(tooltip_text());
9+ _tooltip->text = tooltip_text();
10 }
11
12 void LauncherIcon::LoadQuicklist()
13@@ -475,7 +475,7 @@
14 {
15 target = escaped;
16 if (_tooltip)
17- _tooltip->SetText(target);
18+ _tooltip->text = target;
19 result = true;
20 }
21
22@@ -520,7 +520,7 @@
23
24 if (!_tooltip)
25 LoadTooltip();
26- _tooltip->SetText(tooltip_text());
27+ _tooltip->text = tooltip_text();
28 _tooltip->ShowTooltipWithTipAt(tip_x, tip_y);
29 _tooltip->ShowWindow(!tooltip_text().empty());
30 tooltip_visible.emit(_tooltip);
31
32=== modified file 'launcher/Tooltip.cpp'
33--- launcher/Tooltip.cpp 2012-11-06 18:19:09 +0000
34+++ launcher/Tooltip.cpp 2012-12-11 15:48:21 +0000
35@@ -47,7 +47,6 @@
36 Tooltip::Tooltip() :
37 _anchorX(0),
38 _anchorY(0),
39- _labelText(TEXT("Unity")),
40 _cairo_text_has_changed(true)
41 {
42 _hlayout = new nux::HLayout(TEXT(""), NUX_TRACKER_LOCATION);
43@@ -61,7 +60,7 @@
44
45 _vlayout->AddLayout(_top_space, 0);
46
47- _tooltip_text = new nux::StaticCairoText(_labelText, NUX_TRACKER_LOCATION);
48+ _tooltip_text = new nux::StaticCairoText(TEXT(""), NUX_TRACKER_LOCATION);
49 _tooltip_text->SetTextAlignment(nux::StaticCairoText::AlignState::NUX_ALIGN_CENTRE);
50 _tooltip_text->SetTextVerticalAlignment(nux::StaticCairoText::AlignState::NUX_ALIGN_CENTRE);
51 _tooltip_text->SetMinimumWidth(MINIMUM_TEXT_WIDTH);
52@@ -69,6 +68,22 @@
53 _tooltip_text->sigTextChanged.connect(sigc::mem_fun(this, &Tooltip::RecvCairoTextChanged));
54 _tooltip_text->sigFontChanged.connect(sigc::mem_fun(this, &Tooltip::RecvCairoTextChanged));
55
56+ // getter and setter for the property
57+ text.SetSetterFunction([this](std::string newText)
58+ {
59+ if(_tooltip_text->GetText() == newText)
60+ return false;
61+
62+ _tooltip_text->SetText(newText);
63+ return true;
64+ }
65+ );
66+ text.SetGetterFunction([this]()
67+ {
68+ return _tooltip_text->GetText();
69+ }
70+ );
71+
72 _vlayout->AddView(_tooltip_text.GetPointer(), 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
73
74 _vlayout->AddLayout(_bottom_space, 0);
75@@ -129,6 +144,7 @@
76 }
77
78 _tooltip_text->SetMinimumWidth(text_min_width);
79+ _tooltip_text->SetMinimumHeight(text_height);
80
81 if (text_height < ANCHOR_HEIGHT)
82 {
83@@ -539,17 +555,6 @@
84 {
85 }
86
87-void Tooltip::SetText(std::string const& text)
88-{
89- if (_labelText == text)
90- return;
91-
92- _labelText = text;
93- _tooltip_text->SetText(_labelText);
94-
95- QueueRelayout();
96-}
97-
98 // Introspection
99
100 std::string Tooltip::GetName() const
101@@ -560,7 +565,7 @@
102 void Tooltip::AddProperties(GVariantBuilder* builder)
103 {
104 variant::BuilderWrapper(builder)
105- .add("text", _labelText)
106+ .add("text", text)
107 .add("x", GetBaseX())
108 .add("y", GetBaseY())
109 .add("width", GetBaseWidth())
110
111=== modified file 'launcher/Tooltip.h'
112--- launcher/Tooltip.h 2012-07-09 13:34:41 +0000
113+++ launcher/Tooltip.h 2012-12-11 15:48:21 +0000
114@@ -39,11 +39,11 @@
115 public:
116 Tooltip();
117
118+ nux::RWProperty<std::string> text;
119+
120 void Draw(nux::GraphicsEngine& gfxContext, bool forceDraw);
121 void DrawContent(nux::GraphicsEngine& gfxContext, bool forceDraw);
122
123- void SetText(std::string const& text);
124-
125 void ShowTooltipWithTipAt(int anchor_tip_x, int anchor_tip_y);
126
127 // Introspection
128@@ -52,11 +52,15 @@
129
130 virtual nux::Area* FindAreaUnderMouse(const nux::Point& mouse_position, nux::NuxEventType event_type);
131
132-private:
133+protected:
134+ // protected to simplify testing
135+ nux::ObjectPtr<nux::StaticCairoText> _tooltip_text;
136+
137 void RecvCairoTextChanged(nux::StaticCairoText* cairo_text);
138
139 void PreLayoutManagement();
140
141+private:
142 long PostLayoutManagement(long layoutResult);
143
144 void PositionChildLayout(float offsetX,
145@@ -69,9 +73,6 @@
146
147 int _anchorX;
148 int _anchorY;
149- std::string _labelText;
150-
151- nux::ObjectPtr<nux::StaticCairoText> _tooltip_text;
152
153 nux::HLayout* _hlayout;
154 nux::VLayout* _vlayout;
155
156=== modified file 'tests/CMakeLists.txt'
157--- tests/CMakeLists.txt 2012-12-11 02:33:06 +0000
158+++ tests/CMakeLists.txt 2012-12-11 15:48:21 +0000
159@@ -219,6 +219,7 @@
160 test_launcher_hide_machine.cpp
161 test_launcher_hover_machine.cpp
162 test_launcher_icon.cpp
163+ test_launcher_tooltip.cpp
164 test_keyboard_util.cpp
165 test_panel_style.cpp
166 test_panel_menu_view.cpp
167
168=== added file 'tests/test_launcher_tooltip.cpp'
169--- tests/test_launcher_tooltip.cpp 1970-01-01 00:00:00 +0000
170+++ tests/test_launcher_tooltip.cpp 2012-12-11 15:48:21 +0000
171@@ -0,0 +1,109 @@
172+/*
173+ * Copyright 2012 Canonical Ltd.
174+ *
175+ * This program is free software: you can redistribute it and/or modify it
176+ * under the terms of the GNU Lesser General Public License version 3, as
177+ * published by the Free Software Foundation.
178+ *
179+ * This program is distributed in the hope that it will be useful, but
180+ * WITHOUT ANY WARRANTY; without even the implied warranties of
181+ * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
182+ * PURPOSE. See the applicable version of the GNU Lesser General Public
183+ * License for more details.
184+ *
185+ * You should have received a copy of both the GNU Lesser General Public
186+ * License version 3 along with this program. If not, see
187+ * <http://www.gnu.org/licenses/>
188+ *
189+ * Authored by: Manuel de la Pena <manuel.delapena@canonical.com>
190+ *
191+ */
192+
193+#include <gmock/gmock.h>
194+#include <gtest/gtest.h>
195+
196+#include "unity-shared/StaticCairoText.h"
197+#include "launcher/Tooltip.h"
198+
199+#include <unity-shared/UnitySettings.h>
200+#include "test_utils.h"
201+
202+namespace unity
203+{
204+
205+class MockStaticCairoText : public nux::StaticCairoText
206+{
207+public:
208+ MOCK_METHOD1(SetMinimumWidth, void(int));
209+ MOCK_METHOD1(SetMinimumHeight, void(int));
210+
211+ MockStaticCairoText(std::string const& text):StaticCairoText(text) {}
212+
213+}; // StaticCairoText
214+
215+class TooltipMock : public Tooltip
216+{
217+public:
218+
219+ TooltipMock() : Tooltip()
220+ {
221+ // change the text and reconnect it as it should
222+ std::string old_text = _tooltip_text->GetText();
223+ _tooltip_text = new MockStaticCairoText(old_text);
224+ _tooltip_text->SetTextAlignment(
225+ nux::StaticCairoText::AlignState::NUX_ALIGN_CENTRE);
226+ _tooltip_text->SetTextVerticalAlignment(
227+ nux::StaticCairoText::AlignState::NUX_ALIGN_CENTRE);
228+
229+ _tooltip_text->sigTextChanged.connect(
230+ sigc::mem_fun(this, &TooltipMock::RecvCairoTextChanged));
231+ _tooltip_text->sigFontChanged.connect(
232+ sigc::mem_fun(this, &TooltipMock::RecvCairoTextChanged));
233+ }
234+
235+ using Tooltip::_tooltip_text;
236+ using Tooltip::RecvCairoTextChanged;
237+ using Tooltip::PreLayoutManagement;
238+
239+}; // TooltipMock
240+
241+
242+class TestTooltip : public ::testing::Test
243+{
244+protected:
245+ TestTooltip() : Test()
246+ {
247+ Settings settings;
248+ tooltip = new TooltipMock();
249+ }
250+
251+ nux::ObjectPtr<TooltipMock> tooltip;
252+}; // TestTooltip
253+
254+TEST_F(TestTooltip, StaticCairoTextCorrectSize)
255+{
256+ int text_width;
257+ int text_height;
258+ tooltip->_tooltip_text->GetTextExtents(text_width, text_height);
259+
260+ // do assert that the methods are called with at least the min size provided
261+ // by the GetTextExtents method
262+ MockStaticCairoText* text = dynamic_cast<MockStaticCairoText*>(
263+ tooltip->_tooltip_text.GetPointer());
264+
265+ EXPECT_CALL(*text, SetMinimumWidth(testing::Ge(text_width)));
266+ EXPECT_CALL(*text, SetMinimumHeight(testing::Ge(text_height)));
267+
268+ tooltip->PreLayoutManagement();
269+}
270+
271+TEST_F(TestTooltip, TestSetTooltipText)
272+{
273+ std::string new_tip = "My tooltip";
274+ EXPECT_EQ("", tooltip->_tooltip_text->GetText());
275+
276+ tooltip->text.Set(new_tip);
277+ EXPECT_EQ(new_tip, tooltip->_tooltip_text->GetText());
278+}
279+
280+} // unity
281
282=== modified file 'tests/test_static_cairo_text.cpp'
283--- tests/test_static_cairo_text.cpp 2012-12-10 00:32:30 +0000
284+++ tests/test_static_cairo_text.cpp 2012-12-11 15:48:21 +0000
285@@ -33,17 +33,33 @@
286 class MockStaticCairoText : public nux::StaticCairoText
287 {
288 public:
289+ MOCK_METHOD2(SetBaseSize, void(int, int));
290+
291 MockStaticCairoText():StaticCairoText("") {}
292
293 using StaticCairoText::GetTextureStartIndices;
294 using StaticCairoText::GetTextureEndIndices;
295-};
296-
297-TEST(TestStaticCairoText, TextTextureSize)
298-{
299+ using StaticCairoText::PreLayoutManagement;
300+};
301+
302+class TestStaticCairoText : public ::testing::Test
303+{
304+
305+protected:
306+ TestStaticCairoText() : Test()
307+ {
308+ text = new MockStaticCairoText();
309+ }
310+
311+ nux::ObjectPtr<MockStaticCairoText> text;
312+
313+};
314+
315+TEST_F(TestStaticCairoText, TextTextureSize)
316+{
317+ EXPECT_CALL(*text.GetPointer(), SetBaseSize(_, _)).Times(AnyNumber());
318 // Test multi-texture stitching support.
319
320- nux::ObjectPtr<MockStaticCairoText> text(new MockStaticCairoText());
321 text->SetLines(-2000);
322 text->SetMaximumWidth(100);
323
324@@ -70,4 +86,16 @@
325 }
326 }
327
328+TEST_F(TestStaticCairoText, TextPreLayoutManagementMultipleCalls)
329+{
330+ EXPECT_CALL(*text.GetPointer(), SetBaseSize(_, _)).Times(2);
331+ text->PreLayoutManagement();
332+
333+ // the first prelayout methods should have called set base size and therefore
334+ // we should not call it again
335+
336+ EXPECT_CALL(*text.GetPointer(), SetBaseSize(_, _)).Times(0);
337+ text->PreLayoutManagement();
338+}
339+
340 }
341
342=== modified file 'unity-shared/StaticCairoText.cpp'
343--- unity-shared/StaticCairoText.cpp 2012-12-10 00:32:30 +0000
344+++ unity-shared/StaticCairoText.cpp 2012-12-11 15:48:21 +0000
345@@ -229,12 +229,16 @@
346 void StaticCairoText::PreLayoutManagement()
347 {
348 Geometry geo = GetGeometry();
349- pimpl->pre_layout_size_.width = geo.width;
350- pimpl->pre_layout_size_.height = geo.height;
351-
352- SetBaseSize(pimpl->cached_extent_.width,
353- pimpl->cached_extent_.height);
354-
355+
356+ if(pimpl->pre_layout_size_.width != geo.width
357+ || pimpl->pre_layout_size_.height != geo.height)
358+ {
359+ pimpl->pre_layout_size_.width = geo.width;
360+ pimpl->pre_layout_size_.height = geo.height;
361+
362+ SetBaseSize(pimpl->cached_extent_.width,
363+ pimpl->cached_extent_.height);
364+ }
365 if (pimpl->textures2D_.empty())
366 {
367 pimpl->UpdateTexture();