• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.email.activity;
18 
19 import com.android.email.Email;
20 import com.android.email.MessagingController;
21 import com.android.email.R;
22 import com.android.email.mail.internet.BinaryTempFileBody;
23 
24 import android.app.Application;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.Environment;
28 import android.test.ActivityInstrumentationTestCase2;
29 import android.test.suitebuilder.annotation.LargeTest;
30 import android.test.suitebuilder.annotation.Suppress;
31 import android.webkit.WebView;
32 import android.widget.TextView;
33 
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 
40 /**
41  * Various instrumentation tests for MessageCompose.
42  *
43  * It might be possible to convert these to ActivityUnitTest, which would be faster.
44  */
45 @LargeTest
46 public class MessageViewTests
47         extends ActivityInstrumentationTestCase2<MessageView> {
48 
49     // copied from MessageView (could be package class)
50     private static final String EXTRA_MESSAGE_ID = "com.android.email.MessageView_message_id";
51     private static final String EXTRA_MAILBOX_ID = "com.android.email.MessageView_mailbox_id";
52 
53     private TextView mToView;
54     private TextView mSubjectView;
55     private WebView mMessageContentView;
56     private Context mContext;
57 
MessageViewTests()58     public MessageViewTests() {
59         super("com.android.email", MessageView.class);
60     }
61 
62     @Override
setUp()63     protected void setUp() throws Exception {
64         super.setUp();
65 
66         mContext = getInstrumentation().getTargetContext();
67         Email.setServicesEnabled(mContext);
68 
69         // setup an intent to spin up this activity with something useful
70         // Long.MIN_VALUE are sentinels to command MessageView to skip loading
71         Intent i = new Intent()
72             .putExtra(EXTRA_MESSAGE_ID, Long.MIN_VALUE)
73             .putExtra(EXTRA_MAILBOX_ID, Long.MIN_VALUE);
74         this.setActivityIntent(i);
75 
76         // configure a mock controller
77         MessagingController mockController =
78             new MockMessagingController(getActivity().getApplication());
79         MessagingController.injectMockController(mockController);
80 
81         final MessageView a = getActivity();
82         mToView = (TextView) a.findViewById(R.id.to);
83         mSubjectView = (TextView) a.findViewById(R.id.subject);
84         mMessageContentView = (WebView) a.findViewById(R.id.message_content);
85 
86         // This is needed for mime image bodypart.
87         BinaryTempFileBody.setTempDirectory(getActivity().getCacheDir());
88     }
89 
90     /**
91      * The name 'test preconditions' is a convention to signal that if this
92      * test doesn't pass, the test case was not set up properly and it might
93      * explain any and all failures in other tests.  This is not guaranteed
94      * to run before other tests, as junit uses reflection to find the tests.
95      */
testPreconditions()96     public void testPreconditions() {
97         assertNotNull(mToView);
98         assertEquals(0, mToView.length());
99         assertNotNull(mSubjectView);
100         assertEquals(0, mSubjectView.length());
101         assertNotNull(mMessageContentView);
102     }
103 
104     /**
105      * Test that we have the necessary permissions to write to external storage.
106      */
testAttachmentWritePermissions()107     public void testAttachmentWritePermissions() throws FileNotFoundException, IOException {
108         File file = null;
109         try {
110             // If there's no storage available, this test is moot
111             if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
112                 return;
113             }
114             file = MessageView.createUniqueFile(Environment.getExternalStorageDirectory(),
115                     "write-test");
116             OutputStream out = new FileOutputStream(file);
117             out.write(1);
118             out.close();
119         } finally {
120             try {
121                 if (file != null) {
122                     if (file.exists()) {
123                         file.delete();
124                     }
125                 }
126             } catch (Exception e) {
127                 // ignore cleanup error - it still throws the original
128             }
129         }
130     }
131 
132     /**
133      * Tests that various UI calls can be made safely even before the messaging controller
134      * has completed loading the message.  This catches various race conditions.
135      */
136     @Suppress
testUiRaceConditions()137     public void testUiRaceConditions() {
138 
139         MessageView a = getActivity();
140 
141         // on-streen controls
142         a.onClick(a.findViewById(R.id.reply));
143         a.onClick(a.findViewById(R.id.reply_all));
144         a.onClick(a.findViewById(R.id.delete));
145         a.onClick(a.findViewById(R.id.next));
146         a.onClick(a.findViewById(R.id.previous));
147 //      a.onClick(a.findViewById(R.id.download));    // not revealed yet, so unfair test
148 //      a.onClick(a.findViewById(R.id.view));        // not revealed yet, so unfair test
149         a.onClick(a.findViewById(R.id.show_pictures));
150 
151         // menus
152         a.handleMenuItem(R.id.delete);
153         a.handleMenuItem(R.id.reply);
154         a.handleMenuItem(R.id.reply_all);
155         a.handleMenuItem(R.id.forward);
156         a.handleMenuItem(R.id.mark_as_unread);
157     }
158 
159     /**
160      * Mock Messaging controller, so we can drive its callbacks.  This probably should be
161      * generalized since we're likely to use for other tests eventually.
162      */
163     private static class MockMessagingController extends MessagingController {
164 
MockMessagingController(Application application)165         private MockMessagingController(Application application) {
166             super(application);
167         }
168     }
169 }
170