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