public void ReturnKeyPressed_CommandParameterHasNoAnnoObject_ShouldNotSetSelectedItem()
        {
            // Arrange
            var viewModel = new PresetsTreeViewModel(_mockedTreeLocalization);

            var commandParameter = new GenericTreeItem(null);

            // Act
            viewModel.ReturnKeyPressedCommand.Execute(commandParameter);

            // Assert
            Assert.Null(viewModel.SelectedItem);
        }
        public void ReturnKeyPressed_CommandParameterHasAnnoObject_ShouldSetSelectedItem()
        {
            // Arrange
            var viewModel       = new PresetsTreeViewModel(_mockedTreeLocalization);
            var annoObjectToSet = new AnnoObject();

            var commandParameter = new GenericTreeItem(null);

            commandParameter.AnnoObject = annoObjectToSet;

            // Act
            viewModel.ReturnKeyPressedCommand.Execute(commandParameter);

            // Assert
            Assert.Equal(commandParameter, viewModel.SelectedItem);
        }
예제 #3
0
        private bool Filter(GenericTreeItem curItem)
        {
            //short circuit items without children
            if (!curItem.Children.Any())
            {
                if (string.IsNullOrWhiteSpace(FilterText))
                {
                    curItem.IsVisible  = true;
                    curItem.IsExpanded = false;
                }
                else
                {
                    var matches = curItem.Header.Contains(FilterText, StringComparison.OrdinalIgnoreCase);
                    curItem.IsVisible  = matches;
                    curItem.IsExpanded = false;
                }

                return(curItem.IsVisible);
            }

            //check child items
            var anyChildMatches = false;

            foreach (var curChild in curItem.Children)
            {
                var childMatches = Filter(curChild);
                if (childMatches)
                {
                    anyChildMatches = true;
                }
            }

            //no child matches -> hide item
            if (!anyChildMatches)
            {
                curItem.IsVisible  = false;
                curItem.IsExpanded = false;
            }
            else
            {
                curItem.IsVisible = true;
                //only expand if there is a search, else it is a "reset" of the tree
                curItem.IsExpanded = !string.IsNullOrWhiteSpace(FilterText);
            }

            return(curItem.IsVisible);
        }
        public void ReturnKeyPressed_CommandParameterHasAnnoObject_ShouldRaiseApplySelectedItemEvent()
        {
            // Arrange
            var viewModel = new PresetsTreeViewModel(_mockedTreeLocalization);

            var annoObjectToSet = new AnnoObject();

            var commandParameter = new GenericTreeItem(null);

            commandParameter.AnnoObject = annoObjectToSet;

            // Act/Assert
            Assert.Raises <EventArgs>(
                x => viewModel.ApplySelectedItem += x,
                x => viewModel.ApplySelectedItem -= x,
                () => viewModel.ReturnKeyPressedCommand.Execute(commandParameter));
        }
        public void ReturnKeyPressed_CommandParameterHasNoAnnoObject_ShouldNotRaiseApplySelectedItemEvent()
        {
            // Arrange
            var viewModel = new PresetsTreeViewModel(_mockedTreeLocalization);

            var commandParameter = new GenericTreeItem(null);

            // Act
            var ex = Record.Exception(() => Assert.Raises <EventArgs>(
                                          x => viewModel.ApplySelectedItem += x,
                                          x => viewModel.ApplySelectedItem -= x,
                                          () => viewModel.ReturnKeyPressedCommand.Execute(commandParameter)));

            // Assert
            var exception = Assert.IsType <RaisesException>(ex);

            Assert.Equal("(No event was raised)", exception.Actual);
            Assert.Equal("EventArgs", exception.Expected);
        }
        private void SetCondensedTreeState(GenericTreeItem node, Dictionary <int, bool> savedTreeState)
        {
            if (!node.Children.Any())
            {
                return;
            }

            if (!savedTreeState.ContainsKey(node.Id))
            {
                return;
            }

            node.IsExpanded = savedTreeState[node.Id];

            foreach (var curChildNode in node.Children)
            {
                SetCondensedTreeState(curChildNode, savedTreeState);
            }
        }
        private Dictionary <int, bool> GetCondensedTreeState(GenericTreeItem node, Dictionary <int, bool> result)
        {
            if (!node.Children.Any())
            {
                return(result);
            }

            if (!node.IsExpanded)
            {
                result.Add(node.Id, false);

                return(result);
            }

            result.Add(node.Id, true);

            foreach (var curChildNode in node.Children)
            {
                GetCondensedTreeState(curChildNode, result);
            }

            return(result);
        }
        private (List <GenericTreeItem> items, Dictionary <int, bool> expectedState) GetTreeAndState(bool expandLastMainNode = true)
        {
            //tree state:
            //-item 1       //is expanded, but has no children -> test unexpected behaviour [do not add to list]
            //-item 2       //is not expanded and has no children -> test normal behaviour [do not add to list]
            //-item 3       //is not expanded and has empty children list -> test default behaviour [do not add to list]
            //-item 4       //is expanded and has children -> test condensed behaviour [add true to list]
            // -item 41     //is expanded, but has no children -> test unexpected behaviour [do not add to list]
            //-item 5       //is expanded and has children -> test condensed behaviour [add true to list]
            // -item 51     //is not expanded, but has children -> test normal behaviour [add false to list]
            // -item 52     //is expanded and has children -> test condensed behaviour [add true to list]
            //  -item 521   //is not expanded and has no children -> test normal behaviour [do not add to list]

            var expectedState = new Dictionary <int, bool>();

            if (expandLastMainNode)
            {
                expectedState = new Dictionary <int, bool>
                {
                    { 4, true },
                    { 5, true },
                    { 51, false },
                    { 52, true },
                };
            }
            else
            {
                expectedState = new Dictionary <int, bool>
                {
                    { 4, true },
                    { 5, false },
                };
            }

            var item4 = new GenericTreeItem(null)
            {
                Id = 4, Header = "item 4", IsExpanded = true
            };
            var item41 = new GenericTreeItem(item4)
            {
                Id = 41, Header = "item 41", IsExpanded = true
            };

            item4.Children.Add(item41);

            var item5 = new GenericTreeItem(null)
            {
                Id = 5, Header = "item 5", IsExpanded = expandLastMainNode
            };
            var item51 = new GenericTreeItem(item5)
            {
                Id = 51, Header = "item 51", IsExpanded = false
            };
            var item511 = new GenericTreeItem(item51)
            {
                Id = 511, Header = "item 511", IsExpanded = false
            };

            item51.Children.Add(item511);
            var item52 = new GenericTreeItem(item5)
            {
                Id = 52, Header = "item 52", IsExpanded = expandLastMainNode
            };
            var item521 = new GenericTreeItem(item52)
            {
                Id = 521, Header = "item 521", IsExpanded = false
            };

            item52.Children.Add(item521);
            item5.Children.Add(item51);
            item5.Children.Add(item52);

            var items = new List <GenericTreeItem>
            {
                new GenericTreeItem(null)
                {
                    Id = 1, Header = "item 1", IsExpanded = true
                },
                new GenericTreeItem(null)
                {
                    Id = 2, Header = "item 2", IsExpanded = false
                },
                new GenericTreeItem(null)
                {
                    Id = 3, Header = "item 3", Children = new ObservableCollection <GenericTreeItem>()
                },
                item4,
                item5
            };

            return(items, expectedState);
        }
        public void LoadItems(BuildingPresets buildingPresets)
        {
            if (buildingPresets == null)
            {
                throw new ArgumentNullException(nameof(buildingPresets));
            }

            BuildingPresetsVersion = buildingPresets.Version;

#if DEBUG
            Stopwatch sw = new Stopwatch();
            sw.Start();
#endif

            Items.Clear();

            //manually add roads
            var roadTiles = GetRoadTiles();
            foreach (var curRoad in roadTiles)
            {
                Items.Add(curRoad);
            }

            if (!buildingPresets.Buildings.Any())
            {
                return;
            }

            //Each item needs an id for save/restore of tree state. We don't know how many roads were added.
            var itemId = Items.Count;

            //prepare data
            var headerAnno2205 = "(A6) Anno 2205";

            var excludedTemplates = new[] { "Ark", "Harbour", "OrnamentBuilding" };
            var excludedFactions  = new[] { "third party", "Facility Modules" };

            var filteredBuildingList = buildingPresets.Buildings
                                       .Where(x => !excludedTemplates.Contains(x.Template) &&
                                              !excludedFactions.Contains(x.Faction));

            //For Anno 2205 only
            var modulesList = buildingPresets.Buildings
                              .Where(x => string.Equals(x.Header, headerAnno2205, StringComparison.OrdinalIgnoreCase) &&
                                     string.Equals(x.Faction, "Facility Modules", StringComparison.OrdinalIgnoreCase) &&
                                     !string.Equals(x.Faction, "Facilities", StringComparison.OrdinalIgnoreCase))
                              .ToList();

            #region some checks
#if DEBUG
            var facilityList = filteredBuildingList.Where(x => string.Equals(x.Faction, "Facilities", StringComparison.OrdinalIgnoreCase)).ToList();

            //Get a list of nonMatchedModules;
            var nonMatchedModulesList = modulesList.Except(facilityList, new BuildingInfoComparer()).ToList();
            //These appear to all match. The below statement should notify the progammer if we need to add handling for non matching lists
            System.Diagnostics.Debug.Assert(nonMatchedModulesList.Count == 0, "Module lists do not match, implement handling for this");
#endif
            #endregion

            //add data to tree
            var groupedGames = filteredBuildingList.GroupBy(x => x.Header).OrderBy(x => x.Key);
            foreach (var curGame in groupedGames)
            {
                var gameHeader  = curGame.Key;
                var gameVersion = GetGameVersion(curGame.Key);
                if (gameVersion == CoreConstants.GameVersion.Unknown)
                {
                    gameHeader = _localizationHelper.GetLocalization(curGame.Key);
                }

                var gameItem = new GameHeaderTreeItem
                {
                    Header      = gameHeader,
                    GameVersion = gameVersion,
                    Id          = ++itemId
                };

                var groupedFactions = curGame.Where(x => x.Faction != null).GroupBy(x => x.Faction).OrderBy(x => x.Key);
                foreach (var curFaction in groupedFactions)
                {
                    var factionItem = new GenericTreeItem(gameItem)
                    {
                        Header = _localizationHelper.GetLocalization(curFaction.Key),
                        Id     = ++itemId
                    };

                    var groupedGroups = curFaction.Where(x => x.Group != null).GroupBy(x => x.Group).OrderBy(x => x.Key);
                    foreach (var curGroup in groupedGroups)
                    {
                        var groupItem = new GenericTreeItem(factionItem)
                        {
                            Header = _localizationHelper.GetLocalization(curGroup.Key),
                            Id     = ++itemId
                        };

                        foreach (var curBuildingInfo in curGroup.OrderBy(x => x.GetOrderParameter(_commons.CurrentLanguageCode)))
                        {
                            groupItem.Children.Add(new GenericTreeItem(groupItem)
                            {
                                Header     = curBuildingInfo.ToAnnoObject(_commons.CurrentLanguageCode).Label,
                                AnnoObject = curBuildingInfo.ToAnnoObject(_commons.CurrentLanguageCode),
                                Id         = ++itemId
                            });
                        }

                        //For 2205 only
                        //Add building modules to element list.
                        //Group will be the same for elements in the list.
                        if (string.Equals(curGame.Key, headerAnno2205, StringComparison.OrdinalIgnoreCase))
                        {
                            var moduleItem = new GenericTreeItem(groupItem)
                            {
                                Header = _localizationHelper.GetLocalization(curGroup.ElementAt(0).Group) + " " + _localizationHelper.GetLocalization("Modules"),
                                Id     = ++itemId
                            };

                            foreach (var fourthLevel in modulesList.Where(x => x.Group == curGroup.ElementAt(0).Group))
                            {
                                moduleItem.Children.Add(new GenericTreeItem(moduleItem)
                                {
                                    Header     = fourthLevel.ToAnnoObject(_commons.CurrentLanguageCode).Label,
                                    AnnoObject = fourthLevel.ToAnnoObject(_commons.CurrentLanguageCode),
                                    Id         = ++itemId
                                });
                            }

                            if (moduleItem.Children.Count > 0)
                            {
                                groupItem.Children.Add(moduleItem);
                            }
                        }

                        factionItem.Children.Add(groupItem);
                    }

                    var groupedFactionBuildings = curFaction.Where(x => x.Group == null).OrderBy(x => x.GetOrderParameter(_commons.CurrentLanguageCode));
                    foreach (var curGroup in groupedFactionBuildings)
                    {
                        factionItem.Children.Add(new GenericTreeItem(factionItem)
                        {
                            Header     = curGroup.ToAnnoObject(_commons.CurrentLanguageCode).Label,
                            AnnoObject = curGroup.ToAnnoObject(_commons.CurrentLanguageCode),
                            Id         = ++itemId
                        });
                    }

                    gameItem.Children.Add(factionItem);
                }

                Items.Add(gameItem);
            }

#if DEBUG
            sw.Stop();
            logger.Trace($"loading items for PresetsTree took: {sw.ElapsedMilliseconds}ms");
#endif
        }