/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 android.support.v17.leanback.media;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.times;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class PlaybackGlueTest {


    public static class PlaybackGlueImpl extends PlaybackGlue {

        public PlaybackGlueImpl(Context context) {
            super(context);
        }
    }

    public static class PlaybackGlueHostImpl extends PlaybackGlueHost {

        HostCallback mHostCallback;

        @Override
        public void setHostCallback(HostCallback callback) {
            mHostCallback = callback;
        }

        void notifyOnStart() {
            if (mHostCallback != null) {
                mHostCallback.onHostStart();
            }
        }

        void notifyOnStop() {
            if (mHostCallback != null) {
                mHostCallback.onHostStop();
            }
        }

        void notifyOnResume() {
            if (mHostCallback != null) {
                mHostCallback.onHostResume();
            }
        }

        void notifyOnPause() {
            if (mHostCallback != null) {
                mHostCallback.onHostPause();
            }
        }

        void notifyOnDestroy() {
            if (mHostCallback != null) {
                mHostCallback.onHostDestroy();
            }
        }
    }

    @Test
    public void glueAndHostInteraction() {
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        PlaybackGlue glue = Mockito.spy(new PlaybackGlueImpl(context));
        PlaybackGlueHostImpl host = Mockito.spy(new PlaybackGlueHostImpl());

        glue.setHost(host);
        Mockito.verify(glue, times(1)).onAttachedToHost(host);
        assertSame(glue, host.mGlue);
        assertSame(host, glue.getHost());

        host.notifyOnStart();
        Mockito.verify(glue, times(1)).onHostStart();

        host.notifyOnResume();
        Mockito.verify(glue, times(1)).onHostResume();

        host.notifyOnPause();
        Mockito.verify(glue, times(1)).onHostPause();

        host.notifyOnStop();
        Mockito.verify(glue, times(1)).onHostStop();

        PlaybackGlue glue2 = Mockito.spy(new PlaybackGlueImpl(context));
        glue2.setHost(host);
        Mockito.verify(glue, times(1)).onDetachedFromHost();
        Mockito.verify(glue2, times(1)).onAttachedToHost(host);
        assertSame(glue2, host.mGlue);
        assertSame(host, glue2.getHost());
        assertNull(glue.getHost());

        host.notifyOnDestroy();
        assertNull(glue2.getHost());
        assertNull(host.mGlue);
        Mockito.verify(glue2, times(1)).onDetachedFromHost();
    }

}

@@ -2501,28 +2501,16 @@
  host = 
-Mockito.spy(
 new Play
@@ -2527,17 +2527,16 @@
 stImpl()
-)
 ;%0A%0A     

Fix PlaybackGlueTest on v17