From 2a061cce33dc7fee0e4792324b1db9bda180208d Mon Sep 17 00:00:00 2001 From: Jamie Brynes Date: Tue, 16 Jun 2020 17:25:40 +0100 Subject: [PATCH] add unit tests for paginatedlistview --- .../WorkerInspector/Codegen/AssemblyInfo.cs | 3 + .../Codegen/AssemblyInfo.cs.meta | 3 + .../Codegen/PaginatedListView.cs | 2 +- .../WorkerInspector/Codegen/Tests.meta | 8 + ...rkerInspector.Codegen.EditmodeTests.asmdef | 19 ++ ...nspector.Codegen.EditmodeTests.asmdef.meta | 7 + .../Codegen/Tests/PaginatedListViewTests.cs | 181 ++++++++++++++++++ .../Tests/PaginatedListViewTests.cs.meta | 3 + 8 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs.meta create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests.meta create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef.meta create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/PaginatedListViewTests.cs create mode 100644 workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/PaginatedListViewTests.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs new file mode 100644 index 0000000000..d42e52dbea --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests")] diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs.meta b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs.meta new file mode 100644 index 0000000000..b78753c81a --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/AssemblyInfo.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: af24913a485043009e9bf180b2f1aca8 +timeCreated: 1592324281 \ No newline at end of file diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs index a81d112963..e946d90a62 100644 --- a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/PaginatedListView.cs @@ -69,7 +69,7 @@ public void Update(List newData) RefreshView(); } - private void ChangePageCount(int diff) + internal void ChangePageCount(int diff) { currentPage += diff; currentPage = Mathf.Clamp(currentPage, 0, numPages - 1); diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests.meta b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests.meta new file mode 100644 index 0000000000..5a07ac6a40 --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 15c0ca415a4844838eadcc42afef1dee +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef new file mode 100644 index 0000000000..6e186ba208 --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef @@ -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 +} \ No newline at end of file diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef.meta b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef.meta new file mode 100644 index 0000000000..55ad2565da --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/Improbable.Gdk.Debug.WorkerInspector.Codegen.EditmodeTests.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e484a5bd55ef8694abe8b9342cbf7baf +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/PaginatedListViewTests.cs b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/PaginatedListViewTests.cs new file mode 100644 index 0000000000..e4f0618c44 --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/Codegen/Tests/PaginatedListViewTests.cs @@ -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