From 710e21d45fcc3c890a200db57048adbaa1b8f05d Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Thu, 22 Nov 2012 11:08:38 +0100 Subject: Added tests for the text entry. (bzr r2922.2.1) --- tests/CMakeLists.txt | 1 + tests/test_text_input.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tests/test_text_input.cpp (limited to 'tests') 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..d98976e1b --- /dev/null +++ b/tests/test_text_input.cpp @@ -0,0 +1,86 @@ +/* + * 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 + * + * + * Authored by: Manuel de la Pena + * + */ + + +#include + +#include "unity-shared/TextInput.h" +#include "test_utils.h" + +using namespace nux; + +namespace unity +{ + +class TestTextInput : public ::testing::Test +{ + protected: + TestTextInput() + { + entry = new TextInput(); + entry->Init(); + } + + TextInput* entry; +}; + +TEST_F(TestTextInput, HintCorrectInit) +{ + nux::Color color = entry->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); + + EXPECT_EQ(entry->hint_->GetFont(), "Ubuntu Italic 12px"); +} + +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, OnInputHintChanged) +{ + // change the hint and assert that the internal value is correct + entry->hint_->SetText("foo"); + entry->OnInputHintChanged(); + EXPECT_EQ(entry->hint_->GetText(), ""); +} + +TEST_F(TestTextInput, OnMouseButtonDown) +{ + entry->hint_->SetVisible(true); + entry->OnMouseButtonDown(0, 0, 0, 0); + ASSERT_FALSE(entry->hint_->IsVisible()); +} + +TEST_F(TestTextInput, OnEndKeyFocus) +{ + entry->OnEndKeyFocus(); + EXPECT_EQ(entry->hint_->GetText(), " "); +} + +} // unity -- cgit v1.2.3 From 75a95e15d6192a7f7ed7488d8eefdbbbadd754b0 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Thu, 22 Nov 2012 13:53:25 +0100 Subject: Updated code following the review comments. (bzr r2922.2.4) --- tests/test_text_input.cpp | 77 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/test_text_input.cpp b/tests/test_text_input.cpp index d98976e1b..b636aa86f 100644 --- a/tests/test_text_input.cpp +++ b/tests/test_text_input.cpp @@ -30,28 +30,63 @@ using namespace nux; namespace unity { +class TextInputMock : public TextInput +{ + public: + // expose protected methods we want to test + void Init() + { + TextInput::Init(); + } + + void OnInputHintChanged() + { + TextInput::OnInputHintChanged(); + } + + void OnMouseButtonDown(int x, int y, unsigned long button_flags, + unsigned long key_flags) + { + TextInput::OnMouseButtonDown(x, y, button_flags, key_flags); + } + + void OnEndKeyFocus() + { + TextInput::OnEndKeyFocus(); + } + + std::string get_input_string() const + { + return 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 TextInput(); + entry = new TextInputMock(); entry->Init(); + hint = entry->GetHint(); + pango_entry = entry->GetPangoEntry(); } - TextInput* entry; + TextInputMock* entry; + nux::StaticCairoText* hint; + IMTextEntry* pango_entry; }; TEST_F(TestTextInput, HintCorrectInit) { - nux::Color color = entry->hint_->GetTextColor(); + 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); - - EXPECT_EQ(entry->hint_->GetFont(), "Ubuntu Italic 12px"); } TEST_F(TestTextInput, InputStringCorrectSetter) @@ -62,25 +97,39 @@ TEST_F(TestTextInput, InputStringCorrectSetter) EXPECT_EQ(entry->input_string.Get(), new_input); } -TEST_F(TestTextInput, OnInputHintChanged) +TEST_F(TestTextInput, HintClearedOnInputHintChanged) { // change the hint and assert that the internal value is correct - entry->hint_->SetText("foo"); + hint->SetText("foo"); entry->OnInputHintChanged(); - EXPECT_EQ(entry->hint_->GetText(), ""); + EXPECT_EQ(entry->get_input_string(), ""); } -TEST_F(TestTextInput, OnMouseButtonDown) +TEST_F(TestTextInput, HintHideOnMouseButtonDown) { - entry->hint_->SetVisible(true); - entry->OnMouseButtonDown(0, 0, 0, 0); - ASSERT_FALSE(entry->hint_->IsVisible()); + hint->SetVisible(true); + entry->OnMouseButtonDown(entry->GetBaseWidth()/2, + entry->GetBaseHeight()/2 , 0, 0); + ASSERT_FALSE(hint->IsVisible()); } -TEST_F(TestTextInput, OnEndKeyFocus) +TEST_F(TestTextInput, HintVisibleOnEndKeyFocus) { + // set the text and ensure that later is cleared + pango_entry->SetText("user input"); entry->OnEndKeyFocus(); - EXPECT_EQ(entry->hint_->GetText(), " "); + + ASSERT_FALSE(hint->IsVisible()); + +} + +TEST_F(TestTextInput, HintHiddenOnEndKeyFocus) +{ + + pango_entry->SetText(""); + entry->OnEndKeyFocus(); + + ASSERT_TRUE(hint->IsVisible()); } } // unity -- cgit v1.2.3 From 897f4364330e74815e15be29ec06f366ef31f653 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Thu, 22 Nov 2012 15:47:01 +0100 Subject: Fix memory leak and clean methods declaration. (bzr r2922.2.5) --- tests/test_text_input.cpp | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) (limited to 'tests') diff --git a/tests/test_text_input.cpp b/tests/test_text_input.cpp index b636aa86f..581e1f459 100644 --- a/tests/test_text_input.cpp +++ b/tests/test_text_input.cpp @@ -33,32 +33,13 @@ namespace unity class TextInputMock : public TextInput { public: - // expose protected methods we want to test - void Init() - { - TextInput::Init(); - } - - void OnInputHintChanged() - { - TextInput::OnInputHintChanged(); - } - - void OnMouseButtonDown(int x, int y, unsigned long button_flags, - unsigned long key_flags) - { - TextInput::OnMouseButtonDown(x, y, button_flags, key_flags); - } - - void OnEndKeyFocus() - { - TextInput::OnEndKeyFocus(); - } - - std::string get_input_string() const - { - return TextInput::get_input_string(); - } + 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_; } }; @@ -74,7 +55,7 @@ class TestTextInput : public ::testing::Test pango_entry = entry->GetPangoEntry(); } - TextInputMock* entry; + nux::ObjectPtr entry; nux::StaticCairoText* hint; IMTextEntry* pango_entry; }; -- cgit v1.2.3 From ac05560a928494046dcce1f6872e3e7330d522c6 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Thu, 22 Nov 2012 15:49:17 +0100 Subject: Use expect instead of assert. (bzr r2922.2.6) --- tests/test_text_input.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_text_input.cpp b/tests/test_text_input.cpp index 581e1f459..017aecf42 100644 --- a/tests/test_text_input.cpp +++ b/tests/test_text_input.cpp @@ -91,7 +91,7 @@ TEST_F(TestTextInput, HintHideOnMouseButtonDown) hint->SetVisible(true); entry->OnMouseButtonDown(entry->GetBaseWidth()/2, entry->GetBaseHeight()/2 , 0, 0); - ASSERT_FALSE(hint->IsVisible()); + EXPECT_FALSE(hint->IsVisible()); } TEST_F(TestTextInput, HintVisibleOnEndKeyFocus) @@ -100,7 +100,7 @@ TEST_F(TestTextInput, HintVisibleOnEndKeyFocus) pango_entry->SetText("user input"); entry->OnEndKeyFocus(); - ASSERT_FALSE(hint->IsVisible()); + EXPECT_FALSE(hint->IsVisible()); } @@ -110,7 +110,7 @@ TEST_F(TestTextInput, HintHiddenOnEndKeyFocus) pango_entry->SetText(""); entry->OnEndKeyFocus(); - ASSERT_TRUE(hint->IsVisible()); + EXPECT_TRUE(hint->IsVisible()); } } // unity -- cgit v1.2.3