Пример #1
0
        public void TestUpdateAssetUrl()
        {
            var projectDir = new UFile(Path.Combine(Environment.CurrentDirectory, "testxk"));
            
            // Create a project with an asset reference a raw file
            var project = new Package { FullPath = projectDir };
            var assetItem = new AssetItem("test", new AssetObjectTest() { Reference =  new AssetReference<AssetObjectTest>(Guid.Empty, "good/location")});
            project.Assets.Add(assetItem);
            var goodAsset = new AssetObjectTest();
            project.Assets.Add(new AssetItem("good/location", goodAsset));

            // Add the project to the session to make sure analysis will run correctly
            var session = new PackageSession(project);

            // Create a session with this project
            var analysis = new PackageAnalysis(project,
                new PackageAnalysisParameters()
                    {
                        IsProcessingAssetReferences = true,
                        ConvertUPathTo = UPathType.Absolute,
                        IsProcessingUPaths = true
                    });
            var result = analysis.Run();
            Assert.IsFalse(result.HasErrors);
            Assert.AreEqual(1, result.Messages.Count);
            Assert.IsTrue(result.Messages[0].ToString().Contains("changed"));

            var asset = (AssetObjectTest)assetItem.Asset;
            Assert.AreEqual(goodAsset.Id, asset.Reference.Id);
            Assert.AreEqual("good/location", asset.Reference.Location);
        }
Пример #2
0
        public void TestInheritance()
        {
            // -----------------------------------------------------------
            // Tests inheritance
            // -----------------------------------------------------------
            // 4 assets
            // [asset1] is referencing [asset2]
            // [asset2]
            // [asset3] is inheriting  [asset1]
            // We create a [project1] with [asset1, asset2, asset3]
            // Check direct inherit dependencies for [asset3]: [asset1]
            // -----------------------------------------------------------

            var asset1 = new AssetObjectTest();
            var asset2 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            var assetItem2 = new AssetItem("asset-2", asset2);

            var asset3 = assetItem1.CreateChildAsset();
            var assetItem3 = new AssetItem("asset-3", asset3);

            asset1.Reference = new AssetReference<AssetObjectTest>(assetItem2.Id, assetItem2.Location);

            var project = new Package();
            project.Assets.Add(assetItem1);
            project.Assets.Add(assetItem2);
            project.Assets.Add(assetItem3);

            // Create a session with this project
            using (var session = new PackageSession(project))
            {
                var dependencyManager = session.DependencyManager;

                // Verify inheritance
                {
                    var assets = dependencyManager.FindAssetsInheritingFrom(asset1.Id);
                    Assert.AreEqual(1, assets.Count);
                    Assert.AreEqual(asset3.Id, assets[0].Id);
                }

                // Remove the inheritance
                var copyBase = asset3.Base;
                asset3.Base = null;
                assetItem3.IsDirty = true;
                {
                    var assets = dependencyManager.FindAssetsInheritingFrom(asset1.Id);
                    Assert.AreEqual(0, assets.Count);
                }

                // Add back the inheritance
                asset3.Base = copyBase;
                assetItem3.IsDirty = true;
                {
                    var assets = dependencyManager.FindAssetsInheritingFrom(asset1.Id);
                    Assert.AreEqual(1, assets.Count);
                    Assert.AreEqual(asset3.Id, assets[0].Id);
                }
            }
        }
