DebugLogFileBackendTest.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2018-2022 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.Manifest;
  23. import android.util.Log;
  24. import org.junit.Assert;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.concurrent.TimeUnit;
  32. import androidx.test.ext.junit.runners.AndroidJUnit4;
  33. import androidx.test.rule.GrantPermissionRule;
  34. import ch.threema.app.DangerousTest;
  35. /**
  36. * Debug log file test
  37. */
  38. @RunWith(AndroidJUnit4.class)
  39. @DangerousTest // Deletes logfile
  40. public class DebugLogFileBackendTest {
  41. @Rule
  42. public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  43. @Before
  44. public void disableLogfile() {
  45. DebugLogFileBackend.setEnabled(false);
  46. }
  47. /**
  48. * Make sure that logging into the debug log file actually creates the debug log file.
  49. * Also test that the file is only created when enabled.
  50. */
  51. @Test
  52. public void testEnable() throws Exception {
  53. final File logFilePath = DebugLogFileBackend.getLogFilePath();
  54. // Log with the debug log file disabled
  55. final DebugLogFileBackend backend = new DebugLogFileBackend(Log.INFO);
  56. backend.print(Log.WARN, "3ma", null, "hi");
  57. // Enabling the debug log file won't create the log file just yet
  58. Assert.assertFalse(logFilePath.exists());
  59. DebugLogFileBackend.setEnabled(true);
  60. Assert.assertFalse(logFilePath.exists());
  61. // Logs below the min log level are filtered
  62. backend.printAsync(Log.DEBUG, "3ma", null, "hey").get(500, TimeUnit.MILLISECONDS);
  63. Assert.assertFalse(logFilePath.exists());
  64. // Log with the debug log file enabled
  65. backend.printAsync(Log.WARN, "3ma", null, "hi").get(500, TimeUnit.MILLISECONDS);
  66. Assert.assertTrue(logFilePath.exists());
  67. }
  68. /**
  69. * Make sure that enabling the debug log file actually creates the debug log file.
  70. */
  71. @Test
  72. public void testDisableRemovesFile() throws IOException {
  73. final File logFilePath = DebugLogFileBackend.getLogFilePath();
  74. Assert.assertFalse(logFilePath.exists());
  75. Assert.assertTrue("Could not create logfile", logFilePath.createNewFile());
  76. Assert.assertTrue(logFilePath.exists());
  77. DebugLogFileBackend.setEnabled(false);
  78. Assert.assertFalse(logFilePath.exists());
  79. }
  80. }