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