Beispiel #1
0
        public void ValidateChildRemovedFromParentWhenOwnerIsDifferent()
        {
            RunOnUIThread.Execute(() =>
            {
                var element       = new Button();
                const string key1 = "Key1";
                const string key2 = "Key2";

                RecyclePool pool = new RecyclePool();
                var parent1      = new StackPanel();
                var child1       = new Button();
                var child2       = new Button();
                parent1.Children.Add(child1);
                parent1.Children.Add(child2);

                pool.PutElement(child1, key1, parent1);
                pool.PutElement(child2, key2, parent1);

                var parent2 = new StackPanel();
                // Recycle the second child for a different parent. It should be disconnected
                var recycled1 = (FrameworkElement)pool.TryGetElement(key2, parent2);
                Verify.IsNull(recycled1.Parent);
                var recycled2 = (FrameworkElement)pool.TryGetElement(key1, parent2);
                Verify.IsNull(recycled2.Parent);
            });
        }
Beispiel #2
0
 public void ValidateOwnershipWithStackPanel()
 {
     RunOnUIThread.Execute(() =>
     {
         RecyclePool pool = new RecyclePool();
         var owner        = new StackPanel();
         var child        = new Button();
         owner.Children.Add(child);
         pool.PutElement(child, "Key", owner);
         var recycled = pool.TryGetElement("Key", owner);
         Verify.AreSame(child, recycled);
         Verify.AreEqual(0, owner.Children.IndexOf(child));
     });
 }
Beispiel #3
0
        public void ValidateElementsHaveCorrectKeys()
        {
            RunOnUIThread.Execute(() =>
            {
                var element                = new Button();
                const string buttonKey     = "ButtonKey";
                const string textBlockKey  = "TextBlockKey";
                const string stackPanelKey = "StackPanelKey";

                RecyclePool pool = new RecyclePool();
                pool.PutElement(new Button(), buttonKey);
                pool.PutElement(new TextBlock(), textBlockKey);
                pool.PutElement(new StackPanel(), stackPanelKey);
                pool.PutElement(new Button(), buttonKey);
                pool.PutElement(new TextBlock(), textBlockKey);
                pool.PutElement(new StackPanel(), stackPanelKey);
                pool.PutElement(new Button(), buttonKey);
                pool.PutElement(new TextBlock(), textBlockKey);
                pool.PutElement(new StackPanel(), stackPanelKey);

                Verify.IsNotNull((Button)pool.TryGetElement(buttonKey));
                Verify.IsNotNull((Button)pool.TryGetElement(buttonKey));
                Verify.IsNotNull((Button)pool.TryGetElement(buttonKey));
                Verify.IsNull(pool.TryGetElement(buttonKey));

                Verify.IsNotNull((TextBlock)pool.TryGetElement(textBlockKey));
                Verify.IsNotNull((TextBlock)pool.TryGetElement(textBlockKey));
                Verify.IsNotNull((TextBlock)pool.TryGetElement(textBlockKey));
                Verify.IsNull(pool.TryGetElement(textBlockKey));

                Verify.IsNotNull((StackPanel)pool.TryGetElement(stackPanelKey));
                Verify.IsNotNull((StackPanel)pool.TryGetElement(stackPanelKey));
                Verify.IsNotNull((StackPanel)pool.TryGetElement(stackPanelKey));
                Verify.IsNull(pool.TryGetElement(stackPanelKey));


                Verify.Throws <COMException>(delegate
                {
                    pool.PutElement(new Button(), null, null);
                });

                Verify.Throws <COMException>(delegate
                {
                    pool.PutElement(new Button(), buttonKey, new Button() /* not a panel */);
                });

                Verify.Throws <COMException>(delegate
                {
                    pool.TryGetElement(null, null);
                });
            });
        }