/*
 * Copyright 2011 Google Inc.
 *
 * 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 com.google.android.apps.mytracks.io.sendtogoogle;

import com.google.android.maps.mytracks.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests {@link ResultDialogFactory}.
 *
 * @author Matthew Simmons
 */
public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2<SendActivity> {
  public ResultDialogFactoryTest() {
    super(SendActivity.class);
  }

  private List<SendResult> makeResults(SendResult... results) {
    List<SendResult> resultsList = new ArrayList<SendResult>();
    for (SendResult result : results) {
      resultsList.add(result);
    }
    return resultsList;
  }

  private DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {}
  };

  public void testSuccess_noShare() {
    List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
        new SendResult(SendType.DOCS, true));

    AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null);
    dialog.show();

    ListView listView = (ListView) dialog.findViewById(R.id.send_to_google_result_list);
    ListAdapter listAdapter = listView.getAdapter();
    assertEquals(2, listAdapter.getCount());

    assertEquals(SendType.MYMAPS, ((SendResult) listAdapter.getItem(0)).getType());
    assertEquals(SendType.DOCS, ((SendResult) listAdapter.getItem(1)).getType());

    // Checking the return code of Dialog#getButton doesn't appear to be
    // sufficient, as a Button will be returned even if it doesn't appear in
    // the rendered dialog.
    assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
    assertEquals(View.GONE, dialog.getButton(AlertDialog.BUTTON_NEUTRAL).getVisibility());

    assertEquals(View.VISIBLE,
        dialog.findViewById(R.id.send_to_google_result_comment).getVisibility());
    assertEquals(View.GONE,
        dialog.findViewById(R.id.send_to_google_result_error).getVisibility());

    // TODO: Test correct listener invocation.  Neither TouchUtils#clickView
    // nor Button#performClick appear to cause listener invocation.  This
    // might be Android bug
    // http://code.google.com/p/android/issues/detail?id=6564
  }

  public void testSuccess_share() {
    List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
        new SendResult(SendType.DOCS, true));

    AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results,
        clickListener, clickListener, null);
    dialog.show();

    assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
    assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_NEGATIVE).getVisibility());
  }

  public void testFailure_noShare() {
    List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
        new SendResult(SendType.DOCS, false));

    AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null);
    dialog.show();

    assertEquals(View.GONE,
        dialog.findViewById(R.id.send_to_google_result_comment).getVisibility());
    assertEquals(View.VISIBLE,
        dialog.findViewById(R.id.send_to_google_result_error).getVisibility());
  }

  public void testFailure_share() {
    List<SendResult> results = makeResults(new SendResult(SendType.MYMAPS, true),
        new SendResult(SendType.DOCS, false));

    AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results,
        clickListener, clickListener, null);
    dialog.show();

    assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility());
    assertEquals(View.VISIBLE, dialog.getButton(AlertDialog.BUTTON_NEGATIVE).getVisibility());
  }
}

@@ -643,16 +643,66 @@
 oogle;%0A%0A
+import com.google.android.apps.mytracks.MyTracks;%0A
 import c
@@ -1163,28 +1163,24 @@
 stCase2%3C
-SendActivity
+MyTracks
 %3E %7B%0A  pu
@@ -1226,20 +1226,16 @@
 per(
-SendActivity
+MyTracks
 .cla
@@ -1657,16 +1657,155 @@
 %7B%7D%0A  %7D;%0A
+  %0A  private MyTracks myTracks;%0A  %0A  protected void setUp() throws Exception %7B%0A    super.setUp();    %0A    myTracks = getActivity();%0A  %7D;%0A  
 %0A  publi
@@ -2016,37 +2016,41 @@
 .makeDialog(
-getActivity()
+%0A        myTracks
 , results, c
@@ -3355,32 +3355,41 @@
 s = makeResults(
+%0A        
 new SendResult(S
@@ -3406,32 +3406,24 @@
 MAPS, true),
-%0A       
  new SendRes
@@ -3502,37 +3502,41 @@
 .makeDialog(
-getActivity()
+%0A        myTracks
 , results,%0A 
@@ -3529,32 +3529,24 @@
 ks, results,
-%0A       
  clickListen
@@ -4006,37 +4006,41 @@
 .makeDialog(
-getActivity()
+%0A        myTracks
 , results, c
@@ -4534,29 +4534,33 @@
 eDialog(
-getActivity()
+%0A        myTracks
 , result
@@ -4561,24 +4561,16 @@
 results,
-%0A       
  clickLi

Fix unit test for ResultDialogFactoryTest - change to the MyTracks activity because the SendActivity doesn't have a window to display the dialog.