/* * Copyright (c) 2013, Will Szumski * Copyright (c) 2013, Doug Szumski * * This file is part of Cyclismo. * * Cyclismo is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cyclismo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Cyclismo. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright 2012 Google Inc. * * 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 org.cowboycoders.cyclismo.endtoendtest; import android.app.Instrumentation; import android.content.Intent; import android.test.ActivityInstrumentationTestCase2; import android.view.KeyEvent; import android.view.View; import org.cowboycoders.cyclismo.R; import org.cowboycoders.cyclismo.TrackListActivity; import java.util.ArrayList; /** * Tests some menu items of MyTracks. * * @author Youtao Liu */ public class MenuItemsTest extends ActivityInstrumentationTestCase2<TrackListActivity> { private Instrumentation instrumentation; private TrackListActivity activityMyTracks; private final static String SHARE_ITEM_PARENT_VIEW_NAME = "RecycleListView"; public MenuItemsTest() { super(TrackListActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); instrumentation = getInstrumentation(); activityMyTracks = getActivity(); EndToEndTestUtils.setupForAllTest(instrumentation, activityMyTracks); } /** * Tests following items in More menu. * <ul> * <li>Tests the aggregated statistics activity.</li> * <li>Tests the Sensor state activity.</li> * <li>Tests the help menu.</li> * </ul> */ public void testSomeMenuItems() { // Menu in TrackListActivity. EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_aggregated_statistics), true); EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.stats_distance)); EndToEndTestUtils.SOLO.goBack(); instrumentation.waitForIdleSync(); EndToEndTestUtils.createTrackIfEmpty(1, false); instrumentation.waitForIdleSync(); // Menu in TrackDetailActivity. // When there is no sensor connected this menu will be hidden. if (EndToEndTestUtils .findMenuItem(activityMyTracks.getString(R.string.menu_sensor_state), true)) { EndToEndTestUtils.SOLO.waitForText(activityMyTracks .getString(R.string.sensor_state_last_sensor_time)); } EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_help), true); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.help_about), true, true); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); } /** * Tests search menu item. */ public void testSearch() { EndToEndTestUtils.createSimpleTrack(0, true); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_search), true); EndToEndTestUtils.enterTextAvoidSoftKeyBoard(0, EndToEndTestUtils.trackName); sendKeys(KeyEvent.KEYCODE_ENTER); instrumentation.waitForIdleSync(); assertEquals(1, EndToEndTestUtils.SOLO.getCurrentListViews().size()); } /** * Tests the share menu item. This test to check whether crash will happen during the share. */ public void testShareActivity() { // Try all share items. for (int i = 0;; i++) { EndToEndTestUtils.createTrackIfEmpty(0, false); View oneItemView = findShareItem(i); if (oneItemView == null) { break; } EndToEndTestUtils.SOLO.clickOnView(oneItemView); EndToEndTestUtils.getButtonOnScreen(activityMyTracks.getString(R.string.generic_ok), true, true); if(!GoogleUtils.isAccountAvailable()) { break; } // Waiting the send is finish. while (EndToEndTestUtils.SOLO.waitForText( activityMyTracks.getString(R.string.generic_progress_title), 1, EndToEndTestUtils.SHORT_WAIT_TIME)) {} // Check whether data is correct on Google Map and then delete it. assertTrue(GoogleUtils.deleteMap(EndToEndTestUtils.trackName, activityMyTracks)); // Display the MyTracks activity for the share item may startup other // applications. Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(activityMyTracks.getApplicationContext(), TrackListActivity.class); activityMyTracks.getApplicationContext().startActivity(intent); EndToEndTestUtils.sleep(EndToEndTestUtils.NORMAL_WAIT_TIME); } } /** * Gets the view to click the share item by item index. * * @param index of a share item * @return null when no such item */ private View findShareItem(int index) { EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_share), true); ArrayList<View> views = EndToEndTestUtils.SOLO.getViews(); int i = 0; for (View view : views) { String name = view.getParent().getClass().getName(); // Each share item is a child of a "RecycleListView" if (name.indexOf(SHARE_ITEM_PARENT_VIEW_NAME) > 0) { if (index == i) { return view; } i++; } } return null; } /** * Checks the voice frequency and split frequency menus during recording. When * recording, they should be in both the menu and the recording settings. When * not recording, they should only be in the recording settings. */ public void testFrequencyMenu() { EndToEndTestUtils.startRecording(); assertTrue(EndToEndTestUtils.findMenuItem( activityMyTracks.getString(R.string.menu_voice_frequency), false)); assertTrue(EndToEndTestUtils.findMenuItem( activityMyTracks.getString(R.string.menu_split_frequency), false)); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_settings), true); EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.settings_recording)); assertTrue(EndToEndTestUtils.SOLO.searchText(activityMyTracks.getString(R.string.menu_voice_frequency), 1, true, true)); assertTrue(EndToEndTestUtils.SOLO.searchText(activityMyTracks.getString(R.string.menu_split_frequency), 1, true, true)); EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.SOLO.goBack(); EndToEndTestUtils.stopRecording(true); assertFalse(EndToEndTestUtils.findMenuItem( activityMyTracks.getString(R.string.menu_voice_frequency), false)); assertFalse(EndToEndTestUtils.findMenuItem( activityMyTracks.getString(R.string.menu_split_frequency), false)); EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_settings), true); EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.settings_recording)); assertTrue(EndToEndTestUtils.SOLO.searchText(activityMyTracks.getString(R.string.menu_voice_frequency), 1, true, true)); assertTrue(EndToEndTestUtils.SOLO.searchText(activityMyTracks.getString(R.string.menu_split_frequency), 1, true, true)); } /** * Tests starting and stopping GPS. */ public void testGPSMenu() { boolean GPSStatus = EndToEndTestUtils.findMenuItem( activityMyTracks.getString(R.string.menu_stop_gps), false); // Following starting/stopping or stopping/starting GPS. EndToEndTestUtils.findMenuItem(GPSStatus ? activityMyTracks.getString(R.string.menu_stop_gps) : activityMyTracks.getString(R.string.menu_start_gps), true); GPSStatus = !GPSStatus; EndToEndTestUtils.waitTextToDisappear(GPSStatus ? activityMyTracks .getString(R.string.menu_start_gps) : activityMyTracks.getString(R.string.menu_stop_gps)); assertEquals(GPSStatus, EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false)); EndToEndTestUtils.findMenuItem(GPSStatus ? activityMyTracks.getString(R.string.menu_stop_gps) : activityMyTracks.getString(R.string.menu_start_gps), true); GPSStatus = !GPSStatus; EndToEndTestUtils.waitTextToDisappear(GPSStatus ? activityMyTracks .getString(R.string.menu_start_gps) : activityMyTracks.getString(R.string.menu_stop_gps)); assertEquals(GPSStatus, EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_stop_gps), false)); } @Override protected void tearDown() throws Exception { EndToEndTestUtils.SOLO.finishOpenedActivities(); super.tearDown(); } }