Пример #3
0
        public void TestMoveAssetWithUFile()
        {
            var projectDir = new UFile(Path.Combine(Environment.CurrentDirectory, "testxk"));
            var rawAssetPath = new UFile("../image.png");
            var assetPath = new UFile("sub1/sub2/test");

            // Create a project with an asset reference a raw file
            var project = new Package { FullPath = projectDir };
            project.Profiles.Add(new PackageProfile("Shared", new AssetFolder(".")));
            var asset = new AssetObjectTest() { RawAsset = new UFile(rawAssetPath) };
            var assetItem = new AssetItem(assetPath, asset);
            project.Assets.Add(assetItem);

            // Run an asset reference analysis on this project
            var analysis = new PackageAnalysis(project,
                new PackageAnalysisParameters()
                    {
                        ConvertUPathTo = UPathType.Absolute,
                        IsProcessingUPaths = true
                    });
            var result = analysis.Run();
            Assert.IsFalse(result.HasErrors);
            Assert.AreEqual(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset);

            project.Assets.Remove(assetItem);
            assetItem = new AssetItem("sub1/test", asset);
            project.Assets.Add(assetItem);
            result = analysis.Run();
            Assert.IsFalse(result.HasErrors);
            Assert.AreEqual(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset);

            project.Assets.Remove(assetItem);
            assetItem = new AssetItem("test", asset);
            project.Assets.Add(assetItem);
            result = analysis.Run();
            Assert.IsFalse(result.HasErrors);
            Assert.AreEqual(UPath.Combine(project.RootDirectory, new UFile("sub1/image.png")), asset.RawAsset);

            analysis.Parameters.ConvertUPathTo = UPathType.Relative;
            result = analysis.Run();
            Assert.IsFalse(result.HasErrors);
            Assert.AreEqual(new UFile("sub1/image.png"), asset.RawAsset);
        }
        public void TestSimpleNewGuids()
        {
            var inputs = new List<AssetItem>();

            var asset = new AssetObjectTest();
            for (int i = 0; i < 10; i++)
            {
                var newAsset = new AssetObjectTest() { Id = asset.Id, Reference = new AssetReference(asset.Id, "bad") };
                inputs.Add(new AssetItem("0", newAsset));
            }

            // Force to use new ids
            var outputs = new List<AssetItem>();
            AssetCollision.Clean(null, inputs, outputs, new AssetResolver() { AlwaysCreateNewId = true }, true, false);

            // Make sure we are generating exactly the same number of elements
            Assert.AreEqual(inputs.Count, outputs.Count);

            // Make sure that asset has been cloned
            Assert.AreNotEqual(inputs[0], outputs[0]);

            // First Id should not change
            Assert.AreNotEqual(inputs[0].Id, outputs[0].Id);

            // Make sure that all ids are different
            var ids = new HashSet<AssetId>(outputs.Select(item => item.Id));
            Assert.AreEqual(inputs.Count, ids.Count);

            // Make sure that all locations are different
            var locations = new HashSet<UFile>(outputs.Select(item => item.Location));
            Assert.AreEqual(inputs.Count, locations.Count);

            // Reference location "bad"should be fixed to "0"
            foreach (var output in outputs)
            {
                var assetRef = ((AssetObjectTest)output.Asset).Reference;
                Assert.AreEqual("0", assetRef.Location);
                Assert.AreEqual(outputs[0].Id, assetRef.Id);
            }
        }
Пример #5
0
        public void TestTrackingPackageWithAssetsAndSave()
        {
            var dirPath = Path.Combine(Environment.CurrentDirectory, DirectoryTestBase + @"TestTracking");
            TryDeleteDirectory(dirPath);

            string testGenerated1 = Path.Combine(dirPath, "TestTracking.xkpkg");

            var project = new Package { FullPath = testGenerated1 };
            project.Profiles.Add(new PackageProfile("Shared", new AssetFolder(".")));
            var asset1 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            project.Assets.Add(assetItem1);

            using (var session = new PackageSession(project))
            {

                var dependencies = session.DependencyManager;
                dependencies.TrackingSleepTime = 10;
                dependencies.EnableTracking = true;

                // Save the session
                {
                    var result = session.Save();
                    Assert.IsFalse(result.HasErrors);

                    // Wait enough time for events
                    Thread.Sleep(100);

                    // Make sure that save is not generating events
                    var events = dependencies.FindAssetFileChangedEvents().ToList();
                    Assert.AreEqual(0, events.Count);

                    // Check tracked directories
                    var directoriesTracked = dependencies.DirectoryWatcher.GetTrackedDirectories();
                    Assert.AreEqual(1, directoriesTracked.Count);
                    Assert.AreEqual(dirPath.ToLowerInvariant(), directoriesTracked[0].ToLowerInvariant());

                    // Simulate multiple change an asset on the disk
                    File.SetLastWriteTime(assetItem1.FullPath, DateTime.Now);
                    Thread.Sleep(100);

                    // Check that we are capturing this event
                    events = dependencies.FindAssetFileChangedEvents().ToList();
                    Assert.AreEqual(1, events.Count);
                    Assert.AreEqual(assetItem1.Location, events[0].AssetLocation);
                    Assert.AreEqual(AssetFileChangedType.Updated, events[0].ChangeType);
                }

                // Save the project to another location
                {
                    var dirPath2 = Path.Combine(Environment.CurrentDirectory, DirectoryTestBase + @"TestTracking2");
                    TryDeleteDirectory(dirPath2);
                    string testGenerated2 = Path.Combine(dirPath2, "TestTracking.xkpkg");

                    project.FullPath = testGenerated2;
                    var result = session.Save();
                    Assert.IsFalse(result.HasErrors);

                    // Wait enough time for events
                    Thread.Sleep(200);

                    // Make sure that save is not generating events
                    var events = dependencies.FindAssetFileChangedEvents().ToList();
                    Assert.AreEqual(0, events.Count);

                    // Check tracked directories
                    var directoriesTracked = dependencies.DirectoryWatcher.GetTrackedDirectories();
                    Assert.AreEqual(1, directoriesTracked.Count);
                    Assert.AreEqual(dirPath2.ToLowerInvariant(), directoriesTracked[0].ToLowerInvariant());
                }

                // Copy file to simulate a new file on the disk (we will not try to load it as it has the same guid 
                {
                    var fullPath = assetItem1.FullPath;
                    var newPath = Path.Combine(Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath) + "2" + Path.GetExtension(fullPath));
                    File.Copy(fullPath, newPath);

                    // Wait enough time for events
                    Thread.Sleep(200);

                    // Make sure that save is not generating events
                    var events = dependencies.FindAssetFileChangedEvents().ToList();
                    Assert.AreEqual(1, events.Count);
                    Assert.IsTrue((events[0].ChangeType & AssetFileChangedType.Added) != 0);
                }
            }
        }
