Created
December 3, 2013 20:29
-
-
Save jab416171/7776971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.garagze; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentManager; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.garagze.service.EventService; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
PlaceholderFragment fragment = new PlaceholderFragment(); | |
if (savedInstanceState == null) { | |
getFragmentManager().beginTransaction() | |
.add(R.id.container, fragment) | |
.commit(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
switch (item.getItemId()) { | |
case R.id.action_settings: | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public static class PlaceholderFragment extends Fragment { | |
public PlaceholderFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_main, container, false); | |
assert getActivity() != null; | |
int eventCount = ((MainActivity) getActivity()).getEventCount(); | |
String displayText = "Number of events: " + eventCount; | |
// Reference view element and set display text | |
View view = getView(); | |
TextView textField = (TextView) rootView.findViewById(R.id.textView); | |
textField.setText(displayText); | |
return rootView; | |
} | |
} | |
private int getEventCount() { | |
return EventService.getAllEvents(this).size(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.garagze; | |
import android.content.Intent; | |
import android.test.ActivityUnitTestCase; | |
import android.widget.TextView; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: josephbass | |
* Date: 12/3/13 | |
* Time: 1:00 PM | |
*/ | |
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> { | |
private MainActivity mainActivity; | |
public MainActivityTest() { | |
super(MainActivity.class); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
Intent intent = new Intent(getInstrumentation().getTargetContext(), MainActivity.class); | |
startActivity(intent, null, null); | |
this.mainActivity = getActivity(); | |
} | |
public void testActivityUI() { | |
TextView textView = (TextView) mainActivity.findViewById(R.id.textView); | |
assertNotNull(textView); | |
assertEquals("Number of events: 15", textView.getText().toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment