2011-08-10 11:24:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-08-27 10:11:31 -04:00
|
|
|
// Name: tests/controls/dataviewctrltest.cpp
|
2011-08-10 11:24:25 -04:00
|
|
|
// Purpose: wxDataViewCtrl unit test
|
|
|
|
// Author: Vaclav Slavik
|
|
|
|
// Created: 2011-08-08
|
|
|
|
// Copyright: (c) 2011 Vaclav Slavik <vslavik@gmail.com>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#if wxUSE_DATAVIEWCTRL
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/dataview.h"
|
|
|
|
|
|
|
|
#include "testableframe.h"
|
2018-11-04 12:25:16 -05:00
|
|
|
#include "asserthelper.h"
|
2011-08-10 11:24:25 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
class DataViewCtrlTestCase
|
2011-08-10 11:24:25 -04:00
|
|
|
{
|
|
|
|
public:
|
2018-11-04 08:34:49 -05:00
|
|
|
explicit DataViewCtrlTestCase(long style);
|
|
|
|
~DataViewCtrlTestCase();
|
2011-08-22 08:41:19 -04:00
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
protected:
|
2011-08-22 08:41:19 -04:00
|
|
|
void TestSelectionFor0and1();
|
2011-08-10 11:24:25 -04:00
|
|
|
|
|
|
|
// the dataview control itself
|
|
|
|
wxDataViewTreeCtrl *m_dvc;
|
|
|
|
|
|
|
|
// and some of its items
|
|
|
|
wxDataViewItem m_root,
|
|
|
|
m_child1,
|
|
|
|
m_child2,
|
|
|
|
m_grandchild;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(DataViewCtrlTestCase);
|
2011-08-10 11:24:25 -04:00
|
|
|
};
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
class SingleSelectDataViewCtrlTestCase : public DataViewCtrlTestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SingleSelectDataViewCtrlTestCase()
|
|
|
|
: DataViewCtrlTestCase(wxDV_SINGLE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2011-08-10 11:24:25 -04:00
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
class MultiSelectDataViewCtrlTestCase : public DataViewCtrlTestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MultiSelectDataViewCtrlTestCase()
|
|
|
|
: DataViewCtrlTestCase(wxDV_MULTIPLE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2011-08-10 11:24:25 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test initialization
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
DataViewCtrlTestCase::DataViewCtrlTestCase(long style)
|
2011-08-10 11:24:25 -04:00
|
|
|
{
|
|
|
|
m_dvc = new wxDataViewTreeCtrl(wxTheApp->GetTopWindow(),
|
|
|
|
wxID_ANY,
|
|
|
|
wxDefaultPosition,
|
|
|
|
wxSize(400, 200),
|
2011-08-22 08:41:19 -04:00
|
|
|
style);
|
2011-08-10 11:24:25 -04:00
|
|
|
|
|
|
|
m_root = m_dvc->AppendContainer(wxDataViewItem(), "The root");
|
|
|
|
m_child1 = m_dvc->AppendContainer(m_root, "child1");
|
|
|
|
m_grandchild = m_dvc->AppendItem(m_child1, "grandchild");
|
|
|
|
m_child2 = m_dvc->AppendItem(m_root, "child2");
|
|
|
|
|
2018-11-04 11:49:12 -05:00
|
|
|
m_dvc->Layout();
|
2018-11-04 11:10:23 -05:00
|
|
|
m_dvc->Expand(m_root);
|
2011-08-10 11:24:25 -04:00
|
|
|
m_dvc->Refresh();
|
|
|
|
m_dvc->Update();
|
|
|
|
}
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
DataViewCtrlTestCase::~DataViewCtrlTestCase()
|
2011-08-10 11:24:25 -04:00
|
|
|
{
|
|
|
|
delete m_dvc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the tests themselves
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
TEST_CASE_METHOD(MultiSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::DeleteSelected",
|
|
|
|
"[wxDataViewCtrl][delete]")
|
2011-08-10 11:24:25 -04:00
|
|
|
{
|
|
|
|
wxDataViewItemArray sel;
|
|
|
|
sel.push_back(m_child1);
|
|
|
|
sel.push_back(m_grandchild);
|
|
|
|
sel.push_back(m_child2);
|
|
|
|
m_dvc->SetSelections(sel);
|
|
|
|
|
|
|
|
// delete a selected item
|
|
|
|
m_dvc->DeleteItem(m_child1);
|
|
|
|
|
|
|
|
m_dvc->GetSelections(sel);
|
|
|
|
|
|
|
|
// m_child1 and its children should be removed from the selection now
|
2018-11-04 08:34:49 -05:00
|
|
|
REQUIRE( sel.size() == 1 );
|
|
|
|
CHECK( sel[0] == m_child2 );
|
2011-08-10 11:24:25 -04:00
|
|
|
}
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
TEST_CASE_METHOD(MultiSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::DeleteNotSelected",
|
|
|
|
"[wxDataViewCtrl][delete]")
|
2011-08-10 11:24:25 -04:00
|
|
|
{
|
2013-06-12 20:08:27 -04:00
|
|
|
// TODO not working on OS X as expected
|
2018-11-04 08:34:49 -05:00
|
|
|
#ifdef __WXOSX__
|
|
|
|
WARN("Disabled under MacOS because this test currently fails");
|
|
|
|
#else
|
2011-08-10 11:24:25 -04:00
|
|
|
wxDataViewItemArray sel;
|
|
|
|
sel.push_back(m_child1);
|
|
|
|
sel.push_back(m_grandchild);
|
|
|
|
m_dvc->SetSelections(sel);
|
|
|
|
|
|
|
|
// delete unselected item
|
|
|
|
m_dvc->DeleteItem(m_child2);
|
|
|
|
|
|
|
|
m_dvc->GetSelections(sel);
|
|
|
|
|
2011-08-22 08:41:19 -04:00
|
|
|
// m_child1 and its children should be unaffected
|
2018-11-04 08:34:49 -05:00
|
|
|
REQUIRE( sel.size() == 2 );
|
|
|
|
CHECK( sel[0] == m_child1 );
|
|
|
|
CHECK( sel[1] == m_grandchild );
|
2013-06-12 20:08:27 -04:00
|
|
|
#endif
|
2011-08-10 11:24:25 -04:00
|
|
|
}
|
|
|
|
|
2011-08-22 08:41:19 -04:00
|
|
|
void DataViewCtrlTestCase::TestSelectionFor0and1()
|
|
|
|
{
|
|
|
|
wxDataViewItemArray selections;
|
|
|
|
|
|
|
|
// Initially there is no selection.
|
2018-11-04 08:34:49 -05:00
|
|
|
CHECK( m_dvc->GetSelectedItemsCount() == 0 );
|
|
|
|
CHECK( !m_dvc->HasSelection() );
|
|
|
|
CHECK( !m_dvc->GetSelection().IsOk() );
|
2011-08-22 08:41:19 -04:00
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
CHECK( !m_dvc->GetSelections(selections) );
|
|
|
|
CHECK( selections.empty() );
|
2011-08-22 08:41:19 -04:00
|
|
|
|
|
|
|
// Select one item.
|
|
|
|
m_dvc->Select(m_child1);
|
2018-11-04 08:34:49 -05:00
|
|
|
CHECK( m_dvc->GetSelectedItemsCount() == 1 );
|
|
|
|
CHECK( m_dvc->HasSelection() );
|
|
|
|
CHECK( m_dvc->GetSelection().IsOk() );
|
|
|
|
REQUIRE( m_dvc->GetSelections(selections) == 1 );
|
|
|
|
CHECK( selections[0] == m_child1 );
|
2011-08-22 08:41:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
TEST_CASE_METHOD(MultiSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::GetSelectionForMulti",
|
|
|
|
"[wxDataViewCtrl][select]")
|
2011-08-22 08:41:19 -04:00
|
|
|
{
|
|
|
|
wxDataViewItemArray selections;
|
|
|
|
|
|
|
|
TestSelectionFor0and1();
|
|
|
|
|
|
|
|
m_dvc->Select(m_child2);
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
CHECK( m_dvc->GetSelectedItemsCount() == 2 );
|
|
|
|
CHECK( m_dvc->HasSelection() );
|
|
|
|
CHECK( !m_dvc->GetSelection().IsOk() );
|
|
|
|
REQUIRE( m_dvc->GetSelections(selections) == 2 );
|
|
|
|
CHECK( selections[1] == m_child2 );
|
2011-08-22 08:41:19 -04:00
|
|
|
}
|
|
|
|
|
2018-11-04 08:34:49 -05:00
|
|
|
TEST_CASE_METHOD(SingleSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::SingleSelection",
|
|
|
|
"[wxDataViewCtrl][selection]")
|
2011-08-22 08:41:19 -04:00
|
|
|
{
|
|
|
|
TestSelectionFor0and1();
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:10:23 -05:00
|
|
|
TEST_CASE_METHOD(SingleSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::IsExpanded",
|
|
|
|
"[wxDataViewCtrl][expand]")
|
|
|
|
{
|
|
|
|
CHECK( m_dvc->IsExpanded(m_root) );
|
|
|
|
CHECK( !m_dvc->IsExpanded(m_child1) );
|
|
|
|
CHECK( !m_dvc->IsExpanded(m_grandchild) );
|
|
|
|
CHECK( !m_dvc->IsExpanded(m_child2) );
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:12:08 -05:00
|
|
|
TEST_CASE_METHOD(SingleSelectDataViewCtrlTestCase,
|
|
|
|
"wxDVC::GetItemRect",
|
|
|
|
"[wxDataViewCtrl][item]")
|
|
|
|
{
|
|
|
|
#ifdef __WXGTK__
|
2018-11-04 12:25:16 -05:00
|
|
|
// We need to let the native control have some events to lay itself out.
|
|
|
|
wxYield();
|
|
|
|
#endif // __WXGTK__
|
|
|
|
|
2018-11-04 11:12:08 -05:00
|
|
|
const wxRect rect1 = m_dvc->GetItemRect(m_child1);
|
|
|
|
const wxRect rect2 = m_dvc->GetItemRect(m_child2);
|
|
|
|
|
2018-11-04 11:50:55 -05:00
|
|
|
CHECK( rect1 != wxRect() );
|
|
|
|
CHECK( rect2 != wxRect() );
|
|
|
|
|
2018-11-04 11:12:08 -05:00
|
|
|
CHECK( rect1.x == rect2.x );
|
|
|
|
CHECK( rect1.width == rect2.width );
|
|
|
|
CHECK( rect1.height == rect2.height );
|
2018-11-04 12:25:16 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
INFO("First child: " << rect1 << ", second one: " << rect2);
|
|
|
|
CHECK( rect1.y < rect2.y );
|
|
|
|
}
|
2018-11-04 11:12:08 -05:00
|
|
|
|
|
|
|
const wxRect rectNotShown = m_dvc->GetItemRect(m_grandchild);
|
|
|
|
CHECK( rectNotShown == wxRect() );
|
2018-11-04 11:50:55 -05:00
|
|
|
|
|
|
|
// Append enough items to make the window scrollable.
|
|
|
|
for ( int i = 3; i < 100; ++i )
|
|
|
|
m_dvc->AppendItem(m_root, wxString::Format("child%d", i));
|
|
|
|
|
|
|
|
const wxDataViewItem last = m_dvc->AppendItem(m_root, "last");
|
|
|
|
|
|
|
|
// This should scroll the window to bring this item into view.
|
|
|
|
m_dvc->EnsureVisible(last);
|
|
|
|
|
2018-11-04 12:25:16 -05:00
|
|
|
#ifdef __WXGTK__
|
|
|
|
// And again to let it scroll the correct items into view.
|
|
|
|
wxYield();
|
|
|
|
#endif
|
|
|
|
|
2018-11-04 11:50:55 -05:00
|
|
|
// Check that this was indeed the case.
|
|
|
|
const wxDataViewItem top = m_dvc->GetTopItem();
|
|
|
|
CHECK( top != m_root );
|
|
|
|
|
|
|
|
// Verify that the coordinates are returned in physical coordinates of the
|
|
|
|
// window and not the logical coordinates affected by scrolling.
|
|
|
|
const wxRect rectScrolled = m_dvc->GetItemRect(top);
|
|
|
|
CHECK( rectScrolled.GetBottom() > 0 );
|
|
|
|
CHECK( rectScrolled.GetTop() <= m_dvc->GetClientSize().y );
|
|
|
|
|
|
|
|
// Also check that the root item is not currently visible (because it's
|
|
|
|
// scrolled off).
|
|
|
|
const wxRect rectRoot = m_dvc->GetItemRect(m_root);
|
|
|
|
CHECK( rectRoot == wxRect() );
|
2018-11-04 11:12:08 -05:00
|
|
|
}
|
|
|
|
|
2011-08-22 10:14:55 -04:00
|
|
|
#endif //wxUSE_DATAVIEWCTRL
|