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