public void CreateConversationMessagesViewModelTest()
        {
            for (int i = 0; i < this.conversationMessages.Count; i++)
            {
                for (int j = 0; j < this.conversationMessages[i].Count; j++)
                {

                    MockServiceProxy serviceProxy = new MockServiceProxy() { Messages = this.conversationMessages[i][j].m_Item2};
                    MockDataContextWrapper dataContext = new MockDataContextWrapper(new MockDatabase() { });
                    MockUserSettings userSettings = new MockUserSettings();
                    userSettings.Save(owners[i]);

                    UserModel recipient = null;
                    foreach (UserModel user in this.conversationMessages[i][j].m_Item1.ConversationParticipants)
                    {
                        if (!user.Equals(userSettings.Me))
                        {
                            recipient = user;
                        }
                    }

                    ConversationMessagesViewModel messages = new ConversationMessagesViewModel(serviceProxy, dataContext, userSettings, this.conversationMessages[i][j].m_Item1.ConversationId, recipient);
                    NotifyPropertyChangedTester propertyChangedTester = new NotifyPropertyChangedTester(messages);
                    NotifyCollectionChangedTester<MessageModel> collectionChangedTester = new NotifyCollectionChangedTester<MessageModel>(messages.Messages);
                    messages.LoadMessagesForConversations();

                    Assert.AreEqual(1, propertyChangedTester.Changes.Count, "IsLoaded property changed event was not generated");
                    propertyChangedTester.AssertChange(0, "IsLoading");

                    Assert.AreEqual(this.conversationMessages[i][j].m_Item2.Count, collectionChangedTester.Count, "Number of messages in notify collection changed doesn't match");
                }
            }
        }
        public void HandlePushNotificationForDifferentConversationTest()
        {
            for (int i = 0; i < this.conversationMessages.Count; i++)
            {
                for (int j = 0; j < this.conversationMessages[i].Count; j++)
                {

                    MockServiceProxy serviceProxy = new MockServiceProxy() { Messages = this.conversationMessages[i][j].m_Item2 };
                    MockUserSettings userSettings = new MockUserSettings();
                    userSettings.Save(owners[i]);

                    UserModel recipient = null;
                    foreach (UserModel user in this.conversationMessages[i][j].m_Item1.ConversationParticipants)
                    {
                        if (!user.Equals(userSettings.Me))
                        {
                            recipient = user;
                        }
                    }

                    ConversationMessagesViewModel messages = new ConversationMessagesViewModel(serviceProxy, userSettings, this.conversationMessages[i][j].m_Item1.ConversationId, recipient);

                    messages.LoadMessagesForConversations();

                    Random random = new Random((int)DateTime.Now.Ticks);
                    long differentConversationId = long.MaxValue;
                    int differentConversationUserId = random.Next(1000, 2000);

                    PushNotificationEvent pushEvent = new PushNotificationEvent(this, (long)2000, differentConversationId, "test", DateTime.Now.Ticks, "ConversationMessagesHandlePushNotificationTest" + i.ToString(), differentConversationUserId);

                    int messageCountBeforePush = messages.Messages.Count;
                    NotifyCollectionChangedTester<MessageModel> collectionChangedTester = new NotifyCollectionChangedTester<MessageModel>(messages.Messages);
                    Messenger.Default.Send<PushNotificationEvent>(pushEvent);

                    Assert.AreEqual(0, collectionChangedTester.Count, "Collection changed was generated when it should not be");
                    Assert.AreEqual(messageCountBeforePush, messages.Messages.Count, "Push notification was not handled properly");
                }
            }
        }
        public void SendNewMessageTest()
        {
            for (int i = 0; i < this.conversationMessages.Count; i++)
            {
                for (int j = 0; j < this.conversationMessages[i].Count; j++)
                {

                    MockServiceProxy serviceProxy = new MockServiceProxy() { Messages = this.conversationMessages[i][j].m_Item2 };
                    MockUserSettings userSettings = new MockUserSettings();
                    userSettings.Save(owners[i]);

                    UserModel recipient = null;
                    foreach (UserModel user in this.conversationMessages[i][j].m_Item1.ConversationParticipants)
                    {
                        if (!user.Equals(userSettings.Me))
                        {
                            recipient = user;
                        }
                    }

                    ConversationMessagesViewModel messages = new ConversationMessagesViewModel(serviceProxy, userSettings, this.conversationMessages[i][j].m_Item1.ConversationId, recipient);

                    messages.LoadMessagesForConversations();

                    NotifyCollectionChangedTester<MessageModel> collectionChangedTester = new NotifyCollectionChangedTester<MessageModel>(messages.Messages);

                    /*************************************************
                     * SendMessage chat heads enabled
                     * ***********************************************/

                    int messageCountBeforeNewMessage = messages.Messages.Count;

                    // Send a new message.
                    // The mock proxy will invoke the callback immediately
                    messages.SendNewMessage("SendNewMessageTest message chatheads disabled" + DateTime.Now.Ticks);

                    Assert.AreEqual(1, collectionChangedTester.Count, "Collection changed event was not generated");
                    Assert.AreEqual(messageCountBeforeNewMessage + 1, messages.Messages.Count, "New message was not added to the collection");

                    /*************************************************
                     * SendMessage chat heads enabled
                     * ***********************************************/

                    // Send another message but this time with chatheads enabled.
                    // Ensure that quit application event is generated.
                    messageCountBeforeNewMessage = messages.Messages.Count;
                    userSettings.ChatHeadEnabled = true;
                    bool quitEventGenerated = false;
                    collectionChangedTester.Count = 0;
                    Messenger.Default.Register<QuitApplicationEvent>(this, (QuitApplicationEvent e) => { quitEventGenerated = true; });

                    messages.SendNewMessage("SendNewMessageTest message chatheads disabled" + DateTime.Now.Ticks);

                    Assert.AreEqual(1, collectionChangedTester.Count, "IsLoaded property changed event was not generated");
                    Assert.AreEqual(messageCountBeforeNewMessage + 1, messages.Messages.Count, "New messaged was not added to the collection");

                    /*************************************************
                     * SendMessage simulate failure
                     * ***********************************************/
                    // Remember the count before new message
                    messageCountBeforeNewMessage = messages.Messages.Count;

                    // Set chat heads to enabled to true and
                    userSettings.ChatHeadEnabled = true;
                    // Simulate failure to true
                    serviceProxy.SimulateSendMessageFailure = true;

                    // Set quitEventGenerated to false
                    quitEventGenerated = false;

                    collectionChangedTester.Count = 0;
                    Messenger.Default.Register<QuitApplicationEvent>(this, (QuitApplicationEvent e) => { quitEventGenerated = true; });

                    messages.SendNewMessage("SendNewMessageTest message chatheads disabled" + DateTime.Now.Ticks);

                    Assert.AreEqual(0, collectionChangedTester.Count, "IsLoaded property changed event was not generated");
                    Assert.AreEqual(messageCountBeforeNewMessage, messages.Messages.Count, "New messaged was not added to the collection");
                    Assert.AreEqual(false, quitEventGenerated, "Quit Application event was not generated");
                }
            }
        }