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.DataBinderTrojan; 17 import android.databinding.testapp.databinding.NewApiLayoutBinding; 18 19 import android.os.Build; 20 import android.test.UiThreadTest; 21 import android.view.View; 22 import android.widget.TextView; 23 24 import java.util.ArrayList; 25 26 public class NewApiTest extends BaseDataBinderTest<NewApiLayoutBinding> { 27 public NewApiTest() { 28 super(NewApiLayoutBinding.class); 29 } 30 31 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 } 35 36 @UiThreadTest 37 public void testSetElevation() { 38 initBinder(); 39 mBinder.setElevation(3); 40 mBinder.setName("foo"); 41 mBinder.setChildren(new ArrayList<View>()); 42 mBinder.executePendingBindings(); 43 assertEquals("foo", mBinder.textView.getText().toString()); 44 assertEquals(3f, mBinder.textView.getElevation()); 45 } 46 47 @UiThreadTest 48 public void testSetElevationOlderAPI() { 49 initBinder(); 50 DataBinderTrojan.setBuildSdkInt(1); 51 try { 52 TextView textView = mBinder.textView; 53 float originalElevation = textView.getElevation(); 54 mBinder.setElevation(3); 55 mBinder.setName("foo2"); 56 mBinder.executePendingBindings(); 57 assertEquals("foo2", textView.getText().toString()); 58 assertEquals(originalElevation, textView.getElevation()); 59 } finally { 60 DataBinderTrojan.setBuildSdkInt(Build.VERSION.SDK_INT); 61 } 62 } 63 64 @UiThreadTest 65 public void testGeneric() { 66 initBinder(); 67 ArrayList<View> views = new ArrayList<>(); 68 mBinder.setChildren(views); 69 mBinder.executePendingBindings(); 70 assertEquals(1, views.size()); 71 assertSame(mBinder.textView, views.get(0)); 72 } 73 74 @UiThreadTest 75 public void testGenericOlderApi() { 76 initBinder(); 77 DataBinderTrojan.setBuildSdkInt(1); 78 try { 79 ArrayList<View> views = new ArrayList<>(); 80 mBinder.setChildren(views); 81 mBinder.executePendingBindings(); 82 // we should not call the api on older platforms. 83 assertEquals(0, views.size()); 84 } finally { 85 DataBinderTrojan.setBuildSdkInt(Build.VERSION.SDK_INT); 86 } 87 } 88 } 89