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