ErrorMessageAdapterUnitTest.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.utils;
  21. import android.content.res.Resources;
  22. import com.owncloud.android.R;
  23. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  24. import com.owncloud.android.operations.RemoveFileOperation;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.mockito.Mock;
  28. import org.mockito.runners.MockitoJUnitRunner;
  29. import static org.hamcrest.MatcherAssert.assertThat;
  30. import static org.hamcrest.CoreMatchers.is;
  31. import static org.mockito.Mockito.when;
  32. /**
  33. * Local unit test, to be run out of Android emulator or device.
  34. *
  35. * At the moment, it's a sample to validate the automatic test environment, in the scope of local unit tests with
  36. * mock Android dependencies.
  37. *
  38. * Don't take it as an example of completeness.
  39. *
  40. * See http://developer.android.com/intl/es/training/testing/unit-testing/local-unit-tests.html .
  41. */
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class ErrorMessageAdapterUnitTest {
  44. private final static String MOCK_FORBIDDEN_PERMISSIONS = "You do not have permission %s";
  45. private final static String MOCK_TO_DELETE = "to delete this file";
  46. private final static String PATH_TO_DELETE = "/path/to/a.file";
  47. private final static String EXPECTED_ERROR_MESSAGE = "You do not have permission to delete this file";
  48. @Mock
  49. Resources mMockResources;
  50. @Test
  51. public void getErrorCauseMessageForForbiddenRemoval() {
  52. // Given a mocked set of resources passed to the object under test...
  53. when(mMockResources.getString(R.string.forbidden_permissions))
  54. .thenReturn(MOCK_FORBIDDEN_PERMISSIONS);
  55. when(mMockResources.getString(R.string.forbidden_permissions_delete))
  56. .thenReturn(MOCK_TO_DELETE);
  57. // ... when method under test is called ...
  58. String errorMessage = ErrorMessageAdapter.getErrorCauseMessage(
  59. new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN),
  60. new RemoveFileOperation(PATH_TO_DELETE, false),
  61. mMockResources
  62. );
  63. // ... then the result should be the expected one.
  64. assertThat(errorMessage, is(EXPECTED_ERROR_MESSAGE));
  65. }
  66. }