コード例 #1
0
        private TextBox SetupTextboxTest(Options options)
        {
            /*Arrange*/
            TextBox tb1 = new TextBox();

            tb1.Text = "Data From view";
            tb1.ID   = "tb1";
            WebformControl wrappedTb = new WebformControl(tb1);

            //Create a binding collection, add a binding
            options.Path = "EmployeeName";
            collection   = new Binding.BindingCollection()
            {
                new Binding.BindingDef(wrappedTb, "Text", options, false, controlService)
            };

            //and setup service to return it
            controlService.Expect(cs => cs.Unwrap(null)).Do(new Func <IBindingTarget, object>((t) => tb1));

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            return(tb1);
        }
コード例 #2
0
        private Tuple <TextBox, TextBox> SetupMultipleTextboxTest(Options options1, Options options2)
        {
            /*Arrange*/
            TextBox tb1 = new TextBox();

            tb1.Text = "Data From tb1";
            tb1.ID   = "tb1";
            WebformControl wrappedTb = new WebformControl(tb1);

            TextBox tb2 = new TextBox();

            tb2.Text = "Data From tb2";
            tb2.ID   = "tb2";
            WebformControl wrappedTb2 = new WebformControl(tb2);

            //Create a binding collection, add a binding
            options1.Path = "EmployeeName";
            collection    = new Binding.BindingCollection()
            {
                new Binding.BindingDef(wrappedTb, "Text", options1, false, controlService)
            };

            options2.Path = "EmployeeName";
            collection.Add(new Binding.BindingDef(wrappedTb2, "Text", options2, false, controlService));


            //and setup service to return it
            int count = 0;

            controlService.Expect(cs => cs.Unwrap(null)).Do(new Func <IBindingTarget, object>((t) =>
            {
                if (count == 0)
                {
                    count++;
                    return(tb1);
                }
                else
                {
                    return(tb2);
                }
            }));

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            return(new Tuple <TextBox, TextBox> {
                Item1 = tb1, Item2 = tb2
            });
        }
コード例 #3
0
        public void SetupForTest()
        {
            mockContainer      = MockRepository.GenerateMock <IServiceProviderBindingContainer>();
            dataStorageService = MockRepository.GenerateMock <IDataStorageService>();
            controlService     = MockRepository.GenerateMock <IControlService>();
            viewModel          = MockRepository.GenerateMock <ISimpleViewModel>();
            mockContainer.Expect(c => c.DataContext).IgnoreArguments().Return(viewModel);

            //object under test
            binder = new ViewStateBinder(mockContainer, dataStorageService, controlService);

            //setup mock services
            collection = new Binding.BindingCollection();
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(null)).IgnoreArguments().Return(collection);

            //dataStorageService.Expect(ds => ds.Retrieve<object>(null)).IgnoreArguments()Return(viewModel);
        }
コード例 #4
0
        private List <Control> SetupCollectionUnbindTest(Options options)
        {
            List <Control> createdControls = new List <Control>();

            //Create a repeater from which we will read data
            Repeater rpt = new Repeater();

            rpt.ItemTemplate = new DynamicItemTemplate(new List <Func <Control> > {
                () => new TextBox()
                {
                    ID = "tb"
                }
            });
            var item1 = new Child {
                Name = "Sam"
            };
            var item2 = new Child {
                Name = "Ben"
            };
            var item3 = new Child {
                Name = "Ali"
            };
            var list = new List <Child> {
                item1, item2, item3
            };

            viewModel.ChildrensNames = list;

            //Create a binding collection
            collection = new Binding.BindingCollection();

            //add in the parent binding, this will not be used in the bind but is required for unbind to work within a collection
            collection.Add(new Binding.BindingDef(new WebformControl(rpt), "DataSource", new Options {
                Path = "ChildrensNames"
            }, true, controlService));

            //When the item is created, create a binding for it and add to the binding collection.
            //We will also had the itemtemplate create control to a collection so that we can stick some values
            //in to simulate user input before unbinding to the model.
            int index = 0;

            rpt.ItemDataBound += (s, e) =>
            {
                TextBox        tb        = e.Item.FindControl("tb") as TextBox;
                WebformControl wrappedTb = new WebformControl(tb);
                options.Path = "Name";
                Binding.BindingDef binding = new Binding.BindingDef(wrappedTb, "Text", index, collection, options, false, controlService);
                index++;
                collection.Add(binding);
                createdControls.Add(tb);
            };

            rpt.DataSource = list;


            WebformControl dummyControl = new WebformControl(new TextBox());

            //tell the mock conntrol service to return the collection result when required.
            this.controlService.Expect(cs => cs.FindControlUnique(null, null)).IgnoreArguments()
            .Do(
                new Func <IBindingTarget, string, IBindingTarget>((b, s) =>
            {
                //we only need it return a non-null
                return(dummyControl);
            }));


            int counter = 0;

            //and setup service to return it
            controlService.Expect(cs => cs.Unwrap(null)).IgnoreArguments().Do(new Func <IBindingTarget, object>((t) =>
            {
                Control ctrl = createdControls[counter];
                counter++;
                return(ctrl);
            }));


            //setup viewmodel with default values
            viewModel.ChildrensNames = new List <Child> {
                new Child(), new Child(), new Child()
            };

            //setup the data storage service to return the collection
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(DATA_BINDING_KEY)).Return(collection);
            dataStorageService.Expect(ds => ds.RetrieveOrCreate <Binding.BindingCollection>(COMMAND_BINDING_KEY)).Return(new Binding.BindingCollection());

            //and the viewmodel (we are using the view state binder which will try to resolve the service from storage
            dataStorageService.Expect(ds => ds.Retrieve <object>(null)).IgnoreArguments().Return(viewModel);

            rpt.DataBind();

            return(createdControls);
        }
コード例 #5
0
 public bool TrySetParent(BindingCollection bindingCollection)
 {
     return(TrySetParent(bindingCollection.LastCollectionBinding));
 }
コード例 #6
0
        /// <summary>
        /// Used to create a nested data binding
        /// </summary>
        /// <param name="targetControl">The control to which to bind</param>
        /// <param name="targetPropertyName">The property of the control to which to bind</param>
        /// <param name="dataItemIndex">If the parent binding is a collection, the index is the data item for this binding. Else zerp</param>
        /// <param name="owningCollection">The collection of current bindings</param>
        /// <param name="bindingOptions">Options to control the binding</param>
        /// <param name="isSourceEnumerable">Is the datasource to which you are binidng a collection</param>
        /// <param name="controlService">Service required for binding</param>
        public BindingDef(IBindingTarget targetControl, string targetPropertyName, int dataItemIndex, BindingCollection owningCollection, Options bindingOptions, bool isSourceEnumerable, IControlService controlService)
        {
            this.controlService = controlService;
            this.Container      = targetControl;
            this.TrySetParent(owningCollection);
            this.sourceExpression = new BindingPath(bindingOptions.Path, dataItemIndex, GetParentExpression(), bindingOptions.PathMode);

            string targetExpression = string.Format("{0}.{1}", targetControl.UniqueID, targetPropertyName);

            this.targetExpression = new BindingPath(targetExpression);

            this.DataItemIndex      = dataItemIndex;
            this.bindingOptions     = bindingOptions;
            this.IsSourceEnumerable = isSourceEnumerable;
        }