Home | History | Annotate | Download | only in email
      1 /*
      2  * Copyright (C) 2010 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;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import com.android.emailcommon.VendorPolicyLoader;
     25 import com.android.emailcommon.VendorPolicyLoader.Provider;
     26 
     27 @SmallTest
     28 public class VendorPolicyLoaderTest extends AndroidTestCase {
     29     private String mTestApkPackageName;
     30 
     31     @Override
     32     protected void setUp() throws Exception {
     33         super.setUp();
     34         mTestApkPackageName = getContext().getPackageName() + ".tests";
     35     }
     36 
     37     @Override
     38     protected void tearDown() throws Exception {
     39         super.tearDown();
     40         VendorPolicyLoader.clearInstanceForTest();
     41     }
     42 
     43     /**
     44      * Test for the case where the helper package doesn't exist.
     45      */
     46     public void testPackageNotExist() {
     47         VendorPolicyLoader pl = new VendorPolicyLoader(getContext(), "no.such.package",
     48                 "no.such.Class", true);
     49 
     50         // getPolicy() shouldn't throw any exception.
     51         assertEquals(Bundle.EMPTY, pl.getPolicy(null, null));
     52     }
     53 
     54     public void testIsSystemPackage() {
     55         final Context c = getContext();
     56         assertEquals(false, VendorPolicyLoader.isSystemPackage(c, "no.such.package"));
     57         assertEquals(false, VendorPolicyLoader.isSystemPackage(c, mTestApkPackageName));
     58         assertEquals(true, VendorPolicyLoader.isSystemPackage(c, "com.android.settings"));
     59     }
     60 
     61     /**
     62      * Actually call {@link VendorPolicyLoader#getPolicy}, using MockVendorPolicy as a vendor
     63      * policy.
     64      */
     65     public void testGetPolicy() {
     66         MockVendorPolicy.inject(getContext());
     67         VendorPolicyLoader pl = VendorPolicyLoader.getInstance(getContext());
     68 
     69         // Prepare result
     70         Bundle result = new Bundle();
     71         result.putInt("ret", 1);
     72         MockVendorPolicy.mockResult = result;
     73 
     74         // Arg to pass
     75         Bundle args = new Bundle();
     76         args.putString("arg1", "a");
     77 
     78         // Call!
     79         Bundle actualResult = pl.getPolicy("policy1", args);
     80 
     81         // Check passed args
     82         assertEquals("policy", "policy1", MockVendorPolicy.passedPolicy);
     83         assertEquals("arg", "a", MockVendorPolicy.passedBundle.getString("arg1"));
     84 
     85         // Check return value
     86         assertEquals("result", 1, actualResult.getInt("ret"));
     87     }
     88 
     89     /**
     90      * Same as {@link #testGetPolicy}, but with the system-apk check.  It's a test for the case
     91      * where we have a non-system vendor policy installed, which shouldn't be used.
     92      */
     93     public void testGetPolicyNonSystem() {
     94         VendorPolicyLoader pl = new VendorPolicyLoader(getContext(), mTestApkPackageName,
     95                 MockVendorPolicy.class.getName(), false);
     96 
     97         MockVendorPolicy.passedPolicy = null;
     98 
     99         // getPolicy() shouldn't throw any exception.
    100         assertEquals(Bundle.EMPTY, pl.getPolicy("policy1", null));
    101 
    102         // MockVendorPolicy.getPolicy() shouldn't get called.
    103         assertNull(MockVendorPolicy.passedPolicy);
    104     }
    105 
    106     /**
    107      * Test that any vendor policy that happens to be installed returns legal values
    108      * for getImapIdValues() per its API.
    109      *
    110      * Note, in most cases very little will happen in this test, because there is
    111      * no vendor policy package.  Most of this test exists to test a vendor policy
    112      * package itself, to make sure that its API returns reasonable values.
    113      */
    114     public void testGetImapIdValues() {
    115         VendorPolicyLoader pl = VendorPolicyLoader.getInstance(getContext());
    116         String id = pl.getImapIdValues("user-name", "server.yahoo.com",
    117                 "IMAP4rev1 STARTTLS AUTH=GSSAPI");
    118         // null is a reasonable result
    119         if (id == null) return;
    120 
    121         // if non-null, basic sanity checks on format
    122         assertEquals("\"", id.charAt(0));
    123         assertEquals("\"", id.charAt(id.length()-1));
    124         // see if we can break it up properly
    125         String[] elements = id.split("\"");
    126         assertEquals(0, elements.length % 4);
    127         for (int i = 0; i < elements.length; ) {
    128             // Because we split at quotes, we expect to find:
    129             // [i] = null or one or more spaces
    130             // [i+1] = key
    131             // [i+2] = one or more spaces
    132             // [i+3] = value
    133             // Here are some incomplete checks of the above
    134             assertTrue(elements[i] == null || elements[i].startsWith(" "));
    135             assertTrue(elements[i+1].charAt(0) != ' ');
    136             assertTrue(elements[i+2].startsWith(" "));
    137             assertTrue(elements[i+3].charAt(0) != ' ');
    138             i += 4;
    139         }
    140     }
    141 
    142     /**
    143      * Test that findProviderForDomain() returns legal values, or functions properly when
    144      * none is installed.
    145      */
    146     public void testFindProviderForDomain() {
    147         VendorPolicyLoader pl = VendorPolicyLoader.getInstance(getContext());
    148         Provider p = pl.findProviderForDomain("yahoo.com");
    149         // null is a reasonable result (none installed)
    150         if (p == null) return;
    151 
    152         // if non-null, basic sanity checks on format
    153         assertNull(p.id);
    154         assertNull(p.label);
    155         assertEquals("yahoo.com", p.domain);
    156         assertNotNull(p.incomingUriTemplate);
    157         assertNotNull(p.incomingUsernameTemplate);
    158         assertNotNull(p.outgoingUriTemplate);
    159         assertNotNull(p.outgoingUsernameTemplate);
    160         assertTrue(p.note == null || p.note.length() > 0);  // no empty string
    161     }
    162 }
    163