Пример #6
0
        public void TestCircularAndRecursiveDependencies()
        {
            // -----------------------------------------------------------
            // Tests circular references and dependencies query
            // -----------------------------------------------------------
            // 4 assets
            // [asset1] is referencing [asset2]
            // [asset2] is referencing [asset3]
            // [asset3] is referencing [asset4]
            // [asset4] is referencing [asset1]
            // We create a [project1] with [asset1, asset2, asset3, asset4]
            // Check direct input dependencies for [asset1]: [asset4]
            // Check all all input dependencies for [asset1]: [asset4, asset3, asset2, asset1]
            // Check direct output dependencies for [asset1]: [asset2]
            // Check all all output dependencies for [asset1]: [asset2, asset3, asset4, asset1]
            // -----------------------------------------------------------

            var asset1 = new AssetObjectTest();
            var asset2 = new AssetObjectTest();
            var asset3 = new AssetObjectTest();
            var asset4 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            var assetItem2 = new AssetItem("asset-2", asset2);
            var assetItem3 = new AssetItem("asset-3", asset3);
            var assetItem4 = new AssetItem("asset-4", asset4);
            asset1.Reference = new AssetReference<AssetObjectTest>(assetItem2.Id, assetItem2.Location);
            asset2.Reference = new AssetReference<AssetObjectTest>(assetItem3.Id, assetItem3.Location);
            asset3.Reference = new AssetReference<AssetObjectTest>(assetItem4.Id, assetItem4.Location);
            asset4.Reference = new AssetReference<AssetObjectTest>(assetItem1.Id, assetItem1.Location);

            var project = new Package();
            project.Assets.Add(assetItem1);
            project.Assets.Add(assetItem2);
            project.Assets.Add(assetItem3);
            project.Assets.Add(assetItem4);

            // Create a session with this project
            using (var session = new PackageSession(project))
            {
                var dependencyManager = session.DependencyManager;

                // Check internal states
                Assert.AreEqual(1, dependencyManager.Packages.Count); // only one project
                Assert.AreEqual(4, dependencyManager.Dependencies.Count); // asset1, asset2, asset3, asset4
                Assert.AreEqual(0, dependencyManager.AssetsWithMissingReferences.Count);
                Assert.AreEqual(0, dependencyManager.MissingReferencesToParent.Count);

                // Check direct input references
                var dependenciesFirst = dependencyManager.Find(assetItem1);
                Assert.AreEqual(1, dependenciesFirst.LinksIn.Count());
                var copyItem = dependenciesFirst.LinksIn.FirstOrDefault();
                Assert.NotNull(copyItem.Element);
                Assert.AreEqual(assetItem4.Id, copyItem.Item.Id);

                // Check direct output references
                Assert.AreEqual(1, dependenciesFirst.LinksOut.Count());
                copyItem = dependenciesFirst.LinksOut.FirstOrDefault();
                Assert.NotNull(copyItem.Element);
                Assert.AreEqual(assetItem2.Id, copyItem.Item.Id);

                // Calculate full recursive references
                var dependencies = dependencyManager.ComputeDependencies(assetItem1);

                // Check all input references (recursive)
                var asset1RecursiveInputs = dependencies.LinksIn.OrderBy(item => item.Element.Location).ToList();
                Assert.AreEqual(4, dependencies.LinksOut.Count());
                Assert.AreEqual(assetItem1.Id, asset1RecursiveInputs[0].Item.Id);
                Assert.AreEqual(assetItem2.Id, asset1RecursiveInputs[1].Item.Id);
                Assert.AreEqual(assetItem3.Id, asset1RecursiveInputs[2].Item.Id);
                Assert.AreEqual(assetItem4.Id, asset1RecursiveInputs[3].Item.Id);

                // Check all output references (recursive)
                var asset1RecursiveOutputs = dependencies.LinksOut.OrderBy(item => item.Element.Location).ToList();
                Assert.AreEqual(4, asset1RecursiveOutputs.Count);
                Assert.AreEqual(assetItem1.Id, asset1RecursiveInputs[0].Element.Id);
                Assert.AreEqual(assetItem2.Id, asset1RecursiveInputs[1].Element.Id);
                Assert.AreEqual(assetItem3.Id, asset1RecursiveInputs[2].Element.Id);
                Assert.AreEqual(assetItem4.Id, asset1RecursiveInputs[3].Element.Id);
            }
        }
