Przeglądaj źródła

Fix some smells and upload my test cases

Yiupang 9 lat temu
rodzic
commit
732b3e83c1

+ 2 - 11
app/src/main/java/com/example/yiupang/freefoodfinder/EventArrayAdapter.java

@@ -31,16 +31,10 @@ public class EventArrayAdapter extends ArrayAdapter<Event>
     private Context context;
     List<Event> events;
 
-    // CODE SMELL: unused private field
-    private int layoutResourceId;
-
-
-
     public EventArrayAdapter(Context context, int layoutResourceId, List<Event> events)
     {
         super(context, layoutResourceId, events);
         this.context = context;
-        this.layoutResourceId = layoutResourceId;
         this.events = events;
     }
 
@@ -60,7 +54,8 @@ public class EventArrayAdapter extends ArrayAdapter<Event>
         LayoutInflater inflater = ((Activity) context).getLayoutInflater();
         View newConvertView = convertView;
 
-        if(newConvertView == null) {
+        if(newConvertView == null)
+        {
             newConvertView = inflater.inflate(R.layout.events_list_item, parent, false);
             holder = new ViewHolder();
 
@@ -75,11 +70,7 @@ public class EventArrayAdapter extends ArrayAdapter<Event>
             holder.desc.setText(rowPos.getDescription());
 
             newConvertView.setTag(holder);
-        } else {
-            // CODE SMELL AND BUG: Useless Assignment
-            holder = (ViewHolder) newConvertView.getTag();
         }
-
         return newConvertView;
     }
 }

+ 0 - 1
app/src/main/java/com/example/yiupang/freefoodfinder/MainActivity.java

@@ -61,7 +61,6 @@ public class MainActivity extends AppCompatActivity
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
-        getMenuInflater().inflate(R.menu.top_menu, menu);
         return true;
     }
 

+ 0 - 16
app/src/main/res/menu/top_menu.xml

@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
-    android:id="@+id/top_bar">
-    <!-- "Mark Favorite", should appear as action button if possible -->
-    <item
-        android:id="@+id/action_favorite"
-        android:icon="@drawable/back_icon"
-        android:title="action_favorite"
-        app:showAsAction="ifRoom"/>
-
-    <!-- Settings, should always be in the overflow -->
-    <item android:id="@+id/action_settings"
-        android:title="@string/action_settings"
-        app:showAsAction="never"/>
-</menu>

+ 0 - 17
app/src/test/java/com/example/yiupang/freefoodfinder/ExampleUnitTest.java

@@ -1,17 +0,0 @@
-package com.example.yiupang.freefoodfinder;
-
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- * Example local unit test, which will execute on the development machine (host).
- *
- * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
- */
-public class ExampleUnitTest {
-    @Test
-    public void addition_isCorrect() throws Exception {
-        assertEquals(4, 2 + 2);
-    }
-}

+ 0 - 13
app/src/test/java/com/example/yiupang/freefoodfinder/TestHttpCall.java

@@ -1,13 +0,0 @@
-package com.example.yiupang.freefoodfinder;
-
-import static org.junit.Assert.assertEquals;
-import org.junit.Test;
-
-/**
- * Created by yiupang on 6/1/2017.
- *
- */
-public class TestHttpCall
-{
-
-}

+ 21 - 40
app/src/test/java/com/example/yiupang/freefoodfinder/TestHttpRequest.java

@@ -1,11 +1,10 @@
 package com.example.yiupang.freefoodfinder;
 
-import org.junit.Before;
+
 import org.junit.Test;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.junit.Assert.assertEquals;
 /**
@@ -14,42 +13,24 @@ import static org.junit.Assert.assertEquals;
  */
 public class TestHttpRequest
 {
-    private EventsScreen eventsScreen;
-
-    @Mock
-    private HttpRequest httpRequest;
-
-    @Captor
-    private ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor;
-
-    @Before
-    public void setUp()
-    {
-        MockitoAnnotations.initMocks(this);
-        eventsScreen = new EventsScreen();
-    }
-
     @Test
-    public void testGetCall()
+    public void TestDataString()
     {
-        final String expected = "200";
-
-        HttpCall httpCall = new HttpCall();
-        httpCall.setMethodType(HttpCall.GET);
-        httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
-        new HttpRequest(){
-            @Override
-            public void onResponse(Object response, int code)
-            {
-                super.onResponse(response, code);
-                assertEquals(expected, Integer.toString(code));
-            }
-        }.execute(httpCall);
-    }
-
-    @Test
-    public void testDoInBackground()
-    {
-
+        try
+        {
+            String expected = "?a=2&b=6";
+            HttpRequest request = new HttpRequest();
+            Map<String, String> queryParam = new HashMap<>();
+            queryParam.put("a", "2");
+            queryParam.put("b", "6");
+            Method m1 = request.getClass().getDeclaredMethod("getDataString", Map.class);
+            m1.setAccessible(true);
+            Object o = m1.invoke(request, queryParam);
+            assertEquals(expected, o.toString());
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
     }
 }

+ 5 - 5
app/src/test/java/com/example/yiupang/freefoodfinder/TestMainActivity.java

@@ -1,9 +1,9 @@
 package com.example.yiupang.freefoodfinder;
 
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.instanceOf;
 import org.junit.Test;
 import java.lang.reflect.Method;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
 
 /**
  * Created by yiupang on 6/4/2017.
@@ -12,11 +12,11 @@ import static org.junit.Assert.assertThat;
 public class TestMainActivity
 {
     @Test
-    public void TestSwitchFragment()
+    public void TestSwitchToAboutUs()
     {
-        AboutUsScreen expected = new AboutUsScreen();
         try
         {
+            AboutUsScreen expected = new AboutUsScreen();
             MainActivity m = new MainActivity();
             Method m1 = m.getClass().getDeclaredMethod("getSelectedFragement", int.class);
             m1.setAccessible(true);
@@ -25,7 +25,7 @@ public class TestMainActivity
         }
         catch (Exception e)
         {
-            System.out.println(e);
+            e.printStackTrace();
         }
     }
 }

+ 2 - 2
app/src/test/java/com/example/yiupang/freefoodfinder/TestHttpSuite.java → app/src/test/java/com/example/yiupang/freefoodfinder/TestSwitchScreenAndQueryParamSuite.java

@@ -9,8 +9,8 @@ import org.junit.runners.Suite;
  */
 
 @RunWith(Suite.class)
-@Suite.SuiteClasses({TestHttpCall.class, TestHttpRequest.class})
-public class TestHttpSuite
+@Suite.SuiteClasses({TestHttpRequest.class, TestHttpRequest.class})
+public class TestSwitchScreenAndQueryParamSuite
 {
 
 }