RulesUpdaterContractTest.java revision bede17c216815a849be0c43d5ce7daaf750a9fac
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 17package android.app.timezone; 18 19import static org.junit.Assert.assertEquals; 20import static org.mockito.Matchers.eq; 21import static org.mockito.Mockito.mock; 22import static org.mockito.Mockito.verify; 23import static org.mockito.hamcrest.MockitoHamcrest.argThat; 24 25import android.content.Context; 26import android.content.Intent; 27 28import org.hamcrest.BaseMatcher; 29import org.hamcrest.Description; 30import org.hamcrest.Matcher; 31import org.junit.Test; 32 33/** 34 * Tests for {@link RulesUpdaterContract}. 35 */ 36// TODO(nfuller) Move to CTS once this class is part of the SystemApi. http://b/31008728 37public class RulesUpdaterContractTest { 38 39 @Test 40 public void createUpdaterIntent() throws Exception { 41 String packageName = "foobar"; 42 Intent intent = RulesUpdaterContract.createUpdaterIntent(packageName); 43 44 assertEquals(RulesUpdaterContract.ACTION_TRIGGER_RULES_UPDATE_CHECK, intent.getAction()); 45 assertEquals(packageName, intent.getPackage()); 46 assertEquals(Intent.FLAG_INCLUDE_STOPPED_PACKAGES, intent.getFlags()); 47 } 48 49 @Test 50 public void sendBroadcast() throws Exception { 51 String packageName = "foobar"; 52 byte[] tokenBytes = new byte[] { 1, 2, 3, 4, 5 }; 53 54 Intent expectedIntent = RulesUpdaterContract.createUpdaterIntent(packageName); 55 expectedIntent.putExtra(RulesUpdaterContract.EXTRA_CHECK_TOKEN, tokenBytes); 56 57 Context mockContext = mock(Context.class); 58 59 RulesUpdaterContract.sendBroadcast(mockContext, packageName, tokenBytes); 60 61 verify(mockContext).sendBroadcast( 62 filterEquals(expectedIntent), 63 eq(RulesUpdaterContract.UPDATE_TIME_ZONE_RULES_PERMISSION)); 64 } 65 66 /** 67 * Registers a mockito parameter matcher that uses {@link Intent#filterEquals(Intent)}. to 68 * check the parameter against the intent supplied. 69 */ 70 private static Intent filterEquals(final Intent expected) { 71 final Matcher<Intent> m = new BaseMatcher<Intent>() { 72 @Override 73 public boolean matches(Object actual) { 74 return actual != null && expected.filterEquals((Intent) actual); 75 } 76 @Override 77 public void describeTo(Description description) { 78 description.appendText(expected.toString()); 79 } 80 }; 81 return argThat(m); 82 } 83} 84