1 /* 2 * Copyright 2024 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 androidx.pdf.util; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assert.fail; 23 24 import androidx.pdf.util.ObservableArray.ArrayObserver; 25 import androidx.pdf.util.Observables.ExposedArray; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.RobolectricTestRunner; 31 32 @RunWith(RobolectricTestRunner.class) 33 public class ExposedArrayTest { 34 35 private ExposedArray<String> mArray; 36 37 @Before setUp()38 public void setUp() { 39 mArray = Observables.newExposedArray(); 40 } 41 42 @Test testIteration()43 public void testIteration() { 44 mArray.set(9, "September"); 45 mArray.set(10, "October"); 46 mArray.set(11, "November"); 47 mArray.set(12, "December"); 48 49 int count = 0; 50 for (int i : observable().keys()) { 51 assertThat(observable().get(i)).isNotNull(); 52 count++; 53 } 54 assertWithMessage("Wrong number of items in iteration, count = " + count) 55 .that(count) 56 .isEqualTo(4); 57 } 58 59 @Test testIterationWithNulls()60 public void testIterationWithNulls() { 61 mArray.set(9, "September"); 62 mArray.set(10, null); 63 mArray.set(11, "November"); 64 mArray.set(12, "December"); 65 66 int count = 0; 67 boolean gotNull = false; 68 for (int i : observable().keys()) { 69 if (observable().get(i) == null) { 70 gotNull = true; 71 } 72 count++; 73 } 74 assertWithMessage("Wrong number of items in iteration, count = " + count) 75 .that(count) 76 .isEqualTo(4); 77 assertWithMessage("Didn't get back the null value").that(gotNull).isTrue(); 78 } 79 80 @Test testObserveAdd()81 public void testObserveAdd() { 82 final String[] values = new String[13]; 83 mArray.set(10, "October"); 84 observable().addObserver(new BaseObserver() { 85 @Override 86 public void onValueAdded(int index, String addedValue) { 87 values[index] = addedValue; 88 } 89 }); 90 91 mArray.set(1, "January"); 92 mArray.set(6, "May"); 93 94 assertThat(values[1]).isEqualTo("January"); 95 assertThat(values[6]).isEqualTo("May"); 96 97 for (int i = 0; i < 13; i++) { 98 if (i != 1 && i != 6) { 99 assertThat(values[i]).isNull(); 100 } 101 } 102 } 103 104 @Test testObserveRemove()105 public void testObserveRemove() { 106 final String[] values = new String[13]; 107 mArray.set(1, "January"); 108 mArray.set(3, "Mars"); 109 110 observable().addObserver(new BaseObserver() { 111 @Override 112 public void onValueRemoved(int index, String removedValue) { 113 values[index] = removedValue; 114 } 115 }); 116 117 mArray.remove(3); 118 mArray.remove(6); 119 120 assertThat(values[3]).isEqualTo("Mars"); 121 122 for (int i = 0; i < 13; i++) { 123 if (i != 3) { 124 assertThat(values[i]).isNull(); 125 } 126 } 127 } 128 129 @Test testObserveReplace()130 public void testObserveReplace() { 131 final String[] values = new String[13]; 132 mArray.set(1, "January"); 133 mArray.set(3, "Mars"); 134 observable().addObserver(new BaseObserver() { 135 @Override 136 public void onValueReplaced(int index, String previousValue, String newValue) { 137 values[index] = newValue; 138 } 139 }); 140 141 mArray.set(3, "March"); 142 143 assertThat(values[3]).isEqualTo("March"); 144 145 for (int i = 0; i < 13; i++) { 146 if (i != 3) { 147 assertThat(values[i]).isNull(); 148 } 149 } 150 } 151 observable()152 private ObservableArray<String> observable() { 153 return mArray; 154 } 155 156 private static class BaseObserver implements ArrayObserver<String> { 157 @Override onValueReplaced(int index, String previousValue, String newValue)158 public void onValueReplaced(int index, String previousValue, String newValue) { 159 fail(String.format("onValueReplaced at %d with %s, %s", index, previousValue, 160 newValue)); 161 } 162 163 @Override onValueRemoved(int index, String removedValue)164 public void onValueRemoved(int index, String removedValue) { 165 fail(String.format("onValueRemoved at %d with %s", index, removedValue)); 166 } 167 168 @Override onValueAdded(int index, String addedValue)169 public void onValueAdded(int index, String addedValue) { 170 fail(String.format("onValueAdded at %d with %s", index, addedValue)); 171 } 172 173 } 174 } 175