tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 1 | /* |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 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.content.Context; |
| 22 | import android.graphics.drawable.Drawable; |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.widget.TextView; |
| 26 | |
| 27 | import androidx.preference.PreferenceViewHolder; |
| 28 | |
| 29 | import org.junit.Before; |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 30 | import org.junit.Rule; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 31 | import org.junit.Test; |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 32 | import org.junit.rules.ExpectedException; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 33 | import org.junit.runner.RunWith; |
| 34 | import org.robolectric.RobolectricTestRunner; |
| 35 | import org.robolectric.RuntimeEnvironment; |
| 36 | |
| 37 | @RunWith(RobolectricTestRunner.class) |
| 38 | public class BarChartPreferenceTest { |
| 39 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 40 | @Rule |
| 41 | public final ExpectedException thrown = ExpectedException.none(); |
| 42 | |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 43 | private Context mContext; |
| 44 | private View mBarChartView; |
| 45 | private Drawable mIcon; |
| 46 | private BarView mBarView1; |
| 47 | private BarView mBarView2; |
| 48 | private BarView mBarView3; |
| 49 | private BarView mBarView4; |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 50 | private TextView mTitleView; |
Joel Galenson | 919f6ea4 | 2018-12-20 15:09:47 -0800 | [diff] [blame] | 51 | private TextView mDetailsView; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 52 | private PreferenceViewHolder mHolder; |
| 53 | private BarChartPreference mPreference; |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 54 | private BarChartInfo mBarChartInfo; |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | mContext = RuntimeEnvironment.application; |
| 59 | mBarChartView = View.inflate(mContext, R.layout.settings_bar_chart, null /* parent */); |
| 60 | mHolder = PreferenceViewHolder.createInstanceForTests(mBarChartView); |
| 61 | mPreference = new BarChartPreference(mContext, null /* attrs */); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 62 | |
Amin Shaikh | 10f363b | 2019-01-24 17:59:49 -0500 | [diff] [blame] | 63 | mIcon = mContext.getDrawable(com.android.internal.R.drawable.ic_menu); |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 64 | mBarView1 = mBarChartView.findViewById(R.id.bar_view1); |
| 65 | mBarView2 = mBarChartView.findViewById(R.id.bar_view2); |
| 66 | mBarView3 = mBarChartView.findViewById(R.id.bar_view3); |
| 67 | mBarView4 = mBarChartView.findViewById(R.id.bar_view4); |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 68 | mTitleView = mBarChartView.findViewById(R.id.bar_chart_title); |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 69 | mDetailsView = mBarChartView.findViewById(R.id.bar_chart_details); |
| 70 | |
| 71 | mBarChartInfo = new BarChartInfo.Builder() |
| 72 | .setTitle(R.string.debug_app) |
| 73 | .setDetails(R.string.debug_app) |
| 74 | .setEmptyText(R.string.debug_app) |
| 75 | .setDetailsOnClickListener(v -> { |
| 76 | }) |
| 77 | .build(); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 81 | public void initializeBarChart_titleSet_shouldSetTitleInChartView() { |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 82 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 83 | .setTitle(R.string.debug_app) |
| 84 | .build(); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 85 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 86 | mPreference.initializeBarChart(barChartInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 87 | mPreference.onBindViewHolder(mHolder); |
| 88 | |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 89 | assertThat(mTitleView.getVisibility()).isEqualTo(View.VISIBLE); |
| 90 | assertThat(mTitleView.getText()).isEqualTo(mContext.getText(R.string.debug_app)); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
tmfang | ff3ba83 | 2018-12-20 19:21:04 +0800 | [diff] [blame] | 94 | public void initializeBarChart_noBarViewSet_shouldShowTitleAndEmptyView() { |
| 95 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 96 | .setTitle(R.string.debug_app) |
| 97 | .setEmptyText(R.string.debug_app) |
| 98 | .build(); |
| 99 | |
| 100 | mPreference.initializeBarChart(barChartInfo); |
| 101 | // We don't add any bar view yet. |
| 102 | mPreference.onBindViewHolder(mHolder); |
| 103 | |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 104 | assertThat(mTitleView.getVisibility()).isEqualTo(View.VISIBLE); |
tmfang | ff3ba83 | 2018-12-20 19:21:04 +0800 | [diff] [blame] | 105 | assertThat(mBarChartView.findViewById(R.id.empty_view).getVisibility()) |
| 106 | .isEqualTo(View.VISIBLE); |
| 107 | assertThat(mBarChartView.findViewById(R.id.bar_views_container).getVisibility()) |
| 108 | .isEqualTo(View.GONE); |
| 109 | } |
| 110 | |
| 111 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 112 | public void initializeBarChart_detailsSet_shouldShowBarChartDetailsView() { |
| 113 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 114 | .setTitle(R.string.debug_app) |
| 115 | .setDetails(R.string.debug_app) |
tmfang | ff3ba83 | 2018-12-20 19:21:04 +0800 | [diff] [blame] | 116 | .addBarViewInfo(new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app)) |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 117 | .build(); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 118 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 119 | mPreference.initializeBarChart(barChartInfo); |
Joel Galenson | 418137e | 2018-12-27 09:39:03 -0800 | [diff] [blame] | 120 | mPreference.onBindViewHolder(mHolder); |
| 121 | |
| 122 | assertThat(mDetailsView.getVisibility()).isEqualTo(View.VISIBLE); |
| 123 | assertThat(mDetailsView.getText()).isEqualTo(mContext.getText(R.string.debug_app)); |
| 124 | } |
| 125 | |
| 126 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 127 | public void initializeBarChart_detailsNotSet_shouldHideBarChartDetailsView() { |
| 128 | // We don't call BarChartInfo.Builder#setDetails yet. |
| 129 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 130 | .setTitle(R.string.debug_app) |
tmfang | ff3ba83 | 2018-12-20 19:21:04 +0800 | [diff] [blame] | 131 | .addBarViewInfo(new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app)) |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 132 | .build(); |
| 133 | |
| 134 | mPreference.initializeBarChart(barChartInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 135 | mPreference.onBindViewHolder(mHolder); |
| 136 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 137 | assertThat(mDetailsView.getVisibility()).isEqualTo(View.GONE); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 141 | public void initializeBarChart_clickListenerSet_shouldSetClickListenerOnDetailsView() { |
| 142 | final BarChartInfo barChartInfo = new BarChartInfo.Builder() |
| 143 | .setTitle(R.string.debug_app) |
| 144 | .setDetails(R.string.debug_app) |
| 145 | .setDetailsOnClickListener(v -> { |
| 146 | }) |
tmfang | ff3ba83 | 2018-12-20 19:21:04 +0800 | [diff] [blame] | 147 | .addBarViewInfo(new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app)) |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 148 | .build(); |
| 149 | |
| 150 | mPreference.initializeBarChart(barChartInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 151 | mPreference.onBindViewHolder(mHolder); |
| 152 | |
Joel Galenson | 919f6ea4 | 2018-12-20 15:09:47 -0800 | [diff] [blame] | 153 | assertThat(mDetailsView.getVisibility()).isEqualTo(View.VISIBLE); |
| 154 | assertThat(mDetailsView.hasOnClickListeners()).isTrue(); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 158 | public void setBarViewInfos_oneBarViewInfoSet_shouldShowOneBarView() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 159 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 160 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app) |
| 161 | }; |
| 162 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 163 | mPreference.initializeBarChart(mBarChartInfo); |
| 164 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 165 | mPreference.onBindViewHolder(mHolder); |
| 166 | |
| 167 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 168 | assertThat(mBarView1.getTitle()).isEqualTo("10"); |
| 169 | |
| 170 | assertThat(mBarView2.getVisibility()).isEqualTo(View.GONE); |
| 171 | assertThat(mBarView3.getVisibility()).isEqualTo(View.GONE); |
| 172 | assertThat(mBarView4.getVisibility()).isEqualTo(View.GONE); |
| 173 | } |
| 174 | |
| 175 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 176 | public void setBarViewInfos_twoBarViewInfosSet_shouldShowTwoBarViews() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 177 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 178 | new BarViewInfo(mIcon, 20 /* barNumber */, R.string.debug_app), |
| 179 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app) |
| 180 | }; |
| 181 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 182 | mPreference.initializeBarChart(mBarChartInfo); |
| 183 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 184 | mPreference.onBindViewHolder(mHolder); |
| 185 | |
| 186 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 187 | assertThat(mBarView1.getTitle()).isEqualTo("20"); |
| 188 | assertThat(mBarView2.getVisibility()).isEqualTo(View.VISIBLE); |
| 189 | assertThat(mBarView2.getTitle()).isEqualTo("10"); |
| 190 | |
| 191 | assertThat(mBarView3.getVisibility()).isEqualTo(View.GONE); |
| 192 | assertThat(mBarView4.getVisibility()).isEqualTo(View.GONE); |
| 193 | } |
| 194 | |
| 195 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 196 | public void setBarViewInfos_threeBarViewInfosSet_shouldShowThreeBarViews() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 197 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 198 | new BarViewInfo(mIcon, 20 /* barNumber */, R.string.debug_app), |
| 199 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app), |
| 200 | new BarViewInfo(mIcon, 5 /* barNumber */, R.string.debug_app) |
| 201 | }; |
| 202 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 203 | mPreference.initializeBarChart(mBarChartInfo); |
| 204 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 205 | mPreference.onBindViewHolder(mHolder); |
| 206 | |
| 207 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 208 | assertThat(mBarView1.getTitle()).isEqualTo("20"); |
| 209 | assertThat(mBarView2.getVisibility()).isEqualTo(View.VISIBLE); |
| 210 | assertThat(mBarView2.getTitle()).isEqualTo("10"); |
| 211 | assertThat(mBarView3.getVisibility()).isEqualTo(View.VISIBLE); |
| 212 | assertThat(mBarView3.getTitle()).isEqualTo("5"); |
| 213 | |
| 214 | assertThat(mBarView4.getVisibility()).isEqualTo(View.GONE); |
| 215 | } |
| 216 | |
| 217 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 218 | public void setBarViewInfos_fourBarViewInfosSet_shouldShowFourBarViews() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 219 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 220 | new BarViewInfo(mIcon, 20 /* barNumber */, R.string.debug_app), |
| 221 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app), |
| 222 | new BarViewInfo(mIcon, 5 /* barNumber */, R.string.debug_app), |
| 223 | new BarViewInfo(mIcon, 2 /* barNumber */, R.string.debug_app), |
| 224 | }; |
| 225 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 226 | mPreference.initializeBarChart(mBarChartInfo); |
| 227 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 228 | mPreference.onBindViewHolder(mHolder); |
| 229 | |
| 230 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 231 | assertThat(mBarView1.getTitle()).isEqualTo("20"); |
| 232 | assertThat(mBarView2.getVisibility()).isEqualTo(View.VISIBLE); |
| 233 | assertThat(mBarView2.getTitle()).isEqualTo("10"); |
| 234 | assertThat(mBarView3.getVisibility()).isEqualTo(View.VISIBLE); |
| 235 | assertThat(mBarView3.getTitle()).isEqualTo("5"); |
| 236 | assertThat(mBarView4.getVisibility()).isEqualTo(View.VISIBLE); |
| 237 | assertThat(mBarView4.getTitle()).isEqualTo("2"); |
| 238 | } |
| 239 | |
| 240 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 241 | public void setBarViewInfos_moreInfosThanMaxAllowed_shouldThrowIllegalStateException() { |
| 242 | thrown.expect(IllegalStateException.class); |
| 243 | |
| 244 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 245 | new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app), |
| 246 | new BarViewInfo(mIcon, 50 /* barNumber */, R.string.debug_app), |
| 247 | new BarViewInfo(mIcon, 5 /* barNumber */, R.string.debug_app), |
| 248 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app), |
| 249 | new BarViewInfo(mIcon, 70 /* barNumber */, R.string.debug_app), |
| 250 | }; |
| 251 | |
| 252 | mPreference.setBarViewInfos(barViewsInfo); |
| 253 | } |
| 254 | |
| 255 | @Test |
| 256 | public void setBarViewInfos_barViewInfosSet_shouldBeSortedInDescending() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 257 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 258 | new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app), |
| 259 | new BarViewInfo(mIcon, 50 /* barNumber */, R.string.debug_app), |
| 260 | new BarViewInfo(mIcon, 5 /* barNumber */, R.string.debug_app), |
| 261 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app), |
| 262 | }; |
| 263 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 264 | mPreference.initializeBarChart(mBarChartInfo); |
| 265 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 266 | mPreference.onBindViewHolder(mHolder); |
| 267 | |
| 268 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 269 | assertThat(mBarView1.getTitle()).isEqualTo("50"); |
| 270 | assertThat(mBarView2.getVisibility()).isEqualTo(View.VISIBLE); |
| 271 | assertThat(mBarView2.getTitle()).isEqualTo("30"); |
| 272 | assertThat(mBarView3.getVisibility()).isEqualTo(View.VISIBLE); |
| 273 | assertThat(mBarView3.getTitle()).isEqualTo("10"); |
| 274 | assertThat(mBarView4.getVisibility()).isEqualTo(View.VISIBLE); |
| 275 | assertThat(mBarView4.getTitle()).isEqualTo("5"); |
| 276 | } |
| 277 | |
| 278 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 279 | public void setBarViewInfos_validBarViewSummarySet_barViewShouldShowSummary() { |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 280 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{ |
| 281 | new BarViewInfo(mIcon, 10 /* barNumber */, R.string.debug_app), |
| 282 | }; |
| 283 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 284 | mPreference.initializeBarChart(mBarChartInfo); |
| 285 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 286 | mPreference.onBindViewHolder(mHolder); |
| 287 | |
| 288 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 289 | assertThat(mBarView1.getSummary()).isEqualTo(mContext.getText(R.string.debug_app)); |
| 290 | } |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 291 | |
| 292 | @Test |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 293 | public void setBarViewInfos_clickListenerForBarViewSet_barViewShouldHaveClickListener() { |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 294 | final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app); |
| 295 | viewInfo.setClickListener(v -> { |
| 296 | }); |
| 297 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo}; |
| 298 | |
tmfang | d8551b1 | 2018-12-21 19:53:17 +0800 | [diff] [blame] | 299 | mPreference.initializeBarChart(mBarChartInfo); |
| 300 | mPreference.setBarViewInfos(barViewsInfo); |
tmfang | 7081e7f | 2018-12-19 14:14:34 +0800 | [diff] [blame] | 301 | mPreference.onBindViewHolder(mHolder); |
| 302 | |
| 303 | assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); |
| 304 | assertThat(mBarView1.hasOnClickListeners()).isTrue(); |
| 305 | } |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 306 | |
| 307 | @Test |
tmfang | b052cfa | 2019-02-23 15:08:04 +0800 | [diff] [blame] | 308 | public void onBindViewHolder_loadingStateIsTrue_shouldHideAllViews() { |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 309 | final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app); |
| 310 | viewInfo.setClickListener(v -> { |
| 311 | }); |
| 312 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo}; |
| 313 | |
| 314 | mPreference.initializeBarChart(mBarChartInfo); |
| 315 | mPreference.setBarViewInfos(barViewsInfo); |
| 316 | mPreference.updateLoadingState(true /* isLoading */); |
| 317 | |
| 318 | mPreference.onBindViewHolder(mHolder); |
| 319 | |
tmfang | b052cfa | 2019-02-23 15:08:04 +0800 | [diff] [blame] | 320 | assertThat(mHolder.itemView.getVisibility()).isEqualTo(View.INVISIBLE); |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Test |
| 324 | public void onBindViewHolder_loadingStateIsFalse_shouldInitAnyView() { |
| 325 | final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app); |
| 326 | viewInfo.setClickListener(v -> { |
| 327 | }); |
| 328 | final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo}; |
| 329 | |
| 330 | mPreference.initializeBarChart(mBarChartInfo); |
| 331 | mPreference.setBarViewInfos(barViewsInfo); |
| 332 | mPreference.updateLoadingState(false /* isLoading */); |
| 333 | |
| 334 | mPreference.onBindViewHolder(mHolder); |
| 335 | |
tmfang | b052cfa | 2019-02-23 15:08:04 +0800 | [diff] [blame] | 336 | assertThat(mHolder.itemView.getVisibility()).isEqualTo(View.VISIBLE); |
tmfang | 4a2cd66 | 2019-02-14 14:02:14 +0800 | [diff] [blame] | 337 | assertThat(TextUtils.isEmpty(mTitleView.getText())).isFalse(); |
| 338 | assertThat(TextUtils.isEmpty(mDetailsView.getText())).isFalse(); |
| 339 | } |
tmfang | 2badb3e | 2018-12-10 00:25:02 +0800 | [diff] [blame] | 340 | } |