1/*
2 * Copyright (C) 2011 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.activity;
18
19import com.android.emailcommon.utility.IntentUtilities;
20
21import android.content.Intent;
22import android.net.Uri;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25
26@SmallTest
27public class IntentUtilitiesTests extends AndroidTestCase {
28    public void brokentestSimple() {
29        final Uri.Builder b = IntentUtilities.createActivityIntentUrlBuilder("/abc");
30        IntentUtilities.setAccountId(b, 10);
31        IntentUtilities.setMailboxId(b, 20);
32        IntentUtilities.setMessageId(b, 30);
33        IntentUtilities.setAccountUuid(b, "*uuid*");
34
35        final Uri u = b.build();
36        assertEquals("content", u.getScheme());
37        assertEquals("ui.email.android.com", u.getAuthority());
38        assertEquals("/abc", u.getPath());
39
40        final Intent i = new Intent(Intent.ACTION_MAIN, u);
41        assertEquals(10, IntentUtilities.getAccountIdFromIntent(i));
42        assertEquals(20, IntentUtilities.getMailboxIdFromIntent(i));
43        assertEquals(30, IntentUtilities.getMessageIdFromIntent(i));
44        assertEquals("*uuid*", IntentUtilities.getAccountUuidFromIntent(i));
45    }
46
47    public void brokentestGetIdFromIntent() {
48        Intent i;
49
50        // No URL in intent
51        i = new Intent(getContext(), getClass());
52        assertEquals(-1, IntentUtilities.getAccountIdFromIntent(i));
53        assertEquals(-1, IntentUtilities.getMailboxIdFromIntent(i));
54        assertEquals(-1, IntentUtilities.getMessageIdFromIntent(i));
55
56        // No param
57        checkGetIdFromIntent("content://s/", -1);
58
59        // No value
60        checkGetIdFromIntent("content://s/?ID=", -1);
61
62        // Value not integer
63        checkGetIdFromIntent("content://s/?ID=x", -1);
64
65        // Negative value
66        checkGetIdFromIntent("content://s/?ID=-100", -100);
67
68        // Normal value
69        checkGetIdFromIntent("content://s/?ID=200", 200);
70
71        // With all 3 params
72        i = new Intent(Intent.ACTION_VIEW, Uri.parse(
73                "content://s/?ACCOUNT_ID=5&MAILBOX_ID=6&MESSAGE_ID=7"));
74        assertEquals(5, IntentUtilities.getAccountIdFromIntent(i));
75        assertEquals(6, IntentUtilities.getMailboxIdFromIntent(i));
76        assertEquals(7, IntentUtilities.getMessageIdFromIntent(i));
77    }
78
79    public void checkGetIdFromIntent(String uri, long expected) {
80        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri.replace("ID", "ACCOUNT_ID")));
81        assertEquals(expected, IntentUtilities.getAccountIdFromIntent(i));
82
83        i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri.replace("ID", "MAILBOX_ID")));
84        assertEquals(expected, IntentUtilities.getMailboxIdFromIntent(i));
85
86        i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri.replace("ID", "MESSAGE_ID")));
87        assertEquals(expected, IntentUtilities.getMessageIdFromIntent(i));
88    }
89
90    public void brokentestGetAccountUuidFromIntent() {
91        Intent i;
92
93        // No URL in intent
94        i = new Intent(getContext(), getClass());
95        assertEquals(null, IntentUtilities.getAccountUuidFromIntent(i));
96
97        // No param
98        i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://s/"));
99        assertEquals(null, IntentUtilities.getAccountUuidFromIntent(i));
100
101        // No value
102        i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://s/?ACCOUNT_UUID="));
103        assertEquals(null, IntentUtilities.getAccountUuidFromIntent(i));
104
105        // With valid UUID
106        i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://s/?ACCOUNT_UUID=xyz"));
107        assertEquals("xyz", IntentUtilities.getAccountUuidFromIntent(i));
108    }
109}
110