tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib.widget; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.view.View; |
| 22 | |
| 23 | import androidx.annotation.StringRes; |
| 24 | |
| 25 | import org.junit.Rule; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.rules.ExpectedException; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.robolectric.RobolectricTestRunner; |
| 30 | |
| 31 | @RunWith(RobolectricTestRunner.class) |
| 32 | public class BarChartInfoTest { |
| 33 | |
| 34 | @Rule |
| 35 | public final ExpectedException thrown = ExpectedException.none(); |
| 36 | @StringRes |
| 37 | private final int mTitle = 0x11111111; |
| 38 | @StringRes |
| 39 | private final int mDetails = 0x22222222; |
| 40 | @StringRes |
| 41 | private final int mEmptyText = 0x33333333; |
| 42 | private final View.OnClickListener mClickListener = v -> { |
| 43 | }; |
| 44 | |
| 45 | @Test |
| 46 | public void builder_shouldSetFieldsInTheInfo() { |
| 47 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 48 | .setTitle(mTitle) |
| 49 | .setDetails(mDetails) |
| 50 | .setEmptyText(mEmptyText) |
| 51 | .setDetailsOnClickListener(mClickListener) |
| 52 | .build(); |
| 53 | assertThat(barChartInfo.getTitle()).isEqualTo(mTitle); |
| 54 | assertThat(barChartInfo.getDetails()).isEqualTo(mDetails); |
| 55 | assertThat(barChartInfo.getEmptyText()).isEqualTo(mEmptyText); |
| 56 | assertThat(barChartInfo.getDetailsOnClickListener()).isEqualTo(mClickListener); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void builder_noTitle_shouldThrowIllegalStateException() { |
| 61 | thrown.expect(IllegalStateException.class); |
| 62 | |
| 63 | new BarChartInfo.Builder() |
| 64 | .setDetails(mDetails) |
| 65 | .setEmptyText(mEmptyText) |
| 66 | .setDetailsOnClickListener(mClickListener) |
| 67 | .build(); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void addBarViewInfo_oneBarViewInfo_shouldSetOneBarViewInfo() { |
| 72 | final BarViewInfo barViewInfo = new BarViewInfo( |
| 73 | null /* icon */, |
| 74 | 50, |
Joel Galenson | 9842425f | 2019-03-05 14:14:14 -0800 | [diff] [blame] | 75 | mTitle, |
| 76 | null); |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 77 | |
| 78 | final BarChartInfo mBarChartInfo = new BarChartInfo.Builder() |
| 79 | .setTitle(mTitle) |
| 80 | .setDetails(mDetails) |
| 81 | .setEmptyText(mEmptyText) |
| 82 | .setDetailsOnClickListener(mClickListener) |
| 83 | .addBarViewInfo(barViewInfo) |
| 84 | .build(); |
| 85 | |
| 86 | assertThat(mBarChartInfo.getBarViewInfos().length).isEqualTo(1); |
| 87 | assertThat(mBarChartInfo.getBarViewInfos()[0]).isEqualTo(barViewInfo); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void addBarViewInfo_maxNumberOfInfoAllowed_shouldSetMaxBarViewInfos() { |
| 92 | final BarViewInfo barViewInfo = new BarViewInfo( |
| 93 | null /* icon */, |
| 94 | 50, |
Joel Galenson | 9842425f | 2019-03-05 14:14:14 -0800 | [diff] [blame] | 95 | mTitle, |
| 96 | null); |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 97 | final BarChartInfo mBarChartInfo = new BarChartInfo.Builder() |
| 98 | .setTitle(mTitle) |
| 99 | .setDetails(mDetails) |
| 100 | .setEmptyText(mEmptyText) |
| 101 | .setDetailsOnClickListener(mClickListener) |
| 102 | .addBarViewInfo(barViewInfo) |
| 103 | .addBarViewInfo(barViewInfo) |
| 104 | .addBarViewInfo(barViewInfo) |
| 105 | .addBarViewInfo(barViewInfo) |
| 106 | .build(); |
| 107 | |
| 108 | assertThat(mBarChartInfo.getBarViewInfos().length).isEqualTo(4); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void addBarViewInfo_moreInfosThanMaxAllowed_shouldThrowIllegalStateException() { |
| 113 | thrown.expect(IllegalStateException.class); |
| 114 | |
| 115 | final BarViewInfo barViewInfo = new BarViewInfo( |
| 116 | null /* icon */, |
| 117 | 50, |
Joel Galenson | 9842425f | 2019-03-05 14:14:14 -0800 | [diff] [blame] | 118 | mTitle, |
| 119 | null); |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 120 | new BarChartInfo.Builder() |
| 121 | .setTitle(mTitle) |
| 122 | .setDetails(mDetails) |
| 123 | .setEmptyText(mEmptyText) |
| 124 | .setDetailsOnClickListener(mClickListener) |
| 125 | .addBarViewInfo(barViewInfo) |
| 126 | .addBarViewInfo(barViewInfo) |
| 127 | .addBarViewInfo(barViewInfo) |
| 128 | .addBarViewInfo(barViewInfo) |
| 129 | .addBarViewInfo(barViewInfo) |
| 130 | .build(); |
| 131 | } |
| 132 | } |