1 /* 2 * Copyright (C) 2014 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.inputmethod.latin; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.Matchers.eq; 22 import static org.mockito.Mockito.validateMockitoUsage; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.ContactsContract.Contacts; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 import java.util.ArrayList; 39 40 /** 41 * Tests for {@link ContactsContentObserver}. 42 */ 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 public class ContactsContentObserverTest { 46 private static final int UPDATED_CONTACT_COUNT = 10; 47 private static final int STALE_CONTACT_COUNT = 8; 48 private static final ArrayList<String> STALE_NAMES_LIST = new ArrayList<>(); 49 private static final ArrayList<String> UPDATED_NAMES_LIST = new ArrayList<>(); 50 51 static { 52 STALE_NAMES_LIST.add("Larry Page"); 53 STALE_NAMES_LIST.add("Roger Federer"); 54 UPDATED_NAMES_LIST.add("Larry Page"); 55 UPDATED_NAMES_LIST.add("Roger Federer"); 56 UPDATED_NAMES_LIST.add("Barak Obama"); 57 } 58 59 @Mock private ContactsManager mMockManager; 60 @Mock private Context mContext; 61 62 private ContactsContentObserver mObserver; 63 64 @Before setUp()65 public void setUp() throws Exception { 66 MockitoAnnotations.initMocks(this); 67 mObserver = new ContactsContentObserver(mMockManager, mContext); 68 } 69 70 @After tearDown()71 public void tearDown() { 72 validateMockitoUsage(); 73 } 74 75 @Test testHaveContentsChanged_NoChange()76 public void testHaveContentsChanged_NoChange() { 77 when(mMockManager.getContactCount()).thenReturn(STALE_CONTACT_COUNT); 78 when(mMockManager.getContactCountAtLastRebuild()).thenReturn(STALE_CONTACT_COUNT); 79 when(mMockManager.getValidNames(eq(Contacts.CONTENT_URI))).thenReturn(STALE_NAMES_LIST); 80 when(mMockManager.getHashCodeAtLastRebuild()).thenReturn(STALE_NAMES_LIST.hashCode()); 81 assertFalse(mObserver.haveContentsChanged()); 82 } 83 @Test testHaveContentsChanged_UpdatedCount()84 public void testHaveContentsChanged_UpdatedCount() { 85 when(mMockManager.getContactCount()).thenReturn(UPDATED_CONTACT_COUNT); 86 when(mMockManager.getContactCountAtLastRebuild()).thenReturn(STALE_CONTACT_COUNT); 87 assertTrue(mObserver.haveContentsChanged()); 88 } 89 90 @Test testHaveContentsChanged_HashUpdate()91 public void testHaveContentsChanged_HashUpdate() { 92 when(mMockManager.getContactCount()).thenReturn(STALE_CONTACT_COUNT); 93 when(mMockManager.getContactCountAtLastRebuild()).thenReturn(STALE_CONTACT_COUNT); 94 when(mMockManager.getValidNames(eq(Contacts.CONTENT_URI))).thenReturn(UPDATED_NAMES_LIST); 95 when(mMockManager.getHashCodeAtLastRebuild()).thenReturn(STALE_NAMES_LIST.hashCode()); 96 assertTrue(mObserver.haveContentsChanged()); 97 } 98 } 99