package com.mobify.astro.plugins;

import android.support.test.annotation.UiThreadTest;
import android.view.View;
import android.webkit.WebView;

import com.mobify.astro.ActivityTestBase;
import com.mobify.astro.PluginResolver;
import com.mobify.astro.messaging.EventManager;
import com.mobify.astro.messaging.MessageSender;
import com.mobify.astro.plugins.webviewplugin.AstroWebView;
import com.mobify.astro.plugins.webviewplugin.LoadingContainerView;
import com.mobify.astro.plugins.webviewplugin.WebViewPlugin;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

public class NavigationStackFrameTest extends ActivityTestBase {

    View nativeView;
    AstroWebView astroWebView;
    PluginResolver mockPluginResolver;
    EventManager mockEventManager;
    MessageSender mockMessageSender;

    @UiThreadTest
    @Before
    public void setUp() throws Exception {
        nativeView = new View(getActivity());
        astroWebView = new AstroWebView(getActivity());
        mockPluginResolver = mock(PluginResolver.class);
        mockEventManager = mock(EventManager.class);
        mockMessageSender = mock(MessageSender.class);
    }

    @UiThreadTest
    @Test
    public void testCreateStackFrameWithNativeView() {
        NavigationStackFrame stackFrame = new NavigationPlugin.DefaultNavigationStackFrame(nativeView);
        assertEquals(stackFrame.getView(), nativeView);
    }

    @UiThreadTest
    @Test
    public void testCreateStackFrameWithWebView() {
        NavigationStackFrame  stackFrame = new WebViewPlugin(getActivity(), mockPluginResolver, mockEventManager, mockMessageSender);

        WebViewPlugin webViewPluginFrame = (WebViewPlugin) stackFrame;
        assertNotNull(webViewPluginFrame.getWebView());
        assertNotNull(stackFrame.getView());
        assertTrue(stackFrame.getView() instanceof LoadingContainerView);
        assertFalse(stackFrame.getView() instanceof WebView);
    }

    @UiThreadTest
    @Test
    public void testGetView() {
        NavigationStackFrame frameWithWebView = new NavigationPlugin.DefaultNavigationStackFrame(astroWebView);
        NavigationStackFrame frameWithNativeView = new NavigationPlugin.DefaultNavigationStackFrame(nativeView);

        try {
            assertEquals(astroWebView, frameWithWebView.getView());
            assertEquals(nativeView, frameWithNativeView.getView());
        } catch (Exception e) {
            fail();
        }
    }
}
