1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 17 package android.databinding.testapp; 18 19 import android.databinding.ViewStubProxy; 20 import android.databinding.testapp.databinding.ListenersWithDotBinding; 21 import android.databinding.testapp.vo.ListenerBindingObject; 22 import android.databinding.testapp.vo.ListenerBindingObject.Inner; 23 import android.test.UiThreadTest; 24 import android.view.View; 25 26 public class ListenerWithDotTest extends BaseDataBinderTest<ListenersWithDotBinding> { 27 private ListenerBindingObject mBindingObject; 28 29 public ListenerWithDotTest() { 30 super(ListenersWithDotBinding.class); 31 } 32 33 @Override 34 protected void setUp() throws Exception { 35 mBindingObject = new ListenerBindingObject(getActivity()); 36 super.setUp(); 37 initBinder(new Runnable() { 38 @Override 39 public void run() { 40 mBinder.setObj(mBindingObject); 41 } 42 }); 43 ListenerBindingObject.lastClick = 0; 44 } 45 46 @UiThreadTest 47 public void testInstanceClick() throws Throwable { 48 View view = mBinder.click1; 49 assertEquals(0, ListenerBindingObject.lastClick); 50 view.callOnClick(); 51 assertEquals(1, ListenerBindingObject.lastClick); 52 } 53 54 @UiThreadTest 55 public void testStaticClick() throws Throwable { 56 View view = mBinder.click2; 57 assertEquals(0, ListenerBindingObject.lastClick); 58 view.callOnClick(); 59 assertEquals(2, ListenerBindingObject.lastClick); 60 } 61 62 @UiThreadTest 63 public void testInstanceClickTwoArgs() throws Throwable { 64 View view = mBinder.click3; 65 assertEquals(0, ListenerBindingObject.lastClick); 66 view.callOnClick(); 67 assertEquals(3, ListenerBindingObject.lastClick); 68 assertTrue(view.isClickable()); 69 ListenerBindingObject.lastClick = 0; 70 mBindingObject.clickable.set(false); 71 mBinder.executePendingBindings(); 72 assertFalse(view.isClickable()); 73 mBindingObject.useOne.set(true); 74 mBinder.executePendingBindings(); 75 assertFalse(view.isClickable()); 76 mBindingObject.clickable.set(true); 77 mBinder.executePendingBindings(); 78 view.callOnClick(); 79 assertEquals(1, ListenerBindingObject.lastClick); 80 } 81 82 @UiThreadTest 83 public void testStaticClickTwoArgs() throws Throwable { 84 View view = mBinder.click4; 85 assertEquals(0, ListenerBindingObject.lastClick); 86 view.callOnClick(); 87 assertEquals(4, ListenerBindingObject.lastClick); 88 assertTrue(view.isClickable()); 89 ListenerBindingObject.lastClick = 0; 90 mBindingObject.clickable.set(false); 91 mBinder.executePendingBindings(); 92 assertFalse(view.isClickable()); 93 view.setClickable(true); 94 view.callOnClick(); 95 assertEquals(4, ListenerBindingObject.lastClick); 96 } 97 98 @UiThreadTest 99 public void testClickExpression() throws Throwable { 100 View view = mBinder.click5; 101 assertEquals(0, ListenerBindingObject.lastClick); 102 view.callOnClick(); 103 assertEquals(2, ListenerBindingObject.lastClick); 104 ListenerBindingObject.lastClick = 0; 105 mBindingObject.useOne.set(true); 106 mBinder.executePendingBindings(); 107 view.callOnClick(); 108 assertEquals(1, ListenerBindingObject.lastClick); 109 } 110 111 @UiThreadTest 112 public void testInflateListener() throws Throwable { 113 ViewStubProxy viewStubProxy = mBinder.viewStub; 114 assertFalse(viewStubProxy.isInflated()); 115 assertFalse(mBindingObject.inflateCalled); 116 viewStubProxy.getViewStub().inflate(); 117 assertTrue(mBindingObject.inflateCalled); 118 assertTrue(viewStubProxy.isInflated()); 119 } 120 121 @UiThreadTest 122 public void testBaseObservableClick() throws Throwable { 123 View view = mBinder.click6; 124 Inner inner = new Inner(); 125 mBinder.setObj2(inner); 126 mBinder.executePendingBindings(); 127 assertFalse(inner.clicked); 128 view.callOnClick(); 129 assertTrue(inner.clicked); 130 } 131 } 132