View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.view;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.concurrent.Callable;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.kuali.rice.krad.uif.component.BindingInfo;
25  import org.kuali.rice.krad.uif.component.Component;
26  import org.kuali.rice.krad.uif.control.TextControl;
27  import org.kuali.rice.krad.uif.field.InputField;
28  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
29  import org.kuali.rice.krad.uif.service.ViewHelperService;
30  
31  import static org.junit.Assert.*;
32  import static org.mockito.Mockito.*;
33  
34  /**
35   * ViewIndexTest has various tests for ViewIndex
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  public class ViewIndexTest {
40      @Before
41      public void setUp() throws Exception {
42  
43      }
44  
45      /**
46       * test that clear indexes after render does not clear fields with a value for
47       * refreshWhenChanged and their nested controls
48       * 
49       * @throws Exception
50       */
51      @Test
52      public void testClearIndexesAfterRender() throws Exception {
53          // create an input field
54          final InputField field = ViewLifecycle.encapsulateInitialization(new Callable<InputField>(){
55              @Override
56              public InputField call() throws Exception {
57                  InputField field = new InputField();
58                  BindingInfo bindingInfo = new BindingInfo();
59                  bindingInfo.setBindingPath("property1");
60                  field.setBindingInfo(bindingInfo);
61                  String fieldId = "field1";
62                  field.setId(fieldId);
63  
64                  List<String> refreshWhenChangedPropertyNames = field.getRefreshWhenChangedPropertyNames();
65                  refreshWhenChangedPropertyNames = refreshWhenChangedPropertyNames == null ?
66                          new ArrayList<String>() : new ArrayList<String>(refreshWhenChangedPropertyNames);
67                  refreshWhenChangedPropertyNames.add("#lp.allDay eq true");
68                  field.setRefreshWhenChangedPropertyNames(refreshWhenChangedPropertyNames);
69  
70                  // set a control
71                  TextControl textControl = new TextControl();
72                  String controlId = "text1";
73                  textControl.setId(controlId);
74                  field.setControl(textControl);
75                  return field;
76              }
77          });
78  
79          final ViewIndex viewIndex = new ViewIndex();
80          View view = mock(View.class);
81          ViewHelperService helper = mock(ViewHelperService.class);
82          when(view.getViewHelperService()).thenReturn(helper);
83  
84          ViewLifecycle.encapsulateLifecycle(view, null, null, null, new Runnable(){
85              @Override
86              public void run() {
87                  Component[] components = {field.copy()};
88  
89                  //add to view index
90                  for (Component component : components) {
91                      viewIndex.indexComponent(component);
92                      viewIndex.addInitialComponentStateIfNeeded(component);
93                  }
94  
95                  // verify initial view index state
96                  for (Component component : components) {
97                      assertNotNull(viewIndex.getComponentById(component.getId()));
98                  }
99  
100                 viewIndex.clearIndexesAfterRender();
101                 // confirm that the index still has the components
102                 for (Component component : components) {
103                     assertNotNull(viewIndex.getComponentById(component.getId()));
104                     assertTrue(viewIndex.getInitialComponentStates().containsKey(component.getId()));
105                 }
106             }});
107         
108     }
109 }