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