1 /*
2  * Copyright (C) 2021 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.settingslib.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ApplicationInfo;
24 
25 import androidx.test.core.app.ActivityScenario;
26 import androidx.test.ext.junit.rules.ActivityScenarioRule;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.settings.Settings;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.UUID;
37 
38 @RunWith(AndroidJUnit4.class)
39 @SmallTest
40 public class ApplicationStateComponentTest {
41     private static final String TAG =
42             ApplicationStateComponentTest.class.getSimpleName();
43     private Context mRuntimeApplication;
44     private ApplicationsState mApplicationsState;
45 
46     @Rule
47     public ActivityScenarioRule<Settings.ManageApplicationsActivity> rule =
48             new ActivityScenarioRule<>(
49                     new Intent(
50                             android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS)
51                             .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
52 
createAppEntry(String label, String packageName, int id)53     private ApplicationsState.AppEntry createAppEntry(String label, String packageName, int id) {
54         ApplicationInfo appInfo = createApplicationInfo(packageName, id);
55         ApplicationsState.AppEntry appEntry = new ApplicationsState.AppEntry(mRuntimeApplication,
56                 appInfo, id);
57         appEntry.label = label;
58         appEntry.mounted = true;
59         return appEntry;
60     }
61 
createApplicationInfo(String packageName, int uid)62     private ApplicationInfo createApplicationInfo(String packageName, int uid) {
63         ApplicationInfo appInfo = new ApplicationInfo();
64         appInfo.sourceDir = "foo";
65         appInfo.flags |= ApplicationInfo.FLAG_INSTALLED;
66         appInfo.storageUuid = UUID.randomUUID();
67         appInfo.packageName = packageName;
68         appInfo.uid = uid;
69         return appInfo;
70     }
71 
72     @Test
test_all_apps_sorting_alpha()73     public void test_all_apps_sorting_alpha() {
74         // TODO: Potential unit test candidate.
75         // To test all app list has sorted alphabetical, only need to verify sort function.
76         // This case focus on logic in sort function, and ignore origin locale sorting rule by Java.
77 
78         ActivityScenario scenario = rule.getScenario();
79 
80         scenario.onActivity(activity -> {
81             mRuntimeApplication = activity.getApplication();
82             mApplicationsState = ApplicationsState.getInstance(activity.getApplication());
83 
84             ApplicationsState.AppEntry entry1 = createAppEntry("Info01", "Package1", 0);
85             ApplicationsState.AppEntry entry2 = createAppEntry("Info02", "Package1", 0);
86             ApplicationsState.AppEntry entry3 = createAppEntry("Info01", "Package2", 0);
87             ApplicationsState.AppEntry entry4 = createAppEntry("Info02", "Package2", 0);
88             ApplicationsState.AppEntry entry5 = createAppEntry("Info02", "Package2", 1);
89             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry1, entry2)).isEqualTo(-1);
90             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry2, entry3)).isEqualTo(1);
91             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry3, entry2)).isEqualTo(-1);
92             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry3, entry3)).isEqualTo(0);
93             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry1, entry3)).isEqualTo(-1);
94             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry4, entry5)).isEqualTo(-1);
95             assertThat(ApplicationsState.ALPHA_COMPARATOR.compare(entry5, entry3)).isEqualTo(1);
96 
97         });
98 
99     }
100 }
101