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 package com.android.messaging.ui.mediapicker; 17 18 import android.media.MediaPlayer; 19 import android.media.MediaRecorder.OnErrorListener; 20 import android.media.MediaRecorder.OnInfoListener; 21 import android.net.Uri; 22 23 import com.android.messaging.FakeFactory; 24 import com.android.messaging.R; 25 import com.android.messaging.datamodel.data.MessagePartData; 26 import com.android.messaging.ui.ViewTest; 27 import com.android.messaging.util.FakeMediaUtil; 28 29 import org.mockito.Matchers; 30 import org.mockito.Mock; 31 import org.mockito.Mockito; 32 33 public class AudioRecordViewTest extends ViewTest<AudioRecordView> { 34 35 @Mock AudioRecordView.HostInterface mockHost; 36 @Mock LevelTrackingMediaRecorder mockRecorder; 37 @Mock MediaPlayer mockMediaPlayer; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 FakeFactory.register(getInstrumentation().getContext()) 43 .withMediaUtil(new FakeMediaUtil(mockMediaPlayer)); 44 } 45 46 private void verifyAudioSubmitted() { 47 Mockito.verify(mockHost).onAudioRecorded(Matchers.any(MessagePartData.class)); 48 } 49 50 private AudioRecordView initView() { 51 final AudioRecordView view = getView(); 52 view.testSetMediaRecorder(mockRecorder); 53 view.setHostInterface(mockHost); 54 return view; 55 } 56 57 public void testRecording() { 58 Mockito.when(mockRecorder.isRecording()).thenReturn(false); 59 Mockito.when(mockRecorder.startRecording(Matchers.<OnErrorListener>any(), 60 Matchers.<OnInfoListener>any(), Matchers.anyInt())).thenReturn(true); 61 Mockito.when(mockRecorder.stopRecording()).thenReturn(Uri.parse("content://someaudio/2")); 62 final AudioRecordView view = initView(); 63 view.onRecordButtonTouchDown(); 64 Mockito.verify(mockRecorder).startRecording(Matchers.<OnErrorListener>any(), 65 Matchers.<OnInfoListener>any(), Matchers.anyInt()); 66 Mockito.when(mockRecorder.isRecording()).thenReturn(true); 67 // Record for 1 second to make it meaningful. 68 sleepNoThrow(1000); 69 view.onRecordButtonTouchUp(); 70 // We added some buffer to the end of the audio recording, so sleep for sometime and 71 // verify audio is recorded. 72 sleepNoThrow(700); 73 Mockito.verify(mockRecorder).stopRecording(); 74 verifyAudioSubmitted(); 75 } 76 77 private void sleepNoThrow(final long duration) { 78 try { 79 Thread.sleep(duration); 80 } catch (final InterruptedException e) { 81 Thread.currentThread().interrupt(); 82 } 83 } 84 85 @Override 86 protected int getLayoutIdForView() { 87 return R.layout.mediapicker_audio_chooser; 88 } 89 } 90