1 /*
2  * Copyright (C) 2024 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.bedstead.accounts
17 
18 import com.android.bedstead.accounts.annotations.EnsureHasAccount
19 import com.android.bedstead.accounts.annotations.EnsureHasAccountAuthenticator
20 import com.android.bedstead.accounts.annotations.EnsureHasAccounts
21 import com.android.bedstead.accounts.annotations.EnsureHasNoAccounts
22 import com.android.bedstead.harrier.BedsteadJUnit4
23 import com.android.bedstead.harrier.DeviceState
24 import com.android.bedstead.harrier.UserType
25 import com.android.bedstead.multiuser.additionalUser
26 import com.android.bedstead.multiuser.annotations.EnsureHasAdditionalUser
27 import com.android.bedstead.nene.TestApis.accounts
28 import com.google.common.truth.Truth.assertThat
29 import org.junit.ClassRule
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 @RunWith(BedsteadJUnit4::class)
35 class AccountsAnnotationExecutorTest {
36 
37     @EnsureHasAccountAuthenticator
38     @Test
ensureHasAccountAuthenticatorAnnotation_accountAuthenticatorIsInstallednull39     fun ensureHasAccountAuthenticatorAnnotation_accountAuthenticatorIsInstalled() {
40         assertThat(
41             deviceState
42                 .accounts()
43                 .testApp()
44                 .pkg()
45                 .installedOnUser()
46         ).isTrue()
47     }
48 
49     @Test
50     @EnsureHasAdditionalUser
51     @EnsureHasAccountAuthenticator(onUser = UserType.ADDITIONAL_USER)
ensureHasAccountAuthenticatorAnnotation_differentUser_accountAuthenticatorIsInstalledOnDifferentUsernull52     fun ensureHasAccountAuthenticatorAnnotation_differentUser_accountAuthenticatorIsInstalledOnDifferentUser() {
53         assertThat(
54             deviceState
55                 .accounts(deviceState.additionalUser())
56                 .testApp()
57                 .pkg()
58                 .installedOnUser(deviceState.additionalUser())
59         ).isTrue()
60     }
61 
62     @EnsureHasAccount
63     @Test
ensureHasAccountAnnotation_accountExistsnull64     fun ensureHasAccountAnnotation_accountExists() {
65         assertThat(deviceState.accounts().allAccounts()).isNotEmpty()
66     }
67 
68     @EnsureHasAccount
69     @Test
account_returnsAccountnull70     fun account_returnsAccount() {
71         assertThat(deviceState.account()).isNotNull()
72     }
73 
74     @EnsureHasAccount(key = "testKey")
75     @Test
account_withKey_returnsAccountnull76     fun account_withKey_returnsAccount() {
77         assertThat(deviceState.account("testKey")).isNotNull()
78     }
79 
80     @EnsureHasNoAccounts
81     @Test
ensureHasNoAccountsAnnotation_hasNoAccountsnull82     fun ensureHasNoAccountsAnnotation_hasNoAccounts() {
83         assertThat(accounts().all()).isEmpty()
84     }
85 
86     @EnsureHasAccounts(EnsureHasAccount(), EnsureHasAccount())
87     @Test
ensureHasAccountsAnnotation_hasMultipleAccountsnull88     fun ensureHasAccountsAnnotation_hasMultipleAccounts() {
89         assertThat(deviceState.accounts().allAccounts().size).isGreaterThan(1)
90     }
91 
92     companion object {
93         @JvmField
94         @ClassRule
95         @Rule
96         val deviceState = DeviceState()
97     }
98 }
99