Esempio n. 1
0
        public void ShouldSucceedAndSetCooldown_WhenDecorateeSucceeds()
        {
            MockNode child        = new MockNode();
            Cooldown sut          = new Cooldown(1, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // make child suceed
            child.Finish(true);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock past cooldown and check that the child has been activated
            behaviorTree.Clock.Update(2.0f);
            Assert.AreEqual(Node.State.ACTIVE, child.CurrentState);
        }
Esempio n. 2
0
        public void ShouldSucceed_WhenFirstChildSuccessful()
        {
            MockNode       firstChild   = new MockNode();
            MockNode       secondChild  = new MockNode();
            RandomSelector sut          = new RandomSelector(firstChild, secondChild);
            TestRoot       behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            MockNode firstActiveChild = sut.DebugGetActiveChild() as MockNode;
            MockNode inactiveChild    = firstActiveChild == firstChild ? secondChild : firstChild;

            Assert.IsNotNull(firstActiveChild);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstActiveChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, inactiveChild.CurrentState);

            firstActiveChild.Finish(true);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Esempio n. 3
0
        public void ShouldSucceedAndSetCooldownRightAway_WhenDecorateeSucceeds()
        {
            MockNode child        = new MockNode();
            Cooldown sut          = new Cooldown(1, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // wait 1.5 seconds
            behaviorTree.Clock.Update(1.5f);

            // make child suceed
            child.Finish(true);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has been started as well ( due to cooldown )
            Assert.AreEqual(Node.State.ACTIVE, child.CurrentState);
        }
Esempio n. 4
0
        public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldNotRestartFirstChild_WhenSecondChildFails()
        {
            MockNode       firstChild   = new MockNode();
            MockNode       secondChild  = new MockNode(false);
            RandomSequence sut          = new RandomSequence(firstChild, secondChild);
            TestRoot       behaviorTree = CreateBehaviorTree(sut);

            // TODO: will we keep the priority or will we switch to the priority defined by the randomized children?
            RandomSequence.DebugSetSeed(2);

            behaviorTree.Start();

            firstChild.Finish(true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, true);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Esempio n. 5
0
        public void ShouldProcceedToSecondChild_WhenFirstChildSucceeded()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Sequence sut          = new Sequence(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);

            firstChild.Finish(true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            secondChild.Finish(false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Esempio n. 6
0
        public void ShouldFail_WhenFirstChildFails()
        {
            MockNode       firstChild   = new MockNode();
            MockNode       secondChild  = new MockNode();
            RandomSequence sut          = new RandomSequence(firstChild, secondChild);
            TestRoot       behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            MockNode activeChild = sut.DebugGetActiveChild() as MockNode;

            Assert.NotNull(activeChild);
            MockNode inactiveChild = activeChild == firstChild ? secondChild : firstChild;

            Assert.AreEqual(Node.State.ACTIVE, activeChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, inactiveChild.CurrentState);

            activeChild.Finish(false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Esempio n. 7
0
        public void ShouldFailInsteadOfWait_WhenCooldownActive_DueToParameter()
        {
            MockNode child        = new MockNode();
            Cooldown sut          = new Cooldown(1, false, false, true, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // make child suceed
            child.Finish(true);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();

            // ensure that neither this node nor the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock past cooldown, start the tree again and check that we could be activated again
            behaviorTree.Clock.Update(2.0f);
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, child.CurrentState);
        }
Esempio n. 8
0
        public void ShouldSucceed_WhenDecorateeSucceededGivenTimes()
        {
            MockNode succeedingChild = new MockNode();
            Repeater sut             = new Repeater(3, succeedingChild);
            TestRoot behaviorTree    = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            for (int i = 0; i < 2; i++)
            {
                succeedingChild.Finish(true);
                Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
                Assert.IsFalse(behaviorTree.DidFinish);
                Timer.Update(0.01f);
            }

            succeedingChild.Finish(true);
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Esempio n. 9
0
        public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldThrowError()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            Parallel.Policy successPolicy = Parallel.Policy.ALL;

            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);
            sut.StopLowerPriorityChildrenForChild(firstChild, false);
        }
Esempio n. 10
0
        public void ShouldNotLeaveObserversRegistered_WhenInactive()
        {
            MockNode child        = new MockNode();
            Repeater sut          = new Repeater(-1, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            child.Finish(true);

            Assert.AreEqual(1, behaviorTree.Clock.NumTimers);
            Assert.AreEqual(0, behaviorTree.Clock.NumUpdateObservers);

            Timer.Update(0.01f);
            child.Finish(false);

            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);

            Assert.AreEqual(0, behaviorTree.Clock.NumTimers);
            Assert.AreEqual(0, behaviorTree.Clock.NumUpdateObservers);
        }
Esempio n. 11
0
        public void ShouldFail_WhenDecorateeFails()
        {
            MockNode failingChild = new MockNode();
            Repeater sut          = new Repeater(-1, failingChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            failingChild.Finish(false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Esempio n. 12
0
        public void ShouldSucceed_WhenSingleChildSucceeds()
        {
            MockNode succeedingChild = new MockNode();
            Sequence sut             = new Sequence(succeedingChild);
            TestRoot behaviorTree    = CreateBehaviorTree(sut);

            behaviorTree.Start();

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            succeedingChild.Finish(true);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Esempio n. 13
0
        public void ShouldSucceed_WhenNotAllChildrenFailed_AndFailPolicyAll_AndSucessPolicyOne()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            Parallel.Policy successPolicy = Parallel.Policy.ONE;
            MockNode        firstChild    = new MockNode();
            MockNode        secondChild   = new MockNode();
            Parallel        sut           = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
            TestRoot        behaviorTree  = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);
            secondChild.Finish(true);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsTrue(behaviorTree.WasSuccess);
        }
Esempio n. 14
0
        public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldRestartChild()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            Parallel.Policy successPolicy = Parallel.Policy.ALL;

            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);
            sut.StopLowerPriorityChildrenForChild(firstChild, true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);
        }
Esempio n. 15
0
        public void ShouldFail_WhenAllChildrenFailed_AndFailPolicyAll()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            foreach (Parallel.Policy successPolicy in ALL_SUCCESS_POLICIES)
            {
                MockNode firstChild   = new MockNode();
                MockNode secondChild  = new MockNode();
                Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
                TestRoot behaviorTree = CreateBehaviorTree(sut);

                behaviorTree.Start();
                firstChild.Finish(false);
                secondChild.Finish(false);

                Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
                Assert.IsTrue(behaviorTree.DidFinish);
                Assert.IsFalse(behaviorTree.WasSuccess);
            }
        }
Esempio n. 16
0
        public void ShouldSucceed_WhenOneChildSuccessful_AndSuccessPolicyOne()
        {
            Parallel.Policy successPolicy = Parallel.Policy.ONE;
            foreach (Parallel.Policy failurePolicy in ALL_FAILURE_POLICIES)
            {
                MockNode firstChild   = new MockNode();
                MockNode secondChild  = new MockNode(false);
                Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
                TestRoot behaviorTree = CreateBehaviorTree(sut);

                behaviorTree.Start();
                firstChild.Finish(true);

                Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
                Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
                Assert.IsTrue(behaviorTree.DidFinish);
                Assert.IsTrue(behaviorTree.WasSuccess);
            }
        }
Esempio n. 17
0
        public void ShouldWaitForNextUpdate_WhenDecorateeSucceedsImmediately()
        {
            MockNode succeedingChild = new MockNode();
            Repeater sut             = new Repeater(-1, succeedingChild);
            TestRoot behaviorTree    = CreateBehaviorTree(sut);

            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            succeedingChild.Finish(true);

            // the child should not have been immediately restarted
            Assert.AreEqual(Node.State.INACTIVE, succeedingChild.CurrentState);

            // after update it's ok to have
            Timer.Update(0.01f);
            Assert.AreEqual(Node.State.ACTIVE, succeedingChild.CurrentState);

            Assert.IsFalse(behaviorTree.DidFinish);
        }
Esempio n. 18
0
        public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldRestartFirstChild_WhenSecondChildSucceeds()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode(true);
            Sequence sut          = new Sequence(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, true);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsFalse(behaviorTree.DidFinish);
        }
Esempio n. 19
0
        public void ShouldSucceed_WhenSingleChildSucceeds()
        {
            foreach (Parallel.Policy sucessPolicy in ALL_SUCCESS_POLICIES)
            {
                foreach (Parallel.Policy failurePolicy in ALL_FAILURE_POLICIES)
                {
                    MockNode succeedingChild = new MockNode();
                    Parallel sut             = new Parallel(sucessPolicy, failurePolicy, succeedingChild);
                    TestRoot behaviorTree    = CreateBehaviorTree(sut);

                    behaviorTree.Start();
                    Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

                    succeedingChild.Finish(true);

                    Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
                    Assert.IsTrue(behaviorTree.DidFinish);
                    Assert.IsTrue(behaviorTree.WasSuccess);
                }
            }
        }
        public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldThrowError()
        {
            Parallel.Policy failurePolicy = Parallel.Policy.ALL;
            Parallel.Policy successPolicy = Parallel.Policy.ALL;

            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Parallel sut          = new Parallel(successPolicy, failurePolicy, firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);
            bool bExceptionCought = false;

            try {
                sut.StopLowerPriorityChildrenForChild(firstChild, false);
            } catch (Exception) {
                bExceptionCought = true;
            }
            Assert.AreEqual(bExceptionCought, true);
        }
Esempio n. 21
0
        public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldCancelSecondChild()
        {
            MockNode firstChild   = new MockNode();
            MockNode secondChild  = new MockNode();
            Selector sut          = new Selector(firstChild, secondChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            behaviorTree.Start();
            firstChild.Finish(false);

            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState);

            sut.StopLowerPriorityChildrenForChild(firstChild, false);

            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState);
            Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState);
            Assert.IsTrue(behaviorTree.DidFinish);
            Assert.IsFalse(behaviorTree.WasSuccess);
        }
Esempio n. 22
0
        public void ShouldSucceedButSetCooldownAfterDecorator_WhenDecorateeSucceeds_DueToParameter()
        {
            MockNode child        = new MockNode();
            Cooldown sut          = new Cooldown(1, true, false, false, child);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // wait 1.5 seconds
            behaviorTree.Clock.Update(1.5f);

            // make child suceed
            child.Finish(true);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock to be at 2.0 seconds
            behaviorTree.Clock.Update(0.5f);

            // ensure the child has not been started ( due to cooldown )
            Assert.AreEqual(Node.State.INACTIVE, child.CurrentState);

            // advance clock to be at 3 seconds
            behaviorTree.Clock.Update(1.0f);

            // ensure the child has been started
            Assert.AreEqual(Node.State.ACTIVE, child.CurrentState);
        }
Esempio n. 23
0
        public void ShouldFailAndNotSetCooldown_WhenDecorateeFails_DueToParameter()
        {
            MockNode failingChild = new MockNode();
            Cooldown sut          = new Cooldown(1, false, true, failingChild);
            TestRoot behaviorTree = CreateBehaviorTree(sut);

            // start
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // make child fail
            failingChild.Finish(false);

            // ensure we're stopped
            Assert.AreEqual(Node.State.INACTIVE, sut.CurrentState);

            // start again
            behaviorTree.Start();
            Assert.AreEqual(Node.State.ACTIVE, sut.CurrentState);

            // ensure the child has been started again ( due to cooldown not active )
            Assert.AreEqual(Node.State.ACTIVE, failingChild.CurrentState);
        }