18ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng/* 28ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Copyright (C) 2013 The Android Open Source Project 38ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * 48ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 58ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * you may not use this file except in compliance with the License. 68ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * You may obtain a copy of the License at 78ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * 88ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 98ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * 108ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Unless required by applicable law or agreed to in writing, software 118ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 128ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * See the License for the specific language governing permissions and 148ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * limitations under the License 158ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng */ 168ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 178ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengpackage com.android.providers.contacts; 188ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 198ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport android.test.suitebuilder.annotation.SmallTest; 208ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 218ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport junit.framework.TestCase; 228ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 238ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport java.util.Map; 248ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengimport java.util.Set; 258ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 268ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng/** 278ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng * Unit tests for TransactionContext. 288ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng */ 298ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng@SmallTest 308ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Chengpublic class TransactionContextTest extends TestCase { 318ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 328ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng public void testClearExceptSearchIndexUpdates_returnsNewSets() { 338ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng TransactionContext context = new TransactionContext(false); 348ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.markRawContactDirtyAndChanged(1L, false); 358ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.rawContactUpdated(1L); 368ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.rawContactInserted(1L, 1L); 378ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.syncStateUpdated(1L, new Object()); 388ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 398ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.clearExceptSearchIndexUpdates(); 408ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 418ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng Set<Long> newDirty = context.getDirtyRawContactIds(); 428ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng Set<Long> newChanged = context.getChangedRawContactIds(); 438ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng Set<Long> newInserted = context.getInsertedRawContactIds(); 448ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng Set<Long> newUpdated = context.getUpdatedRawContactIds(); 458ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng Set<Map.Entry<Long, Object>> newSync = context.getUpdatedSyncStates(); 468ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 478ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(newDirty.isEmpty()); 488ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(newChanged.isEmpty()); 498ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(newInserted.isEmpty()); 508ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(newUpdated.isEmpty()); 518ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(newSync.isEmpty()); 528ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng } 538ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 548ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng public void testMarkDirtyAndChanged_onlyUpdatesChanged() { 558ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng TransactionContext context = new TransactionContext(false); 568ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 578ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.markRawContactDirtyAndChanged(1L, true /* isSyncAdapter */); 588ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 598ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(1, context.getChangedRawContactIds().size()); 608ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(0, context.getDirtyRawContactIds().size()); 618ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng } 628ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 638ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng public void testMarkDirtyAndChanged_onlyUpdatesDirtyAndChanged() { 648ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng TransactionContext context = new TransactionContext(false); 658ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 668ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.markRawContactDirtyAndChanged(1L, false /* isSyncAdapter */); 678ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 688ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(1, context.getChangedRawContactIds().size()); 698ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(1, context.getDirtyRawContactIds().size()); 708ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng } 718ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 728ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng public void testRawContactInserted_affectsChangedContacts() { 738ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng TransactionContext context = new TransactionContext(false); 748ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().isEmpty()); 758ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 768ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.rawContactInserted(1L, 2L); 778ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(1, context.getChangedRawContactIds().size()); 788ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().contains(1L)); 798ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 808ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.rawContactInserted(5L, 10L); 818ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(2, context.getChangedRawContactIds().size()); 828ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().contains(5L)); 838ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng } 848ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 858ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng public void testMarkRawContactChangedOrDeletedOrInserted_affectsChangedContacts() { 868ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng TransactionContext context = new TransactionContext(false); 878ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().isEmpty()); 888ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 898ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.markRawContactChangedOrDeletedOrInserted(1L); 908ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(1, context.getChangedRawContactIds().size()); 918ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().contains(1L)); 928ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng 938ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng context.rawContactInserted(5L, 10L); 948ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertEquals(2, context.getChangedRawContactIds().size()); 958ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng assertTrue(context.getChangedRawContactIds().contains(5L)); 968ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng } 978ed367fdc0b086d54c489f68d555e2f0a4035f63Chiao Cheng} 98