Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package android.view.contentcapture; |
| 17 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_CONTEXT_UPDATED; |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 19 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_FINISHED; |
| 20 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_SESSION_STARTED; |
| 21 | import static android.view.contentcapture.ContentCaptureEvent.TYPE_VIEW_DISAPPEARED; |
| 22 | |
| 23 | import static com.google.common.truth.Truth.assertThat; |
| 24 | |
| 25 | import static org.testng.Assert.assertThrows; |
| 26 | |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 27 | import android.content.LocusId; |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.SystemClock; |
| 30 | import android.view.autofill.AutofillId; |
| 31 | |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
| 36 | import java.util.ArrayList; |
| 37 | |
| 38 | /** |
| 39 | * Unit test for {@link ContentCaptureEvent}. |
| 40 | * |
| 41 | * <p>To run it: |
| 42 | * {@code atest FrameworksCoreTests:android.view.contentcapture.ContentCaptureEventTest} |
| 43 | */ |
| 44 | @RunWith(JUnit4.class) |
| 45 | public class ContentCaptureEventTest { |
| 46 | |
| 47 | private static final long MY_EPOCH = SystemClock.uptimeMillis(); |
| 48 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 49 | private static final LocusId ID = new LocusId("WHATEVER"); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 50 | |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 51 | // Not using @Mock because it's final - no need to be fancy here.... |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 52 | private final ContentCaptureContext mClientContext = |
| 53 | new ContentCaptureContext.Builder(ID).build(); |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 54 | |
| 55 | @Test |
| 56 | public void testSetAutofillId_null() { |
| 57 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 58 | |
| 59 | assertThrows(NullPointerException.class, () -> event.setAutofillId(null)); |
| 60 | assertThat(event.getId()).isNull(); |
| 61 | assertThat(event.getIds()).isNull(); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testSetAutofillIds_null() { |
| 66 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 67 | |
| 68 | assertThrows(NullPointerException.class, () -> event.setAutofillIds(null)); |
| 69 | assertThat(event.getId()).isNull(); |
| 70 | assertThat(event.getIds()).isNull(); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testAddAutofillId_null() { |
| 75 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 76 | |
| 77 | assertThrows(NullPointerException.class, () -> event.addAutofillId(null)); |
| 78 | assertThat(event.getId()).isNull(); |
| 79 | assertThat(event.getIds()).isNull(); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testSetAutofillId() { |
| 84 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 85 | |
| 86 | final AutofillId id = new AutofillId(108); |
| 87 | event.setAutofillId(id); |
| 88 | assertThat(event.getId()).isEqualTo(id); |
| 89 | assertThat(event.getIds()).isNull(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testSetAutofillIds() { |
| 94 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 95 | |
| 96 | final AutofillId id = new AutofillId(108); |
| 97 | final ArrayList<AutofillId> ids = new ArrayList<>(1); |
| 98 | ids.add(id); |
| 99 | event.setAutofillIds(ids); |
| 100 | assertThat(event.getId()).isNull(); |
| 101 | assertThat(event.getIds()).containsExactly(id); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testAddAutofillId() { |
| 106 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 107 | |
| 108 | final AutofillId id1 = new AutofillId(108); |
| 109 | event.addAutofillId(id1); |
| 110 | assertThat(event.getId()).isNull(); |
| 111 | assertThat(event.getIds()).containsExactly(id1); |
| 112 | |
| 113 | final AutofillId id2 = new AutofillId(666); |
| 114 | event.addAutofillId(id2); |
| 115 | assertThat(event.getId()).isNull(); |
| 116 | assertThat(event.getIds()).containsExactly(id1, id2).inOrder(); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testAddAutofillId_afterSetId() { |
| 121 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 122 | |
| 123 | final AutofillId id1 = new AutofillId(108); |
| 124 | event.setAutofillId(id1); |
| 125 | assertThat(event.getId()).isEqualTo(id1); |
| 126 | assertThat(event.getIds()).isNull(); |
| 127 | |
| 128 | final AutofillId id2 = new AutofillId(666); |
| 129 | event.addAutofillId(id2); |
| 130 | assertThat(event.getId()).isNull(); |
| 131 | assertThat(event.getIds()).containsExactly(id1, id2).inOrder(); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testAddAutofillId_afterSetIds() { |
| 136 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_VIEW_DISAPPEARED); |
| 137 | |
| 138 | final AutofillId id1 = new AutofillId(108); |
| 139 | final ArrayList<AutofillId> ids = new ArrayList<>(1); |
| 140 | ids.add(id1); |
| 141 | event.setAutofillIds(ids); |
| 142 | assertThat(event.getId()).isNull(); |
| 143 | assertThat(event.getIds()).containsExactly(id1); |
| 144 | |
| 145 | final AutofillId id2 = new AutofillId(666); |
| 146 | event.addAutofillId(id2); |
| 147 | assertThat(event.getId()).isNull(); |
| 148 | assertThat(event.getIds()).containsExactly(id1, id2).inOrder(); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testSessionStarted_directly() { |
| 153 | final ContentCaptureEvent event = newEventForSessionStarted(); |
| 154 | assertSessionStartedEvent(event); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testSessionStarted_throughParcel() { |
| 159 | final ContentCaptureEvent event = newEventForSessionStarted(); |
| 160 | final ContentCaptureEvent clone = cloneThroughParcel(event); |
| 161 | assertSessionStartedEvent(clone); |
| 162 | } |
| 163 | |
| 164 | private ContentCaptureEvent newEventForSessionStarted() { |
| 165 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_SESSION_STARTED) |
| 166 | .setClientContext(mClientContext) |
| 167 | .setParentSessionId("108"); |
| 168 | assertThat(event).isNotNull(); |
| 169 | return event; |
| 170 | } |
| 171 | |
| 172 | private void assertSessionStartedEvent(ContentCaptureEvent event) { |
| 173 | assertThat(event.getType()).isEqualTo(TYPE_SESSION_STARTED); |
| 174 | assertThat(event.getEventTime()).isAtLeast(MY_EPOCH); |
| 175 | assertThat(event.getSessionId()).isEqualTo("42"); |
| 176 | assertThat(event.getParentSessionId()).isEqualTo("108"); |
| 177 | assertThat(event.getId()).isNull(); |
| 178 | assertThat(event.getIds()).isNull(); |
| 179 | assertThat(event.getText()).isNull(); |
| 180 | assertThat(event.getViewNode()).isNull(); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 181 | final ContentCaptureContext clientContext = event.getContentCaptureContext(); |
| 182 | assertThat(clientContext).isNotNull(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 183 | assertThat(clientContext.getLocusId()).isEqualTo(ID); |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testSessionFinished_directly() { |
| 188 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_SESSION_FINISHED) |
| 189 | .setParentSessionId("108"); |
| 190 | assertThat(event).isNotNull(); |
| 191 | assertSessionFinishedEvent(event); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testSessionFinished_throughParcel() { |
| 196 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_SESSION_FINISHED) |
| 197 | .setClientContext(mClientContext) // should not be writting to parcel |
| 198 | .setParentSessionId("108"); |
| 199 | assertThat(event).isNotNull(); |
| 200 | final ContentCaptureEvent clone = cloneThroughParcel(event); |
| 201 | assertSessionFinishedEvent(clone); |
| 202 | } |
| 203 | |
| 204 | private void assertSessionFinishedEvent(ContentCaptureEvent event) { |
| 205 | assertThat(event.getType()).isEqualTo(TYPE_SESSION_FINISHED); |
| 206 | assertThat(event.getEventTime()).isAtLeast(MY_EPOCH); |
| 207 | assertThat(event.getSessionId()).isEqualTo("42"); |
| 208 | assertThat(event.getParentSessionId()).isEqualTo("108"); |
| 209 | assertThat(event.getId()).isNull(); |
| 210 | assertThat(event.getIds()).isNull(); |
| 211 | assertThat(event.getText()).isNull(); |
| 212 | assertThat(event.getViewNode()).isNull(); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 213 | assertThat(event.getContentCaptureContext()).isNull(); |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 216 | @Test |
| 217 | public void testContextUpdated_directly() { |
| 218 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_CONTEXT_UPDATED) |
| 219 | .setClientContext(mClientContext); |
| 220 | assertThat(event).isNotNull(); |
| 221 | assertContextUpdatedEvent(event); |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void testContextUpdated_throughParcel() { |
| 226 | final ContentCaptureEvent event = new ContentCaptureEvent("42", TYPE_CONTEXT_UPDATED) |
| 227 | .setClientContext(mClientContext); |
| 228 | assertThat(event).isNotNull(); |
| 229 | final ContentCaptureEvent clone = cloneThroughParcel(event); |
| 230 | assertContextUpdatedEvent(clone); |
| 231 | } |
| 232 | |
| 233 | private void assertContextUpdatedEvent(ContentCaptureEvent event) { |
| 234 | assertThat(event.getType()).isEqualTo(TYPE_CONTEXT_UPDATED); |
| 235 | assertThat(event.getEventTime()).isAtLeast(MY_EPOCH); |
| 236 | assertThat(event.getSessionId()).isEqualTo("42"); |
| 237 | assertThat(event.getParentSessionId()).isNull(); |
| 238 | assertThat(event.getId()).isNull(); |
| 239 | assertThat(event.getIds()).isNull(); |
| 240 | assertThat(event.getText()).isNull(); |
| 241 | assertThat(event.getViewNode()).isNull(); |
| 242 | final ContentCaptureContext clientContext = event.getContentCaptureContext(); |
| 243 | assertThat(clientContext).isNotNull(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 244 | assertThat(clientContext.getLocusId()).isEqualTo(ID); |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // TODO(b/123036895): add test for all events type (right now we're just testing the 3 types |
| 248 | // that use logic to write to parcel |
| 249 | |
Felipe Leme | 2ca5dfe | 2019-02-05 12:22:50 -0800 | [diff] [blame] | 250 | private ContentCaptureEvent cloneThroughParcel(ContentCaptureEvent event) { |
| 251 | Parcel parcel = Parcel.obtain(); |
| 252 | |
| 253 | try { |
| 254 | // Write to parcel |
| 255 | parcel.setDataPosition(0); // Sanity / paranoid check |
| 256 | event.writeToParcel(parcel, 0); |
| 257 | |
| 258 | // Read from parcel |
| 259 | parcel.setDataPosition(0); |
| 260 | ContentCaptureEvent clone = ContentCaptureEvent.CREATOR.createFromParcel(parcel); |
| 261 | assertThat(clone).isNotNull(); |
| 262 | return clone; |
| 263 | } finally { |
| 264 | parcel.recycle(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |