1/*
2 * Copyright (C) 2016 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
17package com.android.dialer.voicemail;
18
19import android.content.ContentUris;
20import android.content.ContentValues;
21import android.content.res.AssetManager;
22
23import com.android.dialer.database.VoicemailArchiveContract.VoicemailArchive;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.io.OutputStream;
28
29/**
30 * Unit tests for {@link VoicemailArchiveActivity} and {@link VoicemailArchivePlaybackPresenter}.
31 */
32public class VoicemailArchiveTest
33        extends VoicemailActivityInstrumentationTestCase2<VoicemailArchiveActivity> {
34
35    public VoicemailArchiveTest() {
36        super(VoicemailArchiveActivity.class);
37    }
38
39    @Override
40    public void setUp() throws Exception {
41        super.setUp();
42        mPresenter = VoicemailArchivePlaybackPresenter.getInstance(getActivity(), null);
43    }
44
45    @Override
46    public void testFetchingVoicemail() throws Throwable {
47        setUriForRealFileVoicemailEntry();
48        setPlaybackViewForPresenter();
49        getInstrumentation().runOnMainSync(new Runnable() {
50            @Override
51            public void run() {
52                mPresenter.checkForContent(
53                        new VoicemailPlaybackPresenter.OnContentCheckedListener() {
54                            @Override
55                            public void onContentChecked(boolean hasContent) {
56                                mPresenter.resumePlayback();
57                                assertEquals(true, mPresenter.isPlaying());
58                            }
59                        });
60            }
61        });
62    }
63
64    @Override
65    public void testInvalidVoicemailShowsErrorMessage() throws Throwable {
66        setUriForInvalidVoicemailEntry();
67        getInstrumentation().runOnMainSync(new Runnable() {
68            @Override
69            public void run() {
70                mPresenter.checkForContent(
71                        new VoicemailPlaybackPresenter.OnContentCheckedListener() {
72                            @Override
73                            public void onContentChecked(boolean hasContent) {
74                                assertStateTextContains("Couldn't play voicemail");
75                            }
76                        });
77            }
78        });
79    }
80
81    @Override
82    protected void setUriForInvalidVoicemailEntry() {
83        assertNull(mVoicemailUri);
84        ContentValues values = new ContentValues();
85        values.put(VoicemailArchive.NUMBER, CONTACT_NUMBER);
86        values.put(VoicemailArchive.DATE, String.valueOf(System.currentTimeMillis()));
87        values.put(VoicemailArchive.MIME_TYPE, MIME_TYPE);
88        values.put(VoicemailArchive._DATA, VOICEMAIL_FILE_LOCATION);
89        mVoicemailUri = getContentResolver().insert(VoicemailArchive.CONTENT_URI, values);
90    }
91
92    @Override
93    protected void setUriForRealFileVoicemailEntry() throws IOException {
94        assertNull(mVoicemailUri);
95        ContentValues values = new ContentValues();
96        values.put(VoicemailArchive.DATE, String.valueOf(System.currentTimeMillis()));
97        values.put(VoicemailArchive.NUMBER, CONTACT_NUMBER);
98        values.put(VoicemailArchive.MIME_TYPE, MIME_TYPE);
99        values.put(VoicemailArchive.DURATION, 0);
100        mVoicemailUri = getContentResolver().insert(VoicemailArchive.CONTENT_URI, values);
101        AssetManager assets = getAssets();
102        try (InputStream inputStream = assets.open(TEST_ASSET_NAME);
103             OutputStream outputStream = getContentResolver().openOutputStream(mVoicemailUri)) {
104            copyBetweenStreams(inputStream, outputStream);
105        }
106    }
107
108    @Override
109    protected void cleanUpVoicemailUri() {
110        if (mVoicemailUri != null) {
111            getContentResolver().delete(VoicemailArchive.CONTENT_URI,
112                    "_ID = ?", new String[] { String.valueOf(ContentUris.parseId(mVoicemailUri)) });
113            mVoicemailUri = null;
114        }
115    }
116}
117