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 
17 package com.android.settingslib;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.system.StructUtsname;
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.robolectric.RobolectricTestRunner;
28 import org.robolectric.RuntimeEnvironment;
29 
30 @RunWith(RobolectricTestRunner.class)
31 public class DeviceInfoUtilsTest {
32 
33     private Context mContext;
34 
35     @Before
setup()36     public void setup() {
37         mContext = RuntimeEnvironment.application;
38     }
39 
40     @Test
formatKernelVersion_regularInputVersion_shouldStripOptionalValues()41     public void formatKernelVersion_regularInputVersion_shouldStripOptionalValues() {
42         final String sysName = "Linux";
43         final String nodeName = "localhost";
44         final String release = "4.4.88-g134be430baab";
45         final String version = "#1 SMP PREEMPT Tue Dec 31 12:00:00 UTC 2017";
46         final String machine = "aarch64";
47         final StructUtsname uname = new StructUtsname(sysName, nodeName, release, version, machine);
48 
49         final String expected = release + "\n" + "#1 Tue Dec 31 12:00:00 UTC 2017";
50 
51         assertThat(DeviceInfoUtils.formatKernelVersion(mContext, uname)).isEqualTo(expected);
52     }
53 
54     @Test
formatKernelVersion_nonRegularInputVersion_shouldBeUnavailable()55     public void formatKernelVersion_nonRegularInputVersion_shouldBeUnavailable() {
56         final String sysName = "Linux";
57         final String nodeName = "localhost";
58         final String release = "4.4.88-g134be430baab";
59         final String version = "%@%!asd%#@!$" + "\n " + "fasdfasdfa13ta";
60         final String machine = "aarch64";
61         final StructUtsname uname = new StructUtsname(sysName, nodeName, release, version, machine);
62 
63         final String expected = mContext.getString(R.string.status_unavailable);
64 
65         assertThat(DeviceInfoUtils.formatKernelVersion(mContext, uname)).isEqualTo(expected);
66     }
67 
68     @Test
formatKernelVersion_nullInputVersion_shouldBeUnavailable()69     public void formatKernelVersion_nullInputVersion_shouldBeUnavailable() {
70         final String expected = mContext.getString(R.string.status_unavailable);
71 
72         assertThat(DeviceInfoUtils.formatKernelVersion(mContext, null)).isEqualTo(expected);
73     }
74 }
75