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.applications.manageapplications;
18 
19 import static com.android.settings.applications.manageapplications.ManageApplications.LIST_TYPE_NONE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 
25 import android.content.Context;
26 import android.content.pm.ApplicationInfo;
27 import android.content.pm.PackageManager;
28 import android.view.View;
29 import android.widget.FrameLayout;
30 
31 import com.android.settingslib.applications.ApplicationsState;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 import java.util.concurrent.CountDownLatch;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class ApplicationViewHolderTest {
43 
44     private Context mContext;
45     private View mView;
46     private ApplicationViewHolder mHolder;
47 
48     @Before
seUp()49     public void seUp() {
50         mContext = RuntimeEnvironment.application;
51         mView = ApplicationViewHolder.newView(new FrameLayout(mContext));
52         mHolder = new ApplicationViewHolder(mView);
53     }
54 
55     @Test
updateDisableView_appDisabledUntilUsed_shouldSetDisabled()56     public void updateDisableView_appDisabledUntilUsed_shouldSetDisabled() {
57         final ApplicationInfo info = new ApplicationInfo();
58         info.flags = ApplicationInfo.FLAG_INSTALLED;
59         info.enabled = true;
60         info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
61         mHolder.updateDisableView(info);
62 
63         assertThat(mHolder.mDisabled.getText())
64                 .isEqualTo(mContext.getText(com.android.settingslib.R.string.disabled));
65     }
66 
67     @Test
setSummaries()68     public void setSummaries() {
69         mHolder.setSummary("hello");
70         assertThat(mHolder.mSummary.getText()).isEqualTo("hello");
71 
72         mHolder.setSummary(com.android.settingslib.R.string.disabled);
73         assertThat(mHolder.mSummary.getText())
74                 .isEqualTo(mContext.getText(com.android.settingslib.R.string.disabled));
75     }
76 
77     @Test
setTitle_titleIsNotEmptyAndContentIsNotEmpty_shouldSetTitleAndContentDescription()78     public void setTitle_titleIsNotEmptyAndContentIsNotEmpty_shouldSetTitleAndContentDescription() {
79         mHolder.setTitle("title", "content");
80 
81         assertThat(mHolder.mAppName.getText()).isEqualTo("title");
82         assertThat(mHolder.mAppName.getContentDescription()).isEqualTo("content");
83     }
84 
85     @Test
setTitle_titleIsNotEmptyButContentIsEmpty_shouldSetTitle()86     public void setTitle_titleIsNotEmptyButContentIsEmpty_shouldSetTitle() {
87         mHolder.setTitle("title", "");
88 
89         assertThat(mHolder.mAppName.getText()).isEqualTo("title");
90         assertThat(mHolder.mAppName.getContentDescription()).isNull();
91     }
92 
93     @Test
updateSize()94     public void updateSize() {
95         final String invalidStr = "invalid";
96         final ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
97         entry.internalSizeStr = "internal";
98         entry.externalSizeStr = "external";
99         entry.sizeStr = entry.internalSizeStr;
100 
101         mHolder.updateSizeText(entry, invalidStr, ManageApplications.SIZE_INTERNAL);
102         assertThat(mHolder.mSummary.getText()).isEqualTo(entry.internalSizeStr);
103 
104         mHolder.updateSizeText(entry, invalidStr, ManageApplications.SIZE_EXTERNAL);
105         assertThat(mHolder.mSummary.getText()).isEqualTo(entry.externalSizeStr);
106 
107         entry.sizeStr = null;
108         entry.size = ApplicationsState.SIZE_INVALID;
109         mHolder.updateSizeText(entry, invalidStr, ManageApplications.SIZE_EXTERNAL);
110         assertThat(mHolder.mSummary.getText()).isEqualTo(invalidStr);
111     }
112 
113     @Test
oneTouchTarget()114     public void oneTouchTarget() {
115         assertThat(mHolder.mSwitch).isNull();
116         assertThat(mHolder.mWidgetContainer.getChildCount()).isEqualTo(0);
117         // assert no exception
118         mHolder.updateSwitch(null, true, true);
119     }
120 
121     @Test
twoTouchTarget()122     public void twoTouchTarget() {
123         mView = ApplicationViewHolder.newView(new FrameLayout(mContext), true,
124                 LIST_TYPE_NONE);
125         mHolder = new ApplicationViewHolder(mView);
126         assertThat(mHolder.mSwitch).isNotNull();
127         assertThat(mHolder.mWidgetContainer.getChildCount()).isEqualTo(1);
128     }
129 
130     @Test
updateSwitch()131     public void updateSwitch() {
132         final CountDownLatch latch = new CountDownLatch(1);
133         mView = ApplicationViewHolder.newView(new FrameLayout(mContext), true,
134                 LIST_TYPE_NONE);
135         mHolder = new ApplicationViewHolder(mView);
136         mHolder.updateSwitch((buttonView, isChecked) -> latch.countDown(), true, true);
137 
138         assertThat(mHolder.mSwitch.isChecked()).isTrue();
139         assertThat(mHolder.mSwitch.isEnabled()).isTrue();
140         assertThat(mHolder.mSwitch.isFocusable()).isTrue();
141         assertThat(mHolder.mSwitch.isClickable()).isTrue();
142         mHolder.mSwitch.callOnClick();
143         assertThat(latch.getCount()).isEqualTo(0);
144     }
145 }
146