Files
app_Settings/tests/robotests/src/com/android/settings/fuelgauge/PowerGaugePreferenceTest.java
Andrew Sapperstein 9f1e911759 Refactor test runner to use static list of resource paths
Previously everything lived in an inner class method of
SettingsRobolectricTestRunner. That method has now been turned into
a static method so that it can be called by other runners.

Bug: 62460102
Test: robotests
Change-Id: I6612b1f26404587301c534c8ba60e39d59d6c840
2017-06-09 09:21:26 -07:00

108 lines
4.0 KiB
Java

/*
* Copyright (C) 2017 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.android.settings.fuelgauge;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.support.v7.preference.PreferenceViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class PowerGaugePreferenceTest {
private static final String SUBTITLE = "Summary";
private static final String CONTENT_DESCRIPTION = "Content description";
private Context mContext;
private PowerGaugePreference mPowerGaugePreference;
private View mRootView;
private View mWidgetView;
private PreferenceViewHolder mPreferenceViewHolder;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mRootView = LayoutInflater.from(mContext).inflate(R.layout.preference_material_settings,
null);
mWidgetView = LayoutInflater.from(mContext).inflate(R.layout.preference_widget_summary,
null);
((LinearLayout) mRootView.findViewById(android.R.id.widget_frame)).addView(mWidgetView);
mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
mPowerGaugePreference = new PowerGaugePreference(mContext);
}
@Test
public void testOnBindViewHolder_bindSubtitle() {
mPowerGaugePreference.setSubtitle(SUBTITLE);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
assertThat(((TextView) mPreferenceViewHolder.findViewById(
R.id.widget_summary)).getText()).isEqualTo(SUBTITLE);
}
@Test
public void testOnBindViewHolder_showAnomaly_bindAnomalyIcon() {
mPowerGaugePreference.shouldShowAnomalyIcon(true);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
final Drawable[] drawables = ((TextView) mPreferenceViewHolder.findViewById(
R.id.widget_summary)).getCompoundDrawablesRelative();
assertThat(drawables[0]).isInstanceOf(VectorDrawable.class);
}
@Test
public void testOnBindViewHolder_notShowAnomaly_bindAnomalyIcon() {
mPowerGaugePreference.shouldShowAnomalyIcon(false);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
final Drawable[] drawables = ((TextView) mPreferenceViewHolder.findViewById(
R.id.widget_summary)).getCompoundDrawablesRelative();
assertThat(drawables[0]).isNull();
}
@Test
public void testOnBindViewHolder_bindContentDescription() {
mPowerGaugePreference.setContentDescription(CONTENT_DESCRIPTION);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
assertThat(mPreferenceViewHolder.findViewById(android.R.id.title).getContentDescription())
.isEqualTo(CONTENT_DESCRIPTION);
}
}