summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers/downloads/FakeSystemFacade.java
blob: 5263015c140a2bb2354f27d94323a528bf76b821 (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
package com.android.providers.downloads;

import android.app.Notification;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.test.AssertionFailedError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class FakeSystemFacade implements SystemFacade {
    long mTimeMillis = 0;
    Integer mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
    boolean mIsRoaming = false;
    Long mMaxBytesOverMobile = null;
    Long mRecommendedMaxBytesOverMobile = null;
    List<Intent> mBroadcastsSent = new ArrayList<Intent>();
    Map<Long,Notification> mActiveNotifications = new HashMap<Long,Notification>();
    List<Notification> mCanceledNotifications = new ArrayList<Notification>();
    Queue<Thread> mStartedThreads = new LinkedList<Thread>();

    void incrementTimeMillis(long delta) {
        mTimeMillis += delta;
    }

    public long currentTimeMillis() {
        return mTimeMillis;
    }

    public Integer getActiveNetworkType() {
        return mActiveNetworkType;
    }

    public boolean isNetworkRoaming() {
        return mIsRoaming;
    }

    public Long getMaxBytesOverMobile() {
        return mMaxBytesOverMobile ;
    }

    public Long getRecommendedMaxBytesOverMobile() {
        return mRecommendedMaxBytesOverMobile ;
    }

    @Override
    public void sendBroadcast(Intent intent) {
        mBroadcastsSent.add(intent);
    }

    @Override
    public boolean userOwnsPackage(int uid, String pckg) throws NameNotFoundException {
        return true;
    }

    @Override
    public void postNotification(long id, Notification notification) {
        if (notification == null) {
            throw new AssertionFailedError("Posting null notification");
        }
        mActiveNotifications.put(id, notification);
    }

    @Override
    public void cancelNotification(long id) {
        Notification notification = mActiveNotifications.remove(id);
        if (notification != null) {
            mCanceledNotifications.add(notification);
        }
    }

    @Override
    public void cancelAllNotifications() {
        for (long id : mActiveNotifications.keySet()) {
            cancelNotification(id);
        }
    }

    @Override
    public void startThread(Thread thread) {
        mStartedThreads.add(thread);
    }

    public void runAllThreads() {
        while (!mStartedThreads.isEmpty()) {
            mStartedThreads.poll().run();
        }
    }
}