Skip to content

Commit

Permalink
test cases written and bugs fixed to loadmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
cjddmut committed Oct 25, 2016
1 parent 724b5a7 commit 176ce67
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Assets/Test.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Test/Test.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Test/Test.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions Assets/Test/TestFocus.cs
@@ -0,0 +1,45 @@
using UnityEngine;
using System.Collections;
using UnityEngine.Assertions.Comparers;
using UnityLoader;

public class TestFocus : MonoBehaviour, IAssetLoader
{
public float loadDuration = 10;

private void Start()
{
LoadManager.CreateManager(1f/30);
LoadManager.instance.RegisterObject(gameObject);
LoadManager.instance.StartLoading(() => Debug.Log("Loading Complete!"));
}

private void OnApplicationFocus(bool focus)
{
Debug.Log("Focus Change: " + focus);
}

public IEnumerator LoadAssets()
{
float loadTime = Time.realtimeSinceStartup + loadDuration;

while (Time.realtimeSinceStartup < loadTime)
{
Debug.Log("Frame: " + Time.renderedFrameCount);

float delayTime = Time.realtimeSinceStartup + 1f / 30;

while (Time.realtimeSinceStartup < delayTime)
{

}

yield return null;
}
}

public void AssetsLoaded()
{

}
}
12 changes: 12 additions & 0 deletions Assets/Test/TestFocus.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Test/TestFocus.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/Test/TestFocus.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 146 additions & 0 deletions Assets/Test/TestFunctionality.cs
@@ -0,0 +1,146 @@
using UnityEngine;
using System.Collections;
using System.Reflection;
using UnityEngine.Assertions;
using UnityLoader;

namespace UnityLoaderTests
{
public class TestFunctionality : MonoBehaviour
{
public int targetFPS = 30;

private void Awake()
{
LoadManager.CreateManager(targetFPS);
}

//
// Test Correct Steps
//

private const int STEPS_USED = 10;

public class TestCorrectStepsClass : IAssetLoader
{
public IEnumerator LoadAssets()
{
for (int i = 0; i < STEPS_USED; i++)
{
LoadManager.instance.IncrementLoadStep();
yield return null;
}

yield return null;
}

public void AssetsLoaded()
{

}
}

public void TestCorrectSteps()
{
TestCorrectStepsClass test = new TestCorrectStepsClass();
LoadManager.instance.RegisterObject(test, STEPS_USED);
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
}

//
// Test Too Few Steps
//

public class TestTooFewStepsClass : IAssetLoader
{
public IEnumerator LoadAssets()
{
for (int i = 0; i < STEPS_USED; i++)
{
if (i % 2 == 0)
{
LoadManager.instance.IncrementLoadStep();
}

yield return null;
}

yield return null;
}

public void AssetsLoaded()
{

}
}

public void TestTooFewSteps()
{
TestTooFewStepsClass test = new TestTooFewStepsClass();
LoadManager.instance.RegisterObject(test, STEPS_USED);
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
}

//
// Test Too Few Steps
//

public class TestTooManyStepsClass : IAssetLoader
{
public IEnumerator LoadAssets()
{
for (int i = 0; i < STEPS_USED; i++)
{
LoadManager.instance.IncrementLoadStep();
yield return null;
}

LoadManager.instance.IncrementLoadStep();
yield return null;
}

public void AssetsLoaded()
{

}
}

public void TestTooManySteps()
{
TestTooManyStepsClass test = new TestTooManyStepsClass();
LoadManager.instance.RegisterObject(test, STEPS_USED);
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
}

//
// Test Force Yield
//

public class TestForceYieldClass : IAssetLoader
{
public IEnumerator LoadAssets()
{
float time = Time.realtimeSinceStartup + 10;

while (Time.realtimeSinceStartup < time)
{
Debug.Log("Frame: " + Time.renderedFrameCount);
yield return new ForceYield();
}
}

public void AssetsLoaded()
{

}
}

public void TestForceYield()
{
TestForceYieldClass test = new TestForceYieldClass();
LoadManager.instance.secondsAllowedPerFrame = 100;
LoadManager.instance.RegisterObject(test);
LoadManager.instance.StartLoading(() => Assert.IsTrue(LoadManager.instance.progress == 1f));
}
}
}
12 changes: 12 additions & 0 deletions Assets/Test/TestFunctionality.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 41 additions & 5 deletions Assets/UnityLoader/Scripts/LoadManager.cs
Expand Up @@ -12,15 +12,15 @@ public class LoadManager : MonoBehaviour
{
#region public
/// <summary>
/// The amount of time the loader will excute before yield to a new frame.
/// The amount of time the loader will execute before yield to a new frame.
/// </summary>
public float secondsAllowedPerFrame = 1f / 30;

/// <summary>
/// Amount of memory System.GC.GetTotalMemory() can get to before yield to a new frame.
/// </summary>
[Header("WebGL")]
public int memoryThresholdForYield = 128000000;
public int memoryThresholdForYield = DEFAULT_WEBGL_MEMORY_THRESHOLD;

/// <summary>
/// Verbose print out the timing of each loaded object.
Expand Down Expand Up @@ -50,15 +50,43 @@ public static LoadManager instance
if (_instance == null)
{
LogErrorFormat(
"No instance of {0} can be found! Add the {0} component to a GameObject.",
typeof(LoadManager));
"No instance of {0} can be found! Add the {0} component to a GameObject or invoke {0}.{1}().",
typeof(LoadManager),
((System.Action<float, int, bool>)CreateManager).Method.Name);
}
}

return _instance;
}
}

/// <summary>
/// Creates an instance of a LoadManager. This can be done instead of having one precreated in the scene.
/// </summary>
/// <param name="secondsAllowedPerFrame">The amount of time the loader will execute before yield to a new frame.</param>
/// <param name="webglMemoryThresholdForYield">Amount of memory System.GC.GetTotalMemory() can get to before yield to a new frame.</param>
/// <param name="verboseLogging">Verbose print out the timing of each loaded object.</param>
public static void CreateManager(
float secondsAllowedPerFrame,
int webglMemoryThresholdForYield = DEFAULT_WEBGL_MEMORY_THRESHOLD,
bool verboseLogging = false)
{
if (_instance != null)
{
LogWarningFormat(
"{0} already exists, called to {1} will be ignored!",
typeof(LoadManager),
System.Reflection.MethodBase.GetCurrentMethod().Name);

return;
}

LoadManager newLoad = (new GameObject(CREATED_NAME)).AddComponent<LoadManager>();
newLoad.secondsAllowedPerFrame = secondsAllowedPerFrame;
newLoad.memoryThresholdForYield = webglMemoryThresholdForYield;
newLoad.verboseLogging = verboseLogging;
}

/// <summary>
/// Register an object to be loaded later.
/// </summary>
Expand Down Expand Up @@ -220,7 +248,7 @@ public void IncrementLoadStep()
private int _currentStep;

private bool _loadingPaused;
private bool _hasFocus;
private bool _hasFocus = true;
private bool _isLoading;
private bool _originalRunInBackground;

Expand All @@ -229,8 +257,11 @@ public void IncrementLoadStep()
private static LoadManager _instance;
private static bool _webglInitialized;

private const int DEFAULT_WEBGL_MEMORY_THRESHOLD = 128000000;
private const float NO_FOCUS_SECONDS_PER_FRAME = 1;
private const int ADDITIONAL_STEPS = 2;
private const string LOG_HEADER = "[Unity Loader]";
private const string CREATED_NAME = "[Unity Loader]";
private const string METHOD_INVOKE_DURING_LOADING_WARNING = "{0}.{1} invoked in the middle of a load! This isn't allowed. Invoke after the load finishes.";
private const string METHOD_INVOKE_NOT_LOADING_WARNING = "{0}.{1} invoked when not loading! This will be ignored.";

Expand Down Expand Up @@ -432,6 +463,11 @@ private bool ShouldYield(float frameStartTime)
{
if (!Application.isEditor && !_hasFocus)
{
if (Time.realtimeSinceStartup - frameStartTime >= NO_FOCUS_SECONDS_PER_FRAME)
{
return true;
}

// We don't have focus, go nuts.
return false;
}
Expand Down
Binary file modified ProjectSettings/EditorBuildSettings.asset
Binary file not shown.

0 comments on commit 176ce67

Please sign in to comment.