1 /*
2  * Copyright (C) 2022 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.providers.media.photopicker.espresso;
18 
19 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
20 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
21 import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
22 import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
23 
24 import static androidx.test.espresso.Espresso.onIdle;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import androidx.test.core.app.ActivityScenario;
29 
30 class OrientationUtils {
setLandscapeOrientation( ActivityScenario<T> scenario)31     public static <T extends PhotoPickerTestActivity> void setLandscapeOrientation(
32             ActivityScenario<T> scenario) {
33         changeOrientation(scenario, SCREEN_ORIENTATION_LANDSCAPE, ORIENTATION_LANDSCAPE);
34     }
35 
setPortraitOrientation( ActivityScenario<T> scenario)36     public static <T extends PhotoPickerTestActivity> void setPortraitOrientation(
37             ActivityScenario<T> scenario) {
38         changeOrientation(scenario, SCREEN_ORIENTATION_PORTRAIT, ORIENTATION_PORTRAIT);
39     }
40 
changeOrientation( ActivityScenario<T> scenario, int screenOrientation, int configOrientation)41     private static <T extends PhotoPickerTestActivity> void changeOrientation(
42             ActivityScenario<T> scenario,
43             int screenOrientation,
44             int configOrientation) {
45         scenario.onActivity(
46                 activity -> {
47                     activity.setRequestedOrientation(screenOrientation);
48                 });
49 
50         onIdle();
51 
52         scenario.onActivity(
53                 activity -> {
54                     assertThat(activity.getResources().getConfiguration().orientation)
55                             .isEqualTo(configOrientation);
56                 });
57     }
58 }
59