1/*
2 * Copyright (C) 2017 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
17package com.android.settings.bluetooth;
18
19import static org.mockito.Mockito.when;
20
21import android.app.Instrumentation;
22import android.content.Context;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.support.test.uiautomator.UiDevice;
30
31import com.android.settings.SettingsActivity;
32import com.android.settings.core.SubSettingLauncher;
33import com.android.settingslib.bluetooth.CachedBluetoothDevice;
34import com.android.settingslib.bluetooth.LocalBluetoothManager;
35import com.android.settingslib.core.instrumentation.Instrumentable;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Answers;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43
44@RunWith(AndroidJUnit4.class)
45@SmallTest
46public class BluetoothDeviceDetailsRotationTest {
47    private Context mContext;
48    private UiDevice mUiDevice;
49    private Instrumentation mInstrumentation;
50
51    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52    private CachedBluetoothDevice mCachedDevice;
53
54    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55    private LocalBluetoothManager mBluetoothManager;
56
57    private String mDeviceAddress;
58
59    @Before
60    public void setUp() throws Exception {
61        MockitoAnnotations.initMocks(this);
62        mContext = InstrumentationRegistry.getTargetContext();
63        mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
64        mInstrumentation = InstrumentationRegistry.getInstrumentation();
65
66        mDeviceAddress = "AA:BB:CC:DD:EE:FF";
67        when(mCachedDevice.getAddress()).thenReturn(mDeviceAddress);
68        when(mCachedDevice.getName()).thenReturn("Mock Device");
69
70        BluetoothDeviceDetailsFragment.sTestDataFactory =
71                new BluetoothDeviceDetailsFragment.TestDataFactory() {
72            @Override
73            public CachedBluetoothDevice getDevice(String deviceAddress) {
74                return mCachedDevice;
75            }
76
77            @Override
78            public LocalBluetoothManager getManager(Context context) {
79                return mBluetoothManager;
80            }
81        };
82    }
83
84    @Test
85    public void rotation() {
86        Intent intent = new Intent("android.settings.BLUETOOTH_SETTINGS");
87        SettingsActivity activity = (SettingsActivity) mInstrumentation.startActivitySync(intent);
88        Bundle args = new Bundle(1);
89        args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, mDeviceAddress);
90        new SubSettingLauncher(activity)
91                .setDestination(BluetoothDeviceDetailsFragment.class.getName())
92                .setTitle("test")
93                .setArguments(args)
94                .setSourceMetricsCategory(Instrumentable.METRICS_CATEGORY_UNKNOWN)
95                .launch();
96        try {
97            mUiDevice.setOrientationLeft();
98            mUiDevice.setOrientationNatural();
99            mUiDevice.setOrientationRight();
100            mUiDevice.setOrientationNatural();
101        } catch (RemoteException e) {
102            throw new RuntimeException(e);
103        }
104    }
105}
106