コード例 #1
0
        public void InitBrain_Returns_True_If_Logged_And_False_If_Not()
        {
            Brain brain = new Brain();
            var mr = new Mock<IMsgReceiver>();
            var notifier = new Mock<Notifier>();
            var allmessagesform = new Mock<IAllMessagesForm>();

            Assert.IsTrue(brain.InitBrain(mr.Object, notifier.Object, allmessagesform.Object));

            mr.Setup(foo => foo.GetMessagesCount()).Returns(0);

            // Не залогинен
            mr.Setup(foo => foo.IsLogged()).Returns(false);

            Assert.IsFalse(brain.IncreaseEntropy());

            // Залогинен
            mr.Setup(foo => foo.IsLogged()).Returns(true);
            Assert.IsTrue(brain.IncreaseEntropy());
        }
コード例 #2
0
        public void IncreaseEntropy_Doesnt_Work_Without_Initialization()
        {
            Brain brain = new Brain();

            Assert.IsFalse(brain.IncreaseEntropy());
        }
コード例 #3
0
        public void IncreaseEntropy_Correctly_Drops_Unwanted_Messages()
        {
            Brain brain = new Brain();
            var mr = new Mock<IMsgReceiver>();
            var notifier = new Mock<INotifier>();
            var allmessagesform = new Mock<IAllMessagesForm>();
            Message [] msgs = new Message[14];
            int messages_count = 0;
            bool showNotificationCalled = false;
            string desiredNotificationText = "";

            mr.Setup(foo => foo.IsLogged()).Returns(true);
            mr.Setup(foo => foo.GetMessagesCount()).Returns(() => { return messages_count; });
            mr.Setup(foo => foo.PopFirstMsg()).Returns(() =>
            {
                return msgs[messages_count-1];
            }).Callback(() => { messages_count--; });
            notifier.Setup(foo => foo.ShowNotification(It.IsAny<Notification>())).Returns((Notification n) =>
            {
                Assert.AreEqual(desiredNotificationText, n.NotificationText);

                showNotificationCalled = true;

                return true;
            });

            for (int i = 0; i < 2; i++)
            {
                msgs[i] = new Message();

                msgs[i].MsgType = MsgTypes.Personal;
            }

            for (int i = 2; i < 6; i++)
            {
                msgs[i] = new Message();

                msgs[i].MsgType = MsgTypes.Dialog;
            }

            for (int i = 6; i < 14; i++)
            {
                msgs[i] = new Message();

                msgs[i].MsgType = MsgTypes.Group;
            }

            Assert.IsTrue(brain.InitBrain(mr.Object, notifier.Object, allmessagesform.Object));

            for (int i = 0; i < 8; i++)
            {
                bool notifyAboutPersonal = (i & 1) > 0;
                bool notifyAboutDialogs = (i & 2) > 0;
                bool notifyAboutGroups = (i & 4) > 0;

                brain.NotifyAboutPersonal = notifyAboutPersonal;
                brain.NotifyAboutDialogs = notifyAboutDialogs;
                brain.NotifyAboutGroups = notifyAboutGroups;

                showNotificationCalled = false;
                messages_count = 14;
                desiredNotificationText = "У вас " + i * 2 + " непрочитанных сообщений";
                Assert.AreEqual(14, mr.Object.GetMessagesCount());
                brain.BrainDrain();
                Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Получаем " + i * 2 + " новых сообщения");
                Assert.AreEqual(i != 0, showNotificationCalled, "showNotificationCalled, Получаем " + i * 2 + " новых сообщения");
                Assert.AreEqual(0, mr.Object.GetMessagesCount());
            }
        }
コード例 #4
0
        public void BrainDrain_Clears_Message_Ids_And_No_Doubling_Test()
        {
            Brain brain = new Brain();
            var mr = new Mock<IMsgReceiver>();
            var notifier = new Mock<INotifier>();
            var allmessagesform = new Mock<IAllMessagesForm>();
            Message msg1 = new Message(), msg2 = new Message(), msg3 = new Message();
            int messages_count = 0;
            bool showNotificationCalled = false;
            string desiredNotificationText = "";

            Assert.IsTrue(brain.InitBrain(mr.Object, notifier.Object, allmessagesform.Object));

            msg1.Id = 1;
            msg2.Id = 2;
            msg3.Id = 3;

            mr.Setup(foo => foo.IsLogged()).Returns(true);
            mr.Setup(foo => foo.GetMessagesCount()).Returns(() => { return messages_count; });
            mr.Setup(foo => foo.PopFirstMsg()).Returns(() =>
                {
                    if (messages_count == 3)
                        return msg3;
                    else if (messages_count == 2)
                        return msg2;
                    else
                        return msg1;
                } ).Callback(() => { messages_count--; });
            notifier.Setup(foo => foo.ShowNotification(It.IsAny<Notification>())).Returns((Notification n) =>
                {
                    Assert.AreEqual(desiredNotificationText, n.NotificationText);

                    showNotificationCalled = true;

                    return true;
                });

            // Получаем 2 новых сообщения
            showNotificationCalled = false;
            messages_count = 2;
            desiredNotificationText = "У вас 2 непрочитанных сообщений";
            Assert.AreEqual(2, mr.Object.GetMessagesCount());
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Получаем 2 новых сообщения");
            Assert.IsTrue(showNotificationCalled, "showNotificationCalled, Получаем 2 новых сообщения");
            Assert.AreEqual(0, mr.Object.GetMessagesCount());

            // Те же 2 новых сообщения
            showNotificationCalled = false;
            messages_count = 2;
            Assert.AreEqual(2, mr.Object.GetMessagesCount());
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Те же 2 новых сообщения");
            Assert.IsFalse(showNotificationCalled, "showNotificationCalled, Те же 2 новых сообщения");
            Assert.AreEqual(0, mr.Object.GetMessagesCount());

            // Те же 2 + 1 новое сообщение
            showNotificationCalled = false;
            messages_count = 3;
            desiredNotificationText = "У вас 3 непрочитанных сообщений";
            Assert.AreEqual(3, mr.Object.GetMessagesCount());
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Те же 2 + 1 новое сообщение");
            Assert.IsTrue(showNotificationCalled, "showNotificationCalled, Те же 2 + 1 новое сообщение");
            Assert.AreEqual(0, mr.Object.GetMessagesCount());

            // Те же 3 новых сообщения
            showNotificationCalled = false;
            messages_count = 3;
            Assert.AreEqual(3, mr.Object.GetMessagesCount());
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Те же 3 новых сообщения");
            Assert.IsFalse(showNotificationCalled, "showNotificationCalled, Те же 3 новых сообщения");
            Assert.AreEqual(0, mr.Object.GetMessagesCount());

            // Те же 3 новых сообщения после вызова BrainDrain()
            showNotificationCalled = false;
            messages_count = 3;
            Assert.AreEqual(3, mr.Object.GetMessagesCount());
            brain.BrainDrain();
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), Те же 3 новых сообщения после вызова BrainDrain()");
            Assert.IsTrue(showNotificationCalled, "showNotificationCalled, Те же 3 новых сообщения после вызова BrainDrain()");
            Assert.AreEqual(0, mr.Object.GetMessagesCount());
        }
コード例 #5
0
        public void IncreaseEntropy_Breaks_On_Null_Messages()
        {
            Brain brain = new Brain();
            var mr = new Mock<IMsgReceiver>();
            var notifier = new Mock<INotifier>();
            var allmessagesform = new Mock<IAllMessagesForm>();
            int messages_count = 0;
            bool showNotificationCalled = false;

            Assert.IsTrue(brain.InitBrain(mr.Object, notifier.Object, allmessagesform.Object));

            mr.Setup(foo => foo.IsLogged()).Returns(true);
            mr.Setup(foo => foo.GetMessagesCount()).Returns(() => { return messages_count; });
            mr.Setup(foo => foo.PopFirstMsg()).Returns(() =>
            {
                return null;
            }).Callback(() => { messages_count--; });
            notifier.Setup(foo => foo.ShowNotification(It.IsAny<Notification>())).Returns((Notification n) =>
            {
                showNotificationCalled = true;

                return true;
            });

            showNotificationCalled = false;
            messages_count = 3;
            Assert.AreEqual(3, mr.Object.GetMessagesCount());
            brain.BrainDrain();
            Assert.IsTrue(brain.IncreaseEntropy(), "brain.IncreaseEntropy(), 3 null cообщения");
            Assert.IsFalse(showNotificationCalled, "showNotificationCalled, 3 null cообщения");
            Assert.AreEqual(2, mr.Object.GetMessagesCount());
        }