< Summary

Class:LazyLoadingFriendsControllerMock
Assembly:LazyLoadingFriendsController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/LazyLoading/LazyLoadingFriendsControllerMock.cs
Covered lines:0
Uncovered lines:141
Coverable lines:141
Total lines:395
Line coverage:0% (0 of 141)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LazyLoadingFriendsControllerMock(...)0%2100%
add_OnInitialized(...)0%2100%
remove_OnInitialized(...)0%2100%
add_OnUpdateFriendship(...)0%2100%
remove_OnUpdateFriendship(...)0%2100%
add_OnUpdateUserStatus(...)0%2100%
remove_OnUpdateUserStatus(...)0%2100%
add_OnFriendNotFound(...)0%2100%
remove_OnFriendNotFound(...)0%2100%
add_OnAddFriendsWithDirectMessages(...)0%2100%
remove_OnAddFriendsWithDirectMessages(...)0%2100%
add_OnTotalFriendRequestUpdated(...)0%2100%
remove_OnTotalFriendRequestUpdated(...)0%2100%
add_OnTotalFriendsUpdated(...)0%2100%
remove_OnTotalFriendsUpdated(...)0%2100%
GetAllocatedFriends()0%2100%
GetUserStatus(...)0%2100%
ContainsStatus(...)0%2100%
RequestFriendship(...)0%2100%
CancelRequest(...)0%2100%
AcceptFriendship(...)0%2100%
RejectFriendship(...)0%2100%
IsFriend(...)0%2100%
RemoveFriend(...)0%2100%
GetFriends(...)0%2100%
GetFriends(...)0%2100%
GetFriendRequests(...)0%2100%
GetFriendsWithDirectMessages(...)0%2100%
GetFriendsWithDirectMessages(...)0%2100%
SimulateDelayedResponseFor_GetFriendsWithDirectMessages()0%12300%
CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(...)0%12300%
SimulateDelayedResponseFor_GetFriendsWithDirectMessages()0%12300%
CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(...)0%20400%
GetFakeFriendsAsync()0%56700%
GetFakeRequestsAsync()0%90900%
CreateFakeFriend(...)0%20400%
SimulateDelayedResponseFor_InitializeFriends()0%12300%
CreateFakeFriendsInitialization()0%2100%
UpdateFriendshipCount()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/LazyLoading/LazyLoadingFriendsControllerMock.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Cysharp.Threading.Tasks;
 5using DCL.Friends.WebApi;
 6using UnityEngine;
 7using Random = UnityEngine.Random;
 8
 9public class LazyLoadingFriendsControllerMock : IFriendsController
 10{
 11    private const int MAX_AMOUNT_OF_FAKE_FRIENDS = 130;
 12    private const int TOTAL_RECEIVED_REQUESTS = 14;
 13    private const int TOTAL_SENT_REQUESTS = 18;
 14    private const int TOTAL_FRIENDS = 46;
 15
 16    private readonly FriendsController controller;
 17    private readonly UserProfileController userProfileController;
 18
 19    private int amountOfFriendsWithDirectMessagesRequested;
 020    private int lastFriendWithDirectMessageIndex = -1;
 21
 22    public event Action OnInitialized
 23    {
 024        add => controller.OnInitialized += value;
 025        remove => controller.OnInitialized -= value;
 26    }
 27
 28    public event Action<string, FriendshipAction> OnUpdateFriendship
 29    {
 030        add => controller.OnUpdateFriendship += value;
 031        remove => controller.OnUpdateFriendship -= value;
 32    }
 33
 34    public event Action<string, UserStatus> OnUpdateUserStatus
 35    {
 036        add => controller.OnUpdateUserStatus += value;
 037        remove => controller.OnUpdateUserStatus -= value;
 38    }
 39
 40    public event Action<string> OnFriendNotFound
 41    {
 042        add => controller.OnFriendNotFound += value;
 043        remove => controller.OnFriendNotFound -= value;
 44    }
 45
 46    public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages
 47    {
 048        add => controller.OnAddFriendsWithDirectMessages += value;
 049        remove => controller.OnAddFriendsWithDirectMessages -= value;
 50    }
 51
 52    public event Action<int, int> OnTotalFriendRequestUpdated
 53    {
 054        add => controller.OnTotalFriendRequestUpdated += value;
 055        remove => controller.OnTotalFriendRequestUpdated -= value;
 56    }
 57
 58    public event Action<int> OnTotalFriendsUpdated
 59    {
 060        add => controller.OnTotalFriendsUpdated += value;
 061        remove => controller.OnTotalFriendsUpdated -= value;
 62    }
 63
 064    public int AllocatedFriendCount => controller.AllocatedFriendCount;
 065    public bool IsInitialized => controller.IsInitialized;
 066    public int ReceivedRequestCount => controller.ReceivedRequestCount;
 067    public int TotalFriendCount => controller.TotalFriendCount;
 068    public int TotalFriendRequestCount => controller.TotalFriendRequestCount;
 069    public int TotalReceivedFriendRequestCount => controller.TotalReceivedFriendRequestCount;
 070    public int TotalSentFriendRequestCount => controller.TotalSentFriendRequestCount;
 071    public int TotalFriendsWithDirectMessagesCount => controller.TotalFriendsWithDirectMessagesCount;
 72
 073    public LazyLoadingFriendsControllerMock(
 74        FriendsController controller,
 75        UserProfileController userProfileController)
 76    {
 077        this.controller = controller;
 078        this.userProfileController = userProfileController;
 79
 80        // TODO: Use it when the friends service is down
 081        SimulateDelayedResponseFor_InitializeFriends().Forget();
 082    }
 83
 084    public Dictionary<string, UserStatus> GetAllocatedFriends() => controller.GetAllocatedFriends();
 85
 086    public UserStatus GetUserStatus(string userId) => controller.GetUserStatus(userId);
 87
 088    public bool ContainsStatus(string friendId, FriendshipStatus status) => controller.ContainsStatus(friendId, status);
 89
 90    public void RequestFriendship(string friendUserId)
 91    {
 092        controller.RequestFriendship(friendUserId);
 093        UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget();
 094    }
 95
 96    public void CancelRequest(string friendUserId)
 97    {
 098        controller.CancelRequest(friendUserId);
 099        UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget();
 0100    }
 101
 102    public void AcceptFriendship(string friendUserId)
 103    {
 0104        controller.AcceptFriendship(friendUserId);
 0105        UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget();
 0106    }
 107
 108    public void RejectFriendship(string friendUserId)
 109    {
 0110        controller.RejectFriendship(friendUserId);
 0111        UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget();
 0112    }
 113
 0114    public bool IsFriend(string userId) => controller.IsFriend(userId);
 115
 116    public void RemoveFriend(string friendId)
 117    {
 0118        controller.RemoveFriend(friendId);
 0119        UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget();
 0120    }
 121
 122    public void GetFriends(int limit, int skip)
 123    {
 0124        GetFakeFriendsAsync(limit, skip, "ff").Forget();
 0125    }
 126
 127    public void GetFriends(string usernameOrId, int limit)
 128    {
 0129        GetFakeFriendsAsync(limit, 0, usernameOrId).Forget();
 0130    }
 131
 132    public void GetFriendRequests(
 133        int sentLimit,
 134        int sentSkip,
 135        int receivedLimit,
 136        int receivedSkip)
 137    {
 0138        GetFakeRequestsAsync(sentLimit).Forget();
 0139    }
 140
 141    public void GetFriendsWithDirectMessages(int limit, int skip)
 142    {
 0143        SimulateDelayedResponseFor_GetFriendsWithDirectMessages(limit).Forget();
 0144    }
 145
 146    public void GetFriendsWithDirectMessages(string userNameOrId, int limit)
 147    {
 0148        SimulateDelayedResponseFor_GetFriendsWithDirectMessages(userNameOrId, limit).Forget();
 0149    }
 150
 151    private async UniTask SimulateDelayedResponseFor_GetFriendsWithDirectMessages(int limit)
 152    {
 0153        await UniTask.Delay(Random.Range(1000, 3000));
 154
 0155        controller.AddFriendsWithDirectMessages(
 156            CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(limit));
 0157    }
 158
 159    private string CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(int numberOfUsers)
 160    {
 0161        AddFriendsWithDirectMessagesPayload mockedPayload = new AddFriendsWithDirectMessagesPayload();
 0162        List<FriendWithDirectMessages> mockedFriendWithDirectMessages = new List<FriendWithDirectMessages>();
 163
 0164        int indexToStart = lastFriendWithDirectMessageIndex + 1;
 165
 0166        for (int i = indexToStart; i < indexToStart + numberOfUsers; i++)
 167        {
 0168            if (amountOfFriendsWithDirectMessagesRequested >= MAX_AMOUNT_OF_FAKE_FRIENDS)
 169                break;
 170
 0171            string fakeUserId = $"fakeuser{i + 1}";
 172
 0173            mockedFriendWithDirectMessages.Add(
 174                new FriendWithDirectMessages
 175                {
 176                    userId = fakeUserId,
 177                    lastMessageBody = $"This is the last message sent for {fakeUserId}",
 178                    lastMessageTimestamp = DateTimeOffset.UtcNow.AddMinutes(-(i + 1)).ToUnixTimeMilliseconds()
 179                });
 180
 0181            amountOfFriendsWithDirectMessagesRequested++;
 0182            lastFriendWithDirectMessageIndex = i;
 183
 0184            CreateFakeFriend(fakeUserId);
 185        }
 186
 0187        mockedPayload.currentFriendsWithDirectMessages = mockedFriendWithDirectMessages.ToArray();
 0188        mockedPayload.totalFriendsWithDirectMessages = MAX_AMOUNT_OF_FAKE_FRIENDS;
 189
 0190        return JsonUtility.ToJson(mockedPayload);
 191    }
 192
 193    private async UniTask SimulateDelayedResponseFor_GetFriendsWithDirectMessages(string userNameOrId, int limit)
 194    {
 0195        await UniTask.Delay(Random.Range(1000, 3000));
 196
 0197        controller.AddFriendsWithDirectMessages(
 198            CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(userNameOrId, limit));
 0199    }
 200
 201    private string CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(string userNameOrId, int numberOfUsers)
 202    {
 0203        List<string> allFriendsWithDMsInServer = new List<string>();
 0204        for (int i = 0; i < MAX_AMOUNT_OF_FAKE_FRIENDS; i++)
 205        {
 0206            allFriendsWithDMsInServer.Add($"fakeuser{i + 1}");
 207        }
 208
 0209        List<string> resultsFound =
 0210            allFriendsWithDMsInServer.Where(x => x.ToLower().Contains(userNameOrId.ToLower())).ToList();
 211
 0212        AddFriendsWithDirectMessagesPayload mockedPayload = new AddFriendsWithDirectMessagesPayload();
 0213        List<FriendWithDirectMessages> mockedFriendWithDirectMessages = new List<FriendWithDirectMessages>();
 214
 0215        for (int i = 0; i < resultsFound.Count; i++)
 216        {
 0217            if (i >= numberOfUsers)
 218                break;
 219
 0220            mockedFriendWithDirectMessages.Add(
 221                new FriendWithDirectMessages
 222                {
 223                    userId = resultsFound[i],
 224                    lastMessageBody = $"This is the last message sent for {resultsFound[i]}",
 225                    lastMessageTimestamp = DateTimeOffset.UtcNow.AddMinutes(-(i + 1)).ToUnixTimeMilliseconds()
 226                });
 227
 0228            CreateFakeFriend(resultsFound[i]);
 229        }
 230
 0231        mockedPayload.currentFriendsWithDirectMessages = mockedFriendWithDirectMessages.ToArray();
 0232        mockedPayload.totalFriendsWithDirectMessages = MAX_AMOUNT_OF_FAKE_FRIENDS;
 233
 0234        return JsonUtility.ToJson(mockedPayload);
 235    }
 236
 237    private async UniTask GetFakeFriendsAsync(int limit, int skip, string name)
 238    {
 0239        var friendIds = new List<string>();
 240
 0241        await UniTask.Delay(Random.Range(20, 500));
 242
 0243        var characters = new[]
 244        {
 245            'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
 246        };
 247
 0248        var max = Mathf.Min(skip + Random.Range(1, limit), TOTAL_FRIENDS);
 249
 0250        for (var i = skip; i < max; i++)
 251        {
 0252            var userId = "";
 0253            for (var x = 0; x < 8; x++)
 0254                userId += characters[Random.Range(0, characters.Length)];
 255
 0256            friendIds.Add(userId);
 257
 0258            userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel
 259            {
 260                userId = userId,
 261                name = $"{name}-{i}-{userId}",
 262                ethAddress = userId,
 263                snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"}
 264            }));
 265        }
 266
 0267        await UniTask.Delay(Random.Range(20, 500));
 268
 0269        var payload = new AddFriendsPayload
 270        {
 271            friends = friendIds.ToArray(),
 272            totalFriends = TOTAL_FRIENDS
 273        };
 274
 0275        controller.AddFriends(JsonUtility.ToJson(payload));
 0276    }
 277
 278    private async UniTask GetFakeRequestsAsync(int limit)
 279    {
 0280        var characters = new[]
 281        {
 282            'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
 283        };
 284
 0285        await UniTask.Delay(Random.Range(20, 500));
 286
 0287        var fromUserIds = new List<string>();
 0288        var maxReceivedRequests = Mathf.Min(TOTAL_RECEIVED_REQUESTS, Random.Range(1, limit));
 289
 0290        for (var i = 0; i < maxReceivedRequests; i++)
 291        {
 0292            var userId = "";
 0293            for (var x = 0; x < 8; x++)
 0294                userId += characters[Random.Range(0, characters.Length)];
 295
 0296            fromUserIds.Add(userId);
 297
 0298            userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel
 299            {
 300                userId = userId,
 301                name = $"ffrrq-{i}-{userId}",
 302                ethAddress = userId,
 303                snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"}
 304            }));
 305        }
 306
 0307        var toUserIds = new List<string>();
 0308        var maxSentRequests = Mathf.Min(TOTAL_SENT_REQUESTS, Random.Range(1, limit));
 309
 0310        for (var i = 0; i < maxSentRequests; i++)
 311        {
 0312            var userId = "";
 0313            for (var x = 0; x < 8; x++)
 0314                userId += characters[Random.Range(0, characters.Length)];
 315
 0316            toUserIds.Add(userId);
 317
 0318            userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel
 319            {
 320                userId = userId,
 321                name = $"ffsrq-{i}-{userId}",
 322                ethAddress = userId,
 323                snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"}
 324            }));
 325        }
 326
 0327        await UniTask.Delay(Random.Range(20, 500));
 328
 0329        var payload = new AddFriendRequestsPayload
 330        {
 331            requestedFrom = fromUserIds.ToArray(),
 332            requestedTo = toUserIds.ToArray(),
 333            totalReceivedFriendRequests = TOTAL_RECEIVED_REQUESTS,
 334            totalSentFriendRequests = TOTAL_SENT_REQUESTS
 335        };
 336
 0337        controller.AddFriendRequests(JsonUtility.ToJson(payload));
 0338    }
 339
 340    private void CreateFakeFriend(string userId)
 341    {
 0342        if (controller.friends.ContainsKey(userId))
 0343            return;
 344
 0345        controller.friends.Add(userId, new UserStatus
 346        {
 347            userId = userId,
 348            position = new Vector2(Random.Range(-100, 101), Random.Range(-100, 101)),
 349            realm = new UserStatus.Realm
 350            {
 351                serverName = "dg",
 352                layer = ""
 353            },
 354            presence = Random.Range(0, 2) == 0 ? PresenceStatus.OFFLINE : PresenceStatus.ONLINE,
 355            friendshipStatus = FriendshipStatus.FRIEND,
 356            friendshipStartedTime = DateTime.UtcNow
 357        });
 0358    }
 359
 360    private async UniTask SimulateDelayedResponseFor_InitializeFriends()
 361    {
 0362        await UniTask.Delay(Random.Range(1000, 3000));
 363
 0364        controller.InitializeFriends(
 365            CreateFakeFriendsInitialization());
 0366    }
 367
 368    private string CreateFakeFriendsInitialization()
 369    {
 0370        var mockedPayload = new FriendshipInitializationMessage
 371        {
 372            totalReceivedRequests = TOTAL_RECEIVED_REQUESTS
 373        };
 374
 0375        return JsonUtility.ToJson(mockedPayload);
 376    }
 377
 378    private async UniTask UpdateFriendshipCount(int totalReceivedRequests, int totalSentRequests, int totalFriends)
 379    {
 0380        await UniTask.Delay(Random.Range(100, 2000));
 381
 0382        var requestsPayload = new UpdateTotalFriendRequestsPayload
 383        {
 384            totalReceivedRequests = totalReceivedRequests,
 385            totalSentRequests = totalSentRequests
 386        };
 0387        controller.UpdateTotalFriendRequests(JsonUtility.ToJson(requestsPayload));
 388
 0389        var friendsPayload = new UpdateTotalFriendsPayload
 390        {
 391            totalFriends = totalFriends
 392        };
 0393        controller.UpdateTotalFriends(JsonUtility.ToJson(friendsPayload));
 0394    }
 395}

Methods/Properties

LazyLoadingFriendsControllerMock(FriendsController, UserProfileController)
add_OnInitialized(System.Action)
remove_OnInitialized(System.Action)
add_OnUpdateFriendship(System.Action[String,FriendshipAction])
remove_OnUpdateFriendship(System.Action[String,FriendshipAction])
add_OnUpdateUserStatus(System.Action[String,UserStatus])
remove_OnUpdateUserStatus(System.Action[String,UserStatus])
add_OnFriendNotFound(System.Action[String])
remove_OnFriendNotFound(System.Action[String])
add_OnAddFriendsWithDirectMessages(System.Action[List`1])
remove_OnAddFriendsWithDirectMessages(System.Action[List`1])
add_OnTotalFriendRequestUpdated(System.Action[Int32,Int32])
remove_OnTotalFriendRequestUpdated(System.Action[Int32,Int32])
add_OnTotalFriendsUpdated(System.Action[Int32])
remove_OnTotalFriendsUpdated(System.Action[Int32])
AllocatedFriendCount()
IsInitialized()
ReceivedRequestCount()
TotalFriendCount()
TotalFriendRequestCount()
TotalReceivedFriendRequestCount()
TotalSentFriendRequestCount()
TotalFriendsWithDirectMessagesCount()
GetAllocatedFriends()
GetUserStatus(System.String)
ContainsStatus(System.String, FriendshipStatus)
RequestFriendship(System.String)
CancelRequest(System.String)
AcceptFriendship(System.String)
RejectFriendship(System.String)
IsFriend(System.String)
RemoveFriend(System.String)
GetFriends(System.Int32, System.Int32)
GetFriends(System.String, System.Int32)
GetFriendRequests(System.Int32, System.Int32, System.Int32, System.Int32)
GetFriendsWithDirectMessages(System.Int32, System.Int32)
GetFriendsWithDirectMessages(System.String, System.Int32)
SimulateDelayedResponseFor_GetFriendsWithDirectMessages()
CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(System.Int32)
SimulateDelayedResponseFor_GetFriendsWithDirectMessages()
CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(System.String, System.Int32)
GetFakeFriendsAsync()
GetFakeRequestsAsync()
CreateFakeFriend(System.String)
SimulateDelayedResponseFor_InitializeFriends()
CreateFakeFriendsInitialization()
UpdateFriendshipCount()