package org.robolectric.shadows;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.Button;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.Robolectric;
import org.robolectric.TestRunners;
import org.robolectric.res.ResName;
import org.robolectric.res.ResourceLoader;
import org.robolectric.res.Style;
import org.robolectric.util.TestUtil;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.robolectric.Robolectric.shadowOf;

@RunWith(TestRunners.WithDefaults.class)
public class ThemeTest {
    @Test public void whenExplicitlySetOnActivity_activityGetsThemeFromActivityInManifest() throws Exception {
        TestActivity activity = new TestActivity();
        activity.setTheme(R.style.Theme_Robolectric);
        shadowOf(activity).callOnCreate(null);
        System.out.println("theme: " + shadowOf(activity).callGetThemeResId());
        Button theButton = (Button) activity.findViewById(R.id.button);
        assertThat(theButton.getBackground()).isEqualTo(new ColorDrawable(0xff00ff00));
    }

    @Test public void whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest() throws Exception {
        TestActivity activity = Robolectric.buildActivity(TestActivity.class).create().get();
        System.out.println("theme: " + shadowOf(activity).callGetThemeResId());
        Button theButton = (Button) activity.findViewById(R.id.button);
        assertThat(theButton.getBackground()).isEqualTo(new ColorDrawable(0xff00ff00));
    }

    @Test public void whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest() throws Exception {
        TestActivity activity = Robolectric.buildActivity(TestActivityWithAnotherTheme.class).create().get();
        System.out.println("theme: " + shadowOf(activity).callGetThemeResId());
        Button theButton = (Button) activity.findViewById(R.id.button);
        assertThat(theButton.getBackground()).isEqualTo(new ColorDrawable(0xffff0000));
    }

    @Test public void shouldResolveReferencesThatStartWithAQuestionMark() throws Exception {
        TestActivity activity = Robolectric.buildActivity(TestActivityWithAnotherTheme.class).create().get();
        System.out.println("theme: " + shadowOf(activity).callGetThemeResId());
        Button theButton = (Button) activity.findViewById(R.id.button);
        assertThat(theButton.getMinWidth()).isEqualTo(42); // via AnotherTheme.Button -> logoWidth and logoHeight
//        assertThat(theButton.getMinHeight()).isEqualTo(42); todo 2.0-cleanup
    }

    @Test public void shouldLookUpStylesFromStyleResId() throws Exception {
        TestActivity activity = Robolectric.buildActivity(TestActivityWithAnotherTheme.class).create().get();
        TypedArray a = activity.obtainStyledAttributes(null, R.styleable.CustomView, 0, R.style.MyCustomView);
        boolean enabled = a.getBoolean(R.styleable.CustomView_aspectRatioEnabled, false);
        assertThat(enabled).isTrue();
    }

    @Test public void shouldInheritThemeValuesFromImplicitParents() throws Exception {
        TestActivity activity = Robolectric.buildActivity(TestActivityWithAnotherTheme.class).create().get();
        ResourceLoader resourceLoader = Robolectric.shadowOf(activity.getResources()).getResourceLoader();
        Style style = ShadowAssetManager.resolveStyle(resourceLoader,
                new ResName(TestUtil.TEST_PACKAGE, "style", "Widget.AnotherTheme.Button.Blarf"), "");
        assertThat(style.getAttrValue(new ResName("android", "attr", "background")).value)
                .isEqualTo("#ffff0000");
    }

    public static class TestActivity extends Activity {
        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.styles_button_layout);
        }
    }

    public static class TestActivityWithAnotherTheme extends TestActivity {
        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.styles_button_layout);
        }
    }
}

@@ -953,88 +953,8 @@
 l);%0A
-        System.out.println(%22theme: %22 + shadowOf(activity).callGetThemeResId());%0A
     
@@ -1013,32 +1013,32 @@
 d(R.id.button);%0A
+
         assertTh
@@ -1325,88 +1325,8 @@
 ();%0A
-        System.out.println(%22theme: %22 + shadowOf(activity).callGetThemeResId());%0A
     
@@ -1719,88 +1719,8 @@
 ();%0A
-        System.out.println(%22theme: %22 + shadowOf(activity).callGetThemeResId());%0A
     
@@ -2089,88 +2089,8 @@
 ();%0A
-        System.out.println(%22theme: %22 + shadowOf(activity).callGetThemeResId());%0A
     
@@ -3396,32 +3396,1042 @@
 f0000%22);%0A    %7D%0A%0A
+//    @Test public void shouldPerformFastly() throws Exception %7B%0A//        TestActivity activity = Robolectric.buildActivity(TestActivityWithAnotherTheme.class).create().get();%0A//        Class type = type(%22com.android.internal.R$styleable%22).withClassLoader(TestActivity.class.getClassLoader()).load();%0A//        int%5B%5D styleableIds = field(%22TextView%22).ofType(int%5B%5D.class).in(type).get();%0A//        // once for warm-up%0A//        long start = System.currentTimeMillis();%0A//        activity.obtainStyledAttributes(null, styleableIds, 0, R.style.MyCustomView);%0A//        System.out.println(String.format(%22Warm-up took %254dms%22, System.currentTimeMillis() - start));%0A//%0A//        start = System.currentTimeMillis();%0A//        int count = 5000;%0A//        for (int i = 0; i %3C count; i++) %7B%0A//            activity.obtainStyledAttributes(null, styleableIds, 0, R.style.MyCustomView);%0A//        %7D%0A//        System.out.println(String.format(%22Took %254.1fms%22, (System.currentTimeMillis() - start) / ((float) count)));%0A//    %7D%0A%0A
     public stati

Remove some debug output.