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