Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2011 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.email.activity;
     18 
     19 import com.android.email.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.test.AndroidTestCase;
     24 import android.view.View;
     25 
     26 import java.util.Locale;
     27 
     28 public class UiUtilitiesTests extends AndroidTestCase {
     29     public void testFormatSize() {
     30         if (!"en".equalsIgnoreCase(Locale.getDefault().getLanguage())) {
     31             return; // Only works on the EN locale.
     32         }
     33         assertEquals("0B", UiUtilities.formatSize(getContext(), 0));
     34         assertEquals("1B", UiUtilities.formatSize(getContext(), 1));
     35         assertEquals("1023B", UiUtilities.formatSize(getContext(), 1023));
     36         assertEquals("1KB", UiUtilities.formatSize(getContext(), 1024));
     37         assertEquals("1023KB", UiUtilities.formatSize(getContext(), 1024 * 1024 - 1));
     38         assertEquals("1MB", UiUtilities.formatSize(getContext(), 1024 * 1024));
     39         assertEquals("1023MB", UiUtilities.formatSize(getContext(), 1024 * 1024 * 1024 - 1));
     40         assertEquals("1GB", UiUtilities.formatSize(getContext(), 1024 * 1024 * 1024));
     41         assertEquals("5GB", UiUtilities.formatSize(getContext(), 5L * 1024 * 1024 * 1024));
     42     }
     43 
     44     public void testGetMessageCountForUi() {
     45         final Context c = getContext();
     46 
     47         // Negavive valeus not really expected, but at least shouldn't crash.
     48         assertEquals("-1", UiUtilities.getMessageCountForUi(c, -1, true));
     49         assertEquals("-1", UiUtilities.getMessageCountForUi(c, -1, false));
     50 
     51         assertEquals("", UiUtilities.getMessageCountForUi(c, 0, true));
     52         assertEquals("0", UiUtilities.getMessageCountForUi(c, 0, false));
     53 
     54         assertEquals("1", UiUtilities.getMessageCountForUi(c, 1, true));
     55         assertEquals("1", UiUtilities.getMessageCountForUi(c, 1, false));
     56 
     57         assertEquals("999", UiUtilities.getMessageCountForUi(c, 999, true));
     58         assertEquals("999", UiUtilities.getMessageCountForUi(c, 999, false));
     59 
     60         final String moreThan999 = c.getString(R.string.more_than_999);
     61 
     62         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1000, true));
     63         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1000, false));
     64 
     65         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1001, true));
     66         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1001, false));
     67     }
     68 
     69     public void testGetView() {
     70         // Test for getView(Activity, int)
     71         DummyActivity a = new DummyActivity();
     72         DummyView v = new DummyView(getContext());
     73 
     74         a.mDummyViewId = 10;
     75         a.mDummyView = v;
     76         assertEquals(v, UiUtilities.getView(a, 10));
     77 
     78         try {
     79             assertEquals(v, UiUtilities.getView(a, 11));
     80             fail();
     81         } catch (IllegalArgumentException expected) {
     82         }
     83 
     84         // TODO Test for getView(View, int)?
     85         // Unfortunately View.findViewById is final, so can't use the same approach.
     86         // Also it's a huge pain to set up an actual, nested views in unit tests, so let's leave
     87         // it for now.
     88     }
     89 
     90     public void testSetVisibilitySafe() {
     91         {
     92             DummyView v = new DummyView(getContext());
     93             UiUtilities.setVisibilitySafe(v, View.VISIBLE);
     94             assertEquals(View.VISIBLE, v.mVisibility);
     95 
     96             // Shouldn't crash
     97             UiUtilities.setVisibilitySafe(null, View.VISIBLE);
     98         }
     99 
    100         {
    101             DummyActivity a = new DummyActivity();
    102             DummyView v = new DummyView(getContext());
    103             a.mDummyViewId = 3;
    104             a.mDummyView = v;
    105 
    106             UiUtilities.setVisibilitySafe(a, 3, View.VISIBLE);
    107             assertEquals(View.VISIBLE, v.mVisibility);
    108 
    109             // shouldn't crash
    110             UiUtilities.setVisibilitySafe(a, 5, View.VISIBLE);
    111         }
    112         // No test for setVisibilitySafe(View, int, int) -- see testGetView().
    113     }
    114 
    115     private static class DummyActivity extends Activity {
    116         public int mDummyViewId;
    117         public View mDummyView;
    118 
    119         @Override
    120         public View findViewById(int id) {
    121             return (id == mDummyViewId) ? mDummyView : null;
    122         }
    123     }
    124 
    125     private static class DummyView extends View {
    126         public int mVisibility = View.GONE;
    127 
    128         public DummyView(Context context) {
    129             super(context);
    130         }
    131 
    132         @Override
    133         public void setVisibility(int visibility) {
    134             mVisibility = visibility;
    135         }
    136     }
    137 }
    138