| 1 |
/* |
| 2 |
* Copyright (C) 2015 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 |
package android.databinding.testapp; |
| 17 |
|
| 18 |
import android.databinding.testapp.databinding.ExpressionTestBinding; |
| 19 |
import android.test.UiThreadTest; |
| 20 |
|
| 21 |
public class ExpressionTest extends BaseDataBinderTest<ExpressionTestBinding> { |
| 22 |
public ExpressionTest() { |
| 23 |
super(ExpressionTestBinding.class); |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
protected void setUp() throws Exception { |
| 28 |
super.setUp(); |
| 29 |
initBinder(); |
| 30 |
} |
| 31 |
|
| 32 |
@UiThreadTest |
| 33 |
public void testOr() throws Throwable { |
| 34 |
// var1 == 0 || var2 == 0 ? "hello" : "world" |
| 35 |
mBinder.setVar1(0); |
| 36 |
mBinder.setVar2(0); |
| 37 |
mBinder.executePendingBindings(); |
| 38 |
assertEquals("hello", mBinder.textView0.getText().toString()); |
| 39 |
mBinder.setVar2(1); |
| 40 |
mBinder.executePendingBindings(); |
| 41 |
assertEquals("hello", mBinder.textView0.getText().toString()); |
| 42 |
mBinder.setVar1(1); |
| 43 |
mBinder.setVar2(0); |
| 44 |
mBinder.executePendingBindings(); |
| 45 |
assertEquals("hello", mBinder.textView0.getText().toString()); |
| 46 |
mBinder.setVar2(1); |
| 47 |
mBinder.executePendingBindings(); |
| 48 |
assertEquals("world", mBinder.textView0.getText().toString()); |
| 49 |
} |
| 50 |
|
| 51 |
@UiThreadTest |
| 52 |
public void testAnd() throws Throwable { |
| 53 |
// var1 == 0 && var2 == 0 ? "hello" : "world" |
| 54 |
mBinder.setVar1(0); |
| 55 |
mBinder.setVar2(0); |
| 56 |
mBinder.executePendingBindings(); |
| 57 |
assertEquals("hello", mBinder.textView1.getText().toString()); |
| 58 |
mBinder.setVar2(1); |
| 59 |
mBinder.executePendingBindings(); |
| 60 |
assertEquals("world", mBinder.textView1.getText().toString()); |
| 61 |
mBinder.setVar1(1); |
| 62 |
mBinder.setVar2(0); |
| 63 |
mBinder.executePendingBindings(); |
| 64 |
assertEquals("world", mBinder.textView1.getText().toString()); |
| 65 |
mBinder.setVar2(1); |
| 66 |
mBinder.executePendingBindings(); |
| 67 |
assertEquals("world", mBinder.textView1.getText().toString()); |
| 68 |
} |
| 69 |
|
| 70 |
@UiThreadTest |
| 71 |
public void testBinary() throws Throwable { |
| 72 |
mBinder.setVar1(0); |
| 73 |
mBinder.setVar2(0); |
| 74 |
mBinder.executePendingBindings(); |
| 75 |
assertEquals("0", mBinder.textView2.getText().toString()); // var1 & var2 |
| 76 |
assertEquals("0", mBinder.textView3.getText().toString()); // var1 | var2 |
| 77 |
assertEquals("0", mBinder.textView4.getText().toString()); // var1 ^ var2 |
| 78 |
mBinder.setVar2(1); |
| 79 |
mBinder.executePendingBindings(); |
| 80 |
assertEquals("0", mBinder.textView2.getText().toString()); // var1 & var2 |
| 81 |
assertEquals("1", mBinder.textView3.getText().toString()); // var1 | var2 |
| 82 |
assertEquals("1", mBinder.textView4.getText().toString()); // var1 ^ var2 |
| 83 |
mBinder.setVar1(1); |
| 84 |
mBinder.executePendingBindings(); |
| 85 |
assertEquals("1", mBinder.textView2.getText().toString()); // var1 & var2 |
| 86 |
assertEquals("1", mBinder.textView3.getText().toString()); // var1 | var2 |
| 87 |
assertEquals("0", mBinder.textView4.getText().toString()); // var1 ^ var2 |
| 88 |
} |
| 89 |
|
| 90 |
@UiThreadTest |
| 91 |
public void testComparison() throws Throwable { |
| 92 |
mBinder.setVar1(0); |
| 93 |
mBinder.setVar2(0); |
| 94 |
mBinder.executePendingBindings(); |
| 95 |
assertEquals("false", mBinder.textView5.getText().toString()); // < |
| 96 |
assertEquals("false", mBinder.textView6.getText().toString()); // > |
| 97 |
assertEquals("true", mBinder.textView7.getText().toString()); // <= |
| 98 |
assertEquals("true", mBinder.textView8.getText().toString()); // >= |
| 99 |
assertEquals("true", mBinder.textView9.getText().toString()); // == |
| 100 |
|
| 101 |
mBinder.setVar2(1); |
| 102 |
mBinder.executePendingBindings(); |
| 103 |
assertEquals("true", mBinder.textView5.getText().toString()); // < |
| 104 |
assertEquals("false", mBinder.textView6.getText().toString()); // > |
| 105 |
assertEquals("true", mBinder.textView7.getText().toString()); // <= |
| 106 |
assertEquals("false", mBinder.textView8.getText().toString()); // >= |
| 107 |
assertEquals("false", mBinder.textView9.getText().toString()); // == |
| 108 |
mBinder.setVar1(1); |
| 109 |
mBinder.setVar2(0); |
| 110 |
mBinder.executePendingBindings(); |
| 111 |
assertEquals("false", mBinder.textView5.getText().toString()); // < |
| 112 |
assertEquals("true", mBinder.textView6.getText().toString()); // > |
| 113 |
assertEquals("false", mBinder.textView7.getText().toString()); // <= |
| 114 |
assertEquals("true", mBinder.textView8.getText().toString()); // >= |
| 115 |
assertEquals("false", mBinder.textView9.getText().toString()); // == |
| 116 |
} |
| 117 |
|
| 118 |
@UiThreadTest |
| 119 |
public void testShift() throws Throwable { |
| 120 |
mBinder.setVar1(-2); |
| 121 |
mBinder.setVar2(1); |
| 122 |
mBinder.executePendingBindings(); |
| 123 |
int var1 = -2; |
| 124 |
int var2 = 1; |
| 125 |
assertEquals(String.valueOf(var1 << var2), mBinder.textView10.getText().toString()); |
| 126 |
assertEquals(String.valueOf(var1 >> var2), mBinder.textView11.getText().toString()); |
| 127 |
assertEquals(String.valueOf(var1 >>> var2), mBinder.textView12.getText().toString()); |
| 128 |
} |
| 129 |
|
| 130 |
@UiThreadTest |
| 131 |
public void testUnary() throws Throwable { |
| 132 |
mBinder.setVar1(2); |
| 133 |
mBinder.setVar2(1); |
| 134 |
mBinder.executePendingBindings(); |
| 135 |
int var1 = 2; |
| 136 |
int var2 = 1; |
| 137 |
assertEquals("1", mBinder.textView13.getText().toString()); // 2 + -1 |
| 138 |
assertEquals(String.valueOf(var1 + ~var2), mBinder.textView14.getText().toString()); // 2 + ~1 |
| 139 |
} |
| 140 |
@UiThreadTest |
| 141 |
public void testInstanceOf() throws Throwable { |
| 142 |
mBinder.executePendingBindings(); |
| 143 |
assertEquals("true", mBinder.textView15.getText().toString()); |
| 144 |
assertEquals("true", mBinder.textView16.getText().toString()); |
| 145 |
assertEquals("false", mBinder.textView17.getText().toString()); |
| 146 |
} |
| 147 |
|
| 148 |
@UiThreadTest |
| 149 |
public void testTernaryChain() throws Throwable { |
| 150 |
mBinder.setBool1(true); |
| 151 |
mBinder.setBool2(false); |
| 152 |
mBinder.executePendingBindings(); |
| 153 |
String appName = getActivity().getResources().getString(R.string.app_name); |
| 154 |
String rain = getActivity().getResources().getString(R.string.rain); |
| 155 |
assertEquals(mBinder.getBool1() ? appName : mBinder.getBool2() ? rain : "", |
| 156 |
mBinder.textView18.getText().toString()); |
| 157 |
} |
| 158 |
|
| 159 |
@UiThreadTest |
| 160 |
public void testBoundTag() throws Throwable { |
| 161 |
mBinder.setBool1(false); |
| 162 |
mBinder.executePendingBindings(); |
| 163 |
assertEquals("bar", mBinder.textView19.getTag()); |
| 164 |
mBinder.setBool1(true); |
| 165 |
mBinder.executePendingBindings(); |
| 166 |
assertEquals("foo", mBinder.textView19.getTag()); |
| 167 |
} |
| 168 |
|
| 169 |
@UiThreadTest |
| 170 |
public void testConstantExpression() throws Throwable { |
| 171 |
mBinder.setVar1(1000); |
| 172 |
mBinder.setVar2(2000); |
| 173 |
mBinder.executePendingBindings(); |
| 174 |
assertEquals("1000", mBinder.textView20.getText().toString()); |
| 175 |
assertEquals("2000", mBinder.textView21.getText().toString()); |
| 176 |
} |
| 177 |
} |