/*
 * Copyright (C) 2015 Twitter, 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.twitter.sdk.android.tweetui;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

import com.twitter.sdk.android.core.models.TweetBuilder;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.mockito.Mockito.*;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
public class OnShareButtonClickListenerTest extends EnglishLocaleTestCase {

    private static final String REQUIRED_SEND_ACTION = Intent.ACTION_SEND;
    private static final String REQUIRED_MIME_TYPE = "text/plain";
    private static final String A_SHARE_SUBJECT =
            "Tweet from " + TestFixtures.TEST_NAME + " (@" + TestFixtures.TEST_SCREEN_NAME +  ")";
    private static final String A_SHARE_TEXT
            = "Check out @" + TestFixtures.TEST_SCREEN_NAME + "'s Tweet: https://twitter.com/" +
            TestFixtures.TEST_SCREEN_NAME + "/status/" + TestFixtures.TEST_TWEET.id;

    private OnShareButtonClickListener listener;
    private Resources resources;

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        listener = new OnShareButtonClickListener(TestFixtures.TEST_TWEET);
        resources = getContext().getResources();
    }

    @Test
    public void testOnClick_nullTweet() {
        final OnShareButtonClickListener listener = new OnShareButtonClickListener(null);
        final Context context = mock(Context.class);
        listener.onClick(context, resources);
        verify(context, times(0)).startActivity(any(Intent.class));
    }

    @Test
    public void testOnClick_nullTweetUser() {
        final OnShareButtonClickListener listener = new OnShareButtonClickListener(
                new TweetBuilder().build());
        final Context context = mock(Context.class);
        listener.onClick(context, resources);
        verify(context, times(0)).startActivity(any(Intent.class));
    }

    @Test
    public void testOnClick_tweetWithData() {
        final Context context = mock(Context.class);
        listener.onClick(context, resources);
        verify(context, times(1)).startActivity(any(Intent.class));
    }

    @Test
    public void testGetShareContent() {
        final String shareContent = listener.getShareContent(getContext().getResources());
        assertEquals(A_SHARE_TEXT, shareContent);
    }

    @Test
    public void testGetShareSubject() {
        final String shareSubject = listener.getShareSubject(getContext().getResources());
        assertEquals(A_SHARE_SUBJECT, shareSubject);
    }

    @Test
    public void testLaunchShareIntent_startsActivity() {
        final Intent intent = mock(Intent.class);
        final Context context = mock(Context.class);
        listener.launchShareIntent(intent, context);
        verify(context, times(1)).startActivity(intent);
    }

    @Test
    public void testGetShareIntent() {
        final Intent intent = listener.getShareIntent(A_SHARE_SUBJECT, A_SHARE_TEXT);
        assertEquals(REQUIRED_SEND_ACTION, intent.getAction());
        assertEquals(REQUIRED_MIME_TYPE, intent.getType());
        assertEquals(A_SHARE_SUBJECT, intent.getStringExtra(Intent.EXTRA_SUBJECT));
        assertEquals(A_SHARE_TEXT, intent.getStringExtra(Intent.EXTRA_TEXT));
    }
}

@@ -704,16 +704,97 @@
 Intent;%0A
+import android.content.pm.PackageManager;%0Aimport android.content.pm.ResolveInfo;%0A
 import a
@@ -823,16 +823,16 @@
 ources;%0A
-
 %0Aimport 
@@ -1058,16 +1058,68 @@
 onfig;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import s
@@ -1609,17 +1609,16 @@
 N_NAME +
- 
  %22)%22;%0A  
@@ -2913,34 +2913,48 @@
 ntext = 
-mock(
+create
 Context
-.class
+WithPackageManager(
 );%0A     
@@ -3617,34 +3617,48 @@
 ntext = 
-mock(
+create
 Context
-.class
+WithPackageManager(
 );%0A     
@@ -4108,24 +4108,24 @@
 _SUBJECT));%0A
-
         asse
@@ -4195,11 +4195,478 @@
 );%0A    %7D
+%0A%0A    private Context createContextWithPackageManager() %7B%0A        final Context context = mock(Context.class);%0A        final PackageManager pm = mock(PackageManager.class);%0A        final List%3CResolveInfo%3E activities = new ArrayList%3C%3E();%0A        activities.add(mock(ResolveInfo.class));%0A%0A        when(pm.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(activities);%0A        when(context.getPackageManager()).thenReturn(pm);%0A%0A        return context;%0A    %7D
 %0A%7D%0A

Fix broken JVM test for OnShareButtonClickListener