Ejemplo n.º 1
0
        public void TestContains()
        {
            // root -> ( child -> leaf, newLeaf )
            IGroupCategory root    = CategoryFactory.CreateGroup(rootName, null);
            IGroupCategory child   = CategoryFactory.CreateGroup(childName, root);
            ICategory      leaf    = CategoryFactory.CreateCategory(leafName, child);
            ICategory      newLeaf = CategoryFactory.CreateCategory(newLeafName, root);

            /* Contains by name */
            Assert.IsTrue(root.ContainsChild(newLeaf));
            Assert.IsTrue(root.ContainsChild(childName));
            Assert.IsFalse(root.ContainsChild(leafName));
            Assert.IsTrue(root.ContainsChild(leafName, deep: true));
            Assert.IsTrue(child.ContainsChild(leafName, deep: true));
            Assert.IsTrue(child.ContainsChild(leafName, deep: false));
            Assert.IsFalse(child.ContainsChild(newLeafName, deep: true));
            Assert.IsFalse(child.ContainsChild(newLeafName, deep: false));
            /* Contains by obj */
            Assert.IsTrue(root.ContainsChild(child));
            Assert.IsTrue(root.ContainsChild(newLeaf));
            Assert.IsTrue(root.ContainsChild(leaf, deep: true));
            Assert.IsFalse(root.ContainsChild(leaf));
            Assert.IsTrue(child.ContainsChild(leaf));
            Assert.IsFalse(child.ContainsChild(newLeaf));
            Assert.IsFalse(child.ContainsChild(newLeaf, deep: true));
            Assert.IsFalse(root.ContainsChild(root));
        }
Ejemplo n.º 2
0
        public void TestParentAddRemoveChildWithError()
        {
            IGroupCategory root  = CategoryFactory.CreateGroup(rootName, null);
            IGroupCategory child = CategoryFactory.CreateGroup(childName, root);

            // Aggiunto figlio nullo
            Assert.ThrowsException <ArgumentNullException>(() => root.AddChild(null));

            // child è figlio di root, e root vuole diventare figlio di child
            Assert.ThrowsException <Exception>(() => child.AddChild(root));

            // padre di me stesso
            Assert.ThrowsException <Exception>(() => root.AddChild(root));

            // padre di me stesso
            Assert.ThrowsException <Exception>(() => root.Parent = root);


            // root-> ( child, child ) !! Errore figlio uguale
            Assert.ThrowsException <Exception> (() => root.AddChild(child));
            Assert.AreEqual(0, child.Children.Length);
            Assert.AreEqual(1, root.Children.Length);
            Assert.AreSame(child, root.Children[0]);
            Assert.AreSame(root, child.Parent);
        }
Ejemplo n.º 3
0
        public void TestParentAddRemoveChild()
        {
            IGroupCategory root = CategoryFactory.CreateGroup(rootName, null);
            // root -> child
            IGroupCategory child = CategoryFactory.CreateGroup(childName, root);

            Assert.AreEqual(1, root.Children.Length);
            Assert.AreSame(child, root.Children[0]);
            Assert.AreSame(root, child.Parent);

            ICategory leaf = CategoryFactory.CreateCategory(leafName, null);

            // root -> child -> leaf
            leaf.Parent = child;
            Assert.AreEqual(1, child.Children.Length);
            Assert.AreEqual(1, root.Children.Length);
            Assert.AreSame(leaf, child.Children[0]);
            Assert.AreSame(child, leaf.Parent);

            // root -> child    |  leaf
            leaf.Parent = null;
            Assert.AreEqual(0, child.Children.Length);
            Assert.AreEqual(1, root.Children.Length);
            // root     | child     | leaf
            root.RemoveChild(child);
            Assert.AreEqual(0, child.Children.Length);
            Assert.AreEqual(0, root.Children.Length);
        }
        /// <summary>
        /// Gestisce l'azione dell'add button permettendo l'inserimento di una
        /// nuova categoria
        /// </summary>
        private void AddHandler(Object sender, EventArgs eventArgs)
        {
            ICategory selectedNode = _categoryTree.SelectedNode?.Tag as ICategory ?? null;

            if (selectedNode == null)
            {
                MessageBox.Show("Devi selezionare una categoria radice");
                return;
            }
            // Genero una finestra di dialogo per inserire il nome della categoria
            string catName = "";

            using (StringDialog sd = new StringDialog("Inserisci il nome della categoria"))
            {
                if (sd.ShowDialog() == DialogResult.OK)
                {
                    catName = sd.Response;
                }
                else
                {
                    return;
                }
            }
            // Se il nodo selezionato non è un contenitore lo elimino e lo faccio diventare
            // un contenitore
            if (!(selectedNode is IGroupCategory))
            {
                IGroupCategory parent = selectedNode.Parent;
                parent.RemoveChild(selectedNode);
                selectedNode = CategoryFactory.CreateGroup(selectedNode.Name, parent);
            }
            // Creo la categoria
            CategoryFactory.CreateCategory(catName, selectedNode as IGroupCategory);
        }
Ejemplo n.º 5
0
        public void TestCreateGroup()
        {
            IGroupCategory c = CategoryFactory.CreateGroup(rootName, null);

            Assert.IsNotNull(c);
            Assert.AreEqual(rootName, c.Name);
            Assert.IsTrue(c is IGroupCategory);
        }
Ejemplo n.º 6
0
        public void TestContainsWithError()
        {
            IGroupCategory root = CategoryFactory.CreateGroup(rootName, null);

            Assert.ThrowsException <ArgumentException>(() => root.ContainsChild(""));
            Assert.ThrowsException <ArgumentException>(() => root.ContainsChild("  "));
            Assert.ThrowsException <ArgumentException>(() => root.ContainsChild((string)null));
            Assert.ThrowsException <ArgumentNullException>(() => root.ContainsChild((ICategory)null));
        }
Ejemplo n.º 7
0
        public void TestParentAddChildWithErrorTree()
        {
            IGroupCategory root   = CategoryFactory.CreateGroup(rootName, null);
            IGroupCategory child  = CategoryFactory.CreateGroup(childName, root);
            IGroupCategory child2 = CategoryFactory.CreateGroup(leafName, null);

            child2.Parent = child;
            Assert.ThrowsException <Exception>(() => child2.AddChild(root));
            Assert.ThrowsException <Exception>(() => root.Parent = child2);
        }
Ejemplo n.º 8
0
        public void TestIsRoot()
        {
            IGroupCategory c = CategoryFactory.CreateGroup(rootName, null);

            Assert.IsNotNull(c);
            Assert.IsTrue(c.IsRoot);

            IGroupCategory c2 = CategoryFactory.CreateGroup(childName, c);

            Assert.IsNotNull(c2);
            Assert.IsFalse(c2.IsRoot);
        }
Ejemplo n.º 9
0
        public void TestGetCategoryByName()
        {
            CategoryCoordinator coor        = new CategoryCoordinator(new SimpleCoordinator());
            IGroupCategory      root        = coor.RootCategory;
            IGroupCategory      child       = CategoryFactory.CreateGroup("child", root);
            IGroupCategory      subChild    = CategoryFactory.CreateGroup("subChild", child);
            ICategory           subSubChild = CategoryFactory.CreateCategory("subSubChild", subChild);

            Assert.AreEqual(root, coor.getCategoryByPath("\\ROOT"));
            Assert.AreEqual(child, coor.getCategoryByPath("\\ROOT\\child"));
            Assert.AreEqual(subChild, coor.getCategoryByPath("\\ROOT\\child\\subChild"));
            Assert.AreEqual(subSubChild, coor.getCategoryByPath("\\ROOT\\child\\subChild\\subSubChild"));
        }
Ejemplo n.º 10
0
        public void TestIsInside()
        {
            IGroupCategory root    = CategoryFactory.CreateGroup(rootName, null);
            IGroupCategory child   = CategoryFactory.CreateGroup(childName, root);
            ICategory      leaf    = CategoryFactory.CreateCategory(leafName, child);
            ICategory      newLeaf = CategoryFactory.CreateCategory(newLeafName, root);

            Assert.IsTrue(leaf.IsInside(child));
            Assert.IsTrue(leaf.IsInside(root));
            Assert.IsTrue(child.IsInside(root));
            Assert.IsTrue(newLeaf.IsInside(root));
            Assert.IsFalse(newLeaf.IsInside(child));
            Assert.IsFalse(root.IsInside(root));
            Assert.IsFalse(child.IsInside(null));
        }
Ejemplo n.º 11
0
        public void TestEvents()
        {
            IGroupCategory root  = CategoryFactory.CreateGroup(rootName, null);
            IGroupCategory child = CategoryFactory.CreateGroup(childName, null);
            IGroupCategory leaf  = CategoryFactory.CreateGroup(leafName, null);

            int rootCounter = 0, childCounter = 0, leafCounter = 0;

            root.Changed  += (obj, e) => rootCounter++;
            child.Changed += (obj, e) => childCounter++;
            leaf.Changed  += (obj, e) => leafCounter++;

            Assert.AreEqual(0, rootCounter);
            Assert.AreEqual(0, childCounter);
            Assert.AreEqual(0, leafCounter);

            root.AddChild(child);

            Assert.AreEqual(1, rootCounter);
            Assert.AreEqual(1, childCounter);
            Assert.AreEqual(0, leafCounter);

            child.AddChild(leaf);

            Assert.AreEqual(2, rootCounter);
            Assert.AreEqual(2, childCounter);
            Assert.AreEqual(1, leafCounter);

            child.RemoveChild(leaf);
            Assert.AreEqual(3, rootCounter);
            Assert.AreEqual(3, childCounter);
            Assert.AreEqual(2, leafCounter);


            ICategory newLeaf = CategoryFactory.CreateCategory(newLeafName, null);

            int newLeafCounter = 0;

            newLeaf.Changed += (obj, e) => newLeafCounter++;

            newLeaf.Parent = leaf;

            Assert.AreEqual(3, rootCounter);
            Assert.AreEqual(3, childCounter);
            Assert.AreEqual(3, leafCounter);
            Assert.AreEqual(1, newLeafCounter);
        }
Ejemplo n.º 12
0
        protected override void Init()
        {
            base.Init();

            /* Cerco un file di configurazione delle categorie nel fileSystem,
             * se lo trovo carico le categorie contenute, altrimenti inizializzo
             * una nuova categoria
             */
            _root          = CategoryFactory.CreateRoot("ROOT");
            _root.Changed += OnCategoryChanged;

            /* Categorie HardCoded */
            IGroupCategory materiali = CategoryFactory.CreateGroup("materiali", _root);

            CategoryFactory.CreateCategory("testa", materiali);
            CategoryFactory.CreateCategory("staffa", materiali);
            IGroupCategory tessuto = CategoryFactory.CreateGroup("tessuto", materiali);

            CategoryFactory.CreateCategory("decorazioni", tessuto);

            IGroupCategory proprieta = CategoryFactory.CreateGroup("proprietà", _root);

            CategoryFactory.CreateCategory("impermeabilità", proprieta);
        }
Ejemplo n.º 13
0
 public void TestCreateGroupWithError()
 {
     Assert.ThrowsException <ArgumentException>(() => CategoryFactory.CreateGroup("", null));
     Assert.ThrowsException <ArgumentException>(() => CategoryFactory.CreateGroup("  ", null));
     Assert.ThrowsException <ArgumentException>(() => CategoryFactory.CreateGroup(null, null));
 }