summaryrefslogtreecommitdiff
diff options
authorManuel de la Pena <manuel@canonical.com>2012-11-22 18:12:58 +0000
committerTarmac <>2012-11-22 18:12:58 +0000
commitc71ac9d8174a75ca7e0312e898c43eabad8729b4 (patch)
tree77ef926064ee8857065365727510acccef039c2c
parentc145e2bc4de32143a01eaa4c761f13b46fe67f6e (diff)
parentac05560a928494046dcce1f6872e3e7330d522c6 (diff)
- Add tests for the text entry. Tests can be executed from the build dir doing: ./tests/test-gtest --gtest_filter=TestTextInput.*.
Approved by Marco Trevisan (TreviƱo), Nick Dedekind. (bzr r2925)
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/test_text_input.cpp116
-rw-r--r--unity-shared/StaticCairoText.cpp5
-rw-r--r--unity-shared/StaticCairoText.h1
-rw-r--r--unity-shared/TextInput.cpp55
-rw-r--r--unity-shared/TextInput.h41
6 files changed, 153 insertions, 66 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 26e80dd29..71f55b6fc 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -243,6 +243,7 @@ if (ENABLE_X_SUPPORT)
test_switcher_controller.cpp
test_switcher_model.cpp
test_texture_cache.cpp
+ test_text_input.cpp
test_thumbnail_generator.cpp
test_trash_launcher_icon.cpp
test_launcher_minimize_speed.cpp
diff --git a/tests/test_text_input.cpp b/tests/test_text_input.cpp
new file mode 100644
index 000000000..017aecf42
--- /dev/null
+++ b/tests/test_text_input.cpp
@@ -0,0 +1,116 @@
+/*
+ * 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: Manuel de la Pena <manuel.delapena@canonical.com>
+ *
+ */
+
+
+#include <gtest/gtest.h>
+
+#include "unity-shared/TextInput.h"
+#include "test_utils.h"
+
+using namespace nux;
+
+namespace unity
+{
+
+class TextInputMock : public TextInput
+{
+ public:
+ using TextInput::Init;
+ using TextInput::OnInputHintChanged;
+ using TextInput::OnMouseButtonDown;
+ using TextInput::OnEndKeyFocus;
+ using TextInput::get_input_string;
+
+
+ nux::StaticCairoText* GetHint() const { return hint_; }
+ IMTextEntry* GetPangoEntry() const { return pango_entry_; }
+};
+
+class TestTextInput : public ::testing::Test
+{
+ protected:
+ TestTextInput()
+ {
+ entry = new TextInputMock();
+ entry->Init();
+ hint = entry->GetHint();
+ pango_entry = entry->GetPangoEntry();
+ }
+
+ nux::ObjectPtr<TextInputMock> entry;
+ nux::StaticCairoText* hint;
+ IMTextEntry* pango_entry;
+};
+
+TEST_F(TestTextInput, HintCorrectInit)
+{
+ nux::Color color = hint->GetTextColor();
+
+ EXPECT_EQ(color.red, 1.0f);
+ EXPECT_EQ(color.green, 1.0f);
+ EXPECT_EQ(color.blue, 1.0f);
+ EXPECT_EQ(color.alpha, 0.5f);
+}
+
+TEST_F(TestTextInput, InputStringCorrectSetter)
+{
+ // set the string and test that we do indeed set the internal va
+ std::string new_input = "foo";
+ entry->input_string.Set(new_input);
+ EXPECT_EQ(entry->input_string.Get(), new_input);
+}
+
+TEST_F(TestTextInput, HintClearedOnInputHintChanged)
+{
+ // change the hint and assert that the internal value is correct
+ hint->SetText("foo");
+ entry->OnInputHintChanged();
+ EXPECT_EQ(entry->get_input_string(), "");
+}
+
+TEST_F(TestTextInput, HintHideOnMouseButtonDown)
+{
+ hint->SetVisible(true);
+ entry->OnMouseButtonDown(entry->GetBaseWidth()/2,
+ entry->GetBaseHeight()/2 , 0, 0);
+ EXPECT_FALSE(hint->IsVisible());
+}
+
+TEST_F(TestTextInput, HintVisibleOnEndKeyFocus)
+{
+ // set the text and ensure that later is cleared
+ pango_entry->SetText("user input");
+ entry->OnEndKeyFocus();
+
+ EXPECT_FALSE(hint->IsVisible());
+
+}
+
+TEST_F(TestTextInput, HintHiddenOnEndKeyFocus)
+{
+
+ pango_entry->SetText("");
+ entry->OnEndKeyFocus();
+
+ EXPECT_TRUE(hint->IsVisible());
+}
+
+} // unity
diff --git a/unity-shared/StaticCairoText.cpp b/unity-shared/StaticCairoText.cpp
index 7b3ac7729..f3ed86a1e 100644
--- a/unity-shared/StaticCairoText.cpp
+++ b/unity-shared/StaticCairoText.cpp
@@ -375,6 +375,11 @@ void StaticCairoText::SetFont(std::string const& font)
}
}
+std::string StaticCairoText::GetFont()
+{
+ return pimpl->font_;
+}
+
int StaticCairoText::GetLineCount() const
{
return pimpl->actual_lines_;
diff --git a/unity-shared/StaticCairoText.h b/unity-shared/StaticCairoText.h
index 683afac77..0d9503a89 100644
--- a/unity-shared/StaticCairoText.h
+++ b/unity-shared/StaticCairoText.h
@@ -72,6 +72,7 @@ public:
void SetTextAlignment(AlignState state);
void SetTextVerticalAlignment(AlignState state);
void SetFont(std::string const& font);
+ std::string GetFont();
void SetLines(int maximum_lines);
void SetLineSpacing(float line_spacing);
diff --git a/unity-shared/TextInput.cpp b/unity-shared/TextInput.cpp
index 6ce3b50b5..24ee13337 100644
--- a/unity-shared/TextInput.cpp
+++ b/unity-shared/TextInput.cpp
@@ -17,19 +17,7 @@
* Authored by: Manuel de la Pena <manuel.delapena@canonical.com>
*/
-#include "config.h"
-
-#include <Nux/Nux.h>
-#include <Nux/HLayout.h>
-#include <Nux/VLayout.h>
-#include <NuxCore/Logger.h>
-
-#include <UnityCore/Variant.h>
-
-#include <glib/gi18n-lib.h>
-
#include "TextInput.h"
-#include "CairoTexture.h"
namespace
{
@@ -51,50 +39,11 @@ const int PANGO_ENTRY_FONT_SIZE = 14;
}
-DECLARE_LOGGER(logger, "unity.dash.textinput");
-namespace
-{
-class ExpanderView : public nux::View
-{
-public:
- ExpanderView(NUX_FILE_LINE_DECL)
- : nux::View(NUX_FILE_LINE_PARAM)
- {
- SetAcceptKeyNavFocusOnMouseDown(false);
- SetAcceptKeyNavFocusOnMouseEnter(true);
- }
-
-protected:
- void Draw(nux::GraphicsEngine& graphics_engine, bool force_draw)
- {}
-
- void DrawContent(nux::GraphicsEngine& graphics_engine, bool force_draw)
- {
- if (GetLayout())
- GetLayout()->ProcessDraw(graphics_engine, force_draw);
- }
-
- bool AcceptKeyNavFocus()
- {
- return true;
- }
-
- nux::Area* FindAreaUnderMouse(const nux::Point& mouse_position, nux::NuxEventType event_type)
- {
- bool mouse_inside = TestMousePointerInclusionFilterMouseWheel(mouse_position, event_type);
-
- if (mouse_inside == false)
- return nullptr;
-
- return this;
- }
-};
-
-}
-
namespace unity
{
+nux::logging::Logger logger("unity.dash.textinput");
+
NUX_IMPLEMENT_OBJECT_TYPE(TextInput);
TextInput::TextInput(NUX_FILE_LINE_DECL)
diff --git a/unity-shared/TextInput.h b/unity-shared/TextInput.h
index e68f29262..ddcea004c 100644
--- a/unity-shared/TextInput.h
+++ b/unity-shared/TextInput.h
@@ -20,14 +20,22 @@
#ifndef TEXTINPUT_H
#define TEXTINPUT_H
+#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
-#include <NuxCore/Property.h>
+
+#include <Nux/Nux.h>
+#include <Nux/HLayout.h>
#include <Nux/LayeredLayout.h>
#include <Nux/VLayout.h>
#include <Nux/TextEntry.h>
+#include <NuxCore/Logger.h>
+#include <NuxCore/Property.h>
#include <UnityCore/GLibSignal.h>
#include <UnityCore/GLibSource.h>
+#include <UnityCore/Variant.h>
+#include "config.h"
+#include "CairoTexture.h"
#include "unity-shared/IconTexture.h"
#include "unity-shared/IMTextEntry.h"
#include "unity-shared/Introspectable.h"
@@ -41,10 +49,10 @@ class LinearLayout;
namespace unity
{
-
class TextInput : public unity::debug::Introspectable, public nux::View
{
NUX_DECLARE_OBJECT_TYPE(TextInput, nux::View);
+
public:
typedef nux::ObjectPtr<TextInput> Ptr;
TextInput(NUX_FILE_LINE_PROTO);
@@ -59,42 +67,49 @@ public:
private:
- void Init();
-
void OnFontChanged(GtkSettings* settings, GParamSpec* pspec=NULL);
- void OnInputHintChanged();
-
void Draw(nux::GraphicsEngine& GfxContext, bool force_draw);
void DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw);
+ void UpdateBackground(bool force);
+
+ std::string GetName() const;
+
+ void AddProperties(GVariantBuilder* builder);
+ bool AcceptKeyNavFocus();
+
+protected:
- void OnMouseButtonDown(int x, int y, unsigned long button_flags, unsigned long key_flags);
+ void Init();
+ void OnInputHintChanged();
+ void OnMouseButtonDown(int x, int y, unsigned long button_flags,
+ unsigned long key_flags);
void OnEndKeyFocus();
- void UpdateBackground(bool force);
+ // getters & setters
std::string get_input_string() const;
bool set_input_string(std::string const& string);
bool get_im_active() const;
bool get_im_preedit() const;
- std::string GetName() const;
- void AddProperties(GVariantBuilder* builder);
- bool AcceptKeyNavFocus();
+ // instance vars
+ nux::StaticCairoText* hint_;
+ IMTextEntry* pango_entry_;
private:
+
bool ShouldBeHighlighted();
std::unique_ptr<nux::AbstractPaintLayer> bg_layer_;
std::unique_ptr<nux::AbstractPaintLayer> highlight_layer_;
nux::HLayout* layout_;
nux::LayeredLayout* layered_layout_;
- nux::StaticCairoText* hint_;
- IMTextEntry* pango_entry_;
int last_width_;
int last_height_;
glib::SignalManager sig_manager_;
+
};
}