DebugLogFileBackendTest.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2018-2023 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.logging.backend;
  22. import android.util.Log;
  23. import org.junit.Assert;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.concurrent.TimeUnit;
  31. import androidx.test.ext.junit.runners.AndroidJUnit4;
  32. import androidx.test.rule.GrantPermissionRule;
  33. import ch.threema.app.BuildConfig;
  34. import ch.threema.app.DangerousTest;
  35. import static ch.threema.app.PermissionRuleUtilsKt.getReadWriteExternalStoragePermissionRule;
  36. /**
  37. * Debug log file test
  38. */
  39. @RunWith(AndroidJUnit4.class)
  40. @DangerousTest // Deletes logfile
  41. public class DebugLogFileBackendTest {
  42. @Rule
  43. public GrantPermissionRule permissionRule = getReadWriteExternalStoragePermissionRule();
  44. @Before
  45. public void disableLogfile() {
  46. DebugLogFileBackend.setEnabled(false);
  47. }
  48. /**
  49. * Make sure that logging into the debug log file actually creates the debug log file.
  50. * Also test that the file is only created when enabled.
  51. */
  52. @Test
  53. public void testEnable() throws Exception {
  54. final File logFilePath = DebugLogFileBackend.getLogFilePath();
  55. // Log with the debug log file disabled
  56. final DebugLogFileBackend backend = new DebugLogFileBackend(Log.INFO);
  57. backend.print(Log.WARN, BuildConfig.LOG_TAG, null, "hi");
  58. // Enabling the debug log file won't create the log file just yet
  59. Assert.assertFalse(logFilePath.exists());
  60. DebugLogFileBackend.setEnabled(true);
  61. Assert.assertFalse(logFilePath.exists());
  62. // Logs below the min log level are filtered
  63. backend.printAsync(Log.DEBUG, BuildConfig.LOG_TAG, null, "hey").get(500, TimeUnit.MILLISECONDS);
  64. Assert.assertFalse(logFilePath.exists());
  65. // Log with the debug log file enabled
  66. backend.printAsync(Log.WARN, BuildConfig.LOG_TAG, null, "hi").get(500, TimeUnit.MILLISECONDS);
  67. Assert.assertTrue(logFilePath.exists());
  68. }
  69. /**
  70. * Make sure that enabling the debug log file actually creates the debug log file.
  71. */
  72. @Test
  73. public void testDisableRemovesFile() throws IOException {
  74. final File logFilePath = DebugLogFileBackend.getLogFilePath();
  75. Assert.assertFalse(logFilePath.exists());
  76. Assert.assertTrue("Could not create logfile", logFilePath.createNewFile());
  77. Assert.assertTrue(logFilePath.exists());
  78. DebugLogFileBackend.setEnabled(false);
  79. Assert.assertFalse(logFilePath.exists());
  80. }
  81. }