Debian Codegraph @

integration-tests/TestApp/app/src/androidTestApi7/java/android/databinding/testapp/DataBindingUtilTest.java

raw
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.testapp.databinding.BasicBindingBinding;
18 import android.databinding.testapp.databinding.CenteredContentBinding;
19 import android.databinding.testapp.databinding.MergeLayoutBinding;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.test.UiThreadTest;
22 import android.view.InflateException;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.ViewGroup.LayoutParams;
26
27 public class DataBindingUtilTest
28 extends ActivityInstrumentationTestCase2<TestActivity> {
29
30 public DataBindingUtilTest() {
31 super(TestActivity.class);
32 }
33
34 @UiThreadTest
35 public void testFindBinding() throws Throwable {
36 BasicBindingBinding binding = BasicBindingBinding.inflate(getActivity().getLayoutInflater());
37 assertEquals(binding, DataBindingUtil.findBinding(binding.textView));
38 assertEquals(binding, DataBindingUtil.findBinding(binding.getRoot()));
39 ViewGroup root = (ViewGroup) binding.getRoot();
40 getActivity().getLayoutInflater().inflate(R.layout.basic_binding, root, true);
41 View inflated = root.getChildAt(1);
42 assertNull(DataBindingUtil.findBinding(inflated));
43 BasicBindingBinding innerBinding = DataBindingUtil.bind(inflated);
44 assertEquals(innerBinding, DataBindingUtil.findBinding(inflated));
45 assertEquals(innerBinding, DataBindingUtil.findBinding(innerBinding.textView));
46 }
47
48 @UiThreadTest
49 public void testGetBinding() throws Throwable {
50 BasicBindingBinding binding = BasicBindingBinding.inflate(getActivity().getLayoutInflater());
51 assertNull(DataBindingUtil.getBinding(binding.textView));
52 assertEquals(binding, DataBindingUtil.getBinding(binding.getRoot()));
53 }
54
55 @UiThreadTest
56 public void testSetContentView() throws Throwable {
57 CenteredContentBinding binding = DataBindingUtil.setContentView(getActivity(),
58 R.layout.centered_content);
59 assertNotNull(binding);
60 LayoutParams layoutParams = binding.getRoot().getLayoutParams();
61 assertEquals(LayoutParams.WRAP_CONTENT, layoutParams.width);
62 assertEquals(LayoutParams.WRAP_CONTENT, layoutParams.height);
63 }
64
65 @UiThreadTest
66 public void testBind() throws Throwable {
67 getActivity().setContentView(R.layout.basic_binding);
68 ViewGroup content = (ViewGroup) getActivity().findViewById(android.R.id.content);
69 assertEquals(1, content.getChildCount());
70 View view = content.getChildAt(0);
71 BasicBindingBinding binding = DataBindingUtil.bind(view);
72 assertNotNull(binding);
73 assertEquals(binding, DataBindingUtil.<BasicBindingBinding>bind(view));
74 }
75
76 @UiThreadTest
77 public void testInflate() throws Throwable {
78 getActivity().getWindow().getDecorView(); // force a content to exist.
79 ViewGroup content = (ViewGroup) getActivity().findViewById(android.R.id.content);
80 BasicBindingBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(),
81 R.layout.basic_binding, content, false);
82 assertNotNull(binding);
83 assertNotNull(binding.getRoot().getLayoutParams());
84 binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(),
85 R.layout.basic_binding, null, false);
86 assertNotNull(binding);
87 assertNull(binding.getRoot().getLayoutParams());
88
89 assertNull(DataBindingUtil.inflate(getActivity().getLayoutInflater(),
90 R.layout.plain_layout, null, false));
91 MergeLayoutBinding mergeBinding = DataBindingUtil.inflate(getActivity().getLayoutInflater(),
92 R.layout.merge_layout, content, true);
93 assertNotNull(mergeBinding);
94 assertNotNull(mergeBinding.innerTextView1);
95 assertNotNull(mergeBinding.innerTextView2);
96
97 try {
98 DataBindingUtil.inflate(getActivity().getLayoutInflater(),
99 R.layout.merge_layout, content, false);
100 fail("Inflating a merge layout without a root should fail");
101 } catch (InflateException e) {
102 // You can't inflate a merge layout without a root.
103 }
104 }
105 }