DeviceOwnerTest.java revision 776c555d954d9494069f786785877c08add27327
1/*
2 * Copyright (C) 2014 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.server.devicepolicy;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24
25/**
26 * Tests for the DeviceOwner object that saves & loads device and policy owner information.
27 * run this test with:
28 *   make -j FrameworksServicesTests
29 *   runtest --path frameworks/base/services/tests/servicestests/ \
30 *       src/com/android/server/devicepolicy/DeviceOwnerTest.java
31 */
32public class DeviceOwnerTest extends AndroidTestCase {
33
34    private ByteArrayInputStream mInputStreamForTest;
35    private ByteArrayOutputStream mOutputStreamForTest = new ByteArrayOutputStream();
36
37    @SmallTest
38    public void testDeviceOwnerOnly() throws Exception {
39        DeviceOwner out = new DeviceOwner(null, mOutputStreamForTest);
40        out.setDeviceOwner("some.device.owner.package", "owner");
41        out.writeOwnerFile();
42
43        mInputStreamForTest = new ByteArrayInputStream(mOutputStreamForTest.toByteArray());
44        DeviceOwner in = new DeviceOwner(mInputStreamForTest, null);
45        in.readOwnerFile();
46
47        assertEquals("some.device.owner.package", in.getDeviceOwnerPackageName());
48        assertEquals("owner", in.getDeviceOwnerName());
49        assertNull(in.getProfileOwnerPackageName(1));
50    }
51
52    @SmallTest
53    public void testProfileOwnerOnly() throws Exception {
54        DeviceOwner out = new DeviceOwner(null, mOutputStreamForTest);
55        out.setProfileOwner("some.profile.owner.package", "some-company", 1);
56        out.writeOwnerFile();
57
58        mInputStreamForTest = new ByteArrayInputStream(mOutputStreamForTest.toByteArray());
59        DeviceOwner in = new DeviceOwner(mInputStreamForTest, null);
60        in.readOwnerFile();
61
62        assertNull(in.getDeviceOwnerPackageName());
63        assertNull(in.getDeviceOwnerName());
64        assertEquals("some.profile.owner.package", in.getProfileOwnerPackageName(1));
65        assertEquals("some-company", in.getProfileOwnerName(1));
66    }
67
68    @SmallTest
69    public void testDeviceAndProfileOwners() throws Exception {
70        DeviceOwner out = new DeviceOwner(null, mOutputStreamForTest);
71        out.setDeviceOwner("some.device.owner.package", "owner");
72        out.setProfileOwner("some.profile.owner.package", "some-company", 1);
73        out.setProfileOwner("some.other.profile.owner", "some-other-company", 2);
74        out.writeOwnerFile();
75
76        mInputStreamForTest = new ByteArrayInputStream(mOutputStreamForTest.toByteArray());
77
78        DeviceOwner in = new DeviceOwner(mInputStreamForTest, null);
79        in.readOwnerFile();
80
81        assertEquals("some.device.owner.package", in.getDeviceOwnerPackageName());
82        assertEquals("owner", in.getDeviceOwnerName());
83        assertEquals("some.profile.owner.package", in.getProfileOwnerPackageName(1));
84        assertEquals("some-company", in.getProfileOwnerName(1));
85        assertEquals("some.other.profile.owner", in.getProfileOwnerPackageName(2));
86        assertEquals("some-other-company", in.getProfileOwnerName(2));
87    }
88}