1 /* 2 * Copyright (C) 2015 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.messaging.datamodel.action; 18 19 import android.content.pm.ProviderInfo; 20 import android.database.Cursor; 21 import android.net.Uri; 22 import android.test.mock.MockContentProvider; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 import com.android.messaging.BugleTestCase; 26 import com.android.messaging.FakeContext; 27 import com.android.messaging.FakeFactory; 28 import com.android.messaging.datamodel.BugleDatabaseOperations; 29 import com.android.messaging.datamodel.DataModel; 30 import com.android.messaging.datamodel.DatabaseWrapper; 31 import com.android.messaging.datamodel.FakeDataModel; 32 import com.android.messaging.datamodel.MessagingContentProvider; 33 import com.android.messaging.datamodel.action.ActionTestHelpers.StubActionService; 34 import com.android.messaging.datamodel.action.ActionTestHelpers.StubActionService.StubActionServiceCallLog; 35 import com.android.messaging.datamodel.action.GetOrCreateConversationAction.GetOrCreateConversationActionListener; 36 import com.android.messaging.datamodel.action.GetOrCreateConversationAction.GetOrCreateConversationActionMonitor; 37 import com.android.messaging.datamodel.data.ParticipantData; 38 import com.android.messaging.datamodel.data.TestDataFactory; 39 import com.android.messaging.sms.MmsUtils; 40 41 import org.mockito.Mock; 42 43 import java.util.ArrayList; 44 45 @SmallTest 46 public class GetOrCreateConversationActionTest extends BugleTestCase { 47 48 @Mock GetOrCreateConversationActionListener mockListener; 49 testGetOrCreateConversation()50 public void testGetOrCreateConversation() { 51 final DatabaseWrapper db = DataModel.get().getDatabase(); 52 53 final ArrayList<String> recipients = new ArrayList<String>(); 54 recipients.add("5551234567"); 55 recipients.add("5551234568"); 56 57 // Generate a list of partially formed participants 58 final ArrayList<ParticipantData> participants = new 59 ArrayList<ParticipantData>(); 60 61 62 for (final String recipient : recipients) { 63 participants.add(ParticipantData.getFromRawPhoneBySystemLocale(recipient)); 64 } 65 66 // Test that we properly stubbed the SMS provider to return a thread id 67 final long threadId = MmsUtils.getOrCreateThreadId(mContext, recipients); 68 assertEquals(TestDataFactory.SMS_MMS_THREAD_ID_CURSOR_VALUE, threadId); 69 70 ArrayList<StubActionServiceCallLog> calls = mService.getCalls(); 71 72 GetOrCreateConversationActionMonitor monitor = 73 GetOrCreateConversationAction.getOrCreateConversation(participants, null, 74 mockListener); 75 76 assertEquals("Failed to start service once for action", calls.size(), 1); 77 assertTrue("Action not GetOrCreateConversationAction", calls.get(0).action instanceof 78 GetOrCreateConversationAction); 79 80 GetOrCreateConversationAction action = (GetOrCreateConversationAction) 81 calls.get(0).action; 82 83 Object result = action.executeAction(); 84 85 assertTrue(result instanceof String); 86 87 // Make sure that we created a new conversation 88 int expectedConversationId = TestDataFactory.NUM_TEST_CONVERSATIONS + 1; 89 assertEquals(expectedConversationId, Integer.parseInt((String) result)); 90 91 // Now get the conversation that we just created again 92 monitor = GetOrCreateConversationAction.getOrCreateConversation(participants, null, 93 mockListener); 94 95 calls = mService.getCalls(); 96 assertEquals("Failed to start service for second action", calls.size(), 2); 97 assertTrue("Action not GetOrCreateConversationAction", calls.get(1).action instanceof 98 GetOrCreateConversationAction); 99 action = (GetOrCreateConversationAction)calls.get(1).action; 100 result = action.executeAction(); 101 102 assertTrue(result instanceof String); 103 104 final String conversationId = (String) result; 105 106 // Make sure that we found the same conversation id 107 assertEquals(expectedConversationId, Integer.parseInt((String) result)); 108 109 final ArrayList<ParticipantData> conversationParticipants = 110 BugleDatabaseOperations.getParticipantsForConversation(db, conversationId); 111 112 assertEquals("Participant count mismatch", recipients.size(), 113 conversationParticipants.size()); 114 for(final ParticipantData participant : conversationParticipants) { 115 assertTrue(recipients.contains(participant.getSendDestination())); 116 } 117 118 final Uri conversationParticipantsUri = 119 MessagingContentProvider.buildConversationParticipantsUri(conversationId); 120 final Cursor cursor = mContext.getContentResolver().query(conversationParticipantsUri, 121 ParticipantData.ParticipantsQuery.PROJECTION, null, null, null); 122 123 int countSelf = 0; 124 while(cursor.moveToNext()) { 125 final ParticipantData participant = ParticipantData.getFromCursor(cursor); 126 if (participant.isSelf()) { 127 countSelf++; 128 } else { 129 assertTrue(recipients.contains(participant.getSendDestination())); 130 } 131 } 132 cursor.close(); 133 assertEquals("Expect one self participant in conversations", 1, countSelf); 134 assertEquals("Cursor count mismatch", recipients.size(), cursor.getCount() - countSelf); 135 136 final String realId = BugleDatabaseOperations.getExistingConversation(db, threadId, false); 137 assertEquals("Conversation already exists", realId, conversationId); 138 } 139 140 private FakeContext mContext; 141 private StubActionService mService; 142 143 @Override setUp()144 public void setUp() throws Exception { 145 super.setUp(); 146 147 mContext = new FakeContext(getTestContext()); 148 149 final MockContentProvider mockProvider = new MockContentProvider() { 150 @Override 151 public Cursor query(final Uri uri, final String[] projection, final String selection, 152 final String[] selectionArgs, final String sortOrder) { 153 return TestDataFactory.getSmsMmsThreadIdCursor(); 154 } 155 }; 156 157 mContext.addContentProvider("mms-sms", mockProvider); 158 final MessagingContentProvider provider = new MessagingContentProvider(); 159 final ProviderInfo providerInfo = new ProviderInfo(); 160 providerInfo.authority = MessagingContentProvider.AUTHORITY; 161 provider.attachInfo(mContext, providerInfo); 162 mContext.addContentProvider(MessagingContentProvider.AUTHORITY, provider); 163 164 mService = new StubActionService(); 165 final FakeDataModel fakeDataModel = new FakeDataModel(mContext) 166 .withActionService(mService); 167 FakeFactory.registerWithFakeContext(getTestContext(), mContext) 168 .withDataModel(fakeDataModel); 169 provider.setDatabaseForTest(fakeDataModel.getDatabase()); 170 } 171 } 172