• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import android.app.Application;
8 import android.content.ComponentCallbacks;
9 import android.content.ComponentCallbacks2;
10 import android.content.Context;
11 import android.content.res.Configuration;
12 import android.test.InstrumentationTestCase;
13 import android.test.suitebuilder.annotation.SmallTest;
14 
15 import org.chromium.base.test.util.AdvancedMockContext;
16 
17 /**
18  * Tests for {@link org.chromium.base.test.util.AdvancedMockContext}.
19  */
20 public class AdvancedMockContextTest extends InstrumentationTestCase {
21     private static class Callback1 implements ComponentCallbacks {
22         protected Configuration mConfiguration;
23         protected boolean mOnLowMemoryCalled;
24 
25         @Override
onConfigurationChanged(Configuration configuration)26         public void onConfigurationChanged(Configuration configuration) {
27             mConfiguration = configuration;
28         }
29 
30         @Override
onLowMemory()31         public void onLowMemory() {
32             mOnLowMemoryCalled = true;
33         }
34     }
35 
36     private static class Callback2 extends Callback1 implements ComponentCallbacks2 {
37         private int mLevel;
38 
39         @Override
onTrimMemory(int level)40         public void onTrimMemory(int level) {
41             mLevel = level;
42         }
43     }
44 
45     @SmallTest
testComponentCallbacksForTargetContext()46     public void testComponentCallbacksForTargetContext() {
47         Context targetContext = getInstrumentation().getTargetContext();
48         Application targetApplication = (Application) targetContext.getApplicationContext();
49         AdvancedMockContext context = new AdvancedMockContext(targetContext);
50         Callback1 callback1 = new Callback1();
51         Callback2 callback2 = new Callback2();
52         context.registerComponentCallbacks(callback1);
53         context.registerComponentCallbacks(callback2);
54 
55         targetApplication.onLowMemory();
56         assertTrue("onLowMemory should have been called.", callback1.mOnLowMemoryCalled);
57         assertTrue("onLowMemory should have been called.", callback2.mOnLowMemoryCalled);
58 
59         Configuration configuration = new Configuration();
60         targetApplication.onConfigurationChanged(configuration);
61         assertEquals("onConfigurationChanged should have been called.", configuration,
62                 callback1.mConfiguration);
63         assertEquals("onConfigurationChanged should have been called.", configuration,
64                 callback2.mConfiguration);
65 
66         targetApplication.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);
67         assertEquals("onTrimMemory should have been called.", ComponentCallbacks2
68                 .TRIM_MEMORY_MODERATE, callback2.mLevel);
69     }
70 }
71