• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.test.suitebuilder.annotation.SmallTest;
25 import android.view.View;
26 
27 import java.util.Locale;
28 
29 @SmallTest
30 public class UiUtilitiesTests extends AndroidTestCase {
brokentestFormatSize()31     public void brokentestFormatSize() {
32         if (!"en".equalsIgnoreCase(Locale.getDefault().getLanguage())) {
33             return; // Only works on the EN locale.
34         }
35         assertEquals("0B", UiUtilities.formatSize(getContext(), 0));
36         assertEquals("1B", UiUtilities.formatSize(getContext(), 1));
37         assertEquals("1023B", UiUtilities.formatSize(getContext(), 1023));
38         assertEquals("1KB", UiUtilities.formatSize(getContext(), 1024));
39         assertEquals("1023KB", UiUtilities.formatSize(getContext(), 1024 * 1024 - 1));
40         assertEquals("1MB", UiUtilities.formatSize(getContext(), 1024 * 1024));
41         assertEquals("1023MB", UiUtilities.formatSize(getContext(), 1024 * 1024 * 1024 - 1));
42         assertEquals("1GB", UiUtilities.formatSize(getContext(), 1024 * 1024 * 1024));
43         assertEquals("5GB", UiUtilities.formatSize(getContext(), 5L * 1024 * 1024 * 1024));
44     }
45 
brokentestGetMessageCountForUi()46     public void brokentestGetMessageCountForUi() {
47         final Context c = getContext();
48 
49         // Negavive valeus not really expected, but at least shouldn't crash.
50         assertEquals("-1", UiUtilities.getMessageCountForUi(c, -1, true));
51         assertEquals("-1", UiUtilities.getMessageCountForUi(c, -1, false));
52 
53         assertEquals("", UiUtilities.getMessageCountForUi(c, 0, true));
54         assertEquals("0", UiUtilities.getMessageCountForUi(c, 0, false));
55 
56         assertEquals("1", UiUtilities.getMessageCountForUi(c, 1, true));
57         assertEquals("1", UiUtilities.getMessageCountForUi(c, 1, false));
58 
59         assertEquals("999", UiUtilities.getMessageCountForUi(c, 999, true));
60         assertEquals("999", UiUtilities.getMessageCountForUi(c, 999, false));
61 
62         final String moreThan999 = c.getString(R.string.more_than_999);
63 
64         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1000, true));
65         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1000, false));
66 
67         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1001, true));
68         assertEquals(moreThan999, UiUtilities.getMessageCountForUi(c, 1001, false));
69     }
70 
brokentestSetVisibilitySafe()71     public void brokentestSetVisibilitySafe() {
72         {
73             DummyView v = new DummyView(getContext());
74             UiUtilities.setVisibilitySafe(v, View.VISIBLE);
75             assertEquals(View.VISIBLE, v.mVisibility);
76 
77             // Shouldn't crash
78             UiUtilities.setVisibilitySafe(null, View.VISIBLE);
79         }
80 
81         {
82             DummyActivity a = new DummyActivity();
83             DummyView v = new DummyView(getContext());
84             a.mDummyViewId = 3;
85             a.mDummyView = v;
86 
87             UiUtilities.setVisibilitySafe(a, 3, View.VISIBLE);
88             assertEquals(View.VISIBLE, v.mVisibility);
89 
90             // shouldn't crash
91             UiUtilities.setVisibilitySafe(a, 5, View.VISIBLE);
92         }
93         // No test for setVisibilitySafe(View, int, int) -- see testGetView().
94     }
95 
96     private static class DummyActivity extends Activity {
97         public int mDummyViewId;
98         public View mDummyView;
99 
100         @Override
findViewById(int id)101         public View findViewById(int id) {
102             return (id == mDummyViewId) ? mDummyView : null;
103         }
104     }
105 
106     private static class DummyView extends View {
107         public int mVisibility = View.GONE;
108 
DummyView(Context context)109         public DummyView(Context context) {
110             super(context);
111         }
112 
113         @Override
setVisibility(int visibility)114         public void setVisibility(int visibility) {
115             mVisibility = visibility;
116         }
117     }
118 }
119