• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.systemui;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.os.Handler;
22 import android.os.HandlerExecutor;
23 import android.os.Looper;
24 import android.test.mock.MockContext;
25 import android.util.Singleton;
26 
27 import androidx.annotation.NonNull;
28 import androidx.test.InstrumentationRegistry;
29 
30 import org.junit.Rule;
31 import org.mockito.Mockito;
32 
33 import java.util.concurrent.Executor;
34 
35 public class CarSysuiTestCase extends SysuiTestCase {
36 
37     @Rule
38     public SysuiTestableContext mContext = createTestableContext();
39 
createTestableContext()40     private SysuiTestableContext createTestableContext() {
41         SysuiTestableContext context = new SysuiTestableContext(
42                 getTestableContextBase(), getLeakCheck());
43 
44         if (isRobolectricTest()) {
45             // Manually associate a Display to context for Robolectric test. Similar to b/214297409
46             return context.createDefaultDisplayContext();
47         } else {
48             return context;
49         }
50     }
51 
52     @NonNull
getTestableContextBase()53     private Context getTestableContextBase() {
54         if (isRavenwoodTest()) {
55             // TODO(b/292141694): build out Ravenwood support for Context
56             // Ravenwood doesn't yet provide a Context, but many SysUI tests assume one exists;
57             // so here we construct just enough of a Context to be useful; this will be replaced
58             // as more of the Ravenwood environment is built out
59             return new MockContext() {
60                 @Override
61                 public void setTheme(int resid) {
62                     // TODO(b/318393625): build out Ravenwood support for Resources
63                     // until then, ignored as no-op
64                 }
65 
66                 @Override
67                 public Resources getResources() {
68                     // TODO(b/318393625): build out Ravenwood support for Resources
69                     return Mockito.mock(Resources.class);
70                 }
71 
72                 private Singleton<Executor> mMainExecutor = new Singleton<>() {
73                     @Override
74                     protected Executor create() {
75                         return new HandlerExecutor(new Handler(Looper.getMainLooper()));
76                     }
77                 };
78 
79                 @Override
80                 public Executor getMainExecutor() {
81                     return mMainExecutor.get();
82                 }
83             };
84         } else {
85             return InstrumentationRegistry.getContext().getApplicationContext();
86         }
87     }
88 
89     @Override
90     public SysuiTestableContext getContext() {
91         return mContext;
92     }
93 }
94