Added event and draw tests for ComboBox

This commit is contained in:
Bruno Van de Velde 2018-05-21 15:52:25 +02:00
parent a66702c924
commit aba2c4c96e
17 changed files with 250 additions and 32 deletions

View File

@ -2,7 +2,7 @@ comment: false
codecov:
branch: 0.8-dev
coverage:
range: 50...90
range: 50...100
status:
project: false
patch: false

View File

@ -528,26 +528,26 @@ namespace tgui
void ComboBox::mouseWheelScrolled(float delta, Vector2f)
{
// Only act to scrolling when the list is not being shown
if (!m_listBox->isVisible())
if (m_listBox->isVisible())
return;
// Check if you are scrolling down
if (delta < 0)
{
// Check if you are scrolling down
if (delta < 0)
// Select the next item
if (static_cast<std::size_t>(m_listBox->getSelectedItemIndex() + 1) < m_listBox->getItemCount())
{
// Select the next item
if (static_cast<std::size_t>(m_listBox->getSelectedItemIndex() + 1) < m_listBox->getItemCount())
{
m_listBox->setSelectedItemByIndex(static_cast<std::size_t>(m_listBox->getSelectedItemIndex() + 1));
m_text.setString(m_listBox->getSelectedItem());
}
m_listBox->setSelectedItemByIndex(static_cast<std::size_t>(m_listBox->getSelectedItemIndex() + 1));
m_text.setString(m_listBox->getSelectedItem());
}
else // You are scrolling up
}
else // You are scrolling up
{
// Select the previous item
if (m_listBox->getSelectedItemIndex() > 0)
{
// Select the previous item
if (m_listBox->getSelectedItemIndex() > 0)
{
m_listBox->setSelectedItemByIndex(static_cast<std::size_t>(m_listBox->getSelectedItemIndex() - 1));
m_text.setString(m_listBox->getSelectedItem());
}
m_listBox->setSelectedItemByIndex(static_cast<std::size_t>(m_listBox->getSelectedItemIndex() - 1));
m_text.setString(m_listBox->getSelectedItem());
}
}
}
@ -722,13 +722,7 @@ namespace tgui
else // If there are no items, there should be no item ids
{
if (node->propertyValuePairs["itemids"])
{
if (!node->propertyValuePairs["itemids"]->listNode)
throw Exception{"Failed to parse 'ItemIds' property, expected a list as value"};
if (!node->propertyValuePairs["itemids"]->valueList.empty())
throw Exception{"Found 'ItemIds' property while there is no 'Items' property"};
}
throw Exception{"Found 'ItemIds' property while there is no 'Items' property"};
}
if (node->propertyValuePairs["itemstodisplay"])

View File

@ -847,13 +847,7 @@ namespace tgui
else // If there are no items, there should be no item ids
{
if (node->propertyValuePairs["itemids"])
{
if (!node->propertyValuePairs["itemids"]->listNode)
throw Exception{"Failed to parse 'ItemIds' property, expected a list as value"};
if (!node->propertyValuePairs["itemids"]->valueList.empty())
throw Exception{"Found 'ItemIds' property while there is no 'Items' property"};
}
throw Exception{"Found 'ItemIds' property while there is no 'Items' property"};
}
if (node->propertyValuePairs["autoscroll"])

View File

@ -1,3 +1,27 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2018 Bruno Van de Velde (vdv_b@tgui.eu)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Tests.hpp"
#include <TGUI/Widgets/Panel.hpp>

View File

