<commit_before>package com.smartdevicelink.api.screen;

import android.test.AndroidTestCase;

import com.smartdevicelink.api.CompletionListener;
import com.smartdevicelink.api.FileManager;
import com.smartdevicelink.api.MultipleFileCompletionListener;
import com.smartdevicelink.api.datatypes.SdlArtwork;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.Image;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.SoftButton;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.ImageType;
import com.smartdevicelink.proxy.rpc.enums.SoftButtonType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.test.Validator;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

/**
 * This is a unit test class for the SmartDeviceLink library manager class :
 * {@link SoftButtonManager}
 */
public class SoftButtonManagerTests extends AndroidTestCase {

    private SoftButtonManager softButtonManager;
    private boolean fileManagerUploadArtworksGotCalled;
    private boolean internalInterfaceSendRPCRequestGotCalled;
    private boolean softButtonMangerUpdateCompleted;
    private SoftButtonObject softButtonObject1, softButtonObject2;
    private SoftButtonState softButtonState1, softButtonState2, softButtonState3, softButtonState4;


    @Override
    public void setUp() throws Exception {
        super.setUp();

        // When internalInterface.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, OnRPCNotificationListener) is called
        // inside SoftButtonManager, respond with a fake HMILevel.HMI_FULL response to let the SoftButtonManager continue working.
        ISdl internalInterface = mock(ISdl.class);
        Answer<Void> onHMIStatusAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                OnRPCNotificationListener onHMIStatusListener = (OnRPCNotificationListener) args[1];
                OnHMIStatus onHMIStatusFakeNotification = new OnHMIStatus();
                onHMIStatusFakeNotification.setHmiLevel(HMILevel.HMI_FULL);
                onHMIStatusListener.onNotified(onHMIStatusFakeNotification);
                return null;
            }
        };
        doAnswer(onHMIStatusAnswer).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));


        // When fileManager.uploadArtworks() is called inside the SoftButtonManager, respond with
        // a fake onComplete() callback to let the SoftButtonManager continue working.
        FileManager fileManager = mock(FileManager.class);
        Answer<Void> onFileManagerUploadAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                fileManagerUploadArtworksGotCalled = true;
                Object[] args = invocation.getArguments();
                MultipleFileCompletionListener multipleFileCompletionListener = (MultipleFileCompletionListener) args[1];
                multipleFileCompletionListener.onComplete(null);
                return null;
            }
        };
        doAnswer(onFileManagerUploadAnswer).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));


        // Create softButtonManager
        softButtonManager = new SoftButtonManager(internalInterface, fileManager);


        // When internalInterface.sendRPCRequest() is called inside SoftButtonManager:
        // 1) respond with a fake onResponse() callback to let the SoftButtonManager continue working
        // 2) assert that the Show RPC values (ie: MainField1 & SoftButtons) that are created by the SoftButtonManager, match the ones that are provided by the developer
        Answer<Void> onSendShowRPCAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                internalInterfaceSendRPCRequestGotCalled = true;
                Object[] args = invocation.getArguments();
                Show show = (Show) args[0];

                show.getOnRPCResponseListener().onResponse(0, null);

                assertEquals(show.getMainField1(), softButtonManager.getCurrentMainField1());
                assertTrue(Validator.validateSoftButtons(show.getSoftButtons(), softButtonManager.createSoftButtonsForCurrentState()));

                return null;
            }
        };
        doAnswer(onSendShowRPCAnswer).when(internalInterface).sendRPCRequest(any(Show.class));


        // Create soft button objects
        softButtonState1 = new SoftButtonState("object1-state1", "o1s1", new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true));
        softButtonState2 = new SoftButtonState("object1-state2", "o1s2", new SdlArtwork("image2", FileType.GRAPHIC_PNG, 2, true));
        softButtonObject1 = new SoftButtonObject("object1", Arrays.asList(softButtonState1, softButtonState2), softButtonState1.getName());
        softButtonState3 = new SoftButtonState("object2-state1", "o2s1", null);
        softButtonState4 = new SoftButtonState("object2-state2", "o2s2", new SdlArtwork("image3", FileType.GRAPHIC_PNG, 3, true));
        softButtonObject2 = new SoftButtonObject("object2", Arrays.asList(softButtonState3, softButtonState4), softButtonState3.getName());
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testSoftButtonManagerUpdate() {
        // Reset the boolean variables
        fileManagerUploadArtworksGotCalled = false;
        internalInterfaceSendRPCRequestGotCalled = false;
        softButtonMangerUpdateCompleted = false;


        // Test batch update
        softButtonManager.setBatchUpdates(true);
        List<SoftButtonObject> softButtonObjects = Arrays.asList(softButtonObject1, softButtonObject2);
        softButtonManager.setSoftButtonObjects(softButtonObjects);
        softButtonManager.setBatchUpdates(false);
        softButtonManager.update(new CompletionListener() {
            @Override
            public void onComplete(boolean success) {
                softButtonMangerUpdateCompleted = true;
            }
        });

<commit_msg>Fix a missing param issue in SoftButtonManager unit tests<commit_after>package com.smartdevicelink.api.screen;

import android.test.AndroidTestCase;

import com.smartdevicelink.api.CompletionListener;
import com.smartdevicelink.api.FileManager;
import com.smartdevicelink.api.MultipleFileCompletionListener;
import com.smartdevicelink.api.datatypes.SdlArtwork;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.proxy.interfaces.ISdl;
import com.smartdevicelink.proxy.rpc.Image;
import com.smartdevicelink.proxy.rpc.OnHMIStatus;
import com.smartdevicelink.proxy.rpc.Show;
import com.smartdevicelink.proxy.rpc.SoftButton;
import com.smartdevicelink.proxy.rpc.enums.FileType;
import com.smartdevicelink.proxy.rpc.enums.HMILevel;
import com.smartdevicelink.proxy.rpc.enums.ImageType;
import com.smartdevicelink.proxy.rpc.enums.SoftButtonType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.test.Validator;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

/**
 * This is a unit test class for the SmartDeviceLink library manager class :
 * {@link SoftButtonManager}
 */
public class SoftButtonManagerTests extends AndroidTestCase {

    private SoftButtonManager softButtonManager;
    private boolean fileManagerUploadArtworksGotCalled;
    private boolean internalInterfaceSendRPCRequestGotCalled;
    private boolean softButtonMangerUpdateCompleted;
    private SoftButtonObject softButtonObject1, softButtonObject2;
    private SoftButtonState softButtonState1, softButtonState2, softButtonState3, softButtonState4;


    @Override
    public void setUp() throws Exception {
        super.setUp();

        // When internalInterface.addOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, OnRPCNotificationListener) is called
        // inside SoftButtonManager, respond with a fake HMILevel.HMI_FULL response to let the SoftButtonManager continue working.
        ISdl internalInterface = mock(ISdl.class);
        Answer<Void> onHMIStatusAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                OnRPCNotificationListener onHMIStatusListener = (OnRPCNotificationListener) args[1];
                OnHMIStatus onHMIStatusFakeNotification = new OnHMIStatus();
                onHMIStatusFakeNotification.setHmiLevel(HMILevel.HMI_FULL);
                onHMIStatusListener.onNotified(onHMIStatusFakeNotification);
                return null;
            }
        };
        doAnswer(onHMIStatusAnswer).when(internalInterface).addOnRPCNotificationListener(eq(FunctionID.ON_HMI_STATUS), any(OnRPCNotificationListener.class));


        // When fileManager.uploadArtworks() is called inside the SoftButtonManager, respond with
        // a fake onComplete() callback to let the SoftButtonManager continue working.
        FileManager fileManager = mock(FileManager.class);
        Answer<Void> onFileManagerUploadAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                fileManagerUploadArtworksGotCalled = true;
                Object[] args = invocation.getArguments();
                MultipleFileCompletionListener multipleFileCompletionListener = (MultipleFileCompletionListener) args[1];
                multipleFileCompletionListener.onComplete(null);
                return null;
            }
        };
        doAnswer(onFileManagerUploadAnswer).when(fileManager).uploadArtworks(any(List.class), any(MultipleFileCompletionListener.class));


        // Create softButtonManager
        softButtonManager = new SoftButtonManager(internalInterface, fileManager);


        // When internalInterface.sendRPCRequest() is called inside SoftButtonManager:
        // 1) respond with a fake onResponse() callback to let the SoftButtonManager continue working
        // 2) assert that the Show RPC values (ie: MainField1 & SoftButtons) that are created by the SoftButtonManager, match the ones that are provided by the developer
        Answer<Void> onSendShowRPCAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) {
                internalInterfaceSendRPCRequestGotCalled = true;
                Object[] args = invocation.getArguments();
                Show show = (Show) args[0];

                show.getOnRPCResponseListener().onResponse(0, null);

                assertEquals(show.getMainField1(), softButtonManager.getCurrentMainField1());
                assertTrue(Validator.validateSoftButtons(show.getSoftButtons(), softButtonManager.createSoftButtonsForCurrentState()));

                return null;
            }
        };
        doAnswer(onSendShowRPCAnswer).when(internalInterface).sendRPCRequest(any(Show.class));


        // Create soft button objects
        softButtonState1 = new SoftButtonState("object1-state1", "o1s1", new SdlArtwork("image1", FileType.GRAPHIC_PNG, 1, true));
        softButtonState2 = new SoftButtonState("object1-state2", "o1s2", new SdlArtwork("image2", FileType.GRAPHIC_PNG, 2, true));
        softButtonObject1 = new SoftButtonObject("object1", Arrays.asList(softButtonState1, softButtonState2), softButtonState1.getName(), null);
        softButtonState3 = new SoftButtonState("object2-state1", "o2s1", null);
        softButtonState4 = new SoftButtonState("object2-state2", "o2s2", new SdlArtwork("image3", FileType.GRAPHIC_PNG, 3, true));
        softButtonObject2 = new SoftButtonObject("object2", Arrays.asList(softButtonState3, softButtonState4), softButtonState3.getName(), null);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testSoftButtonManagerUpdate() {
        // Reset the boolean variables
        fileManagerUploadArtworksGotCalled = false;
        internalInterfaceSendRPCRequestGotCalled = false;
        softButtonMangerUpdateCompleted = false;


        // Test batch update
        softButtonManager.setBatchUpdates(true);
        List<SoftButtonObject> softButtonObjects = Arrays.asList(softButtonObject1, softButtonObject2);
        softButtonManager.setSoftButtonObjects(softButtonObjects);
        softButtonManager.setBatchUpdates(false);
        softButtonManager.update(new CompletionListener() {
            @Override
            public void onComplete(boolean success) {
                softButtonMangerUpdateCompleted = true;
            }
        });


{'fraction_non_alphanumeric': 0.08124816014130115, 'fraction_numerical': 0.007801000883132176, 'mean_word_length': 3.690714532274767, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1378445', 'n_tokens_mistral': 3661, 'n_tokens_neox': 3577, 'n_words': 732}