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