• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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.google.android.libraries.mobiledatadownload.account;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.accounts.Account;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.robolectric.RobolectricTestRunner;
24 
25 /** Unit tests for {@link AccountUtil}. */
26 @RunWith(RobolectricTestRunner.class)
27 public class AccountUtilTest {
28 
29   @Test
createAccount()30   public void createAccount() {
31     String name = "account-name";
32     String type = "account-type";
33 
34     Account account = AccountUtil.create(name, type);
35 
36     assertThat(account).isNotNull();
37     assertThat(account.name).isEqualTo(name);
38     assertThat(account.type).isEqualTo(type);
39   }
40 
41   @Test
createAccount_emptyField()42   public void createAccount_emptyField() {
43     String name = "account-name";
44     String type = "account-type";
45 
46     // Neither name nor type can be empty.
47     assertThat(AccountUtil.create("" /* name */, type)).isNull();
48     assertThat(AccountUtil.create(null /* name */, type)).isNull();
49     assertThat(AccountUtil.create(name, "" /* type */)).isNull();
50     assertThat(AccountUtil.create(name, null /* type */)).isNull();
51   }
52 
53   @Test
createAccount_containDelimiter()54   public void createAccount_containDelimiter() {
55     String name = "account-name";
56     String type = "account-type";
57 
58     // Neither name or type can contain account delimiter.
59     assertThat(AccountUtil.create("prefix:suffix" /* name */, type)).isNull();
60     assertThat(AccountUtil.create(name, "prefix:suffix" /* type */)).isNull();
61   }
62 
63   @Test
createAccount_containSplitChar()64   public void createAccount_containSplitChar() {
65     String name = "account-name";
66     String type = "account-type";
67 
68     // Neither name or type can contain split char.
69     assertThat(AccountUtil.create("prefix|suffix" /* name */, type)).isNull();
70     assertThat(AccountUtil.create(name, "prefix|suffix" /* type */)).isNull();
71   }
72 
73   @Test
serializeAccount()74   public void serializeAccount() {
75     String name = "account-name";
76     String type = "account-type";
77 
78     Account account = new Account(name, type);
79     String serialized = AccountUtil.serialize(account);
80 
81     assertThat(serialized).isEqualTo("account-type:account-name");
82   }
83 
84   @Test
deserializeAccount()85   public void deserializeAccount() {
86     String accountStr = "foo:bar";
87     Account account = AccountUtil.deserialize(accountStr);
88 
89     assertThat(account.type).isEqualTo("foo");
90     assertThat(account.name).isEqualTo("bar");
91   }
92 
93   @Test
deserializeAccount_noDelimiter()94   public void deserializeAccount_noDelimiter() {
95     // No account delimiter is found.
96     assertThat(AccountUtil.deserialize("type and name")).isNull();
97   }
98 
99   @Test
deserializeAccount_multipleDelimiter()100   public void deserializeAccount_multipleDelimiter() {
101     // Multiple account delimiters are found.
102     assertThat(AccountUtil.deserialize("type:name:foo")).isNull();
103     assertThat(AccountUtil.deserialize("type::name")).isNull();
104   }
105 
106   @Test
deserializeAccount_containsSplitchar()107   public void deserializeAccount_containsSplitchar() {
108     // Neither name or type can contain split char.
109     assertThat(AccountUtil.deserialize("type:na|me")).isNull();
110     assertThat(AccountUtil.deserialize("ty|pe:name")).isNull();
111   }
112 }
113