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.ui.conversation; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.ArgumentMatchers.isNull; 21 22 import android.content.Context; 23 import android.media.MediaPlayer; 24 import android.test.suitebuilder.annotation.MediumTest; 25 import android.view.View; 26 import android.widget.EditText; 27 28 import com.android.messaging.FakeFactory; 29 import com.android.messaging.R; 30 import com.android.messaging.datamodel.DataModel; 31 import com.android.messaging.datamodel.binding.Binding; 32 import com.android.messaging.datamodel.binding.BindingBase; 33 import com.android.messaging.datamodel.data.ConversationData; 34 import com.android.messaging.datamodel.data.DraftMessageData; 35 import com.android.messaging.datamodel.data.DraftMessageData.CheckDraftForSendTask; 36 import com.android.messaging.datamodel.data.DraftMessageData.CheckDraftTaskCallback; 37 import com.android.messaging.datamodel.data.MessageData; 38 import com.android.messaging.datamodel.data.MessagePartData; 39 import com.android.messaging.ui.ViewTest; 40 import com.android.messaging.ui.conversation.ComposeMessageView.IComposeMessageViewHost; 41 import com.android.messaging.util.BugleGservices; 42 import com.android.messaging.util.FakeMediaUtil; 43 import com.android.messaging.util.ImeUtil; 44 45 import org.mockito.ArgumentMatcher; 46 import org.mockito.Matchers; 47 import org.mockito.Mock; 48 import org.mockito.Mockito; 49 import org.mockito.invocation.InvocationOnMock; 50 import org.mockito.stubbing.Answer; 51 52 import java.util.ArrayList; 53 import java.util.Collections; 54 55 @MediumTest 56 public class ComposeMessageViewTest extends ViewTest<ComposeMessageView> { 57 private Context mContext; 58 59 @Mock protected DataModel mockDataModel; 60 @Mock protected DraftMessageData mockDraftMessageData; 61 @Mock protected BugleGservices mockBugleGservices; 62 @Mock protected ImeUtil mockImeUtil; 63 @Mock protected IComposeMessageViewHost mockIComposeMessageViewHost; 64 @Mock protected MediaPlayer mockMediaPlayer; 65 @Mock protected ConversationInputManager mockInputManager; 66 @Mock protected ConversationData mockConversationData; 67 68 Binding<ConversationData> mConversationBinding; 69 70 @Override setUp()71 protected void setUp() throws Exception { 72 super.setUp(); 73 mContext = getInstrumentation().getTargetContext(); 74 FakeFactory.register(mContext) 75 .withDataModel(mockDataModel) 76 .withBugleGservices(mockBugleGservices) 77 .withMediaUtil(new FakeMediaUtil(mockMediaPlayer)); 78 79 Mockito.doReturn(true).when(mockConversationData).isBound(Mockito.anyString()); 80 mConversationBinding = BindingBase.createBinding(this); 81 mConversationBinding.bind(mockConversationData); 82 } 83 84 @Override getView()85 protected ComposeMessageView getView() { 86 final ComposeMessageView view = super.getView(); 87 view.setInputManager(mockInputManager); 88 view.setConversationDataModel(BindingBase.createBindingReference(mConversationBinding)); 89 return view; 90 } 91 92 @Override getLayoutIdForView()93 protected int getLayoutIdForView() { 94 return R.layout.compose_message_view; 95 } 96 testSend()97 public void testSend() { 98 Mockito.when(mockDraftMessageData.getReadOnlyAttachments()) 99 .thenReturn(Collections.unmodifiableList(new ArrayList<MessagePartData>())); 100 Mockito.when(mockDraftMessageData.getIsDefaultSmsApp()).thenReturn(true); 101 Mockito.when(mockIComposeMessageViewHost.isReadyForAction()).thenReturn(true); 102 final ComposeMessageView view = getView(); 103 104 final MessageData message = MessageData.createDraftSmsMessage("fake_id", "just_a_self_id", 105 "Sample Message"); 106 107 Mockito.when(mockDraftMessageData.isBound(Matchers.anyString())) 108 .thenReturn(true); 109 Mockito.when(mockDraftMessageData.getMessageText()).thenReturn(message.getMessageText()); 110 Mockito.when(mockDraftMessageData.prepareMessageForSending( 111 Matchers.<BindingBase<DraftMessageData>>any())) 112 .thenReturn(message); 113 Mockito.when(mockDraftMessageData.hasPendingAttachments()).thenReturn(false); 114 Mockito.doAnswer(new Answer() { 115 @Override 116 public Object answer(InvocationOnMock invocation) throws Throwable { 117 // Synchronously pass the draft check and callback. 118 ((CheckDraftTaskCallback)invocation.getArguments()[2]).onDraftChecked( 119 mockDraftMessageData, CheckDraftForSendTask.RESULT_PASSED); 120 return null; 121 } 122 }).when(mockDraftMessageData).checkDraftForAction(Mockito.anyBoolean(), Mockito.anyInt(), 123 Mockito.<CheckDraftTaskCallback>any(), 124 Mockito.<Binding<DraftMessageData>>any()); 125 126 view.bind(mockDraftMessageData, mockIComposeMessageViewHost); 127 128 final EditText composeEditText = (EditText) view.findViewById(R.id.compose_message_text); 129 final View sendButton = view.findViewById(R.id.send_message_button); 130 131 view.requestDraftMessage(false); 132 133 Mockito.verify(mockDraftMessageData).loadFromStorage(any(BindingBase.class), 134 isNull(), Mockito.eq(false)); 135 136 view.onDraftChanged(mockDraftMessageData, DraftMessageData.ALL_CHANGED); 137 138 assertEquals(message.getMessageText(), composeEditText.getText().toString()); 139 140 sendButton.performClick(); 141 Mockito.verify(mockIComposeMessageViewHost).sendMessage( 142 Mockito.argThat(o -> { 143 assertEquals(message.getMessageText(), o.getMessageText()); 144 return true; 145 })); 146 } 147 testNotDefaultSms()148 public void testNotDefaultSms() { 149 Mockito.when(mockDraftMessageData.getReadOnlyAttachments()) 150 .thenReturn(Collections.unmodifiableList(new ArrayList<MessagePartData>())); 151 Mockito.when(mockDraftMessageData.getIsDefaultSmsApp()).thenReturn(false); 152 Mockito.when(mockIComposeMessageViewHost.isReadyForAction()).thenReturn(false); 153 final ComposeMessageView view = getView(); 154 155 final MessageData message = MessageData.createDraftSmsMessage("fake_id", "just_a_self_id", 156 "Sample Message"); 157 158 Mockito.when(mockDraftMessageData.isBound(Matchers.anyString())) 159 .thenReturn(true); 160 Mockito.when(mockDraftMessageData.getMessageText()).thenReturn(message.getMessageText()); 161 Mockito.when(mockDraftMessageData.prepareMessageForSending( 162 Matchers.<BindingBase<DraftMessageData>>any())) 163 .thenReturn(message); 164 Mockito.when(mockDraftMessageData.hasPendingAttachments()).thenReturn(false); 165 166 view.bind(mockDraftMessageData, mockIComposeMessageViewHost); 167 168 final EditText composeEditText = (EditText) view.findViewById(R.id.compose_message_text); 169 final View sendButton = view.findViewById(R.id.send_message_button); 170 171 view.requestDraftMessage(false); 172 173 Mockito.verify(mockDraftMessageData).loadFromStorage(any(BindingBase.class), 174 isNull(), Mockito.eq(false)); 175 176 view.onDraftChanged(mockDraftMessageData, DraftMessageData.ALL_CHANGED); 177 178 assertEquals(message.getMessageText(), composeEditText.getText().toString()); 179 180 sendButton.performClick(); 181 Mockito.verify(mockIComposeMessageViewHost).warnOfMissingActionConditions( 182 any(Boolean.class), any(Runnable.class)); 183 } 184 } 185