Home | History | Annotate | Download | only in tests
      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.documentcentricapps.tests;
     18 
     19 import com.example.android.documentcentricapps.DocumentCentricActivity;
     20 import com.example.android.documentcentricapps.R;
     21 
     22 import android.content.Intent;
     23 import android.test.ActivityUnitTestCase;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 import android.widget.Button;
     26 import android.widget.CheckBox;
     27 
     28 /**
     29  * Unit tests for DocumentCentricApps sample.
     30  */
     31 @MediumTest
     32 public class DocumentCentricAppsUnitTest extends ActivityUnitTestCase<DocumentCentricActivity> {
     33 
     34     private DocumentCentricActivity mDocumentCentricActivity;
     35 
     36     public DocumentCentricAppsUnitTest() {
     37         super(DocumentCentricActivity.class);
     38     }
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         super.setUp();
     43         final Intent launchIntent = new Intent(getInstrumentation()
     44                 .getTargetContext(), DocumentCentricActivity.class);
     45         mDocumentCentricActivity = startActivity(launchIntent, null, null);
     46     }
     47 
     48     public void testNewDocumentButton_IntentIsSentOnClick() {
     49         // Given a initialized Activity
     50         assertNotNull("mDocumentCentricActivity is null", mDocumentCentricActivity);
     51         final Button createNewDocumentButton = (Button) mDocumentCentricActivity
     52                 .findViewById(R.id.new_document_button);
     53         assertNotNull(createNewDocumentButton);
     54 
     55         // When "Create new Document" Button is clicked
     56         createNewDocumentButton.performClick();
     57 
     58         // Then NewDocumentActivity is started with the correct flags
     59         final Intent newDocumentIntent = getStartedActivityIntent();
     60         assertNotNull("newDocumentIntent is null", newDocumentIntent);
     61         assertEquals("intent is missing flag FLAG_ACTIVITY_NEW_DOCUMENT", Intent.FLAG_ACTIVITY_NEW_DOCUMENT,
     62                 newDocumentIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
     63     }
     64 
     65     public void testNewDocumentButton_FlagMultipleSetWhenCheckboxIsChecked() {
     66         // Given a initialized Activity and ticked "Create new task" checkbox
     67         assertNotNull("mDocumentCentricActivity is null", mDocumentCentricActivity);
     68         final Button createNewDocumentButton = (Button) mDocumentCentricActivity
     69                 .findViewById(R.id.new_document_button);
     70         assertNotNull(createNewDocumentButton);
     71         final CheckBox newTaskCheckbox = (CheckBox) mDocumentCentricActivity
     72                 .findViewById(R.id.multiple_task_checkbox);
     73         assertNotNull(newTaskCheckbox);
     74         newTaskCheckbox.setChecked(true);
     75 
     76         // When "Create new Document" Button is clicked
     77         createNewDocumentButton.performClick();
     78 
     79         // Then NewDocumentActivity is started with the new document and multiple task flags
     80         final Intent newDocumentIntent = getStartedActivityIntent();
     81         assertNotNull("newDocumentIntent is null", newDocumentIntent);
     82         assertEquals("intent is missing flag FLAG_ACTIVITY_NEW_DOCUMENT", Intent.FLAG_ACTIVITY_NEW_DOCUMENT,
     83                 newDocumentIntent.getFlags() & Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
     84         assertEquals("intent is missing flag FLAG_ACTIVITY_MULTIPLE_TASK", Intent.FLAG_ACTIVITY_MULTIPLE_TASK,
     85                 newDocumentIntent.getFlags() & Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
     86     }
     87 
     88 }