<commit_before>/*
Copyright (c) Microsoft Open Technologies, Inc.
All Rights Reserved
Apache 2.0 License
 
   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.
 
See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
 */
package com.microsoft.windowsazure.mobileservices.zumoe2etestapp;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.List;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestCase;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestExecutionCallback;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestGroup;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestResult;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.LoginTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.MiscTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.PushTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.QueryTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.RoundTripTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.UpdateDeleteTests;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	private StringBuilder mLog;

	private SharedPreferences mPrefManager;

	private ListView mTestCaseList;
	
	private Spinner mTestGroupSpinner;

	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// don't restart the activity. Just process the configuration change
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);

		mPrefManager = PreferenceManager.getDefaultSharedPreferences(this);

		mTestCaseList = (ListView) findViewById(R.id.testCaseList);
		TestCaseAdapter testCaseAdapter = new TestCaseAdapter(this, R.layout.row_list_test_case);
		mTestCaseList.setAdapter(testCaseAdapter);

		mTestGroupSpinner = (Spinner) findViewById(R.id.testGroupSpinner);

		ArrayAdapter<TestGroup> testGroupAdapter = new ArrayAdapter<TestGroup>(this, android.R.layout.simple_spinner_item);
		mTestGroupSpinner.setAdapter(testGroupAdapter);
		mTestGroupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				selectTestGroup(pos);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// do nothing
			}
		});

		PushTests.mainActivity = this;
		
		refreshTestGroupsAndLog();
	}

	private void selectTestGroup(int pos) {
		TestGroup tg = (TestGroup) mTestGroupSpinner.getItemAtPosition(pos);
		List<TestCase> testCases = tg.getTestCases();

		fillTestList(testCases);
	}

	@SuppressWarnings("unchecked")
	private void refreshTestGroupsAndLog() {
		mLog = new StringBuilder();

		ArrayAdapter<TestGroup> adapter = (ArrayAdapter<TestGroup>) mTestGroupSpinner.getAdapter();
		adapter.clear();
		adapter.add(new RoundTripTests());
		adapter.add(new QueryTests());
		adapter.add(new UpdateDeleteTests());
		adapter.add(new LoginTests());
		adapter.add(new MiscTests());
		adapter.add(new PushTests());
		mTestGroupSpinner.setSelection(0);
		selectTestGroup(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			startActivity(new Intent(this, ZumoPreferenceActivity.class));
			return true;

		case R.id.menu_run_tests:
			if (getMobileServiceKey().trim() == "" || getMobileServiceURL().trim() == "") {
				startActivity(new Intent(this, ZumoPreferenceActivity.class));
			} else {
				runTests();
			}
			return true;

		case R.id.menu_check_all:
			changeCheckAllTests(true);
			return true;

		case R.id.menu_uncheck_all:
			changeCheckAllTests(false);
			return true;

		case R.id.menu_reset:
			refreshTestGroupsAndLog();
			return true;

		case R.id.menu_view_log:
			AlertDialog.Builder logDialogBuilder = new AlertDialog.Builder(this);
			logDialogBuilder.setTitle("Log");

			final WebView webView = new WebView(this);
			
			String logContent = TextUtils.htmlEncode(mLog.toString()).replace("\n", "<br />");
			String logHtml = "<html><body><pre>" + logContent + "</pre></body></html>";
			webView.loadData(logHtml, "text/html", "utf-8");
			
			logDialogBuilder.setPositiveButton("Copy", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					clipboardManager.setText(mLog.toString());
				}
			});
			
			final String postContent = mLog.toString();
			
			logDialogBuilder.setNeutralButton("Post data", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					new AsyncTask<Void, Void, Void>() {

						@Override
						protected Void doInBackground(Void... params) {
							try {
								String url = getLogPostURL();
								if (url != null && url.trim() != "") {
									HttpPost post = new HttpPost();
									post.setEntity(new StringEntity(postContent, MobileServiceClient.UTF8_ENCODING));
									
									post.setURI(new URI(url));
									
									new DefaultHttpClient().execute(post);
								}
							} catch (Exception e) {
								// Wasn't able to post the data. Do nothing
							}
							
							return null;
						}	
					}.execute();
				}
			});

			logDialogBuilder.setView(webView);

			logDialogBuilder.create().show();
			return true;<commit_msg>[Android Test App] Pasing platform information to the logging service<commit_after>/*
Copyright (c) Microsoft Open Technologies, Inc.
All Rights Reserved
Apache 2.0 License
 
   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.
 
See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
 */
package com.microsoft.windowsazure.mobileservices.zumoe2etestapp;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.List;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestCase;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestExecutionCallback;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestGroup;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.framework.TestResult;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.LoginTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.MiscTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.PushTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.QueryTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.RoundTripTests;
import com.microsoft.windowsazure.mobileservices.zumoe2etestapp.tests.UpdateDeleteTests;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

	private StringBuilder mLog;

	private SharedPreferences mPrefManager;

	private ListView mTestCaseList;
	
	private Spinner mTestGroupSpinner;

	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// don't restart the activity. Just process the configuration change
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);

		mPrefManager = PreferenceManager.getDefaultSharedPreferences(this);

		mTestCaseList = (ListView) findViewById(R.id.testCaseList);
		TestCaseAdapter testCaseAdapter = new TestCaseAdapter(this, R.layout.row_list_test_case);
		mTestCaseList.setAdapter(testCaseAdapter);

		mTestGroupSpinner = (Spinner) findViewById(R.id.testGroupSpinner);

		ArrayAdapter<TestGroup> testGroupAdapter = new ArrayAdapter<TestGroup>(this, android.R.layout.simple_spinner_item);
		mTestGroupSpinner.setAdapter(testGroupAdapter);
		mTestGroupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				selectTestGroup(pos);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// do nothing
			}
		});

		PushTests.mainActivity = this;
		
		refreshTestGroupsAndLog();
	}

	private void selectTestGroup(int pos) {
		TestGroup tg = (TestGroup) mTestGroupSpinner.getItemAtPosition(pos);
		List<TestCase> testCases = tg.getTestCases();

		fillTestList(testCases);
	}

	@SuppressWarnings("unchecked")
	private void refreshTestGroupsAndLog() {
		mLog = new StringBuilder();

		ArrayAdapter<TestGroup> adapter = (ArrayAdapter<TestGroup>) mTestGroupSpinner.getAdapter();
		adapter.clear();
		adapter.add(new RoundTripTests());
		adapter.add(new QueryTests());
		adapter.add(new UpdateDeleteTests());
		adapter.add(new LoginTests());
		adapter.add(new MiscTests());
		adapter.add(new PushTests());
		mTestGroupSpinner.setSelection(0);
		selectTestGroup(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			startActivity(new Intent(this, ZumoPreferenceActivity.class));
			return true;

		case R.id.menu_run_tests:
			if (getMobileServiceKey().trim() == "" || getMobileServiceURL().trim() == "") {
				startActivity(new Intent(this, ZumoPreferenceActivity.class));
			} else {
				runTests();
			}
			return true;

		case R.id.menu_check_all:
			changeCheckAllTests(true);
			return true;

		case R.id.menu_uncheck_all:
			changeCheckAllTests(false);
			return true;

		case R.id.menu_reset:
			refreshTestGroupsAndLog();
			return true;

		case R.id.menu_view_log:
			AlertDialog.Builder logDialogBuilder = new AlertDialog.Builder(this);
			logDialogBuilder.setTitle("Log");

			final WebView webView = new WebView(this);
			
			String logContent = TextUtils.htmlEncode(mLog.toString()).replace("\n", "<br />");
			String logHtml = "<html><body><pre>" + logContent + "</pre></body></html>";
			webView.loadData(logHtml, "text/html", "utf-8");
			
			logDialogBuilder.setPositiveButton("Copy", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					clipboardManager.setText(mLog.toString());
				}
			});
			
			final String postContent = mLog.toString();
			
			logDialogBuilder.setNeutralButton("Post data", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					new AsyncTask<Void, Void, Void>() {

						@Override
						protected Void doInBackground(Void... params) {
							try {
								String url = getLogPostURL();
								if (url != null && url.trim() != "") {
									url = url + "?platform=android";
									HttpPost post = new HttpPost();
									post.setEntity(new StringEntity(postContent, MobileServiceClient.UTF8_ENCODING));
									
									post.setURI(new URI(url));
									
									new DefaultHttpClient().execute(post);
								}
							} catch (Exception e) {
								// Wasn't able to post the data. Do nothing
							}
							
							return null;
						}	
					}.execute();
				}
			});

			logDialogBuilder.setView(webView);

			logDialogBuilder.create().show();
			return true;
{'fraction_non_alphanumeric': 0.10016231570404437, 'fraction_numerical': 0.003246314080887326, 'mean_word_length': 4.704861111111111, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '1122303', 'n_tokens_mistral': 4947, 'n_tokens_neox': 4240, 'n_words': 949}