summaryrefslogtreecommitdiff
path: root/base/android/javatests/src/org/chromium/base/AdvancedMockContextTest.java
blob: 20c626d1945ee81fc9d86344e28d300f0a7f6dc3 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base;

import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.AdvancedMockContext;

/**
 * Tests for {@link org.chromium.base.test.util.AdvancedMockContext}.
 */
@RunWith(BaseJUnit4ClassRunner.class)
public class AdvancedMockContextTest {
    private static class Callback1 implements ComponentCallbacks {
        protected Configuration mConfiguration;
        protected boolean mOnLowMemoryCalled;

        @Override
        public void onConfigurationChanged(Configuration configuration) {
            mConfiguration = configuration;
        }

        @Override
        public void onLowMemory() {
            mOnLowMemoryCalled = true;
        }
    }

    private static class Callback2 extends Callback1 implements ComponentCallbacks2 {
        private int mLevel;

        @Override
        public void onTrimMemory(int level) {
            mLevel = level;
        }
    }

    @Test
    @SmallTest
    public void testComponentCallbacksForTargetContext() {
        Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Application targetApplication = (Application) targetContext.getApplicationContext();
        AdvancedMockContext context = new AdvancedMockContext(targetContext);
        Callback1 callback1 = new Callback1();
        Callback2 callback2 = new Callback2();
        context.registerComponentCallbacks(callback1);
        context.registerComponentCallbacks(callback2);

        targetApplication.onLowMemory();
        Assert.assertTrue("onLowMemory should have been called.", callback1.mOnLowMemoryCalled);
        Assert.assertTrue("onLowMemory should have been called.", callback2.mOnLowMemoryCalled);

        Configuration configuration = new Configuration();
        targetApplication.onConfigurationChanged(configuration);
        Assert.assertEquals("onConfigurationChanged should have been called.", configuration,
                callback1.mConfiguration);
        Assert.assertEquals("onConfigurationChanged should have been called.", configuration,
                callback2.mConfiguration);

        targetApplication.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);
        Assert.assertEquals("onTrimMemory should have been called.",
                ComponentCallbacks2.TRIM_MEMORY_MODERATE, callback2.mLevel);
    }
}