PartnerTest.java revision 66815fe66392bcbb12e2fc93bbf326a5c2d8782f
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
17package com.android.setupwizardlib.util;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertNull;
23import static org.junit.Assert.assertTrue;
24import static org.mockito.Matchers.anyString;
25import static org.mockito.Matchers.eq;
26import static org.mockito.Mockito.doReturn;
27import static org.mockito.Mockito.spy;
28import static org.robolectric.RuntimeEnvironment.application;
29
30import android.content.Context;
31import android.content.Intent;
32import android.content.pm.ActivityInfo;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.pm.ResolveInfo;
36import android.content.res.Resources;
37import android.os.Build.VERSION;
38import android.os.Build.VERSION_CODES;
39
40import com.android.setupwizardlib.R;
41import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
42import com.android.setupwizardlib.util.Partner.ResourceEntry;
43import com.android.setupwizardlib.util.PartnerTest.ShadowApplicationPackageManager;
44
45import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.robolectric.Shadows;
49import org.robolectric.annotation.Config;
50import org.robolectric.annotation.Implementation;
51import org.robolectric.annotation.Implements;
52import org.robolectric.shadows.ShadowResources;
53
54import java.util.Arrays;
55import java.util.Collections;
56
57@RunWith(SuwLibRobolectricTestRunner.class)
58@Config(
59        sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK },
60        shadows = ShadowApplicationPackageManager.class)
61public class PartnerTest {
62
63    private static final String ACTION_PARTNER_CUSTOMIZATION =
64            "com.android.setupwizard.action.PARTNER_CUSTOMIZATION";
65
66    private Context mContext;
67    private Resources mPartnerResources;
68
69    private ShadowApplicationPackageManager mPackageManager;
70
71    @Before
72    public void setUp() throws Exception {
73        Partner.resetForTesting();
74
75        mContext = spy(application);
76        mPartnerResources = spy(ShadowResources.getSystem());
77
78        mPackageManager =
79                (ShadowApplicationPackageManager) Shadows.shadowOf(application.getPackageManager());
80        mPackageManager.partnerResources = mPartnerResources;
81    }
82
83    @Test
84    public void testLoadPartner() {
85        mPackageManager.addResolveInfoForIntent(
86                new Intent(ACTION_PARTNER_CUSTOMIZATION),
87                Arrays.asList(
88                        createResolveInfo("foo.bar", false, true),
89                        createResolveInfo("test.partner.package", true, true))
90        );
91
92        Partner partner = Partner.get(mContext);
93        assertNotNull("Partner should not be null", partner);
94    }
95
96    @Test
97    public void testLoadNoPartner() {
98        Partner partner = Partner.get(mContext);
99        assertNull("Partner should be null", partner);
100    }
101
102    @Test
103    public void testLoadNonSystemPartner() {
104        mPackageManager.addResolveInfoForIntent(
105                new Intent(ACTION_PARTNER_CUSTOMIZATION),
106                Arrays.asList(
107                        createResolveInfo("foo.bar", false, true),
108                        createResolveInfo("test.partner.package", false, true))
109        );
110
111        Partner partner = Partner.get(mContext);
112        assertNull("Partner should be null", partner);
113    }
114
115    @Test
116    public void testLoadPartnerValue() {
117        doReturn(0x7f010000).when(mPartnerResources)
118                .getIdentifier(eq("suwTransitionDuration"), eq("integer"), anyString());
119        doReturn(5000).when(mPartnerResources).getInteger(eq(0x7f010000));
120
121        mPackageManager.addResolveInfoForIntent(
122                new Intent(ACTION_PARTNER_CUSTOMIZATION),
123                Arrays.asList(
124                        createResolveInfo("foo.bar", false, true),
125                        createResolveInfo("test.partner.package", true, true))
126        );
127
128        ResourceEntry entry = Partner.getResourceEntry(mContext, R.integer.suwTransitionDuration);
129        int partnerValue = entry.resources.getInteger(entry.id);
130        assertEquals("Partner value should be overlaid to 5000", 5000, partnerValue);
131        assertTrue("Partner value should come from overlay", entry.isOverlay);
132    }
133
134    @Test
135    public void testLoadDefaultValue() {
136        mPackageManager.addResolveInfoForIntent(
137                new Intent(ACTION_PARTNER_CUSTOMIZATION),
138                Arrays.asList(
139                        createResolveInfo("foo.bar", false, true),
140                        createResolveInfo("test.partner.package", true, true))
141        );
142
143        ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark);
144        int partnerValue = entry.resources.getColor(entry.id);
145        assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue);
146        assertFalse("Partner value should come from fallback", entry.isOverlay);
147    }
148
149    @Test
150    public void testNotDirectBootAware() {
151        mPackageManager.addResolveInfoForIntent(
152                new Intent(ACTION_PARTNER_CUSTOMIZATION),
153                Collections.singletonList(createResolveInfo("test.partner.package", true, false)));
154
155        ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark);
156        int partnerValue = entry.resources.getColor(entry.id);
157        assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue);
158        assertFalse("Partner value should come from fallback", entry.isOverlay);
159    }
160
161    private ResolveInfo createResolveInfo(
162            String packageName,
163            boolean isSystem,
164            boolean directBootAware) {
165        ResolveInfo info = new ResolveInfo();
166        info.resolvePackageName = packageName;
167        ActivityInfo activityInfo = new ActivityInfo();
168        ApplicationInfo appInfo = new ApplicationInfo();
169        appInfo.flags = isSystem ? ApplicationInfo.FLAG_SYSTEM : 0;
170        appInfo.packageName = packageName;
171        activityInfo.applicationInfo = appInfo;
172        activityInfo.packageName = packageName;
173        activityInfo.name = packageName;
174        if (VERSION.SDK_INT >= VERSION_CODES.N) {
175            activityInfo.directBootAware = directBootAware;
176        }
177        info.activityInfo = activityInfo;
178        return info;
179    }
180
181    @Implements(className = "android.app.ApplicationPackageManager")
182    public static class ShadowApplicationPackageManager extends
183            org.robolectric.shadows.ShadowApplicationPackageManager {
184
185        public Resources partnerResources;
186
187        @Implementation
188        @Override
189        public Resources getResourcesForApplication(ApplicationInfo app)
190                throws NameNotFoundException {
191            if (app != null && "test.partner.package".equals(app.packageName)) {
192                return partnerResources;
193            } else {
194                return super.getResourcesForApplication(app);
195            }
196        }
197    }
198}
199