@ -25,6 +25,7 @@
#include "Tests.hpp"
#include <TGUI/Widgets/Label.hpp>
#include <TGUI/Widgets/ComboBox.hpp>
#include <TGUI/Widgets/Group.hpp>
TEST_CASE("[ComboBox]")
{
@ -261,7 +262,73 @@ TEST_CASE("[ComboBox]")
testWidgetSignals(comboBox);
}
// TODO
SECTION("ItemSelected")
{
auto container = tgui::Group::create({400.f, 300.f});
container->add(comboBox);
auto mouseClick = [container](sf::Vector2f pos) {
container->mouseMoved(pos);
container->leftMousePressed(pos);
container->leftMouseReleased(pos);
};
unsigned int itemSelectedCount = 0;
comboBox->connect("ItemSelected", &genericCallback, std::ref(itemSelectedCount));
comboBox->setPosition(25, 6);
comboBox->setSize(150, 24);
comboBox->getRenderer()->setBorders(2);
comboBox->addItem("1");
comboBox->addItem("2");
comboBox->addItem("3");
const sf::Vector2f mousePosOnComboBox = {100, 15};
const sf::Vector2f mousePosOnItem1 = {100, 40};
const sf::Vector2f mousePosOnItem2 = {100, 60};
SECTION("Click")
{
// Selecting second item
mouseClick(mousePosOnComboBox);
REQUIRE(itemSelectedCount == 0);
mouseClick(mousePosOnItem2);
REQUIRE(itemSelectedCount == 1);
REQUIRE(comboBox->getSelectedItemIndex() == 1);
// List was closed when item was clicked
mouseClick(mousePosOnItem1);
REQUIRE(comboBox->getSelectedItemIndex() == 1);
// Clicking on combo box when list is open also closes list
mouseClick(mousePosOnComboBox);
mouseClick(mousePosOnComboBox);
mouseClick(mousePosOnItem1);
REQUIRE(comboBox->getSelectedItemIndex() == 1);
REQUIRE(itemSelectedCount == 1);
}
SECTION("Mouse wheel scroll")
{
REQUIRE(itemSelectedCount == 0);
container->mouseWheelScrolled(-1, mousePosOnComboBox);
REQUIRE(comboBox->getSelectedItemIndex() == 0);
REQUIRE(itemSelectedCount == 1);
container->mouseWheelScrolled(-1, mousePosOnComboBox);
REQUIRE(comboBox->getSelectedItemIndex() == 1);
REQUIRE(itemSelectedCount == 2);
container->mouseWheelScrolled(1, mousePosOnComboBox);
REQUIRE(comboBox->getSelectedItemIndex() == 0);
REQUIRE(itemSelectedCount == 3);
// Scrolling on the combo box has no effect when the list is open
mouseClick(mousePosOnComboBox);
container->mouseWheelScrolled(-1, mousePosOnComboBox);
REQUIRE(comboBox->getSelectedItemIndex() == 0);
REQUIRE(itemSelectedCount == 3);
}
}
}
testWidgetRenderer(comboBox->getRenderer());
@ -384,4 +451,143 @@ TEST_CASE("[ComboBox]")
testSavingWidget("ComboBox", comboBox);
}
SECTION("Draw")
{
TEST_DRAW_INIT(80, 35, comboBox)
comboBox->setEnabled(true);
comboBox->setPosition(10, 5);
comboBox->setSize(60, 24);
comboBox->setTextSize(14);
tgui::ComboBoxRenderer renderer = tgui::RendererData::create();
renderer.setBackgroundColor(sf::Color::Green);
renderer.setTextColor(sf::Color::Red);
renderer.setBorderColor(sf::Color::Blue);
renderer.setArrowBackgroundColor(sf::Color::Cyan);
renderer.setArrowColor(sf::Color::Magenta);
renderer.setBorders({1, 2, 3, 4});
renderer.setTextStyle(sf::Text::Italic);
renderer.setOpacity(0.7f);
comboBox->setRenderer(renderer.getData());
auto setHoverRenderer = [&](bool textured){
renderer.setArrowBackgroundColorHover(sf::Color::Yellow);
renderer.setArrowColorHover(sf::Color::Black);
if (textured)
renderer.setTextureArrowHover("resources/Texture3.png");
};
comboBox->addItem("1");
comboBox->addItem("2");
comboBox->addItem("3");
const sf::Vector2f mousePos{60, 15};
SECTION("Colored")
{
SECTION("No selected item")
{
SECTION("No hover")
{
TEST_DRAW("ComboBox_NoSelectedNoHover.png")
}
SECTION("Hover")
{
comboBox->mouseMoved(mousePos);
SECTION("No hover properties set")
{
TEST_DRAW("ComboBox_NoSelectedHover_NoHoverSet.png")
}
SECTION("Hover properties set")
{
setHoverRenderer(false);
TEST_DRAW("ComboBox_NoSelectedHover_HoverSet.png")
}
}
}
SECTION("Selected item")
{
comboBox->setSelectedItem("2");
SECTION("No hover")
{
TEST_DRAW("ComboBox_SelectedNoHover.png")
}
SECTION("Hover selected")
{
comboBox->mouseMoved(mousePos);
SECTION("No hover properties set")
{
TEST_DRAW("ComboBox_SelectedHoverSelected_NoHoverSet.png")
}
SECTION("Hover properties set")
{
setHoverRenderer(false);
TEST_DRAW("ComboBox_SelectedHoverSelected_HoverSet.png")
}
}
}
}
SECTION("Textured")
{
renderer.setTextureBackground("resources/Texture1.png");
renderer.setTextureArrow("resources/Texture2.png");
SECTION("No selected item")
{
SECTION("No hover")
{
TEST_DRAW("ComboBox_NoSelectedNoHover_Textured.png")
}
SECTION("Hover")
{
comboBox->mouseMoved(mousePos);
SECTION("No hover properties set")
{
TEST_DRAW("ComboBox_NoSelectedHover_NoHoverSet_Textured.png")
}
SECTION("Hover properties set")
{
setHoverRenderer(true);
TEST_DRAW("ComboBox_NoSelectedHover_HoverSet_Textured.png")
}
}
}
SECTION("Selected item")
{
comboBox->setSelectedItem("2");
SECTION("No hover")
{
TEST_DRAW("ComboBox_SelectedNoHover_Textured.png")
}
SECTION("Hover selected")
{
comboBox->mouseMoved(mousePos);
SECTION("No hover properties set")
{
TEST_DRAW("ComboBox_SelectedHoverSelected_NoHoverSet_Textured.png")
}
SECTION("Hover properties set")
{
setHoverRenderer(true);
TEST_DRAW("ComboBox_SelectedHoverSelected_HoverSet_Textured.png")
}
}
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 744 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 743 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 743 B