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