コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        /// <exception cref="System.Exception">
        /// </exception>
        public Form1()
        {
            InitializeComponent();

            this.FormClosing += Form1_FormClosing;

            receiver = new MsgReceiver();
            notifier = new Notifier();
            brain = new Brain();
            unreadMessagesForm = new AllMessagesForm();

            if (!notifier.InitNotifier())
                throw new Exception();

            if (brain.LoadSettings())
                if (!string.IsNullOrEmpty(brain.Login))
                    receiver.LogInVk(brain.Login, brain.Password);

            if (!receiver.IsLogged())
            {
                ShowLoginWindow();

                // Случай, когда пользователь просто закрыл окно входа
                if (!receiver.IsLogged())
                {
                    notifier.ShowAuthError();

                    brain.Login = "";
                    brain.Password = "";
                }
            }
            else
                notifier.ShowDefault();

            SetupNotifyIconMenus();

            if (!notifier.SetContextMenu(contextMenu))
                throw new Exception();

            if (!notifier.SetLaunchCallback(LaunchCallback))
                throw new Exception();

            if (!brain.InitBrain(receiver, notifier, unreadMessagesForm))
                throw new Exception();
        }
コード例 #2
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());
        }
コード例 #3
0
        public void InitBrain_Arguments_Testing()
        {
            Brain brain = new Brain();
            var mr = new Mock<IMsgReceiver>();
            var notifier = new Mock<Notifier>();
            var allmessagesform = new Mock<IAllMessagesForm>();

            Assert.IsFalse(brain.InitBrain(null, null, null));
            Assert.IsFalse(brain.InitBrain(mr.Object, null, null));
            Assert.IsFalse(brain.InitBrain(null, notifier.Object, null));
            Assert.IsFalse(brain.InitBrain(mr.Object, notifier.Object, null));
            Assert.IsFalse(brain.InitBrain(null, null, allmessagesform.Object));
            Assert.IsFalse(brain.InitBrain(mr.Object, null, allmessagesform.Object));
            Assert.IsFalse(brain.InitBrain(null, notifier.Object, allmessagesform.Object));
            Assert.IsTrue(brain.InitBrain(mr.Object, notifier.Object, allmessagesform.Object));
        }
コード例 #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_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());
            }
        }
コード例 #6
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());
        }
コード例 #7
0
        /// <summary>
        /// Запускает приложение.
        /// </summary>
        /// <exception cref="System.Exception">
        /// </exception>
        public void RunApp()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            receiver = new MsgReceiver();
            notifier = new Notifier();
            brain = new Brain();
            unreadMessagesForm = new AllMessagesForm();

            if (!notifier.InitNotifier())
                throw new Exception();

            if (brain.LoadSettings())
                if(!string.IsNullOrEmpty(brain.Login))
                    receiver.LogInVk(brain.Login, brain.Password);

            if (!receiver.IsLogged())
            {
                ShowLoginWindow();

                // Случай, когда пользователь просто закрыл окно входа
                if (!receiver.IsLogged())
                {
                    notifier.ShowAuthError();

                    brain.Login = "";
                    brain.Password = "";
                }
            }
            else
                notifier.ShowDefault();

            SetupNotifyIconMenus();

            if (!notifier.SetContextMenu(contextMenu))
                throw new Exception();

            if (!notifier.SetLaunchCallback(LaunchCallback))
                throw new Exception();

            if (!brain.InitBrain(receiver, notifier, unreadMessagesForm))
                throw new Exception();

            appTimer = new System.Timers.Timer(5000);
            appTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            appTimer.Enabled = true;

            Application.ApplicationExit += AppExitEvent;
            Application.Run();
        }