Commit f907260c authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

JNI refactor: @NativeMethods conversion for ChromeBackupAgent.

This CL was partially created by
//base/android/jni_generator/jni_refactorer.py.

The conversion also required converting unit tests to use the new JNI
mocking approach provided by JniMocker.

Bug: 929661
Change-Id: Iadd97ce4a911792b922cff8803143d0d8c847f43
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1832297Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Commit-Queue: Eric Stevenson <estevenson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702181}
parent 315a0956
...@@ -19,6 +19,7 @@ import org.chromium.base.ContextUtils; ...@@ -19,6 +19,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.PathUtils; import org.chromium.base.PathUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.task.PostTask; import org.chromium.base.task.PostTask;
import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor; import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor;
...@@ -186,8 +187,8 @@ public class ChromeBackupAgent extends BackupAgent { ...@@ -186,8 +187,8 @@ public class ChromeBackupAgent extends BackupAgent {
// immediately, so by the time it does Chrome may not be running. // immediately, so by the time it does Chrome may not be running.
if (!initializeBrowser(backupAgent)) return false; if (!initializeBrowser(backupAgent)) return false;
String[] nativeBackupNames = nativeGetBoolBackupNames(); String[] nativeBackupNames = ChromeBackupAgentJni.get().getBoolBackupNames(this);
boolean[] nativeBackupValues = nativeGetBoolBackupValues(); boolean[] nativeBackupValues = ChromeBackupAgentJni.get().getBoolBackupValues(this);
assert nativeBackupNames.length == nativeBackupValues.length; assert nativeBackupNames.length == nativeBackupValues.length;
for (String name : nativeBackupNames) { for (String name : nativeBackupNames) {
...@@ -362,7 +363,8 @@ public class ChromeBackupAgent extends BackupAgent { ...@@ -362,7 +363,8 @@ public class ChromeBackupAgent extends BackupAgent {
count++; count++;
} }
} }
nativeSetBoolBackupPrefs(nativeBackupNames.toArray(new String[count]), ChromeBackupAgentJni.get().setBoolBackupPrefs(this,
nativeBackupNames.toArray(new String[count]),
Arrays.copyOf(nativeBackupValues, count)); Arrays.copyOf(nativeBackupValues, count));
}); });
...@@ -448,12 +450,10 @@ public class ChromeBackupAgent extends BackupAgent { ...@@ -448,12 +450,10 @@ public class ChromeBackupAgent extends BackupAgent {
} }
} }
@VisibleForTesting @NativeMethods
protected native String[] nativeGetBoolBackupNames(); interface Natives {
String[] getBoolBackupNames(ChromeBackupAgent caller);
@VisibleForTesting boolean[] getBoolBackupValues(ChromeBackupAgent caller);
protected native boolean[] nativeGetBoolBackupValues(); void setBoolBackupPrefs(ChromeBackupAgent caller, String[] name, boolean[] value);
}
@VisibleForTesting
protected native void nativeSetBoolBackupPrefs(String[] name, boolean[] value);
} }
...@@ -12,6 +12,7 @@ import static org.junit.Assert.assertTrue; ...@@ -12,6 +12,7 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
...@@ -29,8 +30,11 @@ import android.content.SharedPreferences; ...@@ -29,8 +30,11 @@ import android.content.SharedPreferences;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
...@@ -41,6 +45,7 @@ import org.chromium.base.ApiCompatibilityUtils; ...@@ -41,6 +45,7 @@ import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.PathUtils; import org.chromium.base.PathUtils;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor; import org.chromium.chrome.browser.firstrun.FirstRunSignInProcessor;
import org.chromium.chrome.browser.firstrun.FirstRunStatus; import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.init.AsyncInitTaskRunner; import org.chromium.chrome.browser.init.AsyncInitTaskRunner;
...@@ -84,6 +89,11 @@ public class ChromeBackupAgentTest { ...@@ -84,6 +89,11 @@ public class ChromeBackupAgentTest {
} }
} }
@Rule
public JniMocker mocker = new JniMocker();
@Mock
private ChromeBackupAgent.Natives mChromeBackupAgentJniMock;
private ChromeBackupAgent mAgent; private ChromeBackupAgent mAgent;
private AsyncInitTaskRunner mTaskRunner; private AsyncInitTaskRunner mTaskRunner;
...@@ -97,28 +107,23 @@ public class ChromeBackupAgentTest { ...@@ -97,28 +107,23 @@ public class ChromeBackupAgentTest {
@Before @Before
public void setUp() { public void setUp() {
// Create the agent to test; override the native calls and fetching the task runner, and // Create the agent to test; override fetching the task runner, and spy on the agent to
// spy on the agent to allow us to validate calls to these methods. // allow us to validate calls to these methods.
mAgent = spy(new ChromeBackupAgent() { mAgent = spy(new ChromeBackupAgent() {
@Override @Override
AsyncInitTaskRunner createAsyncInitTaskRunner(CountDownLatch latch) { AsyncInitTaskRunner createAsyncInitTaskRunner(CountDownLatch latch) {
latch.countDown(); latch.countDown();
return mTaskRunner; return mTaskRunner;
} }
});
@Override MockitoAnnotations.initMocks(this);
protected String[] nativeGetBoolBackupNames() { mocker.mock(ChromeBackupAgentJni.TEST_HOOKS, mChromeBackupAgentJniMock);
return new String[] {"pref1"};
}
@Override when(mChromeBackupAgentJniMock.getBoolBackupNames(mAgent))
protected boolean[] nativeGetBoolBackupValues() { .thenReturn(new String[] {"pref1"});
return new boolean[] {true}; when(mChromeBackupAgentJniMock.getBoolBackupValues(mAgent))
} .thenReturn(new boolean[] {true});
@Override
protected void nativeSetBoolBackupPrefs(String[] s, boolean[] b) {}
});
// Mock initializing the browser // Mock initializing the browser
doReturn(true).when(mAgent).initializeBrowser(any(Context.class)); doReturn(true).when(mAgent).initializeBrowser(any(Context.class));
...@@ -427,8 +432,9 @@ public class ChromeBackupAgentTest { ...@@ -427,8 +432,9 @@ public class ChromeBackupAgentTest {
SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
assertTrue(prefs.getBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, false)); assertTrue(prefs.getBoolean(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE, false));
assertFalse(prefs.contains("junk")); assertFalse(prefs.contains("junk"));
verify(mAgent).nativeSetBoolBackupPrefs( verify(mChromeBackupAgentJniMock)
new String[] {"pref1", "pref2"}, new boolean[] {false, true}); .setBoolBackupPrefs(
mAgent, new String[] {"pref1", "pref2"}, new boolean[] {false, true});
verify(mTaskRunner) verify(mTaskRunner)
.startBackgroundTasks( .startBackgroundTasks(
false /* allocateChildConnection */, true /* initVariationSeed */); false /* allocateChildConnection */, true /* initVariationSeed */);
...@@ -463,7 +469,8 @@ public class ChromeBackupAgentTest { ...@@ -463,7 +469,8 @@ public class ChromeBackupAgentTest {
mAgent.onRestore(backupData, 0, newState); mAgent.onRestore(backupData, 0, newState);
SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
assertFalse(prefs.contains(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE)); assertFalse(prefs.contains(FirstRunStatus.FIRST_RUN_FLOW_COMPLETE));
verify(mAgent, never()).nativeSetBoolBackupPrefs(any(String[].class), any(boolean[].class)); verify(mChromeBackupAgentJniMock, never())
.setBoolBackupPrefs(eq(mAgent), any(String[].class), any(boolean[].class));
verify(mTaskRunner) verify(mTaskRunner)
.startBackgroundTasks( .startBackgroundTasks(
false /* allocateChildConnection */, true /* initVariationSeed */); false /* allocateChildConnection */, true /* initVariationSeed */);
......
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