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