Exemple #1
0
        public void TestLinkMaps()
        {
            bool prefabLoadedEventReceived = false;

            EventBus.instance.Subscribe(new object(), IoEvents.PREFAB_LOADED, () => { prefabLoadedEventReceived = true; });
            GameObject prefab = ResourcesV2.LoadPrefab("ExamplePrefab1.prefab");

            Assert.IsTrue(prefabLoadedEventReceived);

            bool linkMapCreationEventReceived = false;

            EventBus.instance.Subscribe(new object(), LinkingEvents.LINK_MAP_CREATED, () => { linkMapCreationEventReceived = true; });
            var links = prefab.GetLinkMap();

            Assert.IsTrue(linkMapCreationEventReceived);

            Assert.IsNotNull(links.Get <Button>("Button 1"));
            Assert.IsNotNull(links.Get <GameObject>("Button 1"));
            AssertV2.Throws <Exception>(() => { links.Get <Button>("Button 2"); });

            links.Get <Text>("Text 1").text = "Some text";
            Assert.AreEqual("Some text", links.Get <Text>("Text 1").text);
            links.Get <Button>("Button 1").SetOnClickAction(delegate {
                Log.d("Button 1 clicked");
            });
            links.Get <Toggle>("Toggle 1").SetOnValueChangedAction((isNowChecked) => {
                Log.d("Toggle 1 is now " + (isNowChecked ? "checked" : "unchecked"));
                return(true);
            });
        }
Exemple #2
0
 public void TestLoadingPrefabs()
 {
     // Load the ExamplePrefab1.prefab located in Assets\Tests\TestLinking\Resources :
     Assert.IsNotNull(ResourcesV2.LoadPrefab("ExamplePrefab1"));
     // Loading a prefab that does not exist results in an error:
     AssertV2.Throws <Exception>(() => { ResourcesV2.LoadPrefab("ExamplePrefab2"); });
 }
Exemple #3
0
 public void TestAssertV2Throws()
 {
     AssertV2.ThrowExeptionIfAssertionFails(() => {
         try {
             AssertV2.Throws <Exception>(() => {
                 AssertV2.AreEqual(1, 1); // this will not fail..
             });                          // ..so the AssertV2.Throws should fail
             Log.e("This line should never be reached since AssertV2.Throws should fail!");
             throw new Exception("AssertV2.Throws did not fail correctly!");
         } catch (AssertV2.ThrowsException) { // Only catch it if its a ThrowsException
             // AssertV2.Throws failed correctly and threw an ThrowsException error
             Log.d("ThrowsException was expected and arrived correctly");
         }
     });
 }
Exemple #4
0
 public IEnumerator TestExecuteDelayed2()
 {
     {
         MonoBehaviour myMonoBehaviour = CreateSomeMonoBehaviour();
         myMonoBehaviour.enabled = false;
         AssertV2.Throws <Exception>(() => {
             myMonoBehaviour.ExecuteDelayed(() => { throw Log.e("Executing coroutine of disabled mono"); });
         });
     }
     {
         MonoBehaviour myMonoBehaviour = CreateSomeMonoBehaviour();
         myMonoBehaviour.gameObject.SetActive(false);
         AssertV2.Throws <Exception>(() => {
             myMonoBehaviour.ExecuteDelayed(() => { throw Log.e("Executing coroutine of inactive GO"); });
         });
     }
     yield return(null);
 }
Exemple #5
0
        public IEnumerator TestMainThread()
        {
            GameObject go   = null;
            var        task = TaskRunner.instance.RunInBackground(delegate {
                // Test that its not be possible to create a GO in a background thread:
                AssertV2.Throws <Exception>(() => { go = new GameObject(name: "A"); });
                // Test that on MainThread the gameobject can be created:
                MainThread.Invoke(() => { go = new GameObject(name: "B"); });
                Thread.Sleep(1000); // wait for main thread action to execute
                Assert.IsTrue(go != null);
                // Assert.AreEqual("B", go.name); // go.name not allowed in background thread
                Log.d("Background thread now done");
            }).task;

            Assert.IsNull(go);
            yield return(task.AsCoroutine());

            task.ThrowIfException();
            Assert.IsNotNull(go);
            Assert.AreEqual("B", go.name);
        }
