Commit db1ff46b authored by nyquist@chromium.org's avatar nyquist@chromium.org

Fix StackOverFlow in AdvancedMockContext.

The AdvancedMockContext previously did not override registerComponentCallbacks
and unregisterComponentCallbacks. The implementation of these methods in
Context calls getApplicationContext before delegating the call to it, and since
AdvancedMockContext returns |this| in getApplicationContext, this leads to a
loop.

This CL adds overrides for these two methods that make the calls to the base
context instead, which will typically either be a MockContext or the target
context being instrumented.

BUG=394464

Review URL: https://codereview.chromium.org/404553005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284023 0039d316-1c4b-4281-b951-d872f2087c98
parent 8a14e2a3
// Copyright 2014 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.test.InstrumentationTestCase;
import org.chromium.base.test.util.AdvancedMockContext;
/**
* Tests for {@link org.chromium.base.test.util.AdvancedMockContext}.
*/
public class AdvancedMockContextTest extends InstrumentationTestCase {
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;
}
}
public void testComponentCallbacksForTargetContext() {
Context targetContext = 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();
assertTrue("onLowMemory should have been called.", callback1.mOnLowMemoryCalled);
assertTrue("onLowMemory should have been called.", callback2.mOnLowMemoryCalled);
Configuration configuration = new Configuration();
targetApplication.onConfigurationChanged(configuration);
assertEquals("onConfigurationChanged should have been called.", configuration,
callback1.mConfiguration);
assertEquals("onConfigurationChanged should have been called.", configuration,
callback2.mConfiguration);
targetApplication.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);
assertEquals("onTrimMemory should have been called.", ComponentCallbacks2
.TRIM_MEMORY_MODERATE, callback2.mLevel);
}
}
......@@ -4,6 +4,7 @@
package org.chromium.base.test.util;
import android.content.ComponentCallbacks;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
......@@ -64,6 +65,16 @@ public class AdvancedMockContext extends ContextWrapper {
}
}
@Override
public void registerComponentCallbacks(ComponentCallbacks callback) {
getBaseContext().registerComponentCallbacks(callback);
}
@Override
public void unregisterComponentCallbacks(ComponentCallbacks callback) {
getBaseContext().unregisterComponentCallbacks(callback);
}
public void addSharedPreferences(String name, Map<String, Object> data) {
synchronized (mSharedPreferences) {
mSharedPreferences.put(name, new InMemorySharedPreferences(data));
......@@ -82,6 +93,10 @@ public class AdvancedMockContext extends ContextWrapper {
return mFlags.containsKey(key) && mFlags.get(key);
}
/**
* Builder for maps of type Map<String, Object> to be used with
* {@link #addSharedPreferences(String, java.util.Map)}.
*/
public static class MapBuilder {
private final Map<String, Object> mData = new HashMap<String, Object>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment