1 /*******************************************************************************
2  *      Copyright (C) 2014 Google Inc.
3  *      Licensed to The Android Open Source Project.
4  *
5  *      Licensed under the Apache License, Version 2.0 (the "License");
6  *      you may not use this file except in compliance with the License.
7  *      You may obtain a copy of the License at
8  *
9  *           http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *      Unless required by applicable law or agreed to in writing, software
12  *      distributed under the License is distributed on an "AS IS" BASIS,
13  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *      See the License for the specific language governing permissions and
15  *      limitations under the License.
16  *******************************************************************************/
17 
18 package com.android.mail.browse;
19 
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.ContextWrapper;
23 import android.net.Uri;
24 import android.os.ParcelFileDescriptor;
25 import android.provider.OpenableColumns;
26 import android.test.IsolatedContext;
27 import android.test.LoaderTestCase;
28 import android.test.mock.MockContentResolver;
29 import android.test.suitebuilder.annotation.SmallTest;
30 
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.util.HashMap;
35 
36 public class EmlMessageLoaderTest extends LoaderTestCase {
37     private TestEmlProvider mTestProvider;
38 
39     @Override
setUp()40     protected void setUp() throws Exception {
41         super.setUp();
42         MockContentResolver resolver = new MockContentResolver();
43         IsolatedContext context = new IsolatedContext(resolver, getContext());
44         Context wrappedContext = new ContextWrapper(context) {
45             @Override
46             public Context getApplicationContext() {
47                 return this;
48             }
49         };
50         setContext(wrappedContext);
51         mTestProvider = new TestEmlProvider(wrappedContext);
52         resolver.addProvider(TestEmlProvider.AUTHORITY, mTestProvider);
53     }
54 
55     @SmallTest
testLoadingBlankEml()56     public void testLoadingBlankEml() throws Exception {
57         final Uri emptyEmlUri = TestProvider.uri(
58                 new Uri.Builder().scheme("content").authority("empty").path("empty").build());
59         final ContentValues cv = new ContentValues(2);
60         cv.put(OpenableColumns.DISPLAY_NAME, "Empty.eml");
61         cv.put(OpenableColumns.SIZE, 0);
62         mTestProvider.insert(emptyEmlUri, cv);
63         final OutputStream out = new ParcelFileDescriptor.AutoCloseOutputStream(
64                 mTestProvider.makePipeForUri(emptyEmlUri));
65         out.write(0);
66         out.close();
67 
68         EmlMessageLoader loader = new EmlMessageLoader(getContext(), emptyEmlUri);
69 
70         getLoaderResultSynchronously(loader);
71         // If we don't crash, the test passed.
72     }
73 
74     public static class TestEmlProvider extends TestProvider {
75         private final HashMap<Uri, ParcelFileDescriptor> mFileMap =
76                 new HashMap<Uri, ParcelFileDescriptor>();
77 
TestEmlProvider(Context context)78         public TestEmlProvider(Context context) {
79             super(context);
80         }
81 
makePipeForUri(Uri uri)82         public ParcelFileDescriptor makePipeForUri(Uri uri) throws IOException {
83             ParcelFileDescriptor[] descriptors = ParcelFileDescriptor.createPipe();
84             mFileMap.put(uri, descriptors[0]);
85             return descriptors[1];
86         }
87 
88         @Override
openFile(Uri uri, String mode)89         public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
90             final ParcelFileDescriptor descriptor = mFileMap.get(uri);
91             if (descriptor != null) {
92                 return descriptor;
93             }
94             return super.openFile(uri, mode);
95         }
96     }
97 }
98