| 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.DataBindingComponent; |
| 19 |
import android.databinding.DataBindingUtil; |
| 20 |
import android.databinding.testapp.adapter.InstanceAdapter; |
| 21 |
import android.databinding.testapp.adapter.NameClashAdapter; |
| 22 |
import android.databinding.testapp.adapter.NameClashAdapter.MyAdapter; |
| 23 |
import android.databinding.testapp.databinding.IncludeInstanceAdapterBinding; |
| 24 |
import android.databinding.testapp.databinding.InstanceAdapterBinding; |
| 25 |
import android.test.UiThreadTest; |
| 26 |
import android.widget.TextView; |
| 27 |
|
| 28 |
public class InstanceAdapterTest extends BaseDataBinderTest<InstanceAdapterBinding> { |
| 29 |
public InstanceAdapterTest() { |
| 30 |
super(InstanceAdapterBinding.class); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void setUp() throws Exception { |
| 35 |
super.setUp(); |
| 36 |
} |
| 37 |
|
| 38 |
private void initNormal() { |
| 39 |
DataBindingUtil.setDefaultComponent(new TestComponent() { |
| 40 |
private InstanceAdapter mInstanceAdapter = new InstanceAdapter("Hello %s %s %s %s"); |
| 41 |
|
| 42 |
@Override |
| 43 |
public MyAdapter getMyAdapter1() { |
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public android.databinding.testapp.adapter2.NameClashAdapter.MyAdapter getMyAdapter2() { |
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public NameClashAdapter getNameClashAdapter1() { |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public android.databinding.testapp.adapter2.NameClashAdapter getNameClashAdapter2() { |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public InstanceAdapter getInstanceAdapter() { |
| 64 |
return mInstanceAdapter; |
| 65 |
} |
| 66 |
}); |
| 67 |
initBinder(); |
| 68 |
mBinder.executePendingBindings(); |
| 69 |
} |
| 70 |
|
| 71 |
@UiThreadTest |
| 72 |
public void testOneAttr() throws Throwable { |
| 73 |
initNormal(); |
| 74 |
mBinder.setStr("World"); |
| 75 |
mBinder.executePendingBindings(); |
| 76 |
assertEquals("Hello World foo bar baz", mBinder.textView1.getText().toString()); |
| 77 |
} |
| 78 |
|
| 79 |
@UiThreadTest |
| 80 |
public void testTwoAttr() throws Throwable { |
| 81 |
initNormal(); |
| 82 |
mBinder.setStr("World"); |
| 83 |
mBinder.executePendingBindings(); |
| 84 |
assertEquals("Hello World baz foo bar", mBinder.textView2.getText().toString()); |
| 85 |
} |
| 86 |
|
| 87 |
@UiThreadTest |
| 88 |
public void testOneAttrOld() throws Throwable { |
| 89 |
initNormal(); |
| 90 |
mBinder.setStr("World"); |
| 91 |
mBinder.executePendingBindings(); |
| 92 |
assertEquals("Hello null World foo bar", mBinder.textView3.getText().toString()); |
| 93 |
mBinder.setStr("Android"); |
| 94 |
mBinder.executePendingBindings(); |
| 95 |
assertEquals("Hello World Android foo bar", mBinder.textView3.getText().toString()); |
| 96 |
} |
| 97 |
|
| 98 |
@UiThreadTest |
| 99 |
public void testTwoAttrOld() throws Throwable { |
| 100 |
initNormal(); |
| 101 |
mBinder.setStr("World"); |
| 102 |
mBinder.executePendingBindings(); |
| 103 |
assertEquals("Hello null baz World baz", mBinder.textView4.getText().toString()); |
| 104 |
mBinder.setStr("Android"); |
| 105 |
mBinder.executePendingBindings(); |
| 106 |
assertEquals("Hello World baz Android baz", mBinder.textView4.getText().toString()); |
| 107 |
} |
| 108 |
|
| 109 |
@UiThreadTest |
| 110 |
public void testRequiredBinding() throws Throwable { |
| 111 |
try { |
| 112 |
InstanceAdapterBinding.inflate(getActivity().getLayoutInflater(), null); |
| 113 |
fail("Binding should fail if a required BindingAdapter is missing."); |
| 114 |
} catch (IllegalStateException e) { |
| 115 |
// Expected exception |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@UiThreadTest |
| 120 |
public void testInclude() throws Throwable { |
| 121 |
initNormal(); |
| 122 |
DataBindingComponent component = DataBindingUtil.getDefaultComponent(); |
| 123 |
DataBindingUtil.setDefaultComponent(null); |
| 124 |
IncludeInstanceAdapterBinding binding = IncludeInstanceAdapterBinding.inflate(getActivity().getLayoutInflater(), component); |
| 125 |
binding.setStr("World"); |
| 126 |
binding.executePendingBindings(); |
| 127 |
assertEquals("Hello World foo bar baz", binding.includedLayout.textView1.getText().toString()); |
| 128 |
} |
| 129 |
|
| 130 |
@UiThreadTest |
| 131 |
public void testViewStub() throws Throwable { |
| 132 |
initNormal(); |
| 133 |
DataBindingComponent component = DataBindingUtil.getDefaultComponent(); |
| 134 |
DataBindingUtil.setDefaultComponent(null); |
| 135 |
IncludeInstanceAdapterBinding binding = DataBindingUtil.setContentView(getActivity(), |
| 136 |
R.layout.include_instance_adapter, component); |
| 137 |
binding.setStr("World"); |
| 138 |
binding.executePendingBindings(); |
| 139 |
binding.viewStub.getViewStub().inflate(); |
| 140 |
TextView view = (TextView) binding.viewStub.getRoot().findViewById(R.id.textView1); |
| 141 |
assertEquals("Hello World foo bar baz", view.getText().toString()); |
| 142 |
} |
| 143 |
|
| 144 |
@UiThreadTest |
| 145 |
public void testOneAttrWithComponentStatic() throws Throwable { |
| 146 |
initNormal(); |
| 147 |
mBinder.setStr("World"); |
| 148 |
mBinder.executePendingBindings(); |
| 149 |
assertEquals("World component", mBinder.textView6.getText().toString()); |
| 150 |
} |
| 151 |
|
| 152 |
@UiThreadTest |
| 153 |
public void testOneAttrWithComponentInstance() throws Throwable { |
| 154 |
initNormal(); |
| 155 |
mBinder.setStr("World"); |
| 156 |
mBinder.executePendingBindings(); |
| 157 |
assertEquals("Hello World component bar baz", mBinder.textView7.getText().toString()); |
| 158 |
} |
| 159 |
|
| 160 |
@UiThreadTest |
| 161 |
public void testTwoAttrsWithComponentInstance() throws Throwable { |
| 162 |
initNormal(); |
| 163 |
mBinder.setStr("World"); |
| 164 |
mBinder.executePendingBindings(); |
| 165 |
assertEquals("Hello World foo component bar", mBinder.textView8.getText().toString()); |
| 166 |
} |
| 167 |
} |