1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.pdfrendererbasic
18 
19 import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
20 import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
21 import androidx.test.espresso.Espresso.onView
22 import androidx.test.espresso.action.ViewActions.click
23 import androidx.test.espresso.matcher.ViewMatchers.withId
24 import androidx.test.rule.ActivityTestRule
25 import androidx.test.runner.AndroidJUnit4
26 import android.widget.Button
27 import org.junit.Assert.assertEquals
28 import org.junit.Assert.assertFalse
29 import org.junit.Assert.assertTrue
30 import org.junit.Before
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 /**
36  * Tests for PdfRendererBasic sample.
37  */
38 @RunWith(AndroidJUnit4::class)
39 class PdfRendererBasicFragmentTests {
40 
41     private lateinit var fragment: PdfRendererBasicFragment
42     private lateinit var btnPrevious: Button
43     private lateinit var btnNext: Button
44 
45     @Rule @JvmField
46     val activityTestRule = ActivityTestRule(MainActivity::class.java)
47 
beforenull48     @Before fun before() {
49         activityTestRule.activity.supportFragmentManager.beginTransaction()
50         fragment = activityTestRule.activity.supportFragmentManager
51                 .findFragmentByTag(FRAGMENT_PDF_RENDERER_BASIC) as PdfRendererBasicFragment
52     }
53 
testActivityTitlenull54     @Test fun testActivityTitle() {
55         // The title of the activity should be "PdfRendererBasic (1/10)" at first
56         val expectedActivityTitle = activityTestRule.activity.getString(
57                 R.string.app_name_with_index, 1, fragment.getPageCount())
58         assertEquals(expectedActivityTitle, activityTestRule.activity.title)
59     }
60 
testButtons_previousDisabledAtFirstnull61     @Test fun testButtons_previousDisabledAtFirst() {
62         setUpButtons()
63         // Check that the previous button is disabled at first
64         assertFalse(btnPrevious.isEnabled)
65         // The next button should be enabled
66         assertTrue(btnNext.isEnabled)
67     }
68 
testButtons_bothEnabledInMiddlenull69     @Test fun testButtons_bothEnabledInMiddle() {
70         setUpButtons()
71         turnPages(1)
72         // Two buttons should be both enabled
73         assertTrue(btnPrevious.isEnabled)
74         assertTrue(btnNext.isEnabled)
75     }
76 
testButtons_nextDisabledLastPagenull77     @Test fun testButtons_nextDisabledLastPage() {
78         setUpButtons()
79         val pageCount = fragment.getPageCount()
80         // Click till it reaches the last page
81         turnPages(pageCount - 1)
82         // Check the page count
83         val expectedActivityTitle = activityTestRule.activity.getString(
84                 R.string.app_name_with_index, pageCount, pageCount)
85         assertEquals(expectedActivityTitle, activityTestRule.activity.title)
86         // The previous button should be enabled
87         assertTrue(btnPrevious.isEnabled)
88         // Check that the next button is disabled
89         assertFalse(btnNext.isEnabled)
90     }
91 
testOrientationChangePreserveStatenull92     @Test fun testOrientationChangePreserveState() {
93         activityTestRule.activity.requestedOrientation = SCREEN_ORIENTATION_PORTRAIT
94         setUpButtons()
95         turnPages(1)
96         val pageCount = fragment.getPageCount()
97         val expectedActivityTitle = activityTestRule.activity
98                 .getString(R.string.app_name_with_index, 2, pageCount)
99         assertEquals(expectedActivityTitle, activityTestRule.activity.title)
100         activityTestRule.activity.requestedOrientation = SCREEN_ORIENTATION_LANDSCAPE
101         // Check that the title is the same after orientation change
102         assertEquals(expectedActivityTitle, activityTestRule.activity.title)
103     }
104 
105     /**
106      * Prepares references to the buttons "Previous" and "Next".
107      */
setUpButtonsnull108     private fun setUpButtons() {
109         val view = fragment.view ?: return
110         btnPrevious = view.findViewById(R.id.previous)
111         btnNext = view.findViewById(R.id.next)
112     }
113 
114     /**
115      * Click the "Next" button to turn the pages.
116      *
117      * @param count The number of times to turn pages.
118      */
turnPagesnull119     private fun turnPages(count: Int) {
120         for (i in 0 until count) {
121             onView(withId(R.id.next)).perform(click())
122         }
123     }
124 
125 }
126