/*
 *   Copyright 2015 Kirby Banman,
 *                  Stuart Bildfell,
 *                  Elliot Colp,
 *                  Christian Ellinger,
 *                  Braedy Kuzma,
 *                  Ryan Thornhill
 *
 *  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 cmput301w15t07.TravelTracker.test.activity;

import java.util.ArrayList;

import cmput301w15t07.TravelTracker.DataSourceSingleton;
import cmput301w15t07.TravelTracker.R;
import cmput301w15t07.TravelTracker.activity.ManageTagsActivity;
import cmput301w15t07.TravelTracker.activity.TravelTrackerActivity;
import cmput301w15t07.TravelTracker.model.DataSource;
import cmput301w15t07.TravelTracker.model.InMemoryDataSource;
import cmput301w15t07.TravelTracker.model.Tag;
import cmput301w15t07.TravelTracker.model.User;
import cmput301w15t07.TravelTracker.model.UserData;
import cmput301w15t07.TravelTracker.model.UserRole;
import cmput301w15t07.TravelTracker.testutils.DataSourceUtils;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ListView;

/**
 * Test for tag management activities.
 * 
 * Each relevant Use Case UC.XxxYyy is tested with method testXxxYyy()
 * 
 * @author kdbanman
 *
 */
public class ManageTagsActivityTest extends ActivityInstrumentationTestCase2<ManageTagsActivity> {

	DataSource ds;
	User user;
	ManageTagsActivity activity;
	
	public ManageTagsActivityTest() {
		super(ManageTagsActivity.class);
	}
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		ds = new InMemoryDataSource();
		DataSourceSingleton.setDataSource(ds);
		user = DataSourceUtils.addUser("Bob", ds);
	}
	
	public void testManageTagsNumberOfTags() throws InterruptedException {
		int numberOfTags = 10;
		startWithTags(numberOfTags);
		
		ListView listView = (ListView) activity.findViewById(R.id.manageTagsTagListView);
		
		assertEquals(numberOfTags, listView.getCount());
	}
	
	public void testCreateTag() {
		
	}
	
	public void testEditTag() {
		
	}
	
	public void testDeleteTag() {
		
	}
	
	private void startWithTags(int number) throws InterruptedException{
		ArrayList<Tag> tags = addEmptyTags(number);
		startActivity();
	}
	
	private void startActivity() throws InterruptedException{
		// Create the intent
	Intent intent = new Intent();
	intent.putExtra(TravelTrackerActivity.USER_DATA,
					new UserData(user.getUUID(), user.getUserName(), UserRole.CLAIMANT));
	
	// Start the activity
	setActivityIntent(intent);
	activity = getActivity();
	activity.waitUntilLoaded();
	}
	
	private ArrayList<Tag> addEmptyTags(int number){
		ArrayList<Tag> out = new ArrayList<Tag>();
		for (int i=0; i < number; i++){
			Tag tag = DataSourceUtils.addEmptyTag(user, ds);
			tag.setTitle("Tag" + i);
		}
		return out;
	}

}

@@ -1555,16 +1555,149 @@
 tCase2;%0A
+import android.widget.ArrayAdapter;%0Aimport android.widget.Button;%0Aimport android.widget.EditText;%0Aimport android.widget.ListAdapter;%0A
 import a
@@ -2321,30 +2321,17 @@
 test
-ManageTagsNumberOf
+Create
 Tag
-s
 () t
@@ -2336,36 +2336,25 @@
  throws 
-InterruptedException
+Throwable
  %7B%0A%09%09int
@@ -2373,16 +2373,53 @@
 s = 10;%0A
+%09%09final String tagName = %22NewTag%22;%09%09%0A
 %09%09startW
@@ -2537,95 +2537,689 @@
 %09%0A%09%09
-assertEquals(numberOfTags, listView.getCount());%0A%09%7D%0A%09%0A%09public void testCreateTag() %7B%0A%09%09
+final EditText tagBox = (EditText) activity.findViewById(R.id.manageTagsNewTagEditText);%0A%09%09final Button addButton = (Button) activity.findViewById(R.id.manageTagsAddButton);%0A%09%09%0A%09%09runTestOnUiThread(new Runnable() %7B%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09tagBox.setText(tagName);%0A%09%09%09%09addButton.performClick();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09%0A%09%09getInstrumentation().waitForIdleSync();%0A%09%09ListAdapter adapter = listView.getAdapter();%0A%09%09assertEquals(numberOfTags + 1, adapter.getCount());%0A%09%09%0A%09%09for (int i = 0; i %3C listView.getCount(); i++)%7B%0A%09%09%09Tag tag = (Tag) adapter.getItem(i);%0A%09%09%09if (tag.getTitle().equals(tagName))%7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09fail(%22Did not find a tag that matched the added tag%22);
 %0A%09%7D%0A
@@ -3426,24 +3426,138 @@
 Activity();%0A
+%09%09%0A%09%09assertEquals(number, ((ListView)activity.findViewById(R.id.manageTagsTagListView)).getAdapter().getCount());%0A
 %09%7D%0A%09%0A%09privat

Add tag test finished