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