Search code examples
c#wpfnunittogglebuttondatagridrowheader

WPF Toggle Button testing using NUnit


How to test wpf toggle button? I am using the following code for testing.

[Test]
    public void TestToggleButtonClick()
    {
        DataGrid_WpfToolkit dataGrid = window.Get<DataGrid_WpfToolkit>("Data");
        Assert.IsNotNull(dataGrid);
        ToggleButton toggle = dataGrid.Rows[0].Get<ToggleButton>("RowHeaderToggleButton");
        toggle.Toggle();
        Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);

        toggle.Toggle();
        Assert.AreEqual(true, dataGrid.Rows[0].IsSelected);

        toggle.Toggle();
        Assert.AreEqual(false, dataGrid.Rows[0].IsSelected);
    }

The following code is the definition for toggle button click event in my wpf application.

private void ToggleButton_Click(object sender, RoutedEventArgs e)
    {
            DependencyObject obj = (DependencyObject)e.OriginalSource;
            while (!(obj is DataGridRow) && (obj != null))
            {
                obj = VisualTreeHelper.GetParent(obj);
            }
            if (obj is DataGridRow)
            {
                if ((obj as DataGridRow).DetailsVisibility == Visibility.Visible)
                {
                    (obj as DataGridRow).IsSelected = false;
                    (obj as DataGridRow).DetailsVisibility = System.Windows.Visibility.Collapsed;

                    FrameworkElement tb2 = GetTemplateChildByName((obj as DataGridRow), "RowHeaderToggleButton");
                    (tb2 as ToggleButton).IsChecked = false;
                    flag = false;
                }
                else if ((obj as DataGridRow).DetailsVisibility == Visibility.Collapsed)
                {
                    for (int i = 0; i < Data.Items.Count; i++)
                    {
                        DataGridRow itm = GetDataGridRowitem(i);
                        itm.IsSelected = false;
                        itm.DetailsVisibility = System.Windows.Visibility.Collapsed;
                        FrameworkElement tb = GetTemplateChildByName(itm, "RowHeaderToggleButton");
                        (tb as ToggleButton).IsChecked = false;
                    }
                    DataTemplate dt = FindResource("tocchild") as DataTemplate;
                    Data.RowDetailsTemplate = dt;
                    (obj as DataGridRow).IsSelected = true;
                    (obj as DataGridRow).DetailsVisibility = Visibility.Visible;

                    FrameworkElement tb1 = GetTemplateChildByName((obj as DataGridRow), "RowHeaderToggleButton");
                    (tb1 as ToggleButton).IsChecked = true;
                    flag = true;
                }
            }}

But toggle.Toggle() is not invoking the function ToggleButton_Click() and datagrid.Rows[0].IsSelected is always return false. In ToggleButton_Click() function, i am changing the selection of the corresponding row from true to false or false to true. But the control is not coming to the function ToggleButton_Click(). Is there any other method to invoke ToggleButton_Click() function from toggle button?


Solution

  • There are many ways:

    • dirty: use reflection or change accessibility of this method to reach from Test code

    • Change Event: use Checked/Unchecked instead of Click

    • Call toggle.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));

    • Use UIAutomation (I prefer this way) (somekindofsample)