<commit_before>/*
 * 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 com.google.android.apps.mytracks.endtoendtest.others;

import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.maps.mytracks.R;

import android.app.AlertDialog;
import android.app.Instrumentation;
import android.content.Context;
import android.content.DialogInterface;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

/**
 * Tests the battery and memory usage of long tracks. As this test may be run a
 * long time test and without connect to desktop computer during test. The test
 * result will be recorded to a file. And at the beginning of test, it need
 * user's confirm by click a button.
 * 
 * @author <NAME>
 */
public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackListActivity> {

  public ResourceUsageTest() {
    super(TrackListActivity.class);
  }

  private Instrumentation instrumentation;
  private TrackListActivity trackListActivity;
  private boolean runTest = false;
  private static boolean isUserConfirmed = false;
  private Context context;
  /**
   * Default duration for each test is half one hour.
   */
  private int TEST_DURATION_IN_MILLISECONDS = 30 * 60 * 1000;
  private int INTERVALE_TO_CHECK = 5 * 60 * 1000;

  @Override
  protected void setUp() throws Exception {
    runTest = BigTestUtils.runResourceUsageTest;
    super.setUp();
    if (!runTest) {
      return;
    }
    instrumentation = getInstrumentation();
    trackListActivity = getActivity();
    context = trackListActivity.getApplicationContext();
    instrumentation.waitForIdleSync();
    EndToEndTestUtils.setupForDebug(instrumentation, trackListActivity);
    BigTestUtils.unlockDevice();
    if (!isUserConfirmed) {
      confirmTest();
    }
    BigTestUtils.writeToFile("Test case start:" + getName() + "\r\n", true);
  }

  /**
   * Prompts a dialog to make user confirm whether start the test.
   */
  private void confirmTest() {
    try {
      trackListActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
          new AlertDialog.Builder(EndToEndTestUtils.SOLO.getCurrentActivity())
              .setTitle("Confirm long time test.")
              .setMessage(
                  "Please disconnect the power cord of your device and click 'Start' button. Each case need run "
                      + TEST_DURATION_IN_MILLISECONDS / 1000 / 60
                      + " minutes, and the battery/memory usage will be recorded in every "
                      + INTERVALE_TO_CHECK / 60 / 1000 + "minutes.")
              .setPositiveButton("Start Test", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  isUserConfirmed = true;
                  runTest = true;
                  BigTestUtils.writeToFile("Test start:\r\n", false);
                }
              }).setNegativeButton("Cancel Test", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  isUserConfirmed = true;
                  runTest = false;
                  fail();
                }
              }).show();
        }
      });
    } catch (Throwable e) {
      Log.i(EndToEndTestUtils.LOG_TAG, "Meet error when create confirm dialog.");
    }
    // Waiting user confirm the testing
    while (!isUserConfirmed) {
      EndToEndTestUtils.sleep(EndToEndTestUtils.TINY_WAIT_TIME);
    }
  }

  /**
   * Tests the resource usage when display track list activity during recording.
   */
  public void testBatteryUsage_showTrackList() {
    recordingLongTrack(true, R.string.track_detail_map_tab);
  }

  /**
   * Tests the resource usage when display map view during recording.
   */
  public void testBatteryUsage_showMapView() {
    recordingLongTrack(false, R.string.track_detail_map_tab);
  }

  /**
   * Tests the resource usage when display chart view during recording.
   */
  public void testBatteryUsage_showChartView() {
    recordingLongTrack(false, R.string.track_detail_chart_tab);
  }

  /**
   * Tests the resource usage when display stats view during recording.
   */
  public void testBatteryUsage_showStatsView() {
    recordingLongTrack(false, R.string.track_detail_stats_tab);
  }

  /**
   * Records a long time track and keeps in the specified tab or in tracks list
   * during recording.
   * 
   * @param isShowTracksList true means show tracks list
   * @param tabNameId the name string id of tab, it's useless when the first parameter is
   *          true.
   */
  public void recordingLongTrack(boolean isShowTracksList, int tabNameId) {
    if (!runTest) {
      Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
    }

    EndToEndTestUtils.startRecording();
    if (isShowTracksList) {
      EndToEndTestUtils.SOLO.goBack();
    } else {
      EndToEndTestUtils.SOLO.clickOnText(trackListActivity.getString(tabNameId));
    }
    BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
    BigTestUtils.unlockDevice();
    EndToEndTestUtils.stopRecording(true);
  }

  @Override<commit_msg>Fix an error of test.<commit_after>/*
 * 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 com.google.android.apps.mytracks.endtoendtest.others;

import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.maps.mytracks.R;

import android.app.AlertDialog;
import android.app.Instrumentation;
import android.content.Context;
import android.content.DialogInterface;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

/**
 * Tests the battery and memory usage of long tracks. As this test may be run a
 * long time test and without connect to desktop computer during test. The test
 * result will be recorded to a file. And at the beginning of test, it need
 * user's confirm by click a button.
 * 
 * @author <NAME>
 */
public class ResourceUsageTest extends ActivityInstrumentationTestCase2<TrackListActivity> {

  public ResourceUsageTest() {
    super(TrackListActivity.class);
  }

  private Instrumentation instrumentation;
  private TrackListActivity trackListActivity;
  private boolean runTest = false;
  private static boolean isUserConfirmed = false;
  private Context context;
  /**
   * Default duration for each test is half one hour.
   */
  private int TEST_DURATION_IN_MILLISECONDS = 30 * 60 * 1000;
  private int INTERVALE_TO_CHECK = 5 * 60 * 1000;

  @Override
  protected void setUp() throws Exception {
    runTest = BigTestUtils.runResourceUsageTest;
    super.setUp();
    if (!runTest) {
      return;
    }
    instrumentation = getInstrumentation();
    trackListActivity = getActivity();
    context = trackListActivity.getApplicationContext();
    instrumentation.waitForIdleSync();
    EndToEndTestUtils.setupForDebug(instrumentation, trackListActivity);
    BigTestUtils.unlockDevice();
    if (!isUserConfirmed) {
      confirmTest();
    }
    BigTestUtils.writeToFile("Test case start:" + getName() + "\r\n", true);
  }

  /**
   * Prompts a dialog to make user confirm whether start the test.
   */
  private void confirmTest() {
    try {
      trackListActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
          new AlertDialog.Builder(EndToEndTestUtils.SOLO.getCurrentActivity())
              .setTitle("Confirm long time test.")
              .setMessage(
                  "Please disconnect the power cord of your device and click 'Start' button. Each case need run "
                      + TEST_DURATION_IN_MILLISECONDS / 1000 / 60
                      + " minutes, and the battery/memory usage will be recorded in every "
                      + INTERVALE_TO_CHECK / 60 / 1000 + "minutes.")
              .setPositiveButton("Start Test", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  isUserConfirmed = true;
                  runTest = true;
                  BigTestUtils.writeToFile("Test start:\r\n", false);
                }
              }).setNegativeButton("Cancel Test", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  isUserConfirmed = true;
                  runTest = false;
                  fail();
                }
              }).show();
        }
      });
    } catch (Throwable e) {
      Log.i(EndToEndTestUtils.LOG_TAG, "Meet error when create confirm dialog.");
    }
    // Waiting user confirm the testing
    while (!isUserConfirmed) {
      EndToEndTestUtils.sleep(EndToEndTestUtils.TINY_WAIT_TIME);
    }
  }

  /**
   * Tests the resource usage when display track list activity during recording.
   */
  public void testBatteryUsage_showTrackList() {
    recordingLongTrack(true, R.string.track_detail_map_tab);
  }

  /**
   * Tests the resource usage when display map view during recording.
   */
  public void testBatteryUsage_showMapView() {
    recordingLongTrack(false, R.string.track_detail_map_tab);
  }

  /**
   * Tests the resource usage when display chart view during recording.
   */
  public void testBatteryUsage_showChartView() {
    recordingLongTrack(false, R.string.track_detail_chart_tab);
  }

  /**
   * Tests the resource usage when display stats view during recording.
   */
  public void testBatteryUsage_showStatsView() {
    recordingLongTrack(false, R.string.track_detail_stats_tab);
  }

  /**
   * Records a long time track and keeps in the specified tab or in tracks list
   * during recording.
   * 
   * @param isShowTracksList true means show tracks list
   * @param tabNameId the name string id of tab, it's useless when the first parameter is
   *          true.
   */
  public void recordingLongTrack(boolean isShowTracksList, int tabNameId) {
    if (!runTest) {
      Log.i(EndToEndTestUtils.LOG_TAG, BigTestUtils.DISABLE_MESSAGE);
      return;
    }

    EndToEndTestUtils.startRecording();
    if (isShowTracksList) {
      EndToEndTestUtils.SOLO.goBack();
    } else {
      EndToEndTestUtils.SOLO.clickOnText(trackListActivity.getString(tabNameId));
    }
    BigTestUtils.moniterTest(context, INTERVALE_TO_CHECK, TEST_DURATION_IN_MILLISECONDS);
    BigTestUtils.unlockDevice();
    EndToEndTestUtils.stopRecording(true);
  }

  @Override
{'fraction_non_alphanumeric': 0.08505154639175258, 'fraction_numerical': 0.006357388316151203, 'mean_word_length': 3.023850674040788, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3025661', 'n_tokens_mistral': 3313, 'n_tokens_neox': 3164, 'n_words': 940}