|
| 1 | +package org.wordpress.gutenberg |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import android.os.Looper |
| 6 | +import android.webkit.ValueCallback |
| 7 | +import android.webkit.WebChromeClient |
| 8 | +import android.webkit.WebView |
| 9 | +import org.junit.Before |
| 10 | +import org.junit.Test |
| 11 | +import org.junit.runner.RunWith |
| 12 | +import org.mockito.Mock |
| 13 | +import org.mockito.Mockito.`when` |
| 14 | +import org.mockito.MockitoAnnotations |
| 15 | +import org.robolectric.RobolectricTestRunner |
| 16 | +import org.robolectric.RuntimeEnvironment |
| 17 | +import org.robolectric.Shadows.shadowOf |
| 18 | +import org.robolectric.annotation.Config |
| 19 | +import org.junit.Assert.assertEquals |
| 20 | +import org.junit.Assert.assertTrue |
| 21 | +import java.util.concurrent.CountDownLatch |
| 22 | +import java.util.concurrent.TimeUnit |
| 23 | + |
| 24 | +@RunWith(RobolectricTestRunner::class) |
| 25 | +@Config(sdk = [28], manifest = Config.NONE) |
| 26 | +class GutenbergViewTest { |
| 27 | + @Mock |
| 28 | + private lateinit var mockWebView: WebView |
| 29 | + |
| 30 | + @Mock |
| 31 | + private lateinit var mockFilePathCallback: ValueCallback<Array<Uri?>?> |
| 32 | + |
| 33 | + @Mock |
| 34 | + private lateinit var mockFileChooserParams: WebChromeClient.FileChooserParams |
| 35 | + |
| 36 | + private lateinit var gutenbergView: GutenbergView |
| 37 | + |
| 38 | + @Before |
| 39 | + fun setup() { |
| 40 | + MockitoAnnotations.openMocks(this) |
| 41 | + gutenbergView = GutenbergView(RuntimeEnvironment.getApplication()) |
| 42 | + gutenbergView.initializeWebView() |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `onShowFileChooser sets up file chooser with single file selection`() { |
| 47 | + // Given |
| 48 | + val latch = CountDownLatch(1) |
| 49 | + var capturedIntent: Intent? = null |
| 50 | + |
| 51 | + gutenbergView.setOnFileChooserRequestedListener { intent, _ -> |
| 52 | + capturedIntent = intent |
| 53 | + latch.countDown() |
| 54 | + } |
| 55 | + |
| 56 | + // When |
| 57 | + gutenbergView.webChromeClient?.onShowFileChooser( |
| 58 | + mockWebView, |
| 59 | + mockFilePathCallback, |
| 60 | + mockFileChooserParams |
| 61 | + ) |
| 62 | + |
| 63 | + // Process any pending runnables |
| 64 | + shadowOf(Looper.getMainLooper()).runToEndOfTasks() |
| 65 | + |
| 66 | + // Wait for the callback to be executed |
| 67 | + latch.await(1, TimeUnit.SECONDS) |
| 68 | + |
| 69 | + // Then |
| 70 | + assertTrue("Intent should not be null", capturedIntent != null) |
| 71 | + assertTrue("Intent should be a chooser", capturedIntent?.action == Intent.ACTION_CHOOSER) |
| 72 | + |
| 73 | + // Get the original intent from the chooser |
| 74 | + val originalIntent = capturedIntent?.getParcelableExtra<Intent>(Intent.EXTRA_INTENT) |
| 75 | + assertTrue("Original intent should not be null", originalIntent != null) |
| 76 | + assertTrue("Original intent action should be ACTION_GET_CONTENT", |
| 77 | + originalIntent?.action == Intent.ACTION_GET_CONTENT) |
| 78 | + assertTrue("Original intent should have CATEGORY_OPENABLE", |
| 79 | + originalIntent?.hasCategory(Intent.CATEGORY_OPENABLE) == true) |
| 80 | + assertEquals("Pick image request code should be 1", |
| 81 | + 1, gutenbergView.pickImageRequestCode) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `onShowFileChooser sets up file chooser with multiple file selection`() { |
| 86 | + // Given |
| 87 | + val latch = CountDownLatch(1) |
| 88 | + var capturedIntent: Intent? = null |
| 89 | + |
| 90 | + gutenbergView.setOnFileChooserRequestedListener { intent, _ -> |
| 91 | + capturedIntent = intent |
| 92 | + latch.countDown() |
| 93 | + } |
| 94 | + |
| 95 | + // When |
| 96 | + `when`(mockFileChooserParams.mode).thenReturn(WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE) |
| 97 | + gutenbergView.webChromeClient?.onShowFileChooser( |
| 98 | + mockWebView, |
| 99 | + mockFilePathCallback, |
| 100 | + mockFileChooserParams |
| 101 | + ) |
| 102 | + |
| 103 | + // Process any pending runnables |
| 104 | + shadowOf(Looper.getMainLooper()).runToEndOfTasks() |
| 105 | + |
| 106 | + // Wait for the callback to be executed |
| 107 | + latch.await(1, TimeUnit.SECONDS) |
| 108 | + |
| 109 | + // Then |
| 110 | + assertTrue("Intent should not be null", capturedIntent != null) |
| 111 | + assertTrue("Intent should be a chooser", capturedIntent?.action == Intent.ACTION_CHOOSER) |
| 112 | + |
| 113 | + // Get the original intent from the chooser |
| 114 | + val originalIntent = capturedIntent?.getParcelableExtra<Intent>(Intent.EXTRA_INTENT) |
| 115 | + assertTrue("Original intent should not be null", originalIntent != null) |
| 116 | + assertTrue("Original intent should allow multiple selection", |
| 117 | + originalIntent?.getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false) == true) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `onShowFileChooser stores file path callback`() { |
| 122 | + // When |
| 123 | + gutenbergView.webChromeClient?.onShowFileChooser( |
| 124 | + mockWebView, |
| 125 | + mockFilePathCallback, |
| 126 | + mockFileChooserParams |
| 127 | + ) |
| 128 | + |
| 129 | + // Then |
| 130 | + assertEquals("File path callback should be stored", |
| 131 | + mockFilePathCallback, gutenbergView.filePathCallback) |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `resetFilePathCallback clears the callback`() { |
| 136 | + // Given |
| 137 | + gutenbergView.webChromeClient?.onShowFileChooser( |
| 138 | + mockWebView, |
| 139 | + mockFilePathCallback, |
| 140 | + mockFileChooserParams |
| 141 | + ) |
| 142 | + |
| 143 | + // When |
| 144 | + gutenbergView.resetFilePathCallback() |
| 145 | + |
| 146 | + // Then |
| 147 | + assertEquals("File path callback should be null after reset", |
| 148 | + null, gutenbergView.filePathCallback) |
| 149 | + } |
| 150 | +} |
0 commit comments