OSDN Git Service

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