1 /*
2  * Copyright (C) 2021 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.car.debuggingrestrictioncontroller;
18 
19 import static androidx.test.espresso.Espresso.onView;
20 import static androidx.test.espresso.action.ViewActions.click;
21 import static androidx.test.espresso.assertion.ViewAssertions.matches;
22 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
23 import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
24 import static androidx.test.espresso.matcher.ViewMatchers.withId;
25 import static org.junit.Assert.assertThat;
26 
27 import android.app.Activity;
28 import androidx.test.espresso.IdlingRegistry;
29 import androidx.test.espresso.contrib.ActivityResultMatchers;
30 import androidx.test.espresso.idling.CountingIdlingResource;
31 import androidx.test.ext.junit.rules.ActivityScenarioRule;
32 import androidx.test.ext.junit.runners.AndroidJUnit4;
33 import com.android.car.debuggingrestrictioncontroller.ui.token.TokenActivity;
34 import com.google.android.gms.tasks.Tasks;
35 import com.google.firebase.auth.FirebaseAuth;
36 import java.util.concurrent.ExecutionException;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 @RunWith(AndroidJUnit4.class)
44 public class TokenTest {
45 
46   private static final String TEST_EMAIL = BuildConfig.DRC_TEST_EMAIL;
47   private static final String TEST_PASSWORD = BuildConfig.DRC_TEST_PASSWORD;
48   private final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
49 
50   @Rule
51   public ActivityScenarioRule<TokenActivity> activityScenarioRule =
52       new ActivityScenarioRule<TokenActivity>(TokenActivity.class);
53 
54   private CountingIdlingResource idlingResource;
55 
TokenTest()56   public TokenTest() throws ExecutionException, InterruptedException {
57     Tasks.await(firebaseAuth.signInWithEmailAndPassword(TEST_EMAIL, TEST_PASSWORD));
58   }
59 
60   @Before
setUp()61   public void setUp() {
62     activityScenarioRule
63         .getScenario()
64         .onActivity(
65             activity -> {
66               idlingResource = activity.getIdlingResource();
67             });
68     IdlingRegistry.getInstance().register(idlingResource);
69   }
70 
71   @After
tearDown()72   public void tearDown() {
73     IdlingRegistry.getInstance().unregister(idlingResource);
74     firebaseAuth.signOut();
75   }
76 
77   @Test
initialButtonStates()78   public void initialButtonStates() {
79     onView(withId(R.id.agreement)).check(matches(isDisplayed()));
80     onView(withId(R.id.agree)).check(matches(isDisplayed()));
81     onView(withId(R.id.agree)).check(matches(isEnabled()));
82     onView(withId(R.id.disagree)).check(matches(isDisplayed()));
83   }
84 
85   @Test
agree()86   public void agree() {
87     onView(withId(R.id.agree)).check(matches(isEnabled()));
88     onView(withId(R.id.agree)).perform(click());
89     assertThat(
90         activityScenarioRule.getScenario().getResult(),
91         ActivityResultMatchers.hasResultCode(Activity.RESULT_OK));
92   }
93 
94   @Test
disagree()95   public void disagree() {
96     onView(withId(R.id.disagree)).check(matches(isEnabled()));
97     onView(withId(R.id.disagree)).perform(click());
98     assertThat(
99         activityScenarioRule.getScenario().getResult(),
100         ActivityResultMatchers.hasResultCode(Activity.RESULT_CANCELED));
101   }
102 
103   @Test
userNotSignedIn()104   public void userNotSignedIn() {
105     firebaseAuth.signOut();
106     onView(withId(R.id.agree)).perform(click());
107     assertThat(
108         activityScenarioRule.getScenario().getResult(),
109         ActivityResultMatchers.hasResultCode(Activity.RESULT_CANCELED));
110   }
111 }
112