Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[M3][Color] Added Color Scheme demo in Catalog.
PiperOrigin-RevId: 408896531
- Loading branch information
Showing
7 changed files
with
440 additions
and
14 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
catalog/androidTest/javatests/io/material/catalog/color/ColorMainDemoFragmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2021 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 io.material.catalog.color; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withText; | ||
|
||
import androidx.fragment.app.Fragment; | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import androidx.test.filters.MediumTest; | ||
import io.material.catalog.main.MainActivity; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** Tests for {@link ColorMainDemoFragment} */ | ||
@MediumTest | ||
@RunWith(AndroidJUnit4.class) | ||
public final class ColorMainDemoFragmentTest { | ||
|
||
@Rule | ||
public final ActivityScenarioRule<MainActivity> activityScenarioRule = | ||
new ActivityScenarioRule<>(MainActivity.class); | ||
|
||
@Before | ||
public void setUpAndLaunchFragment() { | ||
Fragment fragment = new ColorMainDemoFragment(); | ||
|
||
activityScenarioRule | ||
.getScenario() | ||
.onActivity( | ||
activity -> | ||
activity | ||
.getSupportFragmentManager() | ||
.beginTransaction() | ||
.replace(io.material.catalog.feature.R.id.container, fragment) | ||
.commit()); | ||
} | ||
|
||
@Test | ||
public void checkColorRowTextValueIsShown() { | ||
onView(withText(R.string.cat_color_role_primary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_primary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_primary_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_primary_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_inverse_primary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_secondary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_secondary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_secondary_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_secondary_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_tertiary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_tertiary)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_tertiary_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_tertiary_container)).check(matches(isDisplayed())); | ||
|
||
onView(withText(R.string.cat_color_role_error)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_error)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_error_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_error_container)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_outline)).check(matches(isDisplayed())); | ||
|
||
onView(withText(R.string.cat_color_role_background)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_background)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_surface)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_surface)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_surface_variant)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_on_surface_variant)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_inverse_surface)).check(matches(isDisplayed())); | ||
onView(withText(R.string.cat_color_role_inverse_on_surface)).check(matches(isDisplayed())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2021 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 io.material.catalog.color; | ||
|
||
import androidx.annotation.AttrRes; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.StringRes; | ||
|
||
/** A class for an item in the color scheme. (e.g. colorPrimary, colorSecondary, etc.) */ | ||
final class ColorRoleItem { | ||
|
||
@StringRes private final int colorRoleStringResId; | ||
@AttrRes private final int colorRoleAttrResId; | ||
|
||
ColorRoleItem(@StringRes int colorRoleStringResId, @AttrRes int colorRoleAttrResId) { | ||
this.colorRoleStringResId = colorRoleStringResId; | ||
this.colorRoleAttrResId = colorRoleAttrResId; | ||
} | ||
|
||
/** Returns the attr resource id of the color. */ | ||
@NonNull | ||
@AttrRes | ||
public int getColorRoleAttrResId() { | ||
return colorRoleAttrResId; | ||
} | ||
|
||
/** Returns the string resource id of the color. */ | ||
@NonNull | ||
@StringRes | ||
public int getColorRoleStringResId() { | ||
return colorRoleStringResId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2021 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 io.material.catalog.color; | ||
|
||
import io.material.catalog.R; | ||
|
||
import android.graphics.Color; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.LinearLayout; | ||
import android.widget.TextView; | ||
import androidx.annotation.AttrRes; | ||
import androidx.annotation.IdRes; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.StringRes; | ||
import com.google.android.material.color.MaterialColors; | ||
|
||
/** | ||
* A class for the items in a color row. A ColorRow consists of two {@link ColorRoleItem} objects. | ||
* The left colorRoleItem represents the Container Color and right colorRoleItem represents the | ||
* Content Color. | ||
*/ | ||
final class ColorRow { | ||
|
||
@NonNull private final ColorRoleItem colorRoleItemLeft; | ||
@Nullable private final ColorRoleItem colorRoleItemRight; | ||
|
||
private View catColorsSchemeRow; | ||
|
||
ColorRow(@NonNull ColorRoleItem colorRoleItemLeft, @Nullable ColorRoleItem colorRoleItemRight) { | ||
this.colorRoleItemLeft = colorRoleItemLeft; | ||
this.colorRoleItemRight = colorRoleItemRight; | ||
} | ||
|
||
void addTo(@NonNull LayoutInflater layoutInflater, LinearLayout layout) { | ||
catColorsSchemeRow = | ||
layoutInflater.inflate(R.layout.cat_colors_scheme_row, layout, /* attachToRoot= */ false); | ||
|
||
bindColorRoleItem( | ||
catColorsSchemeRow, | ||
R.id.cat_color_role_left, | ||
colorRoleItemLeft.getColorRoleStringResId(), | ||
colorRoleItemLeft.getColorRoleAttrResId()); | ||
if (colorRoleItemRight != null) { | ||
bindColorRoleItem( | ||
catColorsSchemeRow, | ||
R.id.cat_color_role_right, | ||
colorRoleItemRight.getColorRoleStringResId(), | ||
colorRoleItemRight.getColorRoleAttrResId()); | ||
} | ||
|
||
layout.addView(catColorsSchemeRow); | ||
} | ||
|
||
private void bindColorRoleItem( | ||
View view, | ||
@IdRes int textViewId, | ||
@StringRes int colorRoleTextResID, | ||
@AttrRes int colorAttrResId) { | ||
TextView colorRole = view.findViewById(textViewId); | ||
|
||
colorRole.setText(colorRoleTextResID); | ||
colorRole.setTextColor(getTextColor(colorAttrResId)); | ||
colorRole.setBackgroundColor(MaterialColors.getColor(view, colorAttrResId)); | ||
} | ||
|
||
private int getTextColor(@AttrRes int colorAttrResId) { | ||
if (!MaterialColors.isColorLight(MaterialColors.getColor(catColorsSchemeRow, colorAttrResId))) { | ||
// Use white text color if the color is considered dark. | ||
return Color.WHITE; | ||
} else { | ||
return Color.BLACK; | ||
} | ||
} | ||
} |
Oops, something went wrong.