public void AddToolView_ToolViewWasAlreadyAdded_NoDuplicationNoViewOpenedEventFired()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddToolView(testView1, ToolViewLocation.Left, string.Empty, string.Empty, null);
                avalonDockViewHost.AddToolView(testView2, ToolViewLocation.Left, string.Empty, string.Empty, null);

                var viewOpenedCounter = 0;
                avalonDockViewHost.ViewOpened += (sender, args) => viewOpenedCounter++;

                // Call
                avalonDockViewHost.AddToolView(testView1, ToolViewLocation.Right, string.Empty, string.Empty, null);

                // Assert
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView1,
                    testView2
                },
                    avalonDockViewHost.ToolViews);
                Assert.AreEqual(0, viewOpenedCounter);
            }
        }
 private static void SetActiveView(AvalonDockViewHost avalonDockViewHost, IView view)
 {
     avalonDockViewHost.DockingManager.Layout.Descendents()
     .OfType <LayoutContent>()
     .First(d => ((WindowsFormsHost)d.Content).Child == view)
     .IsActive = true;
 }
        public void BringToFront_NonActiveToolView_ViewBroughtToFrontFiredButActiveViewNotAffectedAndNoActiveViewEventsFired()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();
            var viewBroughtToFrontCounter         = 0;
            var activeDocumentViewChangingCounter = 0;
            var activeDocumentViewChangedCounter  = 0;
            var activeViewChangedCounter          = 0;

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddToolView(testView1, ToolViewLocation.Left, string.Empty, string.Empty, null);
                avalonDockViewHost.AddToolView(testView2, ToolViewLocation.Bottom, string.Empty, string.Empty, null);
                SetActiveView(avalonDockViewHost, testView2);

                avalonDockViewHost.ViewBroughtToFront         += (sender, args) => viewBroughtToFrontCounter++;
                avalonDockViewHost.ActiveDocumentViewChanging += (sender, args) => activeDocumentViewChangingCounter++;
                avalonDockViewHost.ActiveDocumentViewChanged  += (sender, args) => activeDocumentViewChangedCounter++;
                avalonDockViewHost.ActiveViewChanged          += (sender, args) => activeViewChangedCounter++;

                // Call
                avalonDockViewHost.BringToFront(testView1);

                // Assert
                Assert.AreEqual(1, viewBroughtToFrontCounter);
                Assert.AreEqual(0, activeDocumentViewChangingCounter);
                Assert.AreEqual(0, activeDocumentViewChangedCounter);
                Assert.AreEqual(0, activeViewChangedCounter);
                Assert.IsTrue(IsActiveView(avalonDockViewHost, testView2));
            }
        }
        public void AddDocumentView_DocumentViewWasAlreadyAdded_NoDuplicationNoViewOpenedEventFired()
        {
            // Setup
            var testView = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddDocumentView(testView, string.Empty, string.Empty, null);

                var viewOpenedCounter = 0;
                avalonDockViewHost.ViewOpened += (sender, args) => viewOpenedCounter++;

                CollectionAssert.AreEqual(
                    new[]
                {
                    testView
                },
                    avalonDockViewHost.DocumentViews);

                // Call
                avalonDockViewHost.AddDocumentView(testView, string.Empty, string.Empty, null);

                // Assert
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView
                },
                    avalonDockViewHost.DocumentViews);
                Assert.AreEqual(0, viewOpenedCounter);
            }
        }
        private static CustomLayoutAnchorable GetLayoutAnchorable(AvalonDockViewHost avalonDockViewHost, IView toolView, ToolViewLocation toolViewLocation)
        {
            string paneField;

            switch (toolViewLocation)
            {
            case ToolViewLocation.Left:
                paneField = "LeftLayoutAnchorablePaneGroup";
                break;

            case ToolViewLocation.Right:
                paneField = "RightLayoutAnchorablePaneGroup";
                break;

            case ToolViewLocation.Bottom:
                paneField = "BottomLayoutAnchorablePaneGroup";
                break;

            default:
                paneField = "";
                break;
            }

            var layoutAnchorablePaneGroup = TypeUtils.GetField <LayoutAnchorablePaneGroup>(avalonDockViewHost, paneField);

            return(layoutAnchorablePaneGroup.Descendents()
                   .OfType <LayoutAnchorablePane>()
                   .First()
                   .Children
                   .Cast <CustomLayoutAnchorable>()
                   .FirstOrDefault(c => ((WindowsFormsHost)c.Content).Child == toolView));
        }
        public void Remove_ActiveDocumentViewInViewHostWithNoOtherDocumentViews_ActiveDocumentViewSetToNullAndActiveDocumentViewEventsFired()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();
            var activeDocumentViewChangingCounter = 0;
            var activeDocumentViewChangedCounter  = 0;
            var activeViewChangedCounter          = 0;

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddDocumentView(testView1, string.Empty, string.Empty, null);
                avalonDockViewHost.AddToolView(testView2, ToolViewLocation.Left, string.Empty, string.Empty, null);
                SetActiveView(avalonDockViewHost, testView1);

                avalonDockViewHost.ActiveDocumentViewChanging += (sender, args) => activeDocumentViewChangingCounter++;
                avalonDockViewHost.ActiveDocumentViewChanged  += (sender, args) => activeDocumentViewChangedCounter++;
                avalonDockViewHost.ActiveViewChanged          += (sender, args) => activeViewChangedCounter++;

                // Call
                avalonDockViewHost.Remove(testView1);

                // Assert
                Assert.AreEqual(1, activeDocumentViewChangingCounter);
                Assert.AreEqual(1, activeDocumentViewChangedCounter);
                Assert.AreEqual(0, activeViewChangedCounter);
                Assert.IsNull(avalonDockViewHost.ActiveDocumentView);
            }
        }
        public void Remove_DocumentViewInViewHostWithMultipleViews_ViewRemoved()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddDocumentView(testView1, string.Empty, string.Empty, null);
                avalonDockViewHost.AddDocumentView(testView2, string.Empty, string.Empty, null);

                // Precondition
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView1,
                    testView2
                },
                    avalonDockViewHost.DocumentViews);
                Assert.IsTrue(IsDocumentViewPresent(avalonDockViewHost, testView1));

                // Call
                avalonDockViewHost.Remove(testView1);

                // Assert
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView2
                },
                    avalonDockViewHost.DocumentViews);
                Assert.IsFalse(IsDocumentViewPresent(avalonDockViewHost, testView1));
            }
        }
        public void AddDocumentView_MultipleTestViews_ViewsAddedAndViewOpenedEventsFired(int numberOfViewsToAdd)
        {
            // Setup
            var viewList = new List <IView>();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                var viewOpenedCounter = 0;
                avalonDockViewHost.ViewOpened += (sender, args) =>
                {
                    Assert.AreSame(viewList.Last(), args.View);

                    viewOpenedCounter++;
                };

                for (var i = 0; i < numberOfViewsToAdd; i++)
                {
                    var testView = new TestView();

                    viewList.Add(testView);

                    // Call
                    avalonDockViewHost.AddDocumentView(testView, string.Empty, string.Empty, null);
                }

                // Assert
                CollectionAssert.AreEqual(viewList, avalonDockViewHost.DocumentViews);
                Assert.IsTrue(viewList.All(v => IsDocumentViewPresent(avalonDockViewHost, v)));
                Assert.AreEqual(numberOfViewsToAdd, viewOpenedCounter);
                Assert.IsNull(avalonDockViewHost.ActiveDocumentView);
            }
        }
        public void Remove_ToolViewInViewHostWithMultipleViews_ViewRemoved()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddToolView(testView1, ToolViewLocation.Left, string.Empty, string.Empty, null);
                avalonDockViewHost.AddToolView(testView2, ToolViewLocation.Left, string.Empty, string.Empty, null);

                // Precondition
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView1,
                    testView2
                },
                    avalonDockViewHost.ToolViews);

                // Call
                avalonDockViewHost.Remove(testView1);

                // Assert
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView2
                },
                    avalonDockViewHost.ToolViews);
            }
        }
 /// <summary>
 /// Returns whether <paramref name="title"/> is set for <paramref name="view"/>.
 /// </summary>
 /// <param name="avalonDockViewHost">The view host that owns the view.</param>
 /// <param name="view">The view to check the title for.</param>
 /// <param name="title">The title to check for.</param>
 /// <returns>Whether <paramref name="title"/> is set for <paramref name="view"/>.</returns>
 public static bool IsTitleSet(AvalonDockViewHost avalonDockViewHost, IView view, string title)
 {
     return(avalonDockViewHost.DockingManager
            .Layout
            .Descendents()
            .OfType <LayoutContent>()
            .First(lc => ((WindowsFormsHost)lc.Content).Child == view).Title == title);
 }
        private static CustomLayoutDocument GetLayoutDocument(AvalonDockViewHost avalonDockViewHost, IView documentView)
        {
            var layoutDocumentPaneGroup = TypeUtils.GetField <LayoutDocumentPaneGroup>(avalonDockViewHost, "LayoutDocumentPaneGroup");

            return(layoutDocumentPaneGroup.Descendents()
                   .OfType <LayoutDocumentPane>()
                   .First()
                   .Children
                   .Cast <CustomLayoutDocument>()
                   .FirstOrDefault(c => ((WindowsFormsHost)c.Content).Child == documentView));
        }
 public void Constructor_DefaultValues()
 {
     // Call
     using (var avalonDockViewHost = new AvalonDockViewHost())
     {
         // Assert
         Assert.IsInstanceOf <IViewHost>(avalonDockViewHost);
         CollectionAssert.IsEmpty(avalonDockViewHost.DocumentViews);
         CollectionAssert.IsEmpty(avalonDockViewHost.ToolViews);
         Assert.IsNull(avalonDockViewHost.ActiveDocumentView);
         Assert.IsFalse(IsAnyViewActive(avalonDockViewHost));
     }
 }
        public void Dispose_ViewHostWithMultipleViews_ViewsClearedActiveDocumentViewSetToNullAndActiveViewEventsFired()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();
            var testView3 = new TestView();
            var testView4 = new TestView();
            var activeDocumentViewChangingCounter = 0;
            var activeDocumentViewChangedCounter  = 0;
            var activeViewChangedCounter          = 0;

            var avalonDockViewHost = new AvalonDockViewHost();

            avalonDockViewHost.AddDocumentView(testView1, string.Empty, string.Empty, null);
            avalonDockViewHost.AddDocumentView(testView2, string.Empty, string.Empty, null);
            avalonDockViewHost.AddToolView(testView3, ToolViewLocation.Left, string.Empty, string.Empty, null);
            avalonDockViewHost.AddToolView(testView4, ToolViewLocation.Left, string.Empty, string.Empty, null);

            SetActiveView(avalonDockViewHost, testView1);

            CollectionAssert.AreEqual(
                new[]
            {
                testView3,
                testView4
            },
                avalonDockViewHost.ToolViews);

            avalonDockViewHost.ActiveDocumentViewChanging += (sender, args) => activeDocumentViewChangingCounter++;
            avalonDockViewHost.ActiveDocumentViewChanged  += (sender, args) => activeDocumentViewChangedCounter++;
            avalonDockViewHost.ActiveViewChanged          += (sender, args) => activeViewChangedCounter++;

            // Call
            avalonDockViewHost.Dispose();

            // Assert
            CollectionAssert.IsEmpty(avalonDockViewHost.DocumentViews);
            CollectionAssert.IsEmpty(avalonDockViewHost.ToolViews);
            Assert.AreNotEqual(0, activeDocumentViewChangingCounter);
            Assert.AreNotEqual(0, activeDocumentViewChangedCounter);
            Assert.AreNotEqual(0, activeViewChangedCounter);
            Assert.IsNull(avalonDockViewHost.ActiveDocumentView);
            Assert.IsFalse(IsAnyViewActive(avalonDockViewHost));
        }
        public void AddToolView_DocumentViewSetAsActiveView_ActiveDocumentViewNotAffected()
        {
            // Setup
            var testToolView     = new TestView();
            var testDocumentView = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddDocumentView(testDocumentView, string.Empty, string.Empty, null);
                SetActiveView(avalonDockViewHost, testDocumentView);

                // Call
                avalonDockViewHost.AddToolView(testToolView, ToolViewLocation.Left, string.Empty, string.Empty, null);

                // Assert
                Assert.AreSame(testDocumentView, avalonDockViewHost.ActiveDocumentView);
                Assert.IsTrue(IsActiveView(avalonDockViewHost, testDocumentView));
            }
        }
        public void AddToolView_InvalidPosition_ThrowsInvalidEnumArgumentException()
        {
            // Setup
            const int invalidLocation = 4;

            using (var avalonDockViewHost = new AvalonDockViewHost())
                using (var testView = new TestView())
                {
                    // Call
                    void Call() => avalonDockViewHost.AddToolView(testView, (ToolViewLocation)invalidLocation, string.Empty, string.Empty, null);

                    // Assert
                    var    expectedMessage = $"The value of argument 'toolViewLocation' ({invalidLocation}) is invalid for Enum type 'ToolViewLocation'.";
                    string parameter       = TestHelper.AssertThrowsArgumentExceptionAndTestMessage <InvalidEnumArgumentException>(
                        Call,
                        expectedMessage).ParamName;
                    Assert.AreEqual("toolViewLocation", parameter);
                }
        }
        public void AddDocumentView_WithAllParameters_ViewAddedWithExpectedProperties()
        {
            // Setup
            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                const string title      = "Random title";
                const string symbol     = "Random symbol";
                var          fontFamily = new FontFamily();

                var testView = new TestView();

                // Call
                avalonDockViewHost.AddDocumentView(testView, title, symbol, fontFamily);

                // Assert
                CustomLayoutDocument layoutDocument = GetLayoutDocument(avalonDockViewHost, testView);
                Assert.AreEqual(title, layoutDocument.Title);
                Assert.AreEqual(symbol, layoutDocument.Symbol);
                Assert.AreSame(fontFamily, layoutDocument.FontFamily);
            }
        }
        public void SetTitle_ToolView_TitleSet()
        {
            // Setup
            const string titleToSet = "Random title";

            var testView = new TestView();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddToolView(testView, ToolViewLocation.Left, string.Empty, string.Empty, null);

                // Precondition
                Assert.IsFalse(AvalonDockViewHostTestHelper.IsTitleSet(avalonDockViewHost, testView, titleToSet));

                // Call
                avalonDockViewHost.SetTitle(testView, titleToSet);

                // Assert
                Assert.IsTrue(AvalonDockViewHostTestHelper.IsTitleSet(avalonDockViewHost, testView, titleToSet));
            }
        }
        public void AddToolView_WithAllParameters_ViewAddedWithExpectedProperties(ToolViewLocation toolViewLocation)
        {
            // Setup
            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                const string title      = "Random title";
                const string symbol     = "Random symbol";
                var          fontFamily = new FontFamily();

                var testView = new TestView();

                // Call
                avalonDockViewHost.AddToolView(testView, toolViewLocation, title, symbol, fontFamily);

                // Assert
                CustomLayoutAnchorable layoutAnchorable = GetLayoutAnchorable(avalonDockViewHost, testView, toolViewLocation);
                Assert.AreEqual(title, layoutAnchorable.Title);
                Assert.AreEqual(symbol, layoutAnchorable.Symbol);
                Assert.AreSame(fontFamily, layoutAnchorable.FontFamily);
            }
        }
        public void Remove_ToolViewInViewHostWithMultipleOtherViews_ActiveDocumentViewNotAffectedAndNoActiveViewEventsFired()
        {
            // Setup
            var testView1 = new TestView();
            var testView2 = new TestView();
            var testView3 = new TestView();
            var testView4 = new TestView();
            var testView5 = new TestView();
            var activeDocumentViewChangingCounter = 0;
            var activeDocumentViewChangedCounter  = 0;
            var activeViewChangedCounter          = 0;

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddDocumentView(testView1, string.Empty, string.Empty, null);
                avalonDockViewHost.AddDocumentView(testView2, string.Empty, string.Empty, null);
                avalonDockViewHost.AddDocumentView(testView3, string.Empty, string.Empty, null);
                avalonDockViewHost.AddDocumentView(testView4, string.Empty, string.Empty, null);
                avalonDockViewHost.AddToolView(testView5, ToolViewLocation.Left, string.Empty, string.Empty, null);

                SetActiveView(avalonDockViewHost, testView3);
                SetActiveView(avalonDockViewHost, testView5);

                // Precondition
                Assert.AreSame(testView3, avalonDockViewHost.ActiveDocumentView);

                avalonDockViewHost.ActiveDocumentViewChanging += (sender, args) => activeDocumentViewChangingCounter++;
                avalonDockViewHost.ActiveDocumentViewChanged  += (sender, args) => activeDocumentViewChangedCounter++;
                avalonDockViewHost.ActiveViewChanged          += (sender, args) => activeViewChangedCounter++;

                // Call
                avalonDockViewHost.Remove(testView5);

                // Assert
                Assert.AreEqual(0, activeDocumentViewChangingCounter);
                Assert.AreEqual(0, activeDocumentViewChangedCounter);
                Assert.AreEqual(0, activeViewChangedCounter);
                Assert.AreSame(testView3, avalonDockViewHost.ActiveDocumentView);
            }
        }
        public void AddToolView_NonControlView_ViewNotAddedAndNoViewOpenedEventFired()
        {
            // Setup
            var mocks    = new MockRepository();
            var testView = mocks.StrictMock <IView>();

            mocks.ReplayAll();

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                var viewOpenedCounter = 0;
                avalonDockViewHost.ViewOpened += (sender, args) => viewOpenedCounter++;

                // Call
                avalonDockViewHost.AddToolView(testView, ToolViewLocation.Left, string.Empty, string.Empty, null);

                // Assert
                CollectionAssert.IsEmpty(avalonDockViewHost.ToolViews);
                Assert.IsFalse(IsToolViewPresent(avalonDockViewHost, testView, ToolViewLocation.Left));
                Assert.AreEqual(0, viewOpenedCounter);
            }

            mocks.VerifyAll();
        }
        public void Remove_ToolView_ViewClosedEventsFired()
        {
            // Setup
            var testView          = new TestView();
            var viewClosedCounter = 0;

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                avalonDockViewHost.AddToolView(testView, ToolViewLocation.Left, string.Empty, string.Empty, null);

                avalonDockViewHost.ViewClosed += (sender, args) =>
                {
                    Assert.AreSame(testView, args.View);

                    viewClosedCounter++;
                };

                // Call
                avalonDockViewHost.Remove(testView);

                // Assert
                Assert.AreEqual(1, viewClosedCounter);
            }
        }
        public void AddToolView_TestViews_ViewAddedAndViewOpenedEventFired(ToolViewLocation toolViewLocation)
        {
            // Setup
            var testView = new TestView();
            IEnumerable <ToolViewLocation> otherToolViewLocations = Enum.GetValues(typeof(ToolViewLocation))
                                                                    .Cast <ToolViewLocation>()
                                                                    .Except(new[]
            {
                toolViewLocation
            });

            using (var avalonDockViewHost = new AvalonDockViewHost())
            {
                var viewOpenedCounter = 0;
                avalonDockViewHost.ViewOpened += (sender, args) =>
                {
                    Assert.AreSame(testView, args.View);

                    viewOpenedCounter++;
                };

                // Call
                avalonDockViewHost.AddToolView(testView, toolViewLocation, string.Empty, string.Empty, null);

                // Assert
                CollectionAssert.AreEqual(
                    new[]
                {
                    testView
                },
                    avalonDockViewHost.ToolViews);
                Assert.IsTrue(IsToolViewPresent(avalonDockViewHost, testView, toolViewLocation));
                Assert.IsFalse(otherToolViewLocations.Any(tvl => IsToolViewPresent(avalonDockViewHost, testView, tvl)));
                Assert.AreEqual(1, viewOpenedCounter);
            }
        }
 private static bool IsAnyViewActive(AvalonDockViewHost avalonDockViewHost)
 {
     return(avalonDockViewHost.DockingManager.ActiveContent != null);
 }
 private static bool IsActiveView(AvalonDockViewHost avalonDockViewHost, IView view)
 {
     return(((WindowsFormsHost)avalonDockViewHost.DockingManager.ActiveContent).Child == view);
 }
 private static bool IsToolViewPresent(AvalonDockViewHost avalonDockViewHost, IView toolView, ToolViewLocation toolViewLocation)
 {
     return(GetLayoutAnchorable(avalonDockViewHost, toolView, toolViewLocation) != null);
 }
 private static bool IsDocumentViewPresent(AvalonDockViewHost avalonDockViewHost, IView documentView)
 {
     return(GetLayoutDocument(avalonDockViewHost, documentView) != null);
 }