public ZenjectSceneLoader( [InjectOptional] SceneContext sceneRoot, ProjectKernel projectKernel) { _projectKernel = projectKernel; _sceneContainer = sceneRoot == null ? null : sceneRoot.Container; }
public ZenjectSceneLoader( SceneContext sceneRoot, ProjectKernel projectKernel) { _projectKernel = projectKernel; _sceneContainer = sceneRoot.Container; }
public SceneContextRegistryAdderAndRemover( SceneContext sceneContext, SceneContextRegistry registry) { _registry = registry; _sceneContext = sceneContext; }
public void Remove(SceneContext context) { bool removed = _map.Remove(context.gameObject.scene); if (!removed) { ModestTree.Log.Warn("Failed to remove SceneContext from SceneContextRegistry"); } }
public virtual void SetUp() { _hasStarted = false; _isValidating = CurrentTestHasAttribute <ValidateOnlyAttribute>(); ProjectContext.ValidateOnNextRun = _isValidating; _sceneContext = new GameObject("SceneContext").AddComponent <SceneContext>(); _sceneContext.ParentNewObjectsUnderRoot = true; // This creates the container but does not resolve the roots yet _sceneContext.Install(); }
public IEnumerator LoadScenes(params string[] sceneNames) { Assert.That(!_hasLoadedScene, "Attempted to load scene twice!"); _hasLoadedScene = true; // Clean up any leftovers from previous test ZenjectTestUtil.DestroyEverythingExceptTestRunner(false); Assert.That(SceneContainers.IsEmpty()); for (int i = 0; i < sceneNames.Length; i++) { var sceneName = sceneNames[i]; Assert.That(Application.CanStreamedLevelBeLoaded(sceneName), "Cannot load scene '{0}' for test '{1}'. The scenes used by SceneTestFixture derived classes must be added to the build settings for the test to work", sceneName, GetType()); Log.Info("Loading scene '{0}' for testing", sceneName); var loader = SceneManager.LoadSceneAsync(sceneName, i == 0 ? LoadSceneMode.Single : LoadSceneMode.Additive); while (!loader.isDone) { yield return(null); } SceneContext sceneContext = null; if (ProjectContext.HasInstance) // ProjectContext might be null if scene does not have a scene context { var scene = SceneManager.GetSceneByName(sceneName); sceneContext = ProjectContext.Instance.Container.Resolve <SceneContextRegistry>() .TryGetSceneContextForScene(scene); } _sceneContainers.Add(sceneContext == null ? null : sceneContext.Container); } _sceneContainer = _sceneContainers.Where(x => x != null).LastOrDefault(); if (_sceneContainer != null) { _sceneContainer.Inject(this); } }
public DiContainer TryGetContainerForScene(Scene scene) { if (scene == ProjectContext.Instance.gameObject.scene) { return(ProjectContext.Instance.Container); } SceneContext sceneContext = TryGetSceneContextForScene(scene); if (sceneContext != null) { return(sceneContext.Container); } return(null); }
public virtual void SetUp() { // Don't clear the scene to allow tests to initialize the scene how they // want to set it up manually in their own [Setup] method (eg. TestFromComponentInChildren) // Also, I think unity might already clear the scene anyway? //ClearScene(); _hasStarted = false; _isValidating = CurrentTestHasAttribute <ValidateOnlyAttribute>(); ProjectContext.ValidateOnNextRun = _isValidating; _sceneContext = new GameObject("SceneContext").AddComponent <SceneContext>(); _sceneContext.ParentNewObjectsUnderRoot = true; // This creates the container but does not resolve the roots yet _sceneContext.Install(); }
public IEnumerator LoadScenes(params string[] scenePaths) { Assert.That(!_hasLoadedScene, "Attempted to load scene twice!"); _hasLoadedScene = true; // Clean up any leftovers from previous test ZenjectTestUtil.DestroyEverythingExceptTestRunner(false); Assert.That(SceneContainers.IsEmpty()); for (var i = 0; i < scenePaths.Length; i++) { var scenePath = scenePaths[i]; Log.Info("Loading scene '{0}' for testing", scenePath); var loader = EditorSceneManager.LoadSceneAsyncInPlayMode(scenePath, new LoadSceneParameters( i == 0 ? LoadSceneMode.Single : LoadSceneMode.Additive )); while (!loader.isDone) { yield return(null); } SceneContext sceneContext = null; // ProjectContext might be null if scene does not have a scene context if (ProjectContext.HasInstance) { var scene = SceneManager.GetSceneByPath(scenePath); sceneContext = ProjectContext.Instance.Container.Resolve <SceneContextRegistry>() .TryGetSceneContextForScene(scene); } _sceneContainers.Add(sceneContext == null ? null : sceneContext.Container); } SceneContainer = _sceneContainers.LastOrDefault(x => x != null); SceneContainer?.Inject(this); }
public static Context GetContext(Scene scene, Type type, bool create) { Assert.IsNotNull(scene); Assert.DerivesFromOrEqual <Context>(type); if (type.DerivesFromOrEqual <ProjectContext>()) { return(Zenject.ProjectContext.Instance); } var contextType = (type == typeof(Context)) ? typeof(SceneContext) : type; var contextComponent = GameObject .FindObjectsOfType(contextType) .Cast <Component>() .FirstOrDefault(comp => comp.gameObject.scene == scene); if (create && contextComponent == null) { if (contextType == typeof(SceneContext)) { contextComponent = SceneContext.Create(); } else if (contextType.DerivesFromOrEqual <SceneContext>()) { _sceneContextStaticAutoRunBindingField.SetValue(null, false); contextComponent = new GameObject(typeof(SceneContext).Name).AddComponent(contextType); Assert.That((bool)_sceneContextStaticAutoRunBindingField.GetValue(null)); // Should be reset } else { var contextParentContainer = GetContainer(scene, typeof(SceneContext), true); var contextGameObject = new GameObject(contextType.Name); contextComponent = contextParentContainer.InstantiateComponent(contextType, contextGameObject, new object[0]); } if (contextComponent.gameObject.scene != scene) { SceneManager.MoveGameObjectToScene(contextComponent.gameObject, scene); } } return(contextComponent as Context); }
protected void PreInstall() { Assert.That(!_hasStartedInstall, "Called PreInstall twice in test '{0}'!", TestContext.CurrentContext.Test.Name); _hasStartedInstall = true; Assert.That(!ProjectContext.HasInstance); var shouldValidate = CurrentTestHasAttribute <ValidateOnlyAttribute>(); ProjectContext.ValidateOnNextRun = shouldValidate; Assert.IsNull(_sceneContext); _sceneContext = SceneContext.Create(); _sceneContext.Install(); Assert.That(ProjectContext.HasInstance); Assert.IsEqual(shouldValidate, ProjectContext.Instance.Container.IsValidating); Assert.IsEqual(shouldValidate, _sceneContext.Container.IsValidating); }
void DestroyEverythingInternal(bool immediate) { if (_sceneContext != null) { // We need to use DestroyImmediate so that all the IDisposable's etc get processed immediately before // next test runs if (immediate) { GameObject.DestroyImmediate(_sceneContext.gameObject); } else { GameObject.Destroy(_sceneContext.gameObject); } _sceneContext = null; } ZenjectTestUtil.DestroyEverythingExceptTestRunner(immediate); StaticContext.Clear(); }
protected void DestroyAll() { Assert.That(!_hasDestroyedAll, "Called DestroyAll twice in test '{0}'!", TestContext.CurrentContext.Test.Name); _hasDestroyedAll = true; Assert.That(_hasStartedInstall, "Called DestroyAll but did not call PreInstall (or SkipInstall) in test '{0}'!", TestContext.CurrentContext.Test.Name); // We need to use DestroyImmediate so that all the IDisposable's etc get processed immediately before // next test runs GameObject.DestroyImmediate(_sceneContext.gameObject); _sceneContext = null; var allRootObjects = new List <GameObject>(); // We want to clear all objects across all scenes to ensure the next test is not affected // at all by previous tests // TODO: How does this handle cases where the test loads other scenes? for (int i = 0; i < SceneManager.sceneCount; i++) { allRootObjects.AddRange( SceneManager.GetSceneAt(i).GetRootGameObjects()); } // We also want to destroy any objects marked DontDestroyOnLoad, especially ProjectContext allRootObjects.AddRange(ProjectContext.Instance.gameObject.scene.GetRootGameObjects()); foreach (var rootObj in allRootObjects) { // Make sure not to destroy the unity test runner objects that it adds if (!_unityTestRunnerObjects.Contains(rootObj)) { // Use DestroyImmediate for other objects too just to ensure we are fully // cleaned up before the next test starts GameObject.DestroyImmediate(rootObj); } } }
public void Add(SceneContext context) { Assert.That(!_map.ContainsKey(context.gameObject.scene)); _map.Add(context.gameObject.scene, context); }
public void Remove(SceneContext context) { _map.RemoveWithConfirm(context.gameObject.scene); }
public ZenjectSceneLoader(SceneContext sceneRoot) { _sceneContainer = sceneRoot.Container; }