1 /* 2 * Copyright (C) 2017 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 18 package com.android.settings.search; 19 20 import static com.google.common.truth.Truth.assertThat; 21 import static junit.framework.Assert.fail; 22 23 import android.content.Intent; 24 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 @RunWith(SettingsRobolectricTestRunner.class) 32 public class ResultPayloadUtilsTest { 33 34 private static final String EXTRA_KEY = "key"; 35 private static final String EXTRA_VALUE = "value"; 36 37 private ResultPayload payload; 38 39 @Before setUp()40 public void setUp() { 41 Intent intent = new Intent(); 42 intent.putExtra(EXTRA_KEY, EXTRA_VALUE); 43 payload = new ResultPayload(intent); 44 } 45 46 @Test testUnmarshallBadData_ExceptionThrown()47 public void testUnmarshallBadData_ExceptionThrown() { 48 byte[] badData = "I'm going to fail :)".getBytes(); 49 try { 50 ResultPayloadUtils.unmarshall(badData, ResultPayload.CREATOR); 51 fail("unmarshall should throw exception"); 52 } catch (RuntimeException e) { 53 assertThat(e).isNotNull(); 54 } 55 } 56 57 @Test testMarshallResultPayload_NonEmptyArray()58 public void testMarshallResultPayload_NonEmptyArray() { 59 byte[] marshalledPayload = ResultPayloadUtils.marshall(payload); 60 assertThat(marshalledPayload).isNotNull(); 61 assertThat(marshalledPayload).isNotEmpty(); 62 } 63 64 @Test testUnmarshall_PreservedData()65 public void testUnmarshall_PreservedData() { 66 byte[] marshalledPayload = ResultPayloadUtils.marshall(payload); 67 ResultPayload newPayload = 68 ResultPayloadUtils.unmarshall(marshalledPayload, ResultPayload.CREATOR); 69 70 String originalIntentExtra = payload.getIntent().getStringExtra(EXTRA_KEY); 71 String copiedIntentExtra = newPayload.getIntent().getStringExtra(EXTRA_KEY); 72 assertThat(originalIntentExtra).isEqualTo(copiedIntentExtra); 73 } 74 }