Пример #7
0
        public void TestMissingReferences()
        {
            // -----------------------------------------------------------
            // Tests missing references
            // -----------------------------------------------------------
            // 3 assets
            // [asset1] is referencing [asset2]
            // [asset3] is referencing [asset1]
            // Add asset1. Check dependencies
            // Add asset2. Check dependencies
            // Add asset3. Check dependencies
            // Remove asset1. Check dependencies
            // Add asset1. Check dependencies.
            // Modify reference asset3 to asset1 with fake asset. Check dependencies
            // Revert reference asset3 to asset1. Check dependencies
            // -----------------------------------------------------------

            var asset1 = new AssetObjectTest();
            var asset2 = new AssetObjectTest();
            var asset3 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            var assetItem2 = new AssetItem("asset-2", asset2);
            var assetItem3 = new AssetItem("asset-3", asset3);

            asset1.Reference = new AssetReference<AssetObjectTest>(assetItem2.Id, assetItem2.Location);
            asset3.Reference = new AssetReference<AssetObjectTest>(assetItem1.Id, assetItem1.Location);

            var project = new Package();

            // Create a session with this project
            using (var session = new PackageSession(project))
            {
                var dependencyManager = session.DependencyManager;

                // Add asset1
                project.Assets.Add(assetItem1);
                {
                    var assets = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(1, assets.Count);
                    Assert.AreEqual(asset1.Id, assets[0]);

                    // Check dependencies on asset1
                    var dependencySetAsset1 = dependencyManager.FindDependencySet(asset1.Id);
                    Assert.NotNull(dependencySetAsset1);

                    Assert.AreEqual(0, dependencySetAsset1.LinksOut.Count());
                    Assert.IsTrue(dependencySetAsset1.HasMissingDependencies);
                    Assert.AreEqual(asset2.Id, dependencySetAsset1.BrokenLinksOut.First().Element.Id);
                }

                // Add asset2
                project.Assets.Add(assetItem2);
                {
                    var assets = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(0, assets.Count);

                    // Check dependencies on asset1
                    var dependencySetAsset1 = dependencyManager.FindDependencySet(asset1.Id);
                    Assert.NotNull(dependencySetAsset1);

                    Assert.AreEqual(1, dependencySetAsset1.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset1.LinksIn.Count());
                    Assert.AreEqual(asset2.Id, dependencySetAsset1.LinksOut.First().Element.Id);

                    // Check dependencies on asset2
                    var dependencySetAsset2 = dependencyManager.FindDependencySet(asset2.Id);
                    Assert.NotNull(dependencySetAsset2);

                    Assert.AreEqual(0, dependencySetAsset2.LinksOut.Count());
                    Assert.AreEqual(1, dependencySetAsset2.LinksIn.Count());
                    Assert.AreEqual(asset1.Id, dependencySetAsset2.LinksIn.First().Element.Id);
                }

                // Add asset3
                project.Assets.Add(assetItem3);
                Action checkAllOk = () =>
                {
                    var assets = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(0, assets.Count);

                    // Check dependencies on asset1
                    var dependencySetAsset1 = dependencyManager.FindDependencySet(asset1.Id);
                    Assert.NotNull(dependencySetAsset1);

                    Assert.AreEqual(1, dependencySetAsset1.LinksOut.Count());
                    Assert.AreEqual(1, dependencySetAsset1.LinksIn.Count());
                    Assert.AreEqual(asset2.Id, dependencySetAsset1.LinksOut.First().Element.Id);
                    Assert.AreEqual(asset3.Id, dependencySetAsset1.LinksIn.First().Element.Id);

                    // Check dependencies on asset2
                    var dependencySetAsset2 = dependencyManager.FindDependencySet(asset2.Id);
                    Assert.NotNull(dependencySetAsset2);

                    Assert.AreEqual(0, dependencySetAsset2.LinksOut.Count());
                    Assert.AreEqual(1, dependencySetAsset2.LinksIn.Count());
                    Assert.AreEqual(asset1.Id, dependencySetAsset2.LinksIn.First().Element.Id);

                    // Check dependencies on asset3
                    var dependencySetAsset3 = dependencyManager.FindDependencySet(asset3.Id);
                    Assert.NotNull(dependencySetAsset3);

                    Assert.AreEqual(1, dependencySetAsset3.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset3.LinksIn.Count());
                    Assert.AreEqual(asset1.Id, dependencySetAsset3.LinksOut.First().Element.Id);
                };
                checkAllOk();

                // Remove asset1
                project.Assets.Remove(assetItem1);
                {
                    var assets = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(1, assets.Count);
                    Assert.AreEqual(asset3.Id, assets[0]);

                    // Check dependencies on asset2
                    var dependencySetAsset2 = dependencyManager.FindDependencySet(asset2.Id);
                    Assert.NotNull(dependencySetAsset2);

                    Assert.AreEqual(0, dependencySetAsset2.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset2.LinksIn.Count());

                    // Check dependencies on asset3
                    var dependencySetAsset3 = dependencyManager.FindDependencySet(asset3.Id);
                    Assert.NotNull(dependencySetAsset3);

                    Assert.AreEqual(0, dependencySetAsset3.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset3.LinksIn.Count());
                    Assert.IsTrue(dependencySetAsset3.HasMissingDependencies);
                    Assert.AreEqual(asset1.Id, dependencySetAsset3.BrokenLinksOut.First().Element.Id);
                }

                // Add asset1
                project.Assets.Add(assetItem1);
                checkAllOk();

                // Modify reference asset3 to asset1 with fake asset
                var previousAsset3ToAsset1Reference = asset3.Reference;
                asset3.Reference = new AssetReference<AssetObjectTest>(Guid.NewGuid(), "fake");
                assetItem3.IsDirty = true;
                {
                    var assets = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(1, assets.Count);
                    Assert.AreEqual(asset3.Id, assets[0]);

                    // Check dependencies on asset1
                    var dependencySetAsset1 = dependencyManager.FindDependencySet(asset1.Id);
                    Assert.NotNull(dependencySetAsset1);

                    Assert.AreEqual(1, dependencySetAsset1.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset1.LinksIn.Count());
                    Assert.AreEqual(asset2.Id, dependencySetAsset1.LinksOut.First().Element.Id);

                    // Check dependencies on asset2
                    var dependencySetAsset2 = dependencyManager.FindDependencySet(asset2.Id);
                    Assert.NotNull(dependencySetAsset2);

                    Assert.AreEqual(0, dependencySetAsset2.LinksOut.Count());
                    Assert.AreEqual(1, dependencySetAsset2.LinksIn.Count());
                    Assert.AreEqual(asset1.Id, dependencySetAsset2.LinksIn.First().Element.Id);

                    // Check dependencies on asset3
                    var dependencySetAsset3 = dependencyManager.FindDependencySet(asset3.Id);
                    Assert.NotNull(dependencySetAsset3);

                    Assert.AreEqual(0, dependencySetAsset3.LinksOut.Count());
                    Assert.AreEqual(0, dependencySetAsset3.LinksIn.Count());
                    Assert.IsTrue(dependencySetAsset3.HasMissingDependencies);
                    Assert.AreEqual(asset3.Reference.Id, dependencySetAsset3.BrokenLinksOut.First().Element.Id);
                }

                // Revert back reference from asset3 to asset1
                asset3.Reference = previousAsset3ToAsset1Reference;
                assetItem3.IsDirty = true;
                checkAllOk();
            }
        }
Пример #8
0
        public void TestAssetChanged()
        {
            // -----------------------------------------------------------
            // Case where an asset is changing is referencing
            // -----------------------------------------------------------
            // 2 assets [asset1, asset2]
            // Change [asset1] referencing [asset2]
            // Notify the session to mark asset1 dirty
            // 
            // -----------------------------------------------------------

            var asset1 = new AssetObjectTest();
            var asset2 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            var assetItem2 = new AssetItem("asset-2", asset2);

            var project = new Package();
            project.Assets.Add(assetItem1);
            project.Assets.Add(assetItem2);

            // Create a session with this project
            using (var session = new PackageSession(project))
            {
                var dependencyManager = session.DependencyManager;

                // Check internal states
                Assert.AreEqual(1, dependencyManager.Packages.Count); // only one project
                Assert.AreEqual(2, dependencyManager.Dependencies.Count); // asset1, asset2
                Assert.AreEqual(0, dependencyManager.AssetsWithMissingReferences.Count);
                Assert.AreEqual(0, dependencyManager.MissingReferencesToParent.Count);

                asset1.Reference = new AssetReference<AssetObjectTest>(assetItem2.Id, assetItem2.Location);

                // Mark the asset dirty
                assetItem1.IsDirty = true;

                var dependencies1 = dependencyManager.FindDependencySet(asset1.Id);
                var copyItem = dependencies1.LinksOut.FirstOrDefault();
                Assert.NotNull(copyItem.Element);
                Assert.AreEqual(assetItem2.Id, copyItem.Element.Id);

                var dependencies2 = dependencyManager.FindDependencySet(asset2.Id);
                copyItem = dependencies2.LinksIn.FirstOrDefault();
                Assert.NotNull(copyItem.Element);
                Assert.AreEqual(assetItem1.Id, copyItem.Element.Id);
            }
        }
