<commit_before>using Uno;
using Uno.Testing;

using Fuse.Navigation;

using FuseTest;

namespace Fuse.Controls.Test
{
	public class PageControlTest : TestBase
	{
		[Test]
		public void ActiveIndex()
		{
			var p = new UX.PageControl.PageIndex();
			using (var root = TestRootPanel.CreateWithChild(p, int2(100)))
			{
				root.StepFrameJS();

				//onValueChanged is expected to be called immediately
				Assert.AreEqual( 0, p.PC.ActiveIndex );
				Assert.AreEqual( p.A, p.PC.Active );
				Assert.AreEqual( "0", p.Q.Value );
				Assert.AreEqual( "Page0", p.R.Value );

				//the Active/ActiveIndex update immediately, even if the page progress animation is not complete
				p.Goto2.Perform();
				root.StepFrameJS();
				Assert.AreEqual( 2, p.PC.ActiveIndex );
				Assert.AreEqual( p.C, p.PC.Active );
				Assert.AreEqual( "2", p.Q.Value );
				Assert.AreEqual( "Page2", p.R.Value );

				p.PC.Active = p.D;
				root.StepFrameJS();
				Assert.AreEqual( 3, p.PC.ActiveIndex );
				Assert.AreEqual( p.D, p.PC.Active );
				Assert.AreEqual( "3", p.Q.Value );
				Assert.AreEqual( "Page3", p.R.Value );
			}
		}
		
		[Test]
		/** Ensures the SwipeNavigate integration is working (see SwipeNavigate.Test.uno for a more complete feature test */
		public void Swipe()
		{
			var p = new UX.PageControl.Swipe();
			using (var root = TestRootPanel.CreateWithChild(p, int2(1000)))
			{
				Assert.AreEqual(p.P1,p.Active);
				//swipe left (default to go forward)
				root.PointerSwipe( float2(800,100), float2(100,100), 300 );
				root.StepFrame(5); //stabilize
				Assert.AreEqual(p.P2,p.Active);
			}
		}
		
		[Test]
		//ensure specific distance covered by swiping
		public void SwipeProgress()
		{
			var p = new UX.PageControl.Swipe();
			using (var root = TestRootPanel.CreateWithChild(p,int2(1000)))
			{
				Assert.AreEqual(0, (p as INavigation).PageProgress);
				
				(p as INavigation).PageProgressChanged += OnPageProgressChanged;
				
				float speed = 100;
				root.PointerPress( float2(800,200) );
				root.PointerSlide( float2(800,200), float2(550,200), speed);
				//adjust for delayed gesture and accuracy of sliding steps
				const float ZeroTolerance = 1e-05f;
				Assert.AreEqual(0.25 - Fuse.Input.Gesture.HardCaptureSignificanceThreshold/1000,
					(p as INavigation).PageProgress,
					root.StepIncrement * speed / 1000 + ZeroTolerance);
					
				//trying to check jitter https://github.com/fusetools/fuselibs/issues/3597
				//the test doesn't produce "actual" jitter though, but it does detect the extra calls to progress changed
				Assert.IsTrue(_absChangedSum < 0.25);
				Assert.IsTrue(_progressCount < (250/*dist*/ / speed / root.StepIncrement + 1) );
			}
		}

		string SafeFormat( Route r )
		{
			if (r == null) 
				return "*null*";
			return r.Format();
		}

		[Test]
		//tests history through a local Active change
		public void History()
		{
			var p = new UX.PageControl.History();
			using (var root = TestRootPanel.CreateWithChild(p))
			{
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(0)) );
				Assert.AreEqual( null, p.router.GetHistoryRoute(1));
				Assert.AreEqual( 0, p.wcb.Progress );

