1/*
2 * Copyright (C) 2008 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.mms.ui;
18
19import static com.android.mms.ui.MessageListAdapter.COLUMN_ID;
20import static com.android.mms.ui.MessageListAdapter.COLUMN_MSG_TYPE;
21
22import com.android.mms.R;
23import com.android.mms.ui.ComposeMessageActivity;
24import com.android.mms.ui.RecipientsEditor;
25import android.os.SystemClock;
26import android.database.Cursor;
27import android.test.ActivityInstrumentationTestCase2;
28import android.test.suitebuilder.annotation.LargeTest;
29import android.view.View;
30import android.view.ViewStub;
31import android.widget.EditText;
32import android.widget.TextView;
33import android.widget.Button;
34import android.widget.ImageButton;
35import android.util.Log;
36
37
38/**
39 * Test threads with thousands of messages
40 * To run just this test:
41 *       runtest --test-class=com.android.mms.ui.MultiPartSmsTests mms
42 */
43public class MultiPartSmsTests
44extends ActivityInstrumentationTestCase2<ComposeMessageActivity> {
45
46    private static final String TAG = "MultiPartSmsTests";
47    private static final int SMS_RECEIVE_TIMER = 5 * 60 * 1000; //5 minutes;
48    private ComposeMessageActivity mActivity = null;
49    private RecipientsEditor mRecipientsEditor;
50    private EditText mTextEditor;
51
52    // NOTE: the longer the message, the longer is takes to send and get back the
53    // received message. You'll have to adjust the timeout in testLongSmsMessage().
54    // I eventually pared down the message to make the test more reasonable to test.
55    final String mLongMessage =
56        "Is this a dagger which I see before me,"
57        +" The handle toward my hand? Come, let me clutch thee."
58        +" I have thee not, and yet I see thee still."
59        +" Art thou not, fatal vision, sensible"
60        +" To feeling as to sight? or art thou but"
61        +" A dagger of the mind, a false creation,"
62        +" Proceeding from the heat-oppressed brain?"
63        +" I see thee yet, in form as palpable"
64        +" As this which now I draw.";
65//        +" Thou marshall'st me the way that I was going;"
66//        +" And such an instrument I was to use."
67//        +" Mine eyes are made the fools o' the other senses,"
68//        +" Or else worth all the rest; I see thee still,"
69//        +" And on thy blade and dudgeon gouts of blood,"
70//        +" Which was not so before. There's no such thing:"
71//        +" It is the bloody business which informs"
72//        +" Thus to mine eyes. Now o'er the one halfworld"
73//        +" Nature seems dead, and wicked dreams abuse"
74//        +" The curtain'd sleep; witchcraft celebrates"
75//        +" Pale Hecate's offerings, and wither'd murder,"
76//        +" Alarum'd by his sentinel, the wolf,"
77//        +" Whose howl's his watch, thus with his stealthy pace."
78//        +" With Tarquin's ravishing strides, towards his design"
79//        +" Moves like a ghost. Thou sure and firm-set earth,"
80//        +" Hear not my steps, which way they walk, for fear"
81//        +" Thy very stones prate of my whereabout,"
82//        +" And take the present horror from the time,"
83//        +" Which now suits with it. Whiles I threat, he lives:"
84//        +" Words to the heat of deeds too cold breath gives."
85//        +" A bell rings"
86//        +" I go, and it is done; the bell invites me."
87//        +" Hear it not, Duncan; for it is a knell"
88//        +" That summons thee to heaven or to hell.";
89    private String mMyNumber;
90
91    public MultiPartSmsTests() {
92        super(ComposeMessageActivity.class);
93    }
94
95    @Override
96    protected void setUp() throws Exception {
97        super.setUp();
98
99        mActivity = getActivity();
100        ViewStub stub = (ViewStub)mActivity.findViewById(R.id.recipients_editor_stub);
101        if (stub != null) {
102            View stubView = stub.inflate();
103            mRecipientsEditor = (RecipientsEditor) stubView.findViewById(R.id.recipients_editor);
104        } else {
105            mRecipientsEditor = (RecipientsEditor)mActivity.findViewById(R.id.recipients_editor);
106            mRecipientsEditor.setVisibility(View.VISIBLE);
107        }
108        mTextEditor = (EditText)mActivity.findViewById(R.id.embedded_text_editor);
109
110        mMyNumber = MessageUtils.getLocalNumber();
111        assertNotNull("null number for this phone", mMyNumber);
112        // WARNING: MessageUtils.getLocalNumber returned some 206 number as the number
113        // of this phone, which is totally the wrong area code. Therefore, the test
114        // ended up failing because it sent a gigantic message to some unknown number
115        // and never received the number back. For now, I'm just hardwiring the number
116        // of my phone.
117//        mMyNumber = "6502782055";
118        mMyNumber = "6509330537";
119    }
120
121    private abstract class MessageRunnable implements Runnable {
122        protected String mRecipient;
123
124        public void setRecipient(String recipient) {
125            mRecipient = recipient;
126        }
127    }
128
129    private MessageRunnable mSendSmsMessage = new MessageRunnable() {
130        public void run() {
131            // only on the first message will there be a recipients editor
132            if (mRecipientsEditor.getVisibility() == View.VISIBLE) {
133                mRecipientsEditor.setText(mRecipient);
134            }
135            mTextEditor.setText(mLongMessage);
136            ImageButton send = (ImageButton)mActivity.findViewById(R.id.send_button_sms);
137            send.performClick();
138        }
139    };
140
141    private void sleep(long sleepTime) {
142        try {
143            Thread.sleep(sleepTime);
144        } catch (InterruptedException e) {}
145    }
146
147    /**
148     * Send a a long multi-part SMS message
149     */
150    @LargeTest
151    public void testLongSmsMessage() throws Throwable {
152        final ComposeMessageActivity a = getActivity();
153        mActivity.runOnUiThread(new Runnable() {
154            public void run() {
155                a.initialize(null, 0);
156                a.loadMessageContent();
157            }
158        });
159
160        // wait 5 seconds for the activity to run on UI thread and
161        // mMsgListAdapter get updated with latest information.
162        sleep(5 * 1000);
163        int msgCount = mActivity.mMsgListAdapter.getCount();
164        Log.v(TAG, "msgCount: " + msgCount);
165        // Send out message to the recipient
166        mSendSmsMessage.setRecipient(mMyNumber);
167        runTestOnUiThread(mSendSmsMessage);
168
169        // Wait for maximum 5 minutes to send the long message
170        // and then receive it. Make sure the sent and received messages compare the same.
171        boolean received = false;
172        long startTime = System.currentTimeMillis();
173        while ((System.currentTimeMillis() - startTime) <= SMS_RECEIVE_TIMER) {
174            sleep( 5 * 1000);     // wait 5 seconds between checks
175            Log.v(TAG, "Message Count: " + mActivity.mMsgListAdapter.getCount());
176            if (msgCount + 2 == mActivity.mMsgListAdapter.getCount()) {
177                // The "msgCount + 2" is to account for the sent and received message.
178                // Other cases: 1) fail to send/receive sms message, test fail
179                // 2) another message could be received by the target phone during this time
180                //    test will falsely fail
181                Cursor cursor = mActivity.mMsgListAdapter.getCursor();
182                cursor.moveToLast();
183                String type = cursor.getString(COLUMN_MSG_TYPE);
184                long msgId = cursor.getLong(COLUMN_ID);
185                MessageItem msgItem = mActivity.mMsgListAdapter.getCachedMessageItem(type, msgId, cursor);
186                assertNotNull("got a null last MessageItem", msgItem);
187                assertEquals("The sent and received messages aren't the same",
188                        mLongMessage,
189                        msgItem.mBody);
190                received = true;
191                break;
192            }
193        }
194        assertTrue("Never received the sent message", received);
195    }
196}
197