Search in sources :

Example 6 with Mock

use of org.mockito.Mock in project android_packages_apps_Settings by LineageOS.

the class AdvancedPowerUsageDetailTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mContext = spy(RuntimeEnvironment.application);
    FakeFeatureFactory.setupForTest(mContext);
    mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
    mFragment = spy(new AdvancedPowerUsageDetail());
    doReturn(mContext).when(mFragment).getContext();
    doReturn(mActivity).when(mFragment).getActivity();
    doReturn(SUMMARY).when(mFragment).getString(anyInt());
    doReturn(APP_LABEL).when(mBundle).getString(nullable(String.class));
    doReturn(mBundle).when(mFragment).getArguments();
    doReturn(mLoaderManager).when(mFragment).getLoaderManager();
    ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setRecyclerView(nullable(RecyclerView.class), nullable(Lifecycle.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setButtonActions(anyInt(), anyInt());
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setIcon(nullable(Drawable.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setIcon(nullable(ApplicationsState.AppEntry.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setLabel(nullable(String.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setLabel(nullable(String.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setLabel(nullable(ApplicationsState.AppEntry.class));
    doReturn(mEntityHeaderController).when(mEntityHeaderController).setSummary(nullable(String.class));
    doReturn(UID).when(mBatterySipper).getUid();
    doReturn(APP_LABEL).when(mBatteryEntry).getLabel();
    doReturn(BACKGROUND_TIME_US).when(mUid).getProcessStateTime(eq(BatteryStats.Uid.PROCESS_STATE_BACKGROUND), anyLong(), anyInt());
    doReturn(PROCSTATE_TOP_TIME_US).when(mUid).getProcessStateTime(eq(BatteryStats.Uid.PROCESS_STATE_TOP), anyLong(), anyInt());
    doReturn(mForegroundActivityTimer).when(mUid).getForegroundActivityTimer();
    doReturn(FOREGROUND_ACTIVITY_TIME_US).when(mForegroundActivityTimer).getTotalTimeLocked(anyLong(), anyInt());
    ReflectionHelpers.setField(mBatteryEntry, "sipper", mBatterySipper);
    mBatteryEntry.iconId = ICON_ID;
    mBatterySipper.uidObj = mUid;
    mBatterySipper.drainType = BatterySipper.DrainType.APP;
    mFragment.mHeaderPreference = mHeaderPreference;
    mFragment.mState = mState;
    mFragment.mBatteryUtils = new BatteryUtils(RuntimeEnvironment.application);
    mAppEntry.info = mock(ApplicationInfo.class);
    mTestActivity = spy(new SettingsActivity());
    doReturn(mPackageManager).when(mTestActivity).getPackageManager();
    doReturn(mAppOpsManager).when(mTestActivity).getSystemService(Context.APP_OPS_SERVICE);
    mBatteryUtils = spy(BatteryUtils.getInstance(mTestActivity));
    doReturn(FOREGROUND_SERVICE_TIME_US).when(mBatteryUtils).getForegroundServiceTotalTimeUs(any(BatteryStats.Uid.class), anyLong());
    final ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class);
    Answer<Void> callable = invocation -> {
        mBundle = captor.getValue();
        return null;
    };
    doAnswer(callable).when(mTestActivity).startPreferencePanelAsUser(nullable(Fragment.class), nullable(String.class), captor.capture(), anyInt(), nullable(CharSequence.class), nullable(UserHandle.class));
    mForegroundPreference = new Preference(mContext);
    mBackgroundPreference = new Preference(mContext);
    mPowerUsagePreference = new Preference(mContext);
    mFragment.mForegroundPreference = mForegroundPreference;
    mFragment.mBackgroundPreference = mBackgroundPreference;
    mFragment.mPowerUsagePreference = mPowerUsagePreference;
    mFragment.mAnomalySummaryPreferenceController = mAnomalySummaryPreferenceController;
    mAnomalies = new ArrayList<>();
    mAnomalies.add(new Anomaly.Builder().setUid(UID).setType(Anomaly.AnomalyType.WAKE_LOCK).build());
}
Also used : AppOpsManager(android.app.AppOpsManager) Bundle(android.os.Bundle) SettingsRobolectricTestRunner(com.android.settings.testutils.SettingsRobolectricTestRunner) PackageManager(android.content.pm.PackageManager) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Drawable(android.graphics.drawable.Drawable) MockitoAnnotations(org.mockito.MockitoAnnotations) ReflectionHelpers(org.robolectric.util.ReflectionHelpers) AppUtils(com.android.settingslib.applications.AppUtils) Matchers.eq(org.mockito.Matchers.eq) Mockito.doAnswer(org.mockito.Mockito.doAnswer) After(org.junit.After) Matchers.anyInt(org.mockito.Matchers.anyInt) Mockito.doReturn(org.mockito.Mockito.doReturn) R(com.android.settings.R) Anomaly(com.android.settings.fuelgauge.anomaly.Anomaly) BatteryStatsHelper(com.android.internal.os.BatteryStatsHelper) RuntimeEnvironment(org.robolectric.RuntimeEnvironment) Matchers.any(org.mockito.Matchers.any) List(java.util.List) SettingsActivity(com.android.settings.SettingsActivity) EntityHeaderController(com.android.settings.widget.EntityHeaderController) ApplicationInfo(android.content.pm.ApplicationInfo) ShadowEntityHeaderController(com.android.settings.testutils.shadow.ShadowEntityHeaderController) Mockito.mock(org.mockito.Mockito.mock) Context(android.content.Context) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) TestConfig(com.android.settings.TestConfig) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) UserHandle(android.os.UserHandle) Matchers.anyLong(org.mockito.Matchers.anyLong) LayoutPreference(com.android.settings.applications.LayoutPreference) LoaderManager(android.app.LoaderManager) Before(org.junit.Before) Fragment(android.app.Fragment) Answers(org.mockito.Answers) AnomalySummaryPreferenceController(com.android.settings.fuelgauge.anomaly.AnomalySummaryPreferenceController) Test(org.junit.Test) ApplicationsState(com.android.settingslib.applications.ApplicationsState) Truth.assertThat(com.google.common.truth.Truth.assertThat) InstantAppDataProvider(com.android.settingslib.applications.instantapps.InstantAppDataProvider) Mockito.verify(org.mockito.Mockito.verify) Preference(android.support.v7.preference.Preference) RecyclerView(android.support.v7.widget.RecyclerView) FakeFeatureFactory(com.android.settings.testutils.FakeFeatureFactory) BatterySipper(com.android.internal.os.BatterySipper) BatteryStats(android.os.BatteryStats) Activity(android.app.Activity) Lifecycle(com.android.settingslib.core.lifecycle.Lifecycle) Bundle(android.os.Bundle) Lifecycle(com.android.settingslib.core.lifecycle.Lifecycle) Drawable(android.graphics.drawable.Drawable) ApplicationInfo(android.content.pm.ApplicationInfo) Fragment(android.app.Fragment) LayoutPreference(com.android.settings.applications.LayoutPreference) Preference(android.support.v7.preference.Preference) UserHandle(android.os.UserHandle) RecyclerView(android.support.v7.widget.RecyclerView) SettingsActivity(com.android.settings.SettingsActivity) Before(org.junit.Before)

Example 7 with Mock

use of org.mockito.Mock in project kie-wb-common by kiegroup.

the class GeneralSettingsPresenterTest method testExecuteValidationFailing.

@Test
public void testExecuteValidationFailing() {
    final POM pom = mock(POM.class);
    doReturn(false).when(validationService).validate(eq(pom));
    generalSettingsPresenter.executeValidation(s -> s.validate(pom), "Test message").then(valid -> {
        Assert.fail("Promise should've been resolved!");
        return promises.resolve();
    }).catch_(e -> {
        verify(validationService).validate(eq(pom));
        Assert.assertEquals(e, "Test message");
        return promises.resolve();
    });
}
Also used : GAVPreferences(org.guvnor.common.services.project.preferences.GAVPreferences) CallerMock(org.uberfire.mocks.CallerMock) Mock(org.mockito.Mock) Promises(org.uberfire.client.promise.Promises) GAV(org.guvnor.common.services.project.model.GAV) SettingsPresenter(org.kie.workbench.common.screens.library.client.settings.SettingsPresenter) RunWith(org.junit.runner.RunWith) PreferenceScopeResolutionStrategyInfo(org.uberfire.preferences.shared.impl.PreferenceScopeResolutionStrategyInfo) ProjectScopedResolutionStrategySupplier(org.guvnor.common.services.project.client.preferences.ProjectScopedResolutionStrategySupplier) Mockito.spy(org.mockito.Mockito.spy) SettingsSectionChange(org.kie.workbench.common.screens.library.client.settings.SettingsSectionChange) POM(org.guvnor.common.services.project.model.POM) Matchers.eq(org.mockito.Matchers.eq) SyncPromises(org.uberfire.promise.SyncPromises) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ValidationService(org.kie.workbench.common.services.shared.validation.ValidationService) ProjectScreenModel(org.kie.workbench.common.screens.projecteditor.model.ProjectScreenModel) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Mockito.never(org.mockito.Mockito.never) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) EventSourceMock(org.uberfire.mocks.EventSourceMock) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) POM(org.guvnor.common.services.project.model.POM) Test(org.junit.Test)

Example 8 with Mock

use of org.mockito.Mock in project kie-wb-common by kiegroup.

the class GeneralSettingsPresenterTest method testExecuteValidation.

@Test
public void testExecuteValidation() {
    final POM pom = mock(POM.class);
    doReturn(true).when(validationService).validate(eq(pom));
    generalSettingsPresenter.executeValidation(s -> s.validate(pom), "Test message").then(valid -> {
        verify(validationService).validate(eq(pom));
        Assert.assertEquals(valid, true);
        return promises.resolve();
    }).catch_(e -> {
        Assert.fail("Promise should've been resolved!");
        return promises.resolve();
    });
}
Also used : GAVPreferences(org.guvnor.common.services.project.preferences.GAVPreferences) CallerMock(org.uberfire.mocks.CallerMock) Mock(org.mockito.Mock) Promises(org.uberfire.client.promise.Promises) GAV(org.guvnor.common.services.project.model.GAV) SettingsPresenter(org.kie.workbench.common.screens.library.client.settings.SettingsPresenter) RunWith(org.junit.runner.RunWith) PreferenceScopeResolutionStrategyInfo(org.uberfire.preferences.shared.impl.PreferenceScopeResolutionStrategyInfo) ProjectScopedResolutionStrategySupplier(org.guvnor.common.services.project.client.preferences.ProjectScopedResolutionStrategySupplier) Mockito.spy(org.mockito.Mockito.spy) SettingsSectionChange(org.kie.workbench.common.screens.library.client.settings.SettingsSectionChange) POM(org.guvnor.common.services.project.model.POM) Matchers.eq(org.mockito.Matchers.eq) SyncPromises(org.uberfire.promise.SyncPromises) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ValidationService(org.kie.workbench.common.services.shared.validation.ValidationService) ProjectScreenModel(org.kie.workbench.common.screens.projecteditor.model.ProjectScreenModel) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Mockito.never(org.mockito.Mockito.never) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) EventSourceMock(org.uberfire.mocks.EventSourceMock) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) POM(org.guvnor.common.services.project.model.POM) Test(org.junit.Test)

Example 9 with Mock

use of org.mockito.Mock in project kie-wb-common by kiegroup.

the class DeploymentPopupBuilderTest method testBuildDeployWithMultipleServerTemplates.

@Test
public void testBuildDeployWithMultipleServerTemplates() throws Exception {
    final List<ServerTemplate> serverTemplates = new ArrayList<>();
    final ValidateExistingContainerCallback callback = mock(ValidateExistingContainerCallback.class);
    final ParameterizedCommand<DeploymentScreenPopupViewImpl> onSuccess = (v) -> {
    };
    doReturn(callback).when(builder).multipleServerTemplatesValidation(serverTemplates);
    builder.buildDeployWithMultipleServerTemplates(serverTemplates, onSuccess);
    verify(builder).setViewFields(onSuccess, callback);
}
Also used : DeploymentScreenPopupViewImpl(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl) GwtMockitoTestRunner(com.google.gwtmockito.GwtMockitoTestRunner) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) BeforeClass(org.junit.BeforeClass) ServerTemplate(org.kie.server.controller.api.model.spec.ServerTemplate) Mock(org.mockito.Mock) ValidateExistingContainerCallback(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl.ValidateExistingContainerCallback) RunWith(org.junit.runner.RunWith) Set(java.util.Set) Test(org.junit.Test) ContainerSpec(org.kie.server.controller.api.model.spec.ContainerSpec) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito(org.mockito.Mockito) List(java.util.List) GWTMockUtilities(com.google.gwt.junit.GWTMockUtilities) DeploymentScreenPopupViewImpl(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl) Assert(org.junit.Assert) Before(org.junit.Before) ServerTemplate(org.kie.server.controller.api.model.spec.ServerTemplate) ArrayList(java.util.ArrayList) ValidateExistingContainerCallback(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl.ValidateExistingContainerCallback) Test(org.junit.Test)

Example 10 with Mock

use of org.mockito.Mock in project kie-wb-common by kiegroup.

the class DeploymentPopupBuilderTest method testBuildDeployWithOneServerTemplate.

@Test
public void testBuildDeployWithOneServerTemplate() throws Exception {
    final ServerTemplate serverTemplate = mock(ServerTemplate.class);
    final ValidateExistingContainerCallback callback = mock(ValidateExistingContainerCallback.class);
    final ParameterizedCommand<DeploymentScreenPopupViewImpl> onSuccess = (v) -> {
    };
    doReturn(callback).when(builder).singleServerTemplatesValidation(serverTemplate);
    builder.buildDeployWithOneServerTemplate(serverTemplate, onSuccess);
    verify(builder).setViewFields(onSuccess, callback);
}
Also used : DeploymentScreenPopupViewImpl(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl) GwtMockitoTestRunner(com.google.gwtmockito.GwtMockitoTestRunner) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) BeforeClass(org.junit.BeforeClass) ServerTemplate(org.kie.server.controller.api.model.spec.ServerTemplate) Mock(org.mockito.Mock) ValidateExistingContainerCallback(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl.ValidateExistingContainerCallback) RunWith(org.junit.runner.RunWith) Set(java.util.Set) Test(org.junit.Test) ContainerSpec(org.kie.server.controller.api.model.spec.ContainerSpec) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mockito(org.mockito.Mockito) List(java.util.List) GWTMockUtilities(com.google.gwt.junit.GWTMockUtilities) DeploymentScreenPopupViewImpl(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl) Assert(org.junit.Assert) Before(org.junit.Before) ServerTemplate(org.kie.server.controller.api.model.spec.ServerTemplate) ValidateExistingContainerCallback(org.kie.workbench.common.screens.projecteditor.client.editor.DeploymentScreenPopupViewImpl.ValidateExistingContainerCallback) Test(org.junit.Test)

Aggregations

Mock (org.mockito.Mock)128 Mockito.mock (org.mockito.Mockito.mock)103 Test (org.junit.Test)98 Before (org.junit.Before)90 Mockito.verify (org.mockito.Mockito.verify)84 List (java.util.List)80 Mockito.when (org.mockito.Mockito.when)76 RunWith (org.junit.runner.RunWith)72 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)60 Collections (java.util.Collections)55 ArrayList (java.util.ArrayList)54 ArgumentCaptor (org.mockito.ArgumentCaptor)51 Mockito.spy (org.mockito.Mockito.spy)45 Arrays (java.util.Arrays)42 Assert.assertEquals (org.junit.Assert.assertEquals)40 Mockito.doReturn (org.mockito.Mockito.doReturn)40 Map (java.util.Map)38 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)37 Mockito.times (org.mockito.Mockito.times)36