				p.router.Push( new Route("a", null, new Route("ii") ) );
				root.StepFrame();
				Assert.AreEqual("a/ii", SafeFormat(p.router.GetHistoryRoute(0)));
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(1)));
				Assert.AreEqual( null, p.router.GetHistoryRoute(2));
				Assert.AreEqual( 1, p.wcb.Progress );

				p.pc.Active = p.b;
				root.StepFrame();
				Assert.AreEqual("b", SafeFormat(p.router.GetHistoryRoute(0)) );
				Assert.AreEqual( null, p.router.GetHistoryRoute(1));
				Assert.AreEqual( 0, p.wcb.Progress );

				p.pc.Active = p.a;
				root.StepFrame();
				Assert.AreEqual("a/ii", SafeFormat(p.router.GetHistoryRoute(0)));
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(1)));
				Assert.AreEqual( null, p.router.GetHistoryRoute(2));
				Assert.AreEqual( 1, p.wcb.Progress );

			}
		}
		
		double _absChangedSum;
		int _progressCount;
		void OnPageProgressChanged(object s, NavigationArgs args)
		{
			_progressCount++;
			var diff = args.Progress - args.PreviousProgress;
			_absChangedSum += Math.Abs(diff);
		}
		
		[Test]
		public void DynamicActiveIndex()
		{
			var p = new UX.PageControl.DynamicActiveIndex();
			using (var root = TestRootPanel.CreateWithChild(p, int2(1000)))
			{
				//give any changes to ActiveIndex a chance to propagate in both directions
				root.StepFrameJS();
				root.StepFrameJS(); 
				Assert.AreEqual( 2, p.index.Value );
				
				p.callAdd.Perform();
				root.StepFrameJS();
				Assert.AreEqual( 2, p.index.Value );
				
				p.goForward.Pulse();
				root.StepFrame(5);
				root.StepFrameJS();
				Assert.AreEqual( 1, p.index.Value );
				
				//swipe left (default to go forward)
				root.PointerSwipe( float2(100,100), float2(800,100), 300 );
				root.StepFrame(5); //stabilize
				Assert.AreEqual( 0, p.index.Value );
			}
		}
		
		[Test]
		public void Active()
		{
			var p = new UX.PageControl.Active();
			using (var root = TestRootPanel.CreateWithChild(p))
			{
				Assert.AreEqual( p.p2, p.pc1.Active );
				Assert.AreEqual( 1, p.p2.wa.Progress );
				Assert.AreEqual( 1, p.p2.an.Progress );
				Assert.AreEqual( 0, p.p1.wa.Progress );
				Assert.AreEqual( 0, p.p1.an.Progress );<commit_msg>Fuse.Controls.Navigation: step a JS-frame after stabilizing<commit_after>using Uno;
using Uno.Testing;

using Fuse.Navigation;

using FuseTest;

namespace Fuse.Controls.Test
{
	public class PageControlTest : TestBase
	{
		[Test]
		public void ActiveIndex()
		{
			var p = new UX.PageControl.PageIndex();
			using (var root = TestRootPanel.CreateWithChild(p, int2(100)))
			{
				root.StepFrameJS();

				//onValueChanged is expected to be called immediately
				Assert.AreEqual( 0, p.PC.ActiveIndex );
				Assert.AreEqual( p.A, p.PC.Active );
				Assert.AreEqual( "0", p.Q.Value );
				Assert.AreEqual( "Page0", p.R.Value );

				//the Active/ActiveIndex update immediately, even if the page progress animation is not complete
				p.Goto2.Perform();
				root.StepFrameJS();
				Assert.AreEqual( 2, p.PC.ActiveIndex );
				Assert.AreEqual( p.C, p.PC.Active );
				Assert.AreEqual( "2", p.Q.Value );
				Assert.AreEqual( "Page2", p.R.Value );

				p.PC.Active = p.D;
				root.StepFrameJS();
				Assert.AreEqual( 3, p.PC.ActiveIndex );
				Assert.AreEqual( p.D, p.PC.Active );
				Assert.AreEqual( "3", p.Q.Value );
				Assert.AreEqual( "Page3", p.R.Value );
			}
		}
		
		[Test]
		/** Ensures the SwipeNavigate integration is working (see SwipeNavigate.Test.uno for a more complete feature test */
		public void Swipe()
		{
			var p = new UX.PageControl.Swipe();
			using (var root = TestRootPanel.CreateWithChild(p, int2(1000)))
			{
				Assert.AreEqual(p.P1,p.Active);
				//swipe left (default to go forward)
				root.PointerSwipe( float2(800,100), float2(100,100), 300 );
				root.StepFrame(5); //stabilize
				Assert.AreEqual(p.P2,p.Active);
			}
		}
		
		[Test]
		//ensure specific distance covered by swiping
		public void SwipeProgress()
		{
			var p = new UX.PageControl.Swipe();
			using (var root = TestRootPanel.CreateWithChild(p,int2(1000)))
			{
				Assert.AreEqual(0, (p as INavigation).PageProgress);
				
				(p as INavigation).PageProgressChanged += OnPageProgressChanged;
				
				float speed = 100;
				root.PointerPress( float2(800,200) );
				root.PointerSlide( float2(800,200), float2(550,200), speed);
				//adjust for delayed gesture and accuracy of sliding steps
				const float ZeroTolerance = 1e-05f;
				Assert.AreEqual(0.25 - Fuse.Input.Gesture.HardCaptureSignificanceThreshold/1000,
					(p as INavigation).PageProgress,
					root.StepIncrement * speed / 1000 + ZeroTolerance);
					
				//trying to check jitter https://github.com/fusetools/fuselibs/issues/3597
				//the test doesn't produce "actual" jitter though, but it does detect the extra calls to progress changed
				Assert.IsTrue(_absChangedSum < 0.25);
				Assert.IsTrue(_progressCount < (250/*dist*/ / speed / root.StepIncrement + 1) );
			}
		}

		string SafeFormat( Route r )
		{
			if (r == null) 
				return "*null*";
			return r.Format();
		}

		[Test]
		//tests history through a local Active change
		public void History()
		{
			var p = new UX.PageControl.History();
			using (var root = TestRootPanel.CreateWithChild(p))
			{
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(0)) );
				Assert.AreEqual( null, p.router.GetHistoryRoute(1));
				Assert.AreEqual( 0, p.wcb.Progress );

				p.router.Push( new Route("a", null, new Route("ii") ) );
				root.StepFrame();
				Assert.AreEqual("a/ii", SafeFormat(p.router.GetHistoryRoute(0)));
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(1)));
				Assert.AreEqual( null, p.router.GetHistoryRoute(2));
				Assert.AreEqual( 1, p.wcb.Progress );

				p.pc.Active = p.b;
				root.StepFrame();
				Assert.AreEqual("b", SafeFormat(p.router.GetHistoryRoute(0)) );
				Assert.AreEqual( null, p.router.GetHistoryRoute(1));
				Assert.AreEqual( 0, p.wcb.Progress );

				p.pc.Active = p.a;
				root.StepFrame();
				Assert.AreEqual("a/ii", SafeFormat(p.router.GetHistoryRoute(0)));
				Assert.AreEqual("a/i", SafeFormat(p.router.GetHistoryRoute(1)));
				Assert.AreEqual( null, p.router.GetHistoryRoute(2));
				Assert.AreEqual( 1, p.wcb.Progress );

			}
		}
		
		double _absChangedSum;
		int _progressCount;
		void OnPageProgressChanged(object s, NavigationArgs args)
		{
			_progressCount++;
			var diff = args.Progress - args.PreviousProgress;
			_absChangedSum += Math.Abs(diff);
		}
		
		[Test]
		public void DynamicActiveIndex()
		{
			var p = new UX.PageControl.DynamicActiveIndex();
			using (var root = TestRootPanel.CreateWithChild(p, int2(1000)))
			{
				//give any changes to ActiveIndex a chance to propagate in both directions
				root.StepFrameJS();
				root.StepFrameJS(); 
				Assert.AreEqual( 2, p.index.Value );
				
				p.callAdd.Perform();
				root.StepFrameJS();
				Assert.AreEqual( 2, p.index.Value );
				
				p.goForward.Pulse();
				root.StepFrame(5);
				root.StepFrameJS();
				Assert.AreEqual( 1, p.index.Value );
				
				//swipe left (default to go forward)
				root.PointerSwipe( float2(100,100), float2(800,100), 300 );
				root.StepFrame(5); //stabilize
				root.StepFrameJS();
				Assert.AreEqual( 0, p.index.Value );
			}
		}
		
		[Test]
		public void Active()
		{
			var p = new UX.PageControl.Active();
			using (var root = TestRootPanel.CreateWithChild(p))
			{
				Assert.AreEqual( p.p2, p.pc1.Active );
				Assert.AreEqual( 1, p.p2.wa.Progress );
				Assert.AreEqual( 1, p.p2.an.Progress );
				Assert.AreEqual( 0, p.p1.wa.Progress );
				Assert.AreEqual( 0, p.p1.an.Progress );
{'fraction_non_alphanumeric': 0.14256352765321376, 'fraction_numerical': 0.02727952167414051, 'mean_word_length': 3.2095949665749117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6813500', 'n_tokens_mistral': 4598, 'n_tokens_neox': 3659, 'n_words': 794}