Exemple #6
0
        public void TestGetOrAddComponentSingleton()
        {
            Injector injector = GetInjectorForTesting();

            injector.RemoveAllInjectorsFor <MyExampleMono1>();
            AssertV2.Throws <Exception>(() => {
                // It should not be possible to create a mono via default constructor:
                injector.GetOrAddSingleton <MyExampleMono1>(this);
            });
            {
                var singletonsName = "SingletonsMaster1";
                var x1             = injector.GetOrAddComponentSingleton <MyExampleMono1>(this, singletonsName);
                Assert.NotNull(x1);
                var go = x1.gameObject;
                Assert.NotNull(go);
                Assert.AreEqual(singletonsName, go.GetParent().name);

                var x2 = injector.Get <MyExampleMono1>(this);
                Assert.AreEqual(x1, x2);
                Assert.AreEqual(x1.gameObject, x2.gameObject);
            }
        }
Exemple #7
0
        public void TestAssertV2Methods()
        {
            AssertV2.ThrowExeptionIfAssertionFails(() => {
                AssertV2.IsTrue(AssertV2.throwExeptionIfAssertionFails, "AssertV2.throwExeptionIfAssertionFails");

                AssertV2.IsTrue(1 + 1 == 2, "This assertion must not fail");
                AssertV2.Throws <Exception>(() => {
                    AssertV2.IsTrue(1 + 1 == 4, "This assertion has to fail");
                    Log.e("This line should never be printed since throwExeptionIfAssertionFails is true");
                });

                var s1 = "a";
                AssertV2.AreEqual(s1, s1);

                AssertV2.IsTrue(AssertV2.throwExeptionIfAssertionFails, "AssertV2.throwExeptionIfAssertionFails");
                AssertV2.Throws <Exception>(() => { AssertV2.AreNotEqual(s1, s1, "s1"); });

                string myVarX = null;
                AssertV2.IsNull(null, "myVarX");
                myVarX = "Now myVarX is not null anymore";
                AssertV2.Throws <Exception>(() => { AssertV2.IsNull(myVarX, "myVarX"); });

                AssertV2.AreEqual(1, 1);
                AssertV2.Throws <Exception>(() => { AssertV2.AreEqual(1, 2); });

                AssertV2.AreNotEqual(1, 2);
                AssertV2.Throws <Exception>(() => { AssertV2.AreNotEqual(1, 1); });

                var stopWatch = AssertV2.TrackTiming();
                Thread.Sleep(10);
                stopWatch.Stop();
                AssertV2.Throws <Exception>(() => { stopWatch.AssertUnderXms(1); }); // This should always fail

                stopWatch.AssertUnderXms(50);
                AssertV2.IsTrue(stopWatch.IsUnderXms(50), "More time was needed than expected!");
            });
        }
        public IEnumerator TestMainThread()
        {
            var queue = BackgroundTaskQueue.NewBackgroundTaskQueue(1);

            GameObject go   = null;
            var        task = queue.Run(async(cancel) => {
                cancel.ThrowIfCancellationRequested();
                // Test that its not be possible to create a GO in a background thread:
                AssertV2.Throws <Exception>(() => { go = new GameObject(name: "A"); });
                // Test that on MainThread the gameobject can be created:
                MainThread.Invoke(() => { go = new GameObject(name: "B"); });
                await TaskV2.Delay(1000); // wait for main thread action to execute
                Assert.IsTrue(go != null);
                // Assert.AreEqual("B", go.name); // go.name not allowed in background thread
                Log.d("Background thread now done");
            });

            Assert.IsNull(go);
            yield return(task.AsCoroutine());

            task.ThrowIfException();
            Assert.IsNotNull(go);
            Assert.AreEqual("B", go.name);
        }
Exemple #9
0
        public async Task TestDirectoryMethods()
        {
            AssertV2.throwExeptionIfAssertionFails = true;
            var rootDir = CreateDirectoryForTesting("DirMethodsTest_" + DateTimeV2.UtcNow.ToUnixTimestampUtc());

            // Test FullPath:
            var dir1     = rootDir.GetChildDir("TestDir 1");
            var alsoDir1 = dir1.Parent.GetChildDir("TestDir 1");

            Assert.Equal(dir1.FullName, alsoDir1.FullName);

            // Test deleting and creating Dir 1:
            dir1.DeleteV2(); // Make sure dir does not yet exist from previous tests
            Assert.False(dir1.IsNotNullAndExists(), "dir1.IsNotNullAndExists");
            Assert.False(dir1.Exists, "dir1.Exists");
            Assert.True(dir1.CreateV2().Exists, "dir1.CreateV2().Exists");
            Assert.True(dir1.IsNotNullAndExists(), "dir1.IsNotNullAndExists");
            dir1.CreateV2(); // Should do nothing and not throw an exception

            var nameOfChildDir1            = "ChildDir 1";
            var nameOfTestFile1InChildDir1 = "Test file 1";
            // Test creating sub dirs and files:
            var subDir = dir1.CreateSubdirectory(nameOfChildDir1);

            SaveAndLoadTextToFile(subDir.GetChild(nameOfTestFile1InChildDir1), textToSave: "Test 123");
            SaveAndLoadTextToFile(subDir.GetChild("Test file 2.txt"), textToSave: "Test 123");

            { // Test moving folders:
                var oldPath = dir1.Parent.GetChildDir(dir1.Name);
                var dir2    = rootDir.GetChildDir("TestDir 2");
                dir2.DeleteV2(); // Make sure dir does not yet exist from previous tests
                await TaskV2.Delay(20);

                Assert.False(dir2.Exists, "before MOVE: dir2.ExistsV2");

                var moveToWorked = dir1.MoveToV2(dir2, out dir1);
                await TaskV2.Delay(100);

                // After move first test that the new dir is now there:
                Assert.True(dir2.Exists, "after MOVE: dir2.ExistsV2");
                Assert.True(dir2.IsNotNullAndExists(), "dafter MOVE: ir2.IsNotNullAndExists");
                Assert.Equal(dir2.FullName, dir1.FullName);
                Assert.True(dir1.Exists, "after MOVE: dir1.ExistsV2");

                Assert.True(moveToWorked, "dir1.MoveToV2(dir2) failed");
                Assert.False(oldPath.Exists, "oldDir2.Exists");

                var subDirs = dir2.GetDirectories();
                Assert.NotEmpty(subDirs);
                var movedChildDir = dir2.GetChildDir(nameOfChildDir1);
                Assert.True(movedChildDir.Exists,
                            "!movedChildDir.ExistsV2, all childDirs=" + subDirs.ToStringV2(sd => "" + sd));
                Assert.NotEmpty(movedChildDir.EnumerateFiles());
                var movedTestFile1 = movedChildDir.GetChild(nameOfTestFile1InChildDir1);
                Assert.True(movedTestFile1.Exists, "movedTestFile1.ExistsV2");
            }
            { // Test that moving to existing folders fails:
                var dir3 = rootDir.GetChildDir("TestDir 3").CreateV2();
                Assert.Equal("TestDir 3", dir3.Name);
                dir3 = rootDir.CreateSubdirectory(dir3.Name);
                AssertV2.Throws <Exception>(() => {
                    dir1.MoveToV2(dir3, out dir1); // This should fail since dir3 already exists
                });
                Assert.NotEqual(dir3.FullName, dir1.FullName);
                dir3.DeleteV2(); // Cleanup after test
            }
            {                    // Test copying folders:
                var oldPath = dir1.Parent.GetChildDir(dir1.Name);
                var dir4    = rootDir.GetChildDir("TestDir 4");
                dir4.DeleteV2(); // Make sure dir does not yet exist from previous tests
                Assert.False(dir1.IsEmtpy());
                Assert.True(dir1.CopyTo(dir4), "dir1.CopyTo(dir4) failed");
                await TaskV2.Delay(20);

                Assert.True(dir4.IsNotNullAndExists(), "dir4.IsNotNullAndExists");
                Assert.NotEqual(dir4.FullName, dir1.FullName);
                Assert.True(oldPath.Exists, "oldDir4.Exists");

                // Check that the files were really copied from dir1 to dir4:
                var dir4ChildDir1            = dir4.GetChildDir(nameOfChildDir1);
                var testFile1InDir1ChildDir4 = dir4ChildDir1.GetChild(nameOfTestFile1InChildDir1);
                Assert.True(testFile1InDir1ChildDir4.IsNotNullAndExists(), "tf1d4 not found");
                var newTextInTestFile1 = "Some new text for txt file in dir4";
                testFile1InDir1ChildDir4.SaveAsText(newTextInTestFile1);

                // A second copyTo now that dir4 exists should throw an exception:
                Assert.Throws <ArgumentException>(() => { dir1.CopyTo(dir4, replaceExisting: false); });
                // Replacing only works when replaceExisting is set to true:
                Assert.True(dir1.CopyTo(dir4, replaceExisting: true), "dir1.CopyTo(dir4, replaceExisting true) failed");
                // The path to the testFile1 should still exist after copy:
                Assert.True(testFile1InDir1ChildDir4.IsNotNullAndExists(), "old tf1d4 not found");
                // The text should not be newTextInTestFile1 anymore after its replaced by the original again:
                Assert.NotEqual(newTextInTestFile1, testFile1InDir1ChildDir4.LoadAs <string>());

                Assert.True(dir4.DeleteV2(), "dir4.Delete"); // Delete dir4 again
                Assert.False(dir4.DeleteV2(), "dir4.Del");   // dir4 now does not exist anymore so false is returned
            }
            {                                                // Test renaming folders:
                var newName = "TestDir 5";
                rootDir.GetChildDir(newName).DeleteV2();
                var oldPath = dir1.Parent.GetChildDir(dir1.Name);
                Assert.True(dir1.Exists, "dir1.Exists false BEFORE rename");
                Assert.True(dir1.Rename(newName, out dir1), "dir1.Rename(newName) failed");
                Assert.Equal(newName, dir1.Name);
                Assert.True(dir1.Exists, "dir1.Exists false AFTER rename, dir1=" + dir1.FullName);
                if (oldPath.Exists)
                {
                    var e = Log.e("WebGL renamed via copy but could not delete the original dir=" + oldPath);
                    if (!EnvironmentV2.isWebGL)
                    {
                        throw e;
                    }
                }
            }
            try { rootDir.DeleteV2(); } catch (Exception e) { Log.e("COuld not cleanup the rootDir as the final step", e); }
        }
Exemple #10
0
        public void TestDirectoryMethods()
        {
            var rootDir = CreateDirectoryForTesting("TestDirectoryMethods");

            // Test FullPath:
            var dir1     = rootDir.GetChildDir("TestDir 1");
            var alsoDir1 = dir1.Parent.GetChildDir("TestDir 1");

            Assert.Equal(dir1.FullPath(), alsoDir1.FullPath());

            // Test deleting and creating Dir 1:
            dir1.DeleteV2();
            Assert.False(dir1.IsNotNullAndExists());
            Assert.False(dir1.Exists);
            Assert.True(dir1.CreateV2().Exists);
            Assert.True(dir1.IsNotNullAndExists());
            dir1.Create(); // Should do nothing and not throw an exception

            // Test creating sub dirs and files:
            var subDir = dir1.CreateSubdirectory("ChildDir 1");

            SaveAndLoadTextToFile(subDir.GetChild("Test file 1"), textToSave: "Test 123");
            SaveAndLoadTextToFile(subDir.GetChild("Test file 2.txt"), textToSave: "Test 123");

            { // Test moving folders:
                var oldPath = dir1.FullPath();
                var dir2    = rootDir.GetChildDir("TestDir 2");
                dir2.DeleteV2();
                dir1.MoveToV2(dir2);
                Assert.True(dir2.IsNotNullAndExists());
                Assert.Equal(dir2.FullPath(), dir1.FullPath());
                Assert.False(new DirectoryInfo(oldPath).Exists);
            }
            { // Test that moving to existing folders fails:
                var dir3 = rootDir.GetChildDir("TestDir 3").CreateV2();
                Assert.Equal("TestDir 3", dir3.Name);
                dir3 = rootDir.CreateSubdirectory(dir3.Name);
                AssertV2.Throws <Exception>(() => {
                    dir1.MoveToV2(dir3); // This should fail since dir3 already exists
                });
                Assert.NotEqual(dir3.FullPath(), dir1.FullPath());
                dir3.Delete(); // Cleanup after test
            }
            {                  // Test copying folders:
                var oldPath = dir1.FullPath();
                var dir4    = rootDir.GetChildDir("TestDir 4");
                dir4.DeleteV2(); // Make sure dir does not yet exist from previous tests
                dir1.CopyTo(dir4);
                Assert.True(dir4.IsNotNullAndExists(), "dir=" + dir4.FullPath());
                Assert.NotEqual(dir4.FullPath(), dir1.FullPath());
                Assert.True(new DirectoryInfo(oldPath).Exists);
                dir4.DeleteV2(); // Cleanup after test
            }
            {                    // Test renaming folders:
                var newName = "TestDir 5";
                rootDir.GetChildDir(newName).DeleteV2();
                var oldPath = dir1.FullPath();
                dir1.Rename(newName);
                Assert.False(new DirectoryInfo(oldPath).Exists);
                Assert.True(dir1.Exists);
                Assert.Equal(newName, dir1.NameV2());

                // DirectoryInfo.Name does not correctly update after renaming,
                // Thats why DirectoryInfo.name must return an incorrect value:
                Assert.NotEqual(newName, dir1.Name);
                // TODO is DirectoryInfo.Name broken for all .net versions?
            }

            rootDir.DeleteV2(); // Cleanup after test
        }