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