aboutsummaryrefslogtreecommitdiff
path: root/system/AppUsageStatistics/Application/tests/src/com/example/android/appusagestatistics/AppUsageStatisticsFragmentTests.java
blob: d8adb1ce3b9dd5c32e1746b7989cb5118cb3d006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.appusagestatistics;

import com.example.android.appusagestatistics.AppUsageStatisticsFragment.StatsUsageInterval;

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.test.ActivityInstrumentationTestCase2;

import java.util.List;

/**
 * Tests for {@link com.example.android.appusagestatistics.AppUsageStatisticsFragment}.
 */
public class AppUsageStatisticsFragmentTests
        extends ActivityInstrumentationTestCase2<AppUsageStatisticsActivity> {

    private AppUsageStatisticsActivity mTestActivity;
    private AppUsageStatisticsFragment mTestFragment;

    public AppUsageStatisticsFragmentTests() {
        super(AppUsageStatisticsActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Starts the activity under test using the default Intent with:
        // action = {@link Intent#ACTION_MAIN}
        // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
        // All other fields are null or empty.
        mTestActivity = getActivity();
        mTestFragment = (AppUsageStatisticsFragment)
                mTestActivity.getSupportFragmentManager().getFragments().get(0);
    }

    /**
     * Test if the test fixture has been set up correctly.
     */
    public void testPreconditions() {
        assertNotNull(mTestFragment.mUsageStatsManager);
        assertNotNull(mTestFragment.mUsageListAdapter);
        assertNotNull(mTestFragment.mRecyclerView);
        assertNotNull(mTestFragment.mLayoutManager);
        assertNotNull(mTestFragment.mOpenUsageSettingButton);
        assertNotNull(mTestFragment.mSpinner);
    }

    public void testStatsUsageInterval_getValue() {
        assertTrue(StatsUsageInterval.DAILY == StatsUsageInterval.getValue("Daily"));
        assertTrue(StatsUsageInterval.WEEKLY == StatsUsageInterval.getValue("Weekly"));
        assertTrue(StatsUsageInterval.MONTHLY == StatsUsageInterval.getValue("Monthly"));
        assertTrue(StatsUsageInterval.YEARLY == StatsUsageInterval.getValue("Yearly"));
        assertNull(StatsUsageInterval.getValue("NonExistent"));
    }

    public void testGetUsageStatistics() {
        List<UsageStats> usageStatsList = mTestFragment
                .getUsageStatistics(UsageStatsManager.INTERVAL_DAILY);

        // Whether the usageStatsList has any UsageStats depends on if the app is granted
        // the access to App usage statistics.
        // Only check non null here.
        assertNotNull(usageStatsList);
    }

    public void testUpdateAppsList() {
        List<UsageStats> usageStatsList = mTestFragment
                .getUsageStatistics(UsageStatsManager.INTERVAL_DAILY);

        mTestFragment.updateAppsList(usageStatsList);
        getInstrumentation().waitForIdleSync();

        // The result depends on if the app is granted the access to App usage statistics.
        if (usageStatsList.size() == 0) {
            assertTrue(mTestFragment.mUsageListAdapter.getItemCount() == 0);
        } else {
            assertTrue(mTestFragment.mUsageListAdapter.getItemCount() > 0);
        }
    }
}