Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
add unit tests for paginatedlistview
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrynes7 committed Jun 16, 2020
1 parent b0003d0 commit 2a061cc
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests")]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Update(List<TData> newData)
RefreshView();
}

private void ChangePageCount(int diff)
internal void ChangePageCount(int diff)
{
currentPage += diff;
currentPage = Mathf.Clamp(currentPage, 0, numPages - 1);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests",
"references": [
"Improbable.Gdk.Debug.WorkerInspector.Codegen"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine.UIElements;

namespace Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests
{
[TestFixture]
public class PaginatedListViewTests
{
[Test]
public void Only_up_to_elementsPerPage_are_rendered_at_once()
{
var data = GetDummyData(10);
var observer = new PaginatedListViewObserver(5);

observer.UpdateData(data);

var elements = observer.VisibleElements.ToList();

Assert.AreEqual(5, elements.Count);
}

[Test]
public void Correct_list_slice_is_rendered_in_order()
{
var data = GetDummyData(10);
var observer = new PaginatedListViewObserver(5);

observer.UpdateData(data);

var elements = observer.VisibleElements.ToList();

for (var i = 0; i < elements.Count; i++)
{
Assert.AreEqual(i, ((PaginatedListViewObserver.DummyElement) elements[i]).Index);
}
}

[Test]
public void Forward_button_is_disabled_when_no_more_pages()
{
var data = GetDummyData(5);
var observer = new PaginatedListViewObserver(5);
observer.UpdateData(data);
Assert.IsFalse(observer.IsPageForwardButtonEnabled);
}

[Test]
public void Back_button_is_disabled_when_no_more_pages()
{
var data = GetDummyData(5);
var observer = new PaginatedListViewObserver(5);
observer.UpdateData(data);
Assert.IsFalse(observer.IsPageBackButtonEnabled);
}

[Test]
public void Forward_button_is_enabled_if_more_pages()
{
var data = GetDummyData(10);
var observer = new PaginatedListViewObserver(5);
observer.UpdateData(data);
Assert.IsTrue(observer.IsPageForwardButtonEnabled);
}

[Test]
public void Back_button_is_enabled_if_more_pages()
{
var data = GetDummyData(10);
var observer = new PaginatedListViewObserver(5);
observer.UpdateData(data);
observer.TryPageForward();
Assert.IsTrue(observer.IsPageBackButtonEnabled);
}

[Test]
public void Paging_forward_updates_rendered_data()
{
var data = GetDummyData(15);
var observer = new PaginatedListViewObserver(5);

observer.UpdateData(data);
observer.TryPageForward();

var elements = observer.VisibleElements.ToList();

for (var i = 0; i < elements.Count; i++)
{
var expectedIndex = i + 5; // We expect to be on the second page.
Assert.AreEqual(expectedIndex, ((PaginatedListViewObserver.DummyElement) elements[i]).Index);
}
}

[Test]
public void Paging_backwards_updates_rendered_data()
{
var data = GetDummyData(15);
var observer = new PaginatedListViewObserver(5);

observer.UpdateData(data);
observer.TryPageForward();
observer.TryPageBackwards();

var elements = observer.VisibleElements.ToList();

for (var i = 0; i < elements.Count; i++)
{
Assert.AreEqual(i, ((PaginatedListViewObserver.DummyElement) elements[i]).Index);
}
}

[Test]
public void Sub_slice_of_list_can_be_rendered()
{
var data = GetDummyData(8);
var observer = new PaginatedListViewObserver(5);

observer.UpdateData(data);
observer.TryPageForward();

var elements = observer.VisibleElements.ToList();

Assert.AreEqual(3, elements.Count);

for (var i = 0; i < elements.Count; i++)
{
var expectedIndex = i + 5; // We expect to be on the second page.
Assert.AreEqual(expectedIndex, ((PaginatedListViewObserver.DummyElement) elements[i]).Index);
}
}

private class PaginatedListViewObserver
{
public bool IsPageForwardButtonEnabled => listView.Q<Button>(name: "forward-button").enabledSelf;
public bool IsPageBackButtonEnabled => listView.Q<Button>(name: "back-button").enabledSelf;

public IEnumerable<VisualElement> VisibleElements =>
listView
.Q<VisualElement>(className: "user-defined-type-container-data")
.Children();

private readonly PaginatedListView<DummyElement, int> listView;

public PaginatedListViewObserver(int elementsPerPage)
{
listView = new PaginatedListView<DummyElement, int>("My List",
() => new DummyElement(),
(index, _, element) =>
{
element.Index = index;
}, elementsPerPage);
}

public void TryPageForward()
{
listView.ChangePageCount(1);
}

public void TryPageBackwards()
{
listView.ChangePageCount(-1);
}

public void UpdateData(List<int> data)
{
listView.Update(data);
}

public class DummyElement : VisualElement
{
public int Index;
}
}

private static List<int> GetDummyData(int count)
{
return Enumerable.Repeat(0, count).ToList();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a061cc

Please sign in to comment.