Пример #9
0
        public void TestFullSession()
        {
            // -----------------------------------------------------------
            // This is a more complex test mixing several different cases:
            // -----------------------------------------------------------
            // 4 assets
            // [asset1] is referencing [asset2]
            // [asset3] is referencing [asset4]
            // We create a [project1] with [asset1, asset2, asset3]
            // Start to evaluate the dependencies 
            // Check the dependencies for this project, [asset4] is missing
            // We create a [project2] and add it to the session
            // We add [asset4] to the [project2]
            // All depedencies should be fine
            // Remove [project2] from session
            // Check the dependencies for this project, [asset4] is missing
            // -----------------------------------------------------------

            var asset1 = new AssetObjectTest();
            var asset2 = new AssetObjectTest();
            var asset3 = new AssetObjectTest();
            var asset4 = new AssetObjectTest();
            var assetItem1 = new AssetItem("asset-1", asset1);
            var assetItem2 = new AssetItem("asset-2", asset2);
            var assetItem3 = new AssetItem("asset-3", asset3);
            var assetItem4 = new AssetItem("asset-4", asset4);
            asset1.Reference = new AssetReference<AssetObjectTest>(assetItem2.Id, assetItem2.Location);
            asset3.Reference = new AssetReference<AssetObjectTest>(assetItem4.Id, assetItem4.Location);

            var project = new Package();
            project.Assets.Add(assetItem1);
            project.Assets.Add(assetItem2);
            project.Assets.Add(assetItem3);

            // Create a session with this project
            using (var session = new PackageSession(project))
            {
                var dependencyManager = session.DependencyManager;

                // Check internal states
                Action checkState1 = () =>
                {
                    Assert.AreEqual(1, dependencyManager.Packages.Count); // only one project
                    Assert.AreEqual(3, dependencyManager.Dependencies.Count); // asset1, asset2, asset3
                    Assert.AreEqual(1, dependencyManager.AssetsWithMissingReferences.Count); // asset3 => asset4
                    Assert.AreEqual(1, dependencyManager.MissingReferencesToParent.Count); // asset4 => [asset3]

                    // Check missing references for asset3 => X asset4
                    var assetItemWithMissingReferences = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(1, assetItemWithMissingReferences.Count);
                    Assert.AreEqual(assetItem3.Id, assetItemWithMissingReferences[0]);

                    // Check missing reference
                    var missingReferences = dependencyManager.FindMissingReferences(assetItem3).ToList();
                    Assert.AreEqual(1, missingReferences.Count);
                    Assert.AreEqual(asset4.Id, missingReferences[0].Id);

                    // Check references for: asset1 => asset2
                    var referencesFromAsset1 = dependencyManager.ComputeDependencies(assetItem1);
                    Assert.AreEqual(1, referencesFromAsset1.LinksOut.Count());
                    var copyItem = referencesFromAsset1.LinksOut.FirstOrDefault();
                    Assert.NotNull(copyItem.Element);
                    Assert.AreEqual(assetItem2.Id, copyItem.Element.Id);
                };
                checkState1();

                {
                    // Add new project (must be tracked by the dependency manager)
                    var project2 = new Package();
                    session.Packages.Add(project2);

                    // Check internal states
                    Assert.AreEqual(2, dependencyManager.Packages.Count);

                    // Add missing asset4
                    project2.Assets.Add(assetItem4);
                    var assetItemWithMissingReferences = dependencyManager.FindAssetsWithMissingReferences().ToList();
                    Assert.AreEqual(0, assetItemWithMissingReferences.Count);

                    // Check internal states
                    Assert.AreEqual(4, dependencyManager.Dependencies.Count); // asset1, asset2, asset3, asse4
                    Assert.AreEqual(0, dependencyManager.AssetsWithMissingReferences.Count);
                    Assert.AreEqual(0, dependencyManager.MissingReferencesToParent.Count);

                    // Try to remove the project and double check
                    session.Packages.Remove(project2);

                    checkState1();
                }
            }
        }
Пример #10
0
        public void TestAssetItemCollection()
        {
            // Test serialization of asset items.

            var inputs = new List<AssetItem>();
            for (int i = 0; i < 10; i++)
            {
                var newAsset = new AssetObjectTest() { Name = "Test" + i };
                inputs.Add(new AssetItem("" + i, newAsset));
            }

            var asText = ToText(inputs);
            var outputs = FromText(asText);

            Assert.AreEqual(inputs.Select(item => item.Location), outputs.Select(item => item.Location));
            Assert.AreEqual(inputs.Select(item => item.Asset), outputs.Select(item => item.Asset));
        }
        public void TestWithPackage()
        {
            var inputs = new List<AssetItem>();

            var asset = new AssetObjectTest();

            var package = new Package();
            package.Assets.Add(new AssetItem("0", asset));

            for (int i = 0; i < 10; i++)
            {
                var newAsset = new AssetObjectTest() { Id = asset.Id, Reference = new AssetReference(asset.Id, "bad") };
                inputs.Add(new AssetItem("0", newAsset));
            }

            // Tries to use existing ids
            var outputs = new List<AssetItem>();
            AssetCollision.Clean(null, inputs, outputs, AssetResolver.FromPackage(package), true, false);

            // Make sure we are generating exactly the same number of elements
            Assert.AreEqual(inputs.Count, outputs.Count);

            // Make sure that asset has been cloned
            Assert.AreNotEqual(inputs[0], outputs[0]);

            // First Id should not change
            Assert.AreNotEqual(inputs[0].Id, outputs[0].Id);

            // Make sure that all ids are different
            var ids = new HashSet<AssetId>(outputs.Select(item => item.Id));
            Assert.AreEqual(inputs.Count, ids.Count);

            // Make sure that all locations are different
            var locations = new HashSet<UFile>(outputs.Select(item => item.Location));
            Assert.AreEqual(inputs.Count, locations.Count);

            // Reference location "bad"should be fixed to "0_1" pointing to the first element
            foreach (var output in outputs)
            {
                // Make sure of none of the locations are using "0"
                Assert.AreNotEqual("0", output.Location);

                var assetRef = ((AssetObjectTest)output.Asset).Reference;
                Assert.AreEqual("0 (2)", assetRef.Location);
                Assert.AreEqual(outputs[0].Id, assetRef.Id);
            }
        }