| 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.DataBindingUtil; |
| 17 |
import android.databinding.OnRebindCallback; |
| 18 |
import android.databinding.testapp.databinding.BasicBindingBinding; |
| 19 |
import android.databinding.testapp.databinding.NoExpressionsBinding; |
| 20 |
import android.databinding.testapp.databinding.NoDataElementBinding; |
| 21 |
import android.databinding.testapp.databinding.JustIdBinding; |
| 22 |
import android.test.UiThreadTest; |
| 23 |
import android.view.View; |
| 24 |
import android.view.ViewGroup; |
| 25 |
import android.widget.TextView; |
| 26 |
|
| 27 |
public class BasicBindingTest extends BaseDataBinderTest<BasicBindingBinding> { |
| 28 |
public BasicBindingTest() { |
| 29 |
super(BasicBindingBinding.class); |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void setUp() throws Exception { |
| 34 |
super.setUp(); |
| 35 |
initBinder(); |
| 36 |
} |
| 37 |
|
| 38 |
@UiThreadTest |
| 39 |
public void testTextViewContentInInitialization() { |
| 40 |
assertAB("X", "Y"); |
| 41 |
} |
| 42 |
|
| 43 |
@UiThreadTest |
| 44 |
public void testNullValuesInInitialization() { |
| 45 |
assertAB(null, null); |
| 46 |
} |
| 47 |
|
| 48 |
@UiThreadTest |
| 49 |
public void testSecondIsNullInInitialization() { |
| 50 |
assertAB(null, "y"); |
| 51 |
} |
| 52 |
|
| 53 |
@UiThreadTest |
| 54 |
public void testFirstIsNullInInitialization() { |
| 55 |
assertAB("x", null); |
| 56 |
} |
| 57 |
|
| 58 |
@UiThreadTest |
| 59 |
public void testTextViewContent() { |
| 60 |
assertAB("X", "Y"); |
| 61 |
} |
| 62 |
|
| 63 |
@UiThreadTest |
| 64 |
public void testNullValues() { |
| 65 |
assertAB(null, null); |
| 66 |
} |
| 67 |
|
| 68 |
@UiThreadTest |
| 69 |
public void testSecondIsNull() { |
| 70 |
assertAB(null, "y"); |
| 71 |
} |
| 72 |
|
| 73 |
@UiThreadTest |
| 74 |
public void testFirstIsNull() { |
| 75 |
assertAB("x", null); |
| 76 |
} |
| 77 |
|
| 78 |
public void testStopBinding() throws Throwable { |
| 79 |
final NoRebind noRebind = new NoRebind(); |
| 80 |
runTestOnUiThread(new Runnable() { |
| 81 |
@Override |
| 82 |
public void run() { |
| 83 |
assertAB("X", "Y"); |
| 84 |
mBinder.addOnRebindCallback(noRebind); |
| 85 |
} |
| 86 |
}); |
| 87 |
mBinder.setA("Q"); |
| 88 |
WaitForRun waitForRun = new WaitForRun(); |
| 89 |
View root = mBinder.getRoot(); |
| 90 |
root.postOnAnimation(waitForRun); |
| 91 |
waitForRun.waitForRun(); |
| 92 |
runTestOnUiThread(new Runnable() { |
| 93 |
@Override |
| 94 |
public void run() { |
| 95 |
assertEquals(1, noRebind.rebindAttempts); |
| 96 |
assertEquals(1, noRebind.rebindHalted); |
| 97 |
assertEquals(0, noRebind.rebindWillEvaluate); |
| 98 |
assertEquals("XY", mBinder.textView.getText().toString()); |
| 99 |
} |
| 100 |
}); |
| 101 |
mBinder.removeOnRebindCallback(noRebind); |
| 102 |
final AllowRebind allowRebind = new AllowRebind(); |
| 103 |
mBinder.addOnRebindCallback(allowRebind); |
| 104 |
mBinder.setB("R"); |
| 105 |
root.postOnAnimation(waitForRun); |
| 106 |
waitForRun.waitForRun(); |
| 107 |
runTestOnUiThread(new Runnable() { |
| 108 |
@Override |
| 109 |
public void run() { |
| 110 |
assertEquals(1, noRebind.rebindAttempts); |
| 111 |
assertEquals(1, noRebind.rebindHalted); |
| 112 |
assertEquals(0, noRebind.rebindWillEvaluate); |
| 113 |
assertEquals(1, allowRebind.rebindAttempts); |
| 114 |
assertEquals(0, allowRebind.rebindHalted); |
| 115 |
assertEquals(1, allowRebind.rebindWillEvaluate); |
| 116 |
assertEquals("QR", mBinder.textView.getText().toString()); |
| 117 |
} |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
@UiThreadTest |
| 122 |
public void testNoExpressionBinding() throws Throwable { |
| 123 |
NoExpressionsBinding binding = NoExpressionsBinding.inflate(getActivity().getLayoutInflater()); |
| 124 |
assertNotNull(binding); |
| 125 |
} |
| 126 |
|
| 127 |
@UiThreadTest |
| 128 |
public void testNoDataElement() throws Throwable { |
| 129 |
NoDataElementBinding binding = NoDataElementBinding.inflate(getActivity().getLayoutInflater()); |
| 130 |
assertNotNull(binding); |
| 131 |
} |
| 132 |
|
| 133 |
@UiThreadTest |
| 134 |
public void testJustIds() throws Throwable { |
| 135 |
JustIdBinding binding = JustIdBinding.inflate(getActivity().getLayoutInflater()); |
| 136 |
assertNotNull(binding); |
| 137 |
assertNotNull(binding.textView); |
| 138 |
} |
| 139 |
|
| 140 |
@UiThreadTest |
| 141 |
public void testNoBinding() throws Throwable { |
| 142 |
assertNull(DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.plain_layout, |
| 143 |
null, false)); |
| 144 |
} |
| 145 |
|
| 146 |
@UiThreadTest |
| 147 |
public void testInflation() throws Throwable { |
| 148 |
ViewGroup viewGroup = (ViewGroup) mBinder.getRoot(); |
| 149 |
BasicBindingBinding binding = |
| 150 |
BasicBindingBinding.inflate(getActivity().getLayoutInflater(), viewGroup, true); |
| 151 |
assertNotNull(binding); |
| 152 |
assertNotNull(binding.textView); |
| 153 |
assertNotSame(binding.textView, mBinder.textView); |
| 154 |
} |
| 155 |
|
| 156 |
@UiThreadTest |
| 157 |
public void testAndroidId() throws Throwable { |
| 158 |
JustIdBinding binding = JustIdBinding.inflate(getActivity().getLayoutInflater()); |
| 159 |
assertNotNull(binding); |
| 160 |
assertNotNull(binding.empty); |
| 161 |
assertTrue(binding.empty instanceof TextView); |
| 162 |
} |
| 163 |
|
| 164 |
private void assertAB(String a, String b) { |
| 165 |
mBinder.setA(a); |
| 166 |
mBinder.setB(b); |
| 167 |
rebindAndAssert(a + b); |
| 168 |
} |
| 169 |
|
| 170 |
private void rebindAndAssert(String text) { |
| 171 |
mBinder.executePendingBindings(); |
| 172 |
assertEquals(text, mBinder.textView.getText().toString()); |
| 173 |
} |
| 174 |
|
| 175 |
private class AllowRebind extends OnRebindCallback<BasicBindingBinding> { |
| 176 |
public int rebindAttempts; |
| 177 |
public int rebindHalted; |
| 178 |
public int rebindWillEvaluate; |
| 179 |
|
| 180 |
@Override |
| 181 |
public boolean onPreBind(BasicBindingBinding binding) { |
| 182 |
rebindAttempts++; |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public void onCanceled(BasicBindingBinding binding) { |
| 188 |
rebindHalted++; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void onBound(BasicBindingBinding binding) { |
| 193 |
rebindWillEvaluate++; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
private class NoRebind extends AllowRebind { |
| 198 |
@Override |
| 199 |
public boolean onPreBind(BasicBindingBinding binding) { |
| 200 |
super.onPreBind(binding); |
| 201 |
return false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
private static class WaitForRun implements Runnable { |
| 206 |
|
| 207 |
@Override |
| 208 |
public void run() { |
| 209 |
synchronized (this) { |
| 210 |
this.notifyAll(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
public void waitForRun() { |
| 215 |
synchronized (this) { |
| 216 |
try { |
| 217 |
this.wait(1000); |
| 218 |
} catch (InterruptedException e) { |
| 219 |
e.printStackTrace(); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |