Exemplo n.º 1
0
        public void AcceptChat_Initiated()
        {
            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid1,
                Language = "English",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>() { guid2 },
                ConnectionIds = new Dictionary<string, List<string>>() { { "Lobby", new List<string>() { "C7" } } }
            });
            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid2,
                Language = "French",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Lobby", new List<string>() { "C8" } } }
            });

            ChatRoom chatRoom = new ChatRoom();

            var hub = new ChatHub(new MockTranslator());
            var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
            hub.Clients = mockClients.Object;

            var mockGroupManager = new Mock<IGroupManager>();
            hub.Groups = mockGroupManager.Object;

            dynamic all = new ExpandoObject();
            all.joinRoom = new Action<ChatRoom>((room) =>
            {
                chatRoom = room;
            });

            AttachIdentity(hub, "Tester8", "C8");

            mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Caller).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Others).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.OthersInGroup(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.User(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Group(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Clients(It.IsAny<List<string>>())).Returns((ExpandoObject)all);

            hub.AcceptChat(guid2, guid1);

            Assert.AreEqual(1, chatRoom.Id);
            Assert.AreEqual(2, chatRoom.Users.Count);
        }
Exemplo n.º 2
0
        public void AcceptChat(Guid acceptId, Guid inviteId)
        {
            var inviter = ConnectedUsers.FirstOrDefault(cu => cu.Id == inviteId);
            var current = ConnectedUsers.FirstOrDefault(cu => cu.Id == acceptId);

            if (inviter != null && current != null && inviter.Invites.Contains(current.Id))
            {
                var room = new ChatRoom()
                    {
                        Id = roomId++,
                        Users = new List<UserModel>() { inviter, current }
                    };

                ChatRooms.Add(room);

                var ids = inviter.ConnectionIds["Lobby"].Union(current.ConnectionIds["Lobby"]).ToList();

                Clients.Clients(ids).joinRoom(room);
            }
        }
Exemplo n.º 3
0
        public void SendMessage()
        {
            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid1,
                Language = "English",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Room2", new List<string>() { "C9" } } }
            });
            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid2,
                Language = "French",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Room2", new List<string>() { "C10" } } }
            });

            ChatRoom chatRoom = new ChatRoom() { Id = 2, Users = ChatHub.ConnectedUsers.ToList() };
            ChatHub.ChatRooms.Add(chatRoom);

            ChatMessage mess = new ChatMessage();

            var hub = new ChatHub(new MockTranslator());
            var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
            hub.Clients = mockClients.Object;

            var mockGroupManager = new Mock<IGroupManager>();
            hub.Groups = mockGroupManager.Object;

            dynamic all = new ExpandoObject();
            all.receiveMessage = new Action<ChatMessage>((message) =>
            {
                mess = message;
            });

            AttachIdentity(hub, "Tester9", "C9");

            mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Caller).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Others).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.OthersInGroup(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.User(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Client(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Clients(It.IsAny<List<string>>())).Returns((ExpandoObject)all);

            hub.SendMessage(2, new ChatMessage()
            {
                ClientSent = DateTime.Now,
                Message = "Hello World",
                Sender = chatRoom.Users[0]
            }).Wait();

            Assert.AreEqual(1, mess.Id);
            Assert.AreEqual("Tester9", mess.Sender.UserName);

            Assert.IsNotNull(mess.ServerSent);
            Assert.AreNotEqual(DateTime.MinValue, mess.ServerSent);
            Assert.IsTrue(mess.ServerSent > mess.ClientSent);

            Assert.AreEqual("English", mess.Sender.Language);
            Assert.AreEqual("Hello World", mess.Message);
            Assert.AreEqual("French: Hello World", mess.Translation);
        }