1ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount/* 2ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * Copyright (C) 2015 The Android Open Source Project 3ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License"); 4ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * you may not use this file except in compliance with the License. 5ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * You may obtain a copy of the License at 6ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * http://www.apache.org/licenses/LICENSE-2.0 7ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * Unless required by applicable law or agreed to in writing, software 8ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS, 9ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * See the License for the specific language governing permissions and 11ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount * limitations under the License. 12ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount */ 13ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 14ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountpackage android.databinding.testapp; 15ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 16ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.databinding.DataBindingUtil; 17ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.databinding.testapp.databinding.BasicBindingBinding; 186a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mountimport android.databinding.testapp.databinding.CenteredContentBinding; 196a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mountimport android.databinding.testapp.databinding.MergeLayoutBinding; 20ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.test.ActivityInstrumentationTestCase2; 21ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.test.UiThreadTest; 226a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mountimport android.view.InflateException; 23ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.view.View; 24ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.view.ViewGroup; 25ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountimport android.view.ViewGroup.LayoutParams; 26ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 27ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mountpublic class DataBindingUtilTest 28ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount extends ActivityInstrumentationTestCase2<TestActivity> { 29ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 30ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount public DataBindingUtilTest() { 31ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount super(TestActivity.class); 32ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount } 33ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 34ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount @UiThreadTest 35ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount public void testFindBinding() throws Throwable { 36ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount BasicBindingBinding binding = BasicBindingBinding.inflate(getActivity().getLayoutInflater()); 37ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertEquals(binding, DataBindingUtil.findBinding(binding.textView)); 38ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertEquals(binding, DataBindingUtil.findBinding(binding.getRoot())); 39ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount ViewGroup root = (ViewGroup) binding.getRoot(); 40ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount getActivity().getLayoutInflater().inflate(R.layout.basic_binding, root, true); 41ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount View inflated = root.getChildAt(1); 42ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertNull(DataBindingUtil.findBinding(inflated)); 43ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount BasicBindingBinding innerBinding = DataBindingUtil.bind(inflated); 44ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertEquals(innerBinding, DataBindingUtil.findBinding(inflated)); 45ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertEquals(innerBinding, DataBindingUtil.findBinding(innerBinding.textView)); 46ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount } 47ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount 48ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount @UiThreadTest 49ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount public void testGetBinding() throws Throwable { 50ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount BasicBindingBinding binding = BasicBindingBinding.inflate(getActivity().getLayoutInflater()); 51ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertNull(DataBindingUtil.getBinding(binding.textView)); 52ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount assertEquals(binding, DataBindingUtil.getBinding(binding.getRoot())); 53ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount } 546a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount 556a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount @UiThreadTest 566a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount public void testSetContentView() throws Throwable { 576a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount CenteredContentBinding binding = DataBindingUtil.setContentView(getActivity(), 586a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.centered_content); 596a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(binding); 606a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount LayoutParams layoutParams = binding.getRoot().getLayoutParams(); 616a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertEquals(LayoutParams.WRAP_CONTENT, layoutParams.width); 626a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertEquals(LayoutParams.WRAP_CONTENT, layoutParams.height); 636a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount } 646a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount 656a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount @UiThreadTest 666a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount public void testBind() throws Throwable { 676a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount getActivity().setContentView(R.layout.basic_binding); 686a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount ViewGroup content = (ViewGroup) getActivity().findViewById(android.R.id.content); 696a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertEquals(1, content.getChildCount()); 706a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount View view = content.getChildAt(0); 716a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount BasicBindingBinding binding = DataBindingUtil.bind(view); 726a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(binding); 736a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertEquals(binding, DataBindingUtil.<BasicBindingBinding>bind(view)); 746a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount } 756a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount 766a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount @UiThreadTest 776a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount public void testInflate() throws Throwable { 786a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount getActivity().getWindow().getDecorView(); // force a content to exist. 796a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount ViewGroup content = (ViewGroup) getActivity().findViewById(android.R.id.content); 806a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount BasicBindingBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), 816a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.basic_binding, content, false); 826a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(binding); 836a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(binding.getRoot().getLayoutParams()); 846a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), 856a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.basic_binding, null, false); 866a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(binding); 876a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNull(binding.getRoot().getLayoutParams()); 886a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount 896a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNull(DataBindingUtil.inflate(getActivity().getLayoutInflater(), 906a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.plain_layout, null, false)); 916a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount MergeLayoutBinding mergeBinding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), 926a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.merge_layout, content, true); 936a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(mergeBinding); 946a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(mergeBinding.innerTextView1); 956a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount assertNotNull(mergeBinding.innerTextView2); 966a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount 976a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount try { 986a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount DataBindingUtil.inflate(getActivity().getLayoutInflater(), 996a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount R.layout.merge_layout, content, false); 1006a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount fail("Inflating a merge layout without a root should fail"); 1016a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount } catch (InflateException e) { 1026a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount // You can't inflate a merge layout without a root. 1036a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount } 1046a5e8f49ed279b7ff0fe8097010ba985bf5c5ed6George Mount } 105ed6428586a939e00d9e66314d5cf1056ad48767eGeorge Mount} 106