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.android.cts.verifier.sample; 18 19 import com.android.compatibility.common.util.ReportLog; 20 import com.android.compatibility.common.util.ResultType; 21 import com.android.compatibility.common.util.ResultUnit; 22 import com.android.cts.verifier.PassFailButtons; 23 import com.android.cts.verifier.R; 24 import com.android.cts.verifier.TestResult; 25 26 import android.content.Intent; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.view.View; 30 import android.widget.Button; 31 32 import java.io.File; 33 import java.io.FileOutputStream; 34 35 /** 36 * A sample CTS Verifier test case for testing file transfers using bluetooth sharing. 37 * 38 * This test assumes bluetooth is turned on and the device is already paired with a second device. 39 * Note: the second device need not be an Android device; it could be a laptop or desktop. 40 */ 41 public class SampleTestActivity extends PassFailButtons.Activity { 42 43 /** 44 * The name of the test file being transferred. 45 */ 46 private static final String FILE_NAME = "test.txt"; 47 48 /** 49 * The content of the test file being transferred. 50 */ 51 private static final String TEST_STRING = "Sample Test String"; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 57 // Setup the UI. 58 setContentView(R.layout.pass_fail_sample); 59 setPassFailButtonClickListeners(); 60 setInfoResources(R.string.sample_test, R.string.sample_test_info, -1); 61 // Get the share button and attach the listener. 62 Button shareBtn = (Button) findViewById(R.id.sample_share_btn); 63 shareBtn.setOnClickListener(new View.OnClickListener() { 64 @Override 65 public void onClick(View v) { 66 try { 67 createFileAndShare(); 68 recordMetricsExample(); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 }); 74 } 75 recordMetricsExample()76 private void recordMetricsExample() { 77 double[] metricValues = new double[] {1, 11, 21, 1211, 111221}; 78 79 // Record metric results 80 getReportLog().setSummary( 81 "Sample Summary", 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 82 getReportLog().addValues("Sample Values", metricValues, ResultType.NEUTRAL, ResultUnit.FPS); 83 84 // Alternatively, activities can invoke TestResult directly to record metrics 85 ReportLog reportLog = new PassFailButtons.CtsVerifierReportLog(); 86 reportLog.setSummary("Sample Summary", 1.0, ResultType.HIGHER_BETTER, ResultUnit.BYTE); 87 getReportLog().addValues("Sample Values", metricValues, ResultType.NEUTRAL, ResultUnit.FPS); 88 TestResult.setPassedResult(this, "manualSample", "manualDetails", reportLog); 89 } 90 91 /** 92 * Creates a temporary file containing the test string and then issues the intent to share it. 93 * 94 * @throws Exception 95 */ createFileAndShare()96 private void createFileAndShare() throws Exception { 97 // Use the external cache directory so the file will be deleted when the app is uninstalled 98 // and the file can be accessed by other apps, such as the sharing app. 99 File dir = getExternalCacheDir (); 100 // Create the file with the given name. 101 File file = new File(dir, FILE_NAME); 102 FileOutputStream outputStream = null; 103 try { 104 // Write the test string to the test file. 105 outputStream = new FileOutputStream(file); 106 outputStream.write(TEST_STRING.getBytes()); 107 108 // Create the share intent. 109 Intent intent = new Intent(); 110 intent.setAction(Intent.ACTION_SEND); 111 intent.setType("text/plain"); 112 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 113 startActivity(intent); 114 } finally { 115 // Clean up. 116 if (outputStream != null) { 117 outputStream.close(); 118 } 119 } 120 121 } 122 } 123