< Summary

Class:DCL.WebRequestAsyncOperation
Assembly:WebRequestControllerInterfaces
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/Interfaces/WebRequestAsyncOperation.cs
Covered lines:31
Uncovered lines:2
Coverable lines:33
Total lines:109
Line coverage:93.9% (31 of 33)
Covered branches:0
Total branches:0
Covered methods:17
Total methods:19
Method coverage:89.4% (17 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebRequestAsyncOperation(...)0%110100%
SetAsCompleted(...)0%330100%
Abort()0%330100%
Dispose()0%220100%
GetResultData()0%2100%
SetNewWebRequest(...)0%2100%
SendWebRequest()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/Interfaces/WebRequestAsyncOperation.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4
 5namespace DCL
 6{
 7    /// <summary>
 8    /// Our custom request async operation to be used with the WebRequestController.
 9    /// </summary>
 10    public class WebRequestAsyncOperation : CustomYieldInstruction, IWebRequestAsyncOperation
 11    {
 12        /// <summary>
 13        /// Event that will be invoked when the request has been completed.
 14        /// </summary>
 15        public event Action<IWebRequestAsyncOperation> completed;
 16
 17        /// <summary>
 18        /// WebRequest that is being managed.
 19        /// </summary>
 185220        public UnityWebRequest webRequest { get; private set; }
 21
 40422        public UnityWebRequestAsyncOperation asyncOp { get; private set; }
 23
 24        /// <summary>
 25        /// Returns true after the request has finished communicating with the remote server.
 26        /// </summary>
 202527        public bool isDone { get; private set; }
 28
 29        /// <summary>
 30        /// Returns true if the request was successfully finished.
 31        /// </summary>
 51932        public bool isSucceeded { get; private set; }
 33
 34        /// <summary>
 35        /// Returns true if webRequest has been disposed (webRequest = null).
 36        /// </summary>
 22637        public bool isDisposed => webRequest == null;
 38
 39        /// <summary>
 40        /// Set to true for disposing the request just after it has been completed.
 41        /// </summary>
 58142        public bool disposeOnCompleted { get; set; }
 43
 145344        public override bool keepWaiting { get { return !isDone; } }
 45
 20246        public WebRequestAsyncOperation(UnityWebRequest webRequest)
 47        {
 20248            this.webRequest = webRequest;
 20249            isDone = false;
 20250            isSucceeded = false;
 20251            disposeOnCompleted = true;
 20252        }
 53
 54        /// <summary>
 55        /// Mark the request as completed and throw the corresponding event.
 56        /// </summary>
 57        /// <param name="success">True if the request was successfully ended.</param>
 58        public void SetAsCompleted(bool success)
 59        {
 17760            completed?.Invoke(this);
 17761            isDone = true;
 17762            isSucceeded = success;
 63
 17764            if (disposeOnCompleted)
 465                Dispose();
 17766        }
 67
 68        /// <summary>
 69        /// If in progress, halts the request as soon as possible.
 70        /// </summary>
 71        public void Abort()
 72        {
 20673            if (webRequest == null || isDone)
 15374                return;
 75
 5376            webRequest.Abort();
 5377        }
 78
 79        /// <summary>
 80        /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts 
 81        /// </summary>
 82        public void Dispose()
 83        {
 20684            Abort();
 85
 20686            if (webRequest == null)
 1387                return;
 88
 19389            webRequest.Dispose();
 19390            webRequest = null;
 19391        }
 92
 93        /// <summary>
 94        /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar
 95        /// </summary>
 96        public byte[] GetResultData()
 97        {
 098            return webRequest.downloadHandler.data;
 99        }
 100
 0101        internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; }
 102
 103        public UnityWebRequestAsyncOperation SendWebRequest()
 104        {
 202105            asyncOp = webRequest.SendWebRequest();
 202106            return asyncOp;
 107        }
 108    }
 109}