1/*
2 * Copyright (C) 2009 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
18package com.android.mms;
19
20import com.android.mms.LogTag;
21import com.android.mms.R;
22import com.android.mms.data.WorkingMessage;
23import com.android.mms.ui.ComposeMessageActivity;
24
25import android.app.Activity;
26import android.app.Instrumentation;
27import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31
32import android.test.suitebuilder.annotation.LargeTest;
33import android.test.ActivityInstrumentationTestCase2;
34
35import android.util.Log;
36import android.view.KeyEvent;
37import android.widget.Button;
38import android.widget.EditText;
39import android.widget.TextView;
40
41/**
42 *
43 * Junit / Instrumentation test case for testing intercepting the send sms intent just
44 * like a 3rd party might want to do.
45 *
46 */
47
48public class InterceptSendSms extends ActivityInstrumentationTestCase2 <ComposeMessageActivity> {
49    private static String TAG = "InterceptSendSms";
50    private static int WAIT_TIME = 4000; //Set the short wait time for 4 sec.
51    private static String RECIPIENTS = "4258365497,4258365496";
52    private static String MESSAGE = "This is a test message of intercepting a SMS";
53
54    private InterceptSmsReceiver mInterceptReceiver;
55    private TextView mRecipientsView;
56    private EditText mTextEditor;
57    private boolean mInterceptedSend;
58
59    public InterceptSendSms() {
60        super("com.android.mms", ComposeMessageActivity.class);
61    }
62
63    @Override
64    protected void setUp() throws Exception {
65        Activity activity = getActivity();
66        super.setUp();
67        mRecipientsView = (TextView)activity.findViewById(R.id.recipients_editor);
68        mTextEditor = (EditText)activity.findViewById(R.id.embedded_text_editor);
69
70        // Setup our receiver to listen for SMS's about to be sent.
71        mInterceptReceiver = new InterceptSmsReceiver();
72        IntentFilter filter = new IntentFilter(WorkingMessage.ACTION_SENDING_SMS);
73        activity.registerReceiver(mInterceptReceiver, filter);
74    }
75
76    @Override
77    protected void tearDown() throws Exception {
78        getActivity().unregisterReceiver(mInterceptReceiver);
79
80        super.tearDown();
81    }
82
83 // Create the object with the run() method
84    Runnable runnable = new sendMms();
85
86    class sendMms implements Runnable {
87        // This method is called when the thread runs
88        public void run() {
89            Instrumentation inst = getInstrumentation();
90
91            mRecipientsView.setText(RECIPIENTS);
92            mTextEditor.setText(MESSAGE);
93
94            Button mSendButton = (Button) getActivity().getWindow().findViewById(R.id.send_button);
95            mSendButton.performClick();
96
97            Log.v(TAG, "sendMms hitting send now");
98            boolean messageSend = mSendButton.performClick();
99            if (!messageSend) {
100                assertTrue("Fails to send mms", false);
101                Log.v(TAG, "messageSend is true");
102            }
103        }
104    }
105
106    // Send sms and see if we get a chance to handle the send in our receiver.
107    @LargeTest
108    public void testInterceptSendSms(){
109        try{
110            Instrumentation inst = getInstrumentation();
111
112            // Send the sms message
113            inst.runOnMainSync(runnable);
114            Thread.sleep(WAIT_TIME);
115            assertTrue("Intercepted send SMS", mInterceptedSend);
116        } catch (Exception e){
117            assertTrue("Failed to send sms", false);
118            Log.v(TAG, e.toString());
119        }
120    }
121
122    /**
123     * InterceptSmsReceiver catches the NEW_SENDING_SMS broadcast from the messaging
124     * app when the app is about to send a SMS message. We pretend to be an app that
125     * takes over and does the sending ourself. We set the result code RESULT_OK so
126     * the message app doesn't actually send the message.
127     */
128    public class InterceptSmsReceiver extends BroadcastReceiver {
129        public void onReceive(Context context, Intent intent) {
130            Log.v(TAG, "doReceive: " + intent);
131            mInterceptedSend = true;
132
133            final String msgText = intent.getStringExtra(WorkingMessage.EXTRA_SMS_MESSAGE);
134            final String semiSepRecipients =
135                intent.getStringExtra(WorkingMessage.EXTRA_SMS_RECIPIENTS);
136            final long threadId = intent.getLongExtra(WorkingMessage.EXTRA_SMS_THREAD_ID, 0);
137
138            assertEquals(msgText, MESSAGE);
139            assertEquals(semiSepRecipients, RECIPIENTS.replace(',', ';'));
140            assertTrue(threadId > 0);
141
142            // Mark that we're handling the sending of the sms.
143            setResultCode(android.app.Activity.RESULT_OK);
144        }
145    }
146
147}
148