Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2014 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.inputmethod.latin.utils;
     18 
     19 import static com.android.inputmethod.latin.utils.ImportantNoticeUtils.KEY_TIMESTAMP_OF_CONTACTS_NOTICE;
     20 import static org.mockito.Mockito.when;
     21 
     22 import android.content.Context;
     23 import android.content.SharedPreferences;
     24 import android.test.AndroidTestCase;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 import android.text.TextUtils;
     27 
     28 import com.android.inputmethod.latin.settings.SettingsValues;
     29 
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 
     33 import java.util.concurrent.TimeUnit;
     34 
     35 @MediumTest
     36 public class ImportantNoticeUtilsTests extends AndroidTestCase {
     37 
     38     private ImportantNoticePreferences mImportantNoticePreferences;
     39 
     40     @Mock private SettingsValues mMockSettingsValues;
     41 
     42     private static class ImportantNoticePreferences {
     43         private final SharedPreferences mPref;
     44 
     45         private Integer mVersion;
     46         private Long mLastTime;
     47 
     48         public ImportantNoticePreferences(final Context context) {
     49             mPref = ImportantNoticeUtils.getImportantNoticePreferences(context);
     50         }
     51 
     52         private Integer getInt(final String key) {
     53             if (mPref.contains(key)) {
     54                 return mPref.getInt(key, 0);
     55             }
     56             return null;
     57         }
     58 
     59         public Long getLong(final String key) {
     60             if (mPref.contains(key)) {
     61                 return mPref.getLong(key, 0);
     62             }
     63             return null;
     64         }
     65 
     66         private void putInt(final String key, final Integer value) {
     67             if (value == null) {
     68                 removePreference(key);
     69             } else {
     70                 mPref.edit().putInt(key, value).apply();
     71             }
     72         }
     73 
     74         private void putLong(final String key, final Long value) {
     75             if (value == null) {
     76                 removePreference(key);
     77             } else {
     78                 mPref.edit().putLong(key, value).apply();
     79             }
     80         }
     81 
     82         private void removePreference(final String key) {
     83             mPref.edit().remove(key).apply();
     84         }
     85 
     86         public void save() {
     87             mLastTime = getLong(KEY_TIMESTAMP_OF_CONTACTS_NOTICE);
     88         }
     89 
     90         public void restore() {
     91             putLong(KEY_TIMESTAMP_OF_CONTACTS_NOTICE, mLastTime);
     92         }
     93 
     94         public void clear() {
     95             removePreference(KEY_TIMESTAMP_OF_CONTACTS_NOTICE);
     96         }
     97     }
     98 
     99     @Override
    100     protected void setUp() throws Exception {
    101         super.setUp();
    102         MockitoAnnotations.initMocks(this);
    103         mImportantNoticePreferences = new ImportantNoticePreferences(getContext());
    104         mImportantNoticePreferences.save();
    105         when(mMockSettingsValues.isPersonalizationEnabled()).thenReturn(true);
    106     }
    107 
    108     @Override
    109     protected void tearDown() throws Exception {
    110         super.tearDown();
    111         mImportantNoticePreferences.restore();
    112     }
    113 
    114     public void testPersonalizationSetting() {
    115         mImportantNoticePreferences.clear();
    116 
    117         // Personalization enabled.
    118         when(mMockSettingsValues.isPersonalizationEnabled()).thenReturn(true);
    119         assertEquals("Current boolean with personalization enabled", true,
    120                 ImportantNoticeUtils.shouldShowImportantNotice(getContext(), mMockSettingsValues));
    121 
    122         // Personalization disabled.
    123         when(mMockSettingsValues.isPersonalizationEnabled()).thenReturn(false);
    124         assertEquals("Current boolean with personalization disabled", false,
    125                 ImportantNoticeUtils.shouldShowImportantNotice(getContext(), mMockSettingsValues));
    126     }
    127 }
    128