OSDN Git Service

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