Merge "Add a new View class for the loading screen." into androidx-main
diff --git a/pdf/pdf-viewer/src/main/java/androidx/pdf/viewer/LoadingView.kt b/pdf/pdf-viewer/src/main/java/androidx/pdf/viewer/LoadingView.kt
new file mode 100644
index 0000000..15cd00d
--- /dev/null
+++ b/pdf/pdf-viewer/src/main/java/androidx/pdf/viewer/LoadingView.kt
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package androidx.pdf.viewer
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.LayoutInflater
+import android.widget.LinearLayout
+import android.widget.ProgressBar
+import android.widget.TextView
+import androidx.annotation.RestrictTo
+import androidx.pdf.R
+
+/**
+ * A view which displays the loading spinner when the pdf is loading and displays an error messages
+ * if there is a failure when rendering.
+ */
+// TODO(b/344386251): Update the error message and the text view once the mocks are finalised.
+@RestrictTo(RestrictTo.Scope.LIBRARY)
+class LoadingView : LinearLayout {
+    private var progressBar: ProgressBar
+    private var errorMessage: TextView
+
+    constructor(context: Context) : this(context, null)
+
+    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
+
+    constructor(
+        context: Context,
+        attrs: AttributeSet?,
+        defStyleAttr: Int = 0
+    ) : super(context, attrs, defStyleAttr) {
+
+        // Inflate the layout
+        LayoutInflater.from(context).inflate(R.layout.loading_view, this, true)
+
+        // Find views
+        progressBar = findViewById(R.id.loadingProgressBar)
+        errorMessage = findViewById(R.id.errorTextView)
+    }
+
+    fun showLoadingView() {
+        progressBar.visibility = VISIBLE
+        errorMessage.visibility = GONE
+    }
+
+    fun showErrorView(message: String) {
+        progressBar.visibility = GONE
+        errorMessage.text = message
+        errorMessage.visibility = VISIBLE
+    }
+}
diff --git a/pdf/pdf-viewer/src/main/res/layout/loading_view.xml b/pdf/pdf-viewer/src/main/res/layout/loading_view.xml
new file mode 100644
index 0000000..35c28a2
--- /dev/null
+++ b/pdf/pdf-viewer/src/main/res/layout/loading_view.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  Copyright 2024 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:gravity="center"
+    android:orientation="vertical"
+    android:padding="16dp">
+
+    <ProgressBar
+        android:id="@+id/loadingProgressBar"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:indeterminate="true"/>
+
+    <TextView
+        android:id="@+id/errorTextView"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:textColor="@android:color/holo_red_dark"
+        android:visibility="gone"/>
+</LinearLayout>
\ No newline at end of file
diff --git a/pdf/pdf-viewer/src/test/java/androidx/pdf/viewer/LoadingViewTest.kt b/pdf/pdf-viewer/src/test/java/androidx/pdf/viewer/LoadingViewTest.kt
new file mode 100644
index 0000000..8a3fc66
--- /dev/null
+++ b/pdf/pdf-viewer/src/test/java/androidx/pdf/viewer/LoadingViewTest.kt
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package androidx.pdf.viewer
+
+import android.content.Context
+import android.view.View
+import android.widget.ProgressBar
+import android.widget.TextView
+import androidx.pdf.R
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.filters.SmallTest
+import com.google.common.truth.Truth
+import org.junit.Assert
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+
+/** Tests for [LoadingView]. */
+@SmallTest
+@RunWith(RobolectricTestRunner::class)
+class LoadingViewTest {
+    private lateinit var loadingView: LoadingView
+
+    @Before
+    fun setUp() {
+        val context = ApplicationProvider.getApplicationContext<Context>()
+        loadingView = LoadingView(context)
+    }
+
+    @Test
+    fun testLoadingView_isNotNull() {
+        Assert.assertNotNull(loadingView)
+    }
+
+    @Test
+    fun showLoadingView_showsLoadingSpinner() {
+        val progressBar = loadingView.findViewById<ProgressBar>(R.id.loadingProgressBar)
+        val errorMessage = loadingView.findViewById<TextView>(R.id.errorTextView)
+
+        loadingView.showLoadingView()
+
+        Assert.assertEquals(View.VISIBLE.toLong(), progressBar.visibility.toLong())
+        Assert.assertEquals(View.GONE.toLong(), errorMessage.visibility.toLong())
+    }
+
+    @Test
+    fun showErrorView_showsErrorMessage() {
+        val dummyErrorMessage = "testing error"
+        val progressBar = loadingView.findViewById<ProgressBar>(R.id.loadingProgressBar)
+        val errorMessage = loadingView.findViewById<TextView>(R.id.errorTextView)
+
+        loadingView.showErrorView(dummyErrorMessage)
+
+        Assert.assertEquals(View.GONE.toLong(), progressBar.visibility.toLong())
+        Assert.assertEquals(View.VISIBLE.toLong(), errorMessage.visibility.toLong())
+        Truth.assertThat(errorMessage.text).isEqualTo(dummyErrorMessage)
+    }
+}