Commit 7dab1297 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

Fix NPEs from null EnterpriseInfo#OwnedState

Bug: 1124000
Change-Id: I9df9d450e7e1cd053407b24e83421a7d1242a19f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2389241Reviewed-by: default avatarWenyu Fu <wenyufu@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Commit-Queue: Sky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805386}
parent 551bbd27
......@@ -246,7 +246,8 @@ public class TosAndUmaFirstRunFragmentWithEnterpriseSupport
}
private void onIsDeviceOwnedDetected(EnterpriseInfo.OwnedState ownedState) {
mIsDeviceOwned = ownedState.mDeviceOwned;
// If unable to determine the owned state then fail closed, no skipping.
mIsDeviceOwned = ownedState != null && ownedState.mDeviceOwned;
maybeHideSpinner();
RecordHistogram.recordTimesHistogram(mViewCreated
......
......@@ -237,10 +237,10 @@ public class EnterpriseInfo {
if (result == null) {
// Unable to determine the owned state, assume it's not owned.
EnterpriseInfoJni.get().updateNativeOwnedState(false, false);
} else {
EnterpriseInfoJni.get().updateNativeOwnedState(
result.mDeviceOwned, result.mProfileOwned);
}
EnterpriseInfoJni.get().updateNativeOwnedState(
result.mDeviceOwned, result.mProfileOwned);
};
EnterpriseInfo.getInstance().getDeviceEnterpriseInfo(callback);
......
......@@ -281,6 +281,20 @@ public class TosAndUmaFirstRunFragmentWithEnterpriseSupportTest {
"MobileFre.CctTos.IsDeviceOwnedCheckSpeed.SlowerThanInflation"));
}
@Test
@SmallTest
public void testNullOwnedState() {
setAppRestrictiosnMockInitialized(true);
setPolicyServiceMockInitializedWithDialogEnabled(false);
launchFirstRunThroughCustomTab();
assertUIState(FragmentState.LOADING);
// Null means loading checking if the device is owned failed. This should be treated the
// same as not being owned, and no skipping should occur.
setEnterpriseInfoInitializedWithOwnedState(null);
assertUIState(FragmentState.NO_POLICY);
}
/**
* Launch chrome through custom tab and trigger first run.
*/
......@@ -407,7 +421,11 @@ public class TosAndUmaFirstRunFragmentWithEnterpriseSupportTest {
}
private void setEnterpriseInfoInitializedWithDeviceOwner(boolean hasDeviceOwner) {
EnterpriseInfo.OwnedState ownedState = new EnterpriseInfo.OwnedState(hasDeviceOwner, false);
setEnterpriseInfoInitializedWithOwnedState(
new EnterpriseInfo.OwnedState(hasDeviceOwner, false));
}
private void setEnterpriseInfoInitializedWithOwnedState(EnterpriseInfo.OwnedState ownedState) {
Mockito.doAnswer(invocation -> {
Callback<EnterpriseInfo.OwnedState> callback = invocation.getArgument(0);
callback.onResult(ownedState);
......
......@@ -8,25 +8,46 @@ import android.os.Handler;
import android.os.Looper;
import android.support.test.filters.SmallTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.Callback;
import org.chromium.base.task.TaskTraits;
import org.chromium.base.task.test.ShadowPostTask;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.CallbackHelper;
import java.util.concurrent.RejectedExecutionException;
/**
* Tests EnterpriseInfo.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE, shadows = {ShadowPostTask.class})
public class EnterpriseInfoTest {
@Mock
public EnterpriseInfo.Natives mNatives;
@Before
public void setUp() {
EnterpriseInfo.reset();
// Skip the AsyncTask, we don't actually want to query the device, just enqueue callbacks.
EnterpriseInfo.getInstance().setSkipAsyncCheckForTesting(true);
MockitoAnnotations.initMocks(this);
EnterpriseInfoJni.TEST_HOOKS.setInstanceForTesting(mNatives);
}
@After
public void tearDown() {
EnterpriseInfoJni.TEST_HOOKS.setInstanceForTesting(null);
}
/**
......@@ -207,4 +228,30 @@ public class EnterpriseInfoTest {
Assert.assertNotEquals("Wrong value check failed.", trueTrue, falseTrue);
Assert.assertEquals("Correct value check failed.", trueTrue, trueTrue2);
}
@Test
@SmallTest
public void testGetManagedStateForNative() {
EnterpriseInfo.getManagedStateForNative();
Mockito.verifyZeroInteractions(mNatives);
EnterpriseInfo.getInstance().setCacheResult(new EnterpriseInfo.OwnedState(true, false));
EnterpriseInfo.getInstance().onEnterpriseInfoResultAvailable();
Mockito.verify(mNatives, Mockito.times(1)).updateNativeOwnedState(true, false);
}
@Test
@SmallTest
public void testGetManagedStateForNativeNullOwnedState() {
EnterpriseInfo.getInstance().setSkipAsyncCheckForTesting(false);
ShadowPostTask.setTestImpl(new ShadowPostTask.TestImpl() {
@Override
public void postDelayedTask(TaskTraits taskTraits, Runnable task, long delay) {
throw new RejectedExecutionException();
}
});
EnterpriseInfo.getManagedStateForNative();
Mockito.verify(mNatives, Mockito.times(1)).updateNativeOwnedState(false, false);
}
}
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