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 17 package com.android.messaging.datamodel; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 21 import com.android.messaging.BugleTestCase; 22 import com.android.messaging.datamodel.binding.BindableData; 23 import com.android.messaging.datamodel.binding.Binding; 24 import com.android.messaging.datamodel.binding.BindingBase; 25 import com.android.messaging.datamodel.binding.ImmutableBindingRef; 26 27 /** 28 * Test binding 29 */ 30 @SmallTest 31 public class BindingTest extends BugleTestCase { 32 private static final Object TEST_DATA_ID = "myDataId"; 33 private static final Object YOUR_DATA_ID = "yourDataId"; 34 testBindingStartsUnbound()35 public void testBindingStartsUnbound() { 36 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 37 assertNull(binding.getBindingId()); 38 } 39 testDataStartsUnbound()40 public void testDataStartsUnbound() { 41 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 42 assertFalse(data.isBound()); 43 } 44 testBindingUpdatesDataAndBindee()45 public void testBindingUpdatesDataAndBindee() { 46 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 47 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 48 binding.bind(data); 49 assertTrue(binding.isBound()); 50 assertEquals(binding.getData(), data); 51 assertTrue(data.isBound(binding.getBindingId())); 52 assertFalse(data.isBound("SomeRandomString")); 53 assertNotNull(binding.getBindingId()); 54 assertFalse(data.mListenersUnregistered); 55 } 56 testRebindingFails()57 public void testRebindingFails() { 58 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 59 final TestBindableData yours = new TestBindableData(YOUR_DATA_ID); 60 binding.bind(yours); 61 assertEquals(binding.getData(), yours); 62 assertTrue(yours.isBound(binding.getBindingId())); 63 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 64 try { 65 binding.bind(data); 66 fail(); 67 } catch (final IllegalStateException e) { 68 } 69 assertTrue(binding.isBound()); 70 assertEquals(binding.getData(), yours); 71 assertTrue(yours.isBound(binding.getBindingId())); 72 } 73 testUnbindingClearsDataAndBindee()74 public void testUnbindingClearsDataAndBindee() { 75 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 76 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 77 binding.bind(data); 78 assertTrue(data.isBound(binding.getBindingId())); 79 assertTrue(binding.isBound()); 80 binding.unbind(); 81 try { 82 final TestBindableData other = binding.getData(); 83 fail(); 84 } catch (final IllegalStateException e) { 85 } 86 assertFalse(data.isBound()); 87 assertNull(binding.getBindingId()); 88 assertTrue(data.mListenersUnregistered); 89 } 90 testUnbindingAndRebinding()91 public void testUnbindingAndRebinding() { 92 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 93 final TestBindableData yours = new TestBindableData(YOUR_DATA_ID); 94 binding.bind(yours); 95 assertEquals(binding.getData(), yours); 96 assertTrue(yours.isBound(binding.getBindingId())); 97 binding.unbind(); 98 assertFalse(yours.isBound()); 99 assertNull(binding.getBindingId()); 100 101 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 102 binding.bind(data); 103 assertEquals(binding.getData(), data); 104 assertTrue(data.isBound(binding.getBindingId())); 105 assertFalse(data.isBound("SomeRandomString")); 106 assertTrue(binding.isBound()); 107 assertNotNull(binding.getBindingId()); 108 } 109 testBindingReference()110 public void testBindingReference() { 111 final Binding<TestBindableData> binding = BindingBase.createBinding(this); 112 final TestBindableData data = new TestBindableData(TEST_DATA_ID); 113 binding.bind(data); 114 assertEquals(binding.getData(), data); 115 assertTrue(data.isBound(binding.getBindingId())); 116 117 final ImmutableBindingRef<TestBindableData> bindingRef = 118 BindingBase.createBindingReference(binding); 119 assertEquals(bindingRef.getData(), data); 120 assertTrue(data.isBound(bindingRef.getBindingId())); 121 122 binding.unbind(); 123 assertFalse(binding.isBound()); 124 assertNull(binding.getBindingId()); 125 assertFalse(bindingRef.isBound()); 126 assertNull(bindingRef.getBindingId()); 127 } 128 129 static class TestBindableData extends BindableData { 130 private final Object mDataId; 131 public boolean mListenersUnregistered; 132 TestBindableData(final Object dataId)133 public TestBindableData(final Object dataId) { 134 mDataId = dataId; 135 mListenersUnregistered = false; 136 } 137 138 @Override unregisterListeners()139 public void unregisterListeners() { 140 mListenersUnregistered = true; 141 } 142 143 @Override isBound()144 public boolean isBound() { 145 return super.isBound(); 146 } 147 } 148 } 149