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 package com.android.phone.vvm.omtp; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.preference.PreferenceManager; 22 import android.test.AndroidTestCase; 23 import android.util.ArraySet; 24 25 import com.android.phone.vvm.omtp.OmtpBootCompletedReceiver.SubIdProcessor; 26 27 import java.util.Set; 28 29 public class OmtpBootCompletedReceiverTests extends AndroidTestCase { 30 OmtpBootCompletedReceiver mReceiver = new OmtpBootCompletedReceiver(); 31 @Override 32 public void setUp() { 33 } 34 35 @Override 36 public void tearDown() { 37 PreferenceManager 38 .getDefaultSharedPreferences(getContext().createDeviceProtectedStorageContext()) 39 .edit().clear().apply(); 40 } 41 42 public void testReadWriteList() { 43 readWriteList(new int[] {1}); 44 } 45 46 public void testReadWriteList_Multiple() { 47 readWriteList(new int[] {1, 2}); 48 } 49 50 public void testReadWriteList_Duplicate() { 51 readWriteList(new int[] {1, 1}); 52 } 53 54 private void readWriteList(int[] values) { 55 for (int value : values) { 56 OmtpBootCompletedReceiver.addDeferredSubId(getContext(), value); 57 } 58 TestSubIdProcessor processor = new TestSubIdProcessor(values); 59 mReceiver.setSubIdProcessorForTest(processor); 60 Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED); 61 mReceiver.onReceive(getContext(), intent); 62 processor.assertMatch(); 63 // after onReceive() is called the list should be empty 64 TestSubIdProcessor emptyProcessor = new TestSubIdProcessor(new int[] {}); 65 mReceiver.setSubIdProcessorForTest(processor); 66 mReceiver.onReceive(getContext(), intent); 67 processor.assertMatch(); 68 } 69 70 private static class TestSubIdProcessor implements SubIdProcessor { 71 private final Set<Integer> mExpectedSubIds; 72 73 public TestSubIdProcessor(int[] expectedSubIds) { 74 mExpectedSubIds = new ArraySet<>(); 75 for(int subId : expectedSubIds){ 76 mExpectedSubIds.add(subId); 77 } 78 } 79 80 @Override 81 public void process(Context context, int subId){ 82 assertTrue(mExpectedSubIds.contains(subId)); 83 mExpectedSubIds.remove(subId); 84 } 85 86 public void assertMatch(){ 87 assertTrue(mExpectedSubIds.isEmpty()); 88 } 89 } 90 } 91