package de.nenick.espressomacchiato.assertions.support;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.v7.widget.Toolbar;
import android.view.View;

import static org.hamcrest.Matchers.is;

public class AppBarLayoutAssertion {

    public static ViewAssertion assertIsExpanded() {
        return new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                AppBarLayout appBarLayout = (AppBarLayout) view;
                boolean isFullExpanded = appBarLayout.getHeight() - appBarLayout.getBottom() == 0;
                ViewMatchers.assertThat("is full expanded", isFullExpanded, is(true));
            }
        };
    }


    public static ViewAssertion assertIsCollapsed() {
        return new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                CollapsingToolbarLayout collapsingToolbarLayout = null;
                Toolbar toolbar = null;
                AppBarLayout appBarLayout = (AppBarLayout) view;

                for (int i = 0; i < appBarLayout.getChildCount(); i++) {
                    if(appBarLayout.getChildAt(i) instanceof CollapsingToolbarLayout) {
                        collapsingToolbarLayout = (CollapsingToolbarLayout) appBarLayout.getChildAt(i);
                    }
                }

                if(collapsingToolbarLayout == null) {
                    throw new IllegalStateException("Current only AppBarLayouts with CollapsingToolbarLayout are supported.");
                }

                for (int i = 0; i < collapsingToolbarLayout.getChildCount(); i++) {
                    if(collapsingToolbarLayout.getChildAt(i) instanceof Toolbar) {
                        toolbar = (Toolbar) collapsingToolbarLayout.getChildAt(i);
                    }
                }

                if(toolbar == null) {
                    throw new IllegalStateException("Current only CollapsingToolbarLayout with support Toolbar are supported.");
                }

                boolean isFullCollapsed = appBarLayout.getBottom() - toolbar.getHeight() == 0;
                ViewMatchers.assertThat("is full collapsed", isFullCollapsed, is(true));
            }
        };
    }
}

@@ -50,16 +50,48 @@
 pport;%0A%0A
+import android.content.Context;%0A
 import a
@@ -437,16 +437,75 @@
 .View;%0A%0A
+import de.nenick.espressomacchiato.tools.EspResourceTool;%0A%0A
 import s
@@ -2538,79 +2538,601 @@
     
-ViewMatchers.assertThat(%22is full collapsed%22, isFullCollapsed, is(true))
+isFullCollapsed %7C= appBarLayout.getBottom() - toolbar.getHeight() - getStatusBarHeight(view.getContext()) == 0;%0A                ViewMatchers.assertThat(%22is full collapsed%22, isFullCollapsed, is(true));%0A            %7D%0A%0A            public int getStatusBarHeight(Context context) %7B%0A                int result = 0;%0A                int resourceId = context.getResources().getIdentifier(%22status_bar_height%22, %22dimen%22, %22android%22);%0A                if (resourceId %3E 0) %7B%0A                    result = context.getResources().getDimensionPixelSize(resourceId);%0A                %7D%0A                return result
 ;%0A  

support check for collapsed toolbar when fitSystemWindows