Skip to content

Commit

Permalink
[M3][Color] Added Color Scheme demo in Catalog.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 408896531
  • Loading branch information
Material Design Team authored and drchen committed Nov 10, 2021
1 parent e6db8a4 commit f898ba8
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 14 deletions.
@@ -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()));
}
}
94 changes: 91 additions & 3 deletions catalog/java/io/material/catalog/color/ColorMainDemoFragment.java
Expand Up @@ -22,20 +22,108 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.material.catalog.feature.DemoFragment;
import java.util.Arrays;
import java.util.List;

/** A placeholder fragment that displays the main Color demo for the Catalog app. */
public class ColorMainDemoFragment extends DemoFragment {
public final class ColorMainDemoFragment extends DemoFragment {

private LinearLayout colorsLayoutSurfaces;
private LinearLayout colorsLayoutContent;
private LinearLayout colorsLayoutUtility;

@Nullable
@Override
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
return layoutInflater.inflate(
R.layout.cat_colors_fragment, viewGroup, false /* attachToRoot */);
View view =
layoutInflater.inflate(R.layout.cat_colors_fragment, viewGroup, false/* attachToRoot= */ );

colorsLayoutSurfaces = view.findViewById(R.id.cat_colors_surfaces);
colorsLayoutContent = view.findViewById(R.id.cat_colors_content);
colorsLayoutUtility = view.findViewById(R.id.cat_colors_utility);

for (ColorRow colorRow : getColorRolesSurfaces()) {
colorRow.addTo(layoutInflater, colorsLayoutSurfaces);
}

for (ColorRow colorRow : getColorRolesContent()) {
colorRow.addTo(layoutInflater, colorsLayoutContent);
}

for (ColorRow colorRow : getColorRolesUtility()) {
colorRow.addTo(layoutInflater, colorsLayoutUtility);
}

return view;
}

private List<ColorRow> getColorRolesSurfaces() {
return Arrays.asList(
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_background, android.R.attr.colorBackground),
new ColorRoleItem(R.string.cat_color_role_on_background, R.attr.colorOnBackground)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_surface, R.attr.colorSurface),
new ColorRoleItem(R.string.cat_color_role_on_surface, R.attr.colorOnSurface)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_surface_variant, R.attr.colorSurfaceVariant),
new ColorRoleItem(
R.string.cat_color_role_on_surface_variant, R.attr.colorOnSurfaceVariant)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_inverse_surface, R.attr.colorSurfaceInverse),
new ColorRoleItem(
R.string.cat_color_role_inverse_on_surface, R.attr.colorOnSurfaceInverse)));
}

private List<ColorRow> getColorRolesContent() {
return Arrays.asList(
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_primary, R.attr.colorPrimary),
new ColorRoleItem(R.string.cat_color_role_on_primary, R.attr.colorOnPrimary)),
new ColorRow(
new ColorRoleItem(
R.string.cat_color_role_primary_container, R.attr.colorPrimaryContainer),
new ColorRoleItem(
R.string.cat_color_role_on_primary_container, R.attr.colorOnPrimaryContainer)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_inverse_primary, R.attr.colorPrimaryInverse),
/* colorRoleItemRight= */ null),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_secondary, R.attr.colorSecondary),
new ColorRoleItem(R.string.cat_color_role_on_secondary, R.attr.colorOnSecondary)),
new ColorRow(
new ColorRoleItem(
R.string.cat_color_role_secondary_container, R.attr.colorSecondaryContainer),
new ColorRoleItem(
R.string.cat_color_role_on_secondary_container, R.attr.colorOnSecondaryContainer)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_tertiary, R.attr.colorTertiary),
new ColorRoleItem(R.string.cat_color_role_on_tertiary, R.attr.colorOnTertiary)),
new ColorRow(
new ColorRoleItem(
R.string.cat_color_role_tertiary_container, R.attr.colorTertiaryContainer),
new ColorRoleItem(
R.string.cat_color_role_on_tertiary_container, R.attr.colorOnTertiaryContainer)));
}

private List<ColorRow> getColorRolesUtility() {
return Arrays.asList(
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_error, R.attr.colorError),
new ColorRoleItem(R.string.cat_color_role_on_error, R.attr.colorOnError)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_error_container, R.attr.colorErrorContainer),
new ColorRoleItem(
R.string.cat_color_role_on_error_container, R.attr.colorOnErrorContainer)),
new ColorRow(
new ColorRoleItem(R.string.cat_color_role_outline, R.attr.colorOutline),
/* colorRoleItemRight= */ null));
}
}
47 changes: 47 additions & 0 deletions catalog/java/io/material/catalog/color/ColorRoleItem.java
@@ -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;
}
}
90 changes: 90 additions & 0 deletions catalog/java/io/material/catalog/color/ColorRow.java
@@ -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;
}
}
}

0 comments on commit f898ba8

Please sign in to comment.