OSDN Git Service

Import translations. DO NOT MERGE am: 9351e5f083 -s ours
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / testutils / FakeFeatureFactory.java
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.testutils;
17
18 import android.content.Context;
19
20 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
21 import com.android.settings.dashboard.DashboardFeatureProvider;
22 import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
23 import com.android.settings.localepicker.LocaleFeatureProvider;
24 import com.android.settings.overlay.FeatureFactory;
25 import com.android.settings.overlay.SupportFeatureProvider;
26
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 /**
32  * Test util to provide fake FeatureFactory. To use this factory, call {@code setupForTest} in
33  * {@code @Before} method of the test class.
34  */
35 public class FakeFeatureFactory extends FeatureFactory {
36
37     public final SupportFeatureProvider supportFeatureProvider;
38     public final MetricsFeatureProvider metricsFeatureProvider;
39     public final PowerUsageFeatureProvider powerUsageFeatureProvider;
40     public final DashboardFeatureProvider dashboardFeatureProvider;
41     public final LocaleFeatureProvider localeFeatureProvider;
42
43     /**
44      * Call this in {@code @Before} method of the test class to use fake factory.
45      *
46      * @param context The context must be a deep mock.
47      */
48     public static void setupForTest(Context context) {
49         sFactory = null;
50         when(context.getString(com.android.settings.R.string.config_featureFactory))
51                 .thenReturn(FakeFeatureFactory.class.getName());
52         try {
53             Class c = FakeFeatureFactory.class;
54             when(context.getClassLoader().loadClass(anyString())).thenReturn(c);
55         } catch (ClassNotFoundException e) {
56             // Ignore.
57         }
58     }
59
60     /**
61      * Used by reflection. Do not call directly.
62      */
63     public FakeFeatureFactory() {
64         supportFeatureProvider = mock(SupportFeatureProvider.class);
65         metricsFeatureProvider = mock(MetricsFeatureProvider.class);
66         powerUsageFeatureProvider = mock(PowerUsageFeatureProvider.class);
67         dashboardFeatureProvider = mock(DashboardFeatureProvider.class);
68         localeFeatureProvider = mock(LocaleFeatureProvider.class);
69     }
70
71     @Override
72     public SupportFeatureProvider getSupportFeatureProvider(Context context) {
73         return supportFeatureProvider;
74     }
75
76     @Override
77     public MetricsFeatureProvider getMetricsFeatureProvider() {
78         return metricsFeatureProvider;
79     }
80
81     @Override
82     public PowerUsageFeatureProvider getPowerUsageFeatureProvider() {
83         return powerUsageFeatureProvider;
84     }
85
86     @Override
87     public DashboardFeatureProvider getDashboardFeatureProvider(Context context) {
88         return dashboardFeatureProvider;
89     }
90
91     @Override
92     public LocaleFeatureProvider getLocaleFeatureProvider() {
93         return localeFeatureProvider;
94     }
95 }