Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *      http://www.apache.org/licenses/LICENSE-2.0
      7  * Unless required by applicable law or agreed to in writing, software
      8  * distributed under the License is distributed on an "AS IS" BASIS,
      9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     10  * See the License for the specific language governing permissions and
     11  * limitations under the License.
     12  */
     13 
     14 package android.databinding.testapp;
     15 
     16 import android.databinding.testapp.databinding.BasicDependantBindingBinding;
     17 import android.databinding.testapp.vo.NotBindableVo;
     18 
     19 import android.test.UiThreadTest;
     20 
     21 import java.util.ArrayList;
     22 import java.util.List;
     23 
     24 public class BasicDependantBindingTest extends BaseDataBinderTest<BasicDependantBindingBinding> {
     25 
     26     public BasicDependantBindingTest() {
     27         super(BasicDependantBindingBinding.class);
     28     }
     29 
     30     @Override
     31     protected void setUp() throws Exception {
     32         super.setUp();
     33         initBinder();
     34     }
     35 
     36     public List<NotBindableVo> permutations(String value) {
     37         List<NotBindableVo> result = new ArrayList<>();
     38         result.add(null);
     39         result.add(new NotBindableVo(null));
     40         result.add(new NotBindableVo(value));
     41         return result;
     42     }
     43 
     44     @UiThreadTest
     45     public void testAllPermutations() {
     46         List<NotBindableVo> obj1s = permutations("a");
     47         List<NotBindableVo> obj2s = permutations("b");
     48         for (NotBindableVo obj1 : obj1s) {
     49             for (NotBindableVo obj2 : obj2s) {
     50                 reCreateBinder(null); //get a new one
     51                 testWith(obj1, obj2);
     52                 reCreateBinder(null);
     53                 mBinder.executePendingBindings();
     54                 testWith(obj1, obj2);
     55             }
     56         }
     57     }
     58 
     59     private void testWith(NotBindableVo obj1, NotBindableVo obj2) {
     60         mBinder.setObj1(obj1);
     61         mBinder.setObj2(obj2);
     62         mBinder.executePendingBindings();
     63         assertValues(safeGet(obj1), safeGet(obj2),
     64                 obj1 == null ? "" : obj1.mergeStringFields(obj2),
     65                 obj2 == null ? "" : obj2.mergeStringFields(obj1),
     66                 (obj1 == null ? null : obj1.getStringValue())
     67                         + (obj2 == null ? null : obj2.getStringValue())
     68         );
     69     }
     70 
     71     private String safeGet(NotBindableVo vo) {
     72         if (vo == null || vo.getStringValue() == null) {
     73             return "";
     74         }
     75         return vo.getStringValue();
     76     }
     77 
     78     private void assertValues(String textView1, String textView2,
     79             String mergedView1, String mergedView2, String rawMerge) {
     80         assertEquals(textView1, mBinder.textView1.getText().toString());
     81         assertEquals(textView2, mBinder.textView2.getText().toString());
     82         assertEquals(mergedView1, mBinder.mergedTextView1.getText().toString());
     83         assertEquals(mergedView2, mBinder.mergedTextView2.getText().toString());
     84         assertEquals(rawMerge, mBinder.rawStringMerge.getText().toString());
     85     }
     86 }
     87