Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] Color utility to resolving material theme color attrs programmatically #1656

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added test for MaterialColorUtils
  • Loading branch information
gabrielemariotti committed Aug 26, 2020
commit 7b5cba4b9a5d57963847ef805bba27b1406893fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 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.
-->

<manifest xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.material.color">

<uses-sdk
tools:overrideLibrary="androidx.test.core"/>

<application/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 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 com.google.android.material.color;

import android.content.Context;
import android.view.View;
import androidx.annotation.AttrRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.material.R;
import com.google.android.material.button.MaterialButton;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.internal.DoNotInstrument;

import static com.google.common.truth.Truth.assertThat;

/** Tests for the Material themes. */
@RunWith(RobolectricTestRunner.class)
@DoNotInstrument
public class MaterialColorUtilsTest {

private static final String TAG = MaterialColorUtilsTest.class.getSimpleName();

private AppCompatActivity activity;
private final Context context = ApplicationProvider.getApplicationContext();

@Before
public void themeApplicationContext() {
context.setTheme(R.style.Theme_MaterialComponents_Light_NoActionBar_Bridge);
activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
}

@Test
public void testMaterialColor_usingContext() {
context.setTheme(R.style.Theme_MaterialComponents_Light);
assertThat(MaterialColorUtils.colorPrimary(context)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorPrimaryVariant(context)).isEqualTo(getColor(R.attr.colorPrimaryVariant));
assertThat(MaterialColorUtils.colorOnPrimary(context)).isEqualTo(getColor(R.attr.colorOnPrimary));
assertThat(MaterialColorUtils.colorSecondary(context)).isEqualTo(getColor(R.attr.colorSecondary));
assertThat(MaterialColorUtils.colorSecondaryVariant(context)).isEqualTo(getColor(R.attr.colorSecondaryVariant));
assertThat(MaterialColorUtils.colorOnSecondary(context)).isEqualTo(getColor(R.attr.colorOnSecondary));
assertThat(MaterialColorUtils.colorSurface(context)).isEqualTo(getColor(R.attr.colorSurface));
assertThat(MaterialColorUtils.colorOnSurface(context)).isEqualTo(getColor(R.attr.colorOnSurface));
assertThat(MaterialColorUtils.colorBackground(context)).isEqualTo(getColor(android.R.attr.colorBackground));
assertThat(MaterialColorUtils.colorOnBackground(context)).isEqualTo(getColor(R.attr.colorOnBackground));
assertThat(MaterialColorUtils.colorError(context)).isEqualTo(getColor(R.attr.colorError));
assertThat(MaterialColorUtils.colorOnError(context)).isEqualTo(getColor(R.attr.colorOnError));
}

@Test
public void testMaterialColor_usingView() {
View inflated = activity.getLayoutInflater().inflate(R.layout.test_color_view, null);
MaterialButton view = inflated.findViewById(R.id.viewTestColor);
assertThat(MaterialColorUtils.colorPrimary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorPrimaryVariant(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnPrimary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSecondary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSecondaryVariant(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnSecondary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSurface(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnSurface(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorBackground(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnBackground(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorError(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnError(view)).isEqualTo(getColor(R.attr.colorPrimary));
}

private int getColor(@AttrRes int colorAttributeResId) {
return MaterialColors.getColor(context, colorAttributeResId, TAG);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 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.
-->

<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewTestColor"
style="@style/Widget.MaterialComponents.Button"
android:theme="@style/ThemeOverlay.Test.AllColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="@string/chip_text" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2018 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
~
~ https://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.
-->

<resources xmlns:tools="http://schemas.android.com/tools">

<style name="ThemeOverlay.Test.AllColor" parent="" tools:ignore="NewApi">

<item name="colorPrimaryVariant">?attr/colorPrimary</item>
<item name="colorOnPrimary">?attr/colorPrimary</item>
<item name="colorSecondary">?attr/colorPrimary</item>
<item name="colorOnSecondary">?attr/colorPrimary</item>
<item name="colorSecondaryVariant">?attr/colorPrimary</item>
<item name="colorSurface">?attr/colorPrimary</item>
<item name="colorOnSurface">?attr/colorPrimary</item>
<item name="android:colorBackground">?attr/colorPrimary</item>
<item name="colorOnBackground">?attr/colorPrimary</item>
<item name="colorError">?attr/colorPrimary</item>
<item name="colorOnError">?attr/colorPrimary</item>
</style>

</resources>