1dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar/*
2dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
3dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
4dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * you may not use this file except in compliance with the License.
5dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * You may obtain a copy of the License at
6dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
7dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * Unless required by applicable law or agreed to in writing, software
8dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
9dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * See the License for the specific language governing permissions and
11dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar * limitations under the License.
12dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar */
13dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
14fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.testapp;
15dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
162e2043d30f95585de2069e1cbb23e022ab0e71c4George Mountimport android.databinding.testapp.databinding.BasicDependantBindingBinding;
17fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.testapp.vo.NotBindableVo;
18dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
19dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyarimport android.test.UiThreadTest;
20dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
210fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyarimport java.util.ArrayList;
220fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyarimport java.util.List;
230fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar
244c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mountpublic class BasicDependantBindingTest extends BaseDataBinderTest<BasicDependantBindingBinding> {
25dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
26dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    public BasicDependantBindingTest() {
274c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mount        super(BasicDependantBindingBinding.class);
28dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    }
29dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
303f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar    @Override
313f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar    protected void setUp() throws Exception {
323f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar        super.setUp();
333f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar        initBinder();
343f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar    }
353f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar
360fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar    public List<NotBindableVo> permutations(String value) {
370fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        List<NotBindableVo> result = new ArrayList<>();
380fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        result.add(null);
390fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        result.add(new NotBindableVo(null));
400fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        result.add(new NotBindableVo(value));
410fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        return result;
420fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar    }
430fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar
440fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar    @UiThreadTest
450fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar    public void testAllPermutations() {
460fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        List<NotBindableVo> obj1s = permutations("a");
470fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        List<NotBindableVo> obj2s = permutations("b");
480fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        for (NotBindableVo obj1 : obj1s) {
490fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar            for (NotBindableVo obj2 : obj2s) {
503f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                reCreateBinder(null); //get a new one
510fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar                testWith(obj1, obj2);
523f73e3149732132c02f5d19aab7b5da429794058Yigit Boyar                reCreateBinder(null);
534c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mount                mBinder.executePendingBindings();
540fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar                testWith(obj1, obj2);
550fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar            }
560fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar        }
57dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    }
58dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
59dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    private void testWith(NotBindableVo obj1, NotBindableVo obj2) {
60dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        mBinder.setObj1(obj1);
61dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        mBinder.setObj2(obj2);
624c5cc009bcbcfb19e33fb19db5ec80f83f7b3326George Mount        mBinder.executePendingBindings();
63dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        assertValues(safeGet(obj1), safeGet(obj2),
64dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar                obj1 == null ? "" : obj1.mergeStringFields(obj2),
65dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar                obj2 == null ? "" : obj2.mergeStringFields(obj1),
660fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar                (obj1 == null ? null : obj1.getStringValue())
670fa158e8aa91297cc246e3bb9e5d1388dc2355ccYigit Boyar                        + (obj2 == null ? null : obj2.getStringValue())
68dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        );
69dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    }
70dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
71dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    private String safeGet(NotBindableVo vo) {
72dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        if (vo == null || vo.getStringValue() == null) {
73dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar            return "";
74dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        }
75dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar        return vo.getStringValue();
76dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    }
77dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar
78dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    private void assertValues(String textView1, String textView2,
79dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar            String mergedView1, String mergedView2, String rawMerge) {
8034a18e6a231f3b64726bd93e7e097a0d5a75995dGeorge Mount        assertEquals(textView1, mBinder.textView1.getText().toString());
8134a18e6a231f3b64726bd93e7e097a0d5a75995dGeorge Mount        assertEquals(textView2, mBinder.textView2.getText().toString());
8234a18e6a231f3b64726bd93e7e097a0d5a75995dGeorge Mount        assertEquals(mergedView1, mBinder.mergedTextView1.getText().toString());
8334a18e6a231f3b64726bd93e7e097a0d5a75995dGeorge Mount        assertEquals(mergedView2, mBinder.mergedTextView2.getText().toString());
8434a18e6a231f3b64726bd93e7e097a0d5a75995dGeorge Mount        assertEquals(rawMerge, mBinder.rawStringMerge.getText().toString());
85dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar    }
86dc69f49d687ec036947f26a9bf9025a305de0721Yigit Boyar}
87