1 /* 2 * Copyright (C) 2014 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.tests; 18 19 import android.content.pm.ActivityInfo; 20 import android.test.ActivityInstrumentationTestCase2; 21 import android.test.TouchUtils; 22 import android.view.View; 23 import android.widget.Button; 24 25 import androidx.test.filters.LargeTest; 26 27 import com.example.android.pdfrendererbasic.MainActivity; 28 import com.example.android.pdfrendererbasic.PdfRendererBasicFragment; 29 import com.example.android.pdfrendererbasic.R; 30 31 /** 32 * Tests for PdfRendererBasic sample. 33 */ 34 public class PdfRendererBasicFragmentTests extends ActivityInstrumentationTestCase2<MainActivity> { 35 36 private MainActivity mActivity; 37 private PdfRendererBasicFragment mFragment; 38 39 private Button mButtonPrevious; 40 private Button mButtonNext; 41 PdfRendererBasicFragmentTests()42 public PdfRendererBasicFragmentTests() { 43 super(MainActivity.class); 44 } 45 46 @Override setUp()47 protected void setUp() throws Exception { 48 super.setUp(); 49 mActivity = getActivity(); 50 mFragment = (PdfRendererBasicFragment) mActivity.getFragmentManager() 51 .findFragmentByTag(MainActivity.FRAGMENT_PDF_RENDERER_BASIC); 52 } 53 54 @LargeTest testActivityTitle()55 public void testActivityTitle() { 56 // The title of the activity should be "PdfRendererBasic (1/10)" at first 57 String expectedActivityTitle = mActivity.getString(R.string.app_name_with_index, 1, 58 mFragment.getPageCount()); 59 assertEquals(expectedActivityTitle, mActivity.getTitle()); 60 } 61 62 @LargeTest testButtons_previousDisabledAtFirst()63 public void testButtons_previousDisabledAtFirst() { 64 setUpButtons(); 65 // Check that the previous button is disabled at first 66 assertFalse(mButtonPrevious.isEnabled()); 67 // The next button should be enabled 68 assertTrue(mButtonNext.isEnabled()); 69 } 70 71 @LargeTest testButtons_bothEnabledInMiddle()72 public void testButtons_bothEnabledInMiddle() { 73 setUpButtons(); 74 turnPages(1); 75 // Two buttons should be both enabled 76 assertTrue(mButtonPrevious.isEnabled()); 77 assertTrue(mButtonNext.isEnabled()); 78 } 79 80 @LargeTest testButtons_nextDisabledLastPage()81 public void testButtons_nextDisabledLastPage() { 82 setUpButtons(); 83 int pageCount = mFragment.getPageCount(); 84 // Click till it reaches the last page 85 turnPages(pageCount - 1); 86 // Check the page count 87 String expectedActivityTitle = mActivity.getString(R.string.app_name_with_index, 88 pageCount, pageCount); 89 assertEquals(expectedActivityTitle, mActivity.getTitle()); 90 // The previous button should be enabled 91 assertTrue(mButtonPrevious.isEnabled()); 92 // Check that the next button is disabled 93 assertFalse(mButtonNext.isEnabled()); 94 } 95 96 @LargeTest testOrientationChangePreserveState()97 public void testOrientationChangePreserveState() { 98 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 99 setUpButtons(); 100 turnPages(1); 101 int pageCount = mFragment.getPageCount(); 102 String expectedActivityTitle = mActivity.getString(R.string.app_name_with_index, 103 2, pageCount); 104 assertEquals(expectedActivityTitle, mActivity.getTitle()); 105 mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 106 // Check that the title is the same after orientation change 107 assertEquals(expectedActivityTitle, mActivity.getTitle()); 108 } 109 110 /** 111 * Prepares references to the buttons "Previous" and "Next". 112 */ setUpButtons()113 private void setUpButtons() { 114 View view = mFragment.getView(); 115 assertNotNull(view); 116 mButtonPrevious = (Button) view.findViewById(R.id.previous); 117 assertNotNull(mButtonPrevious); 118 mButtonNext = (Button) view.findViewById(R.id.next); 119 assertNotNull(mButtonNext); 120 } 121 122 /** 123 * Click the "Next" button to turn the pages. 124 * 125 * @param count The number of times to turn pages. 126 */ turnPages(int count)127 private void turnPages(int count) { 128 for (int i = 0; i < count; ++i) { 129 TouchUtils.clickView(this, mButtonNext); 130 } 131 } 132 133 } 134