/*
 * Copyright 2012 GitHub Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.mobile.tests.issue;

import static com.github.mobile.Intents.EXTRA_ISSUE_NUMBERS;
import static com.github.mobile.Intents.EXTRA_REPOSITORY;
import android.content.Intent;
import android.net.Uri;
import android.test.AndroidTestCase;

import com.github.mobile.core.issue.IssueUriMatcher;

import org.eclipse.egit.github.core.Repository;

/**
 * Unit tests of {@link IssueUriMatcher}
 */
public class IssueUriMatcherTest extends AndroidTestCase {

    /**
     * Verify empty uri
     */
    public void testEmptyUri() {
        assertNull(IssueUriMatcher.getIssueIntent(Uri.parse("").getPathSegments()));
    }

    /**
     * Verify non-numeric issue number in uri
     */
    public void testNonNumericIssueNumber() {
        assertNull(IssueUriMatcher.getIssueIntent(Uri
                .parse("https://github.com/defunkt/resque/issues/fourty").getPathSegments()));
    }

    /**
     * Verify http uri
     */
    public void testHttpUri() {
        Intent intent = IssueUriMatcher.getIssueIntent(Uri
                .parse("https://github.com/defunkt/resque/issues/3").getPathSegments());
        assertNotNull(intent);
        assertEquals(3, intent.getIntArrayExtra(EXTRA_ISSUE_NUMBERS)[0]);
        Repository repo = (Repository) intent.getSerializableExtra(EXTRA_REPOSITORY);
        assertNotNull(repo);
        assertEquals("resque", repo.getName());
        assertNotNull(repo.getOwner());
        assertEquals("defunkt", repo.getOwner().getLogin());
    }

    /**
     * Verify pull uri
     */
    public void testPullUri() {
        Intent intent = IssueUriMatcher.getIssueIntent(Uri
                .parse("https://github.com/defunkt/resque/pull/3").getPathSegments());
        assertNotNull(intent);
        assertEquals(3, intent.getIntArrayExtra(EXTRA_ISSUE_NUMBERS)[0]);
        Repository repo = (Repository) intent.getSerializableExtra(EXTRA_REPOSITORY);
        assertNotNull(repo);
        assertEquals("resque", repo.getName());
        assertNotNull(repo.getOwner());
        assertEquals("defunkt", repo.getOwner().getLogin());
    }

    /**
     * Verify https uri
     */
    public void testHttpsUri() {
        Intent intent = IssueUriMatcher.getIssueIntent(Uri
                .parse("http://github.com/defunkt/resque/issues/15").getPathSegments());
        assertNotNull(intent);
        assertEquals(15, intent.getIntArrayExtra(EXTRA_ISSUE_NUMBERS)[0]);
        Repository repo = (Repository) intent.getSerializableExtra(EXTRA_REPOSITORY);
        assertNotNull(repo);
        assertEquals("resque", repo.getName());
        assertNotNull(repo.getOwner());
        assertEquals("defunkt", repo.getOwner().getLogin());
    }

    /**
     * Verify uri with comment fragment
     */
    public void testCommentUri() {
        Intent intent = IssueUriMatcher.getIssueIntent(Uri
                .parse("https://github.com/defunkt/resque/issues/300#issuecomment-123456").getPathSegments());
        assertNotNull(intent);
        assertEquals(300, intent.getIntArrayExtra(EXTRA_ISSUE_NUMBERS)[0]);
        Repository repo = (Repository) intent.getSerializableExtra(EXTRA_REPOSITORY);
        assertNotNull(repo);
        assertEquals("resque", repo.getName());
        assertNotNull(repo.getOwner());
        assertEquals("defunkt", repo.getOwner().getLogin());
    }
}

@@ -889,16 +889,65 @@
 Matcher;
+%0Aimport com.github.mobile.ui.UriLauncherActivity;
 %0A%0Aimport
@@ -1394,38 +1394,43 @@
 ull(
-IssueUriMatcher.getIssueIntent
+UriLauncherActivity.getIntentForURI
 (Uri
@@ -1503,34 +1503,16 @@
 fourty%22)
-.getPathSegments()
 ));%0A    
@@ -1613,38 +1613,43 @@
 t = 
-IssueUriMatcher.getIssueIntent
+UriLauncherActivity.getIntentForURI
 (Uri
@@ -1717,34 +1717,16 @@
 sues/3%22)
-.getPathSegments()
 );%0A     
@@ -1740,32 +1740,107 @@
 rtNotNull(intent
+);%0A        assertEquals(intent.getAction(), %22jp.forkhub.mobile.issues.VIEW%22
 );%0A        asser
@@ -2270,38 +2270,43 @@
 t = 
-IssueUriMatcher.getIssueIntent
+UriLauncherActivity.getIntentForURI
 (Uri
@@ -2372,34 +2372,16 @@
 pull/3%22)
-.getPathSegments()
 );%0A     
@@ -2395,32 +2395,107 @@
 rtNotNull(intent
+);%0A        assertEquals(intent.getAction(), %22jp.forkhub.mobile.issues.VIEW%22
 );%0A        asser
@@ -2927,38 +2927,43 @@
 t = 
-IssueUriMatcher.getIssueIntent
+UriLauncherActivity.getIntentForURI
 (Uri
@@ -3031,34 +3031,16 @@
 ues/15%22)
-.getPathSegments()
 );%0A     
@@ -3054,32 +3054,107 @@
 rtNotNull(intent
+);%0A        assertEquals(intent.getAction(), %22jp.forkhub.mobile.issues.VIEW%22
 );%0A        asser
@@ -3605,38 +3605,43 @@
 t = 
-IssueUriMatcher.getIssueIntent
+UriLauncherActivity.getIntentForURI
 (Uri
@@ -3731,34 +3731,16 @@
 123456%22)
-.getPathSegments()
 );%0A     
@@ -3754,32 +3754,107 @@
 rtNotNull(intent
+);%0A        assertEquals(intent.getAction(), %22jp.forkhub.mobile.issues.VIEW%22
 );%0A        asser

Improve error detection in IssueUriMatcherTest