1 /* 2 * Copyright (C) 2017 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.settings.widget; 18 19 import static org.mockito.Mockito.times; 20 import static org.mockito.Mockito.verify; 21 22 import android.content.Context; 23 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.Mock; 28 import org.mockito.MockitoAnnotations; 29 import org.robolectric.RobolectricTestRunner; 30 import org.robolectric.RuntimeEnvironment; 31 32 @RunWith(RobolectricTestRunner.class) 33 public class SummaryUpdaterTest { 34 35 private Context mContext; 36 37 private SummaryUpdaterTestable mSummaryUpdater; 38 @Mock 39 private SummaryListener mListener; 40 41 @Before setUp()42 public void setUp() { 43 MockitoAnnotations.initMocks(this); 44 mContext = RuntimeEnvironment.application.getApplicationContext(); 45 mSummaryUpdater = new SummaryUpdaterTestable(mContext, mListener); 46 } 47 48 @Test notifyChangeIfNeeded_fistTimeInit_shouldNotifyChange()49 public void notifyChangeIfNeeded_fistTimeInit_shouldNotifyChange() { 50 final String summary = "initialized"; 51 mSummaryUpdater.setTestSummary(summary); 52 mSummaryUpdater.notifyChangeIfNeeded(); 53 54 verify(mListener).onSummaryChanged(summary); 55 } 56 57 @Test notifyChangeIfNeeded_summaryUpdated_shouldNotifyChange()58 public void notifyChangeIfNeeded_summaryUpdated_shouldNotifyChange() { 59 final String summaryInit = "initialized"; 60 mSummaryUpdater.setTestSummary(summaryInit); 61 mSummaryUpdater.notifyChangeIfNeeded(); 62 final String summaryUpdate = "updated"; 63 64 mSummaryUpdater.setTestSummary(summaryUpdate); 65 mSummaryUpdater.notifyChangeIfNeeded(); 66 67 verify(mListener).onSummaryChanged(summaryUpdate); 68 } 69 70 @Test notifyChangeIfNeeded_summaryNotUpdated_shouldOnlyNotifyChangeOnce()71 public void notifyChangeIfNeeded_summaryNotUpdated_shouldOnlyNotifyChangeOnce() { 72 final String summaryInit = "initialized"; 73 mSummaryUpdater.setTestSummary(summaryInit); 74 mSummaryUpdater.notifyChangeIfNeeded(); 75 final String summaryUpdate = "updated"; 76 mSummaryUpdater.setTestSummary(summaryUpdate); 77 78 mSummaryUpdater.notifyChangeIfNeeded(); 79 mSummaryUpdater.notifyChangeIfNeeded(); 80 81 verify(mListener, times(1)).onSummaryChanged(summaryUpdate); 82 } 83 84 private class SummaryListener implements SummaryUpdater.OnSummaryChangeListener { 85 String summary; 86 87 @Override onSummaryChanged(String summary)88 public void onSummaryChanged(String summary) { 89 this.summary = summary; 90 } 91 } 92 93 public final class SummaryUpdaterTestable extends SummaryUpdater { 94 private String mTestSummary; 95 SummaryUpdaterTestable(Context context, SummaryUpdater.OnSummaryChangeListener listener)96 SummaryUpdaterTestable(Context context, SummaryUpdater.OnSummaryChangeListener listener) { 97 super(context, listener); 98 } 99 100 @Override register(boolean register)101 public void register(boolean register) { 102 } 103 104 @Override notifyChangeIfNeeded()105 public void notifyChangeIfNeeded() { 106 super.notifyChangeIfNeeded(); 107 } 108 109 @Override getSummary()110 public String getSummary() { 111 return mTestSummary; 112 } 113 setTestSummary(String summary)114 private void setTestSummary(String summary) { 115 mTestSummary = summary; 116 } 117 } 118 } 119