/*
 * Copyright 2012 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.maps;

import com.google.android.testing.mocking.AndroidMock;
import com.google.android.testing.mocking.UsesMocks;

import android.accounts.Account;
import android.test.AndroidTestCase;

/**
 * Tests {@link ChooseMapAsyncTask}.
 * 
 * @author Youtao Liu
 */
public class ChooseMapAsyncTaskTest extends AndroidTestCase {

  private ChooseMapActivity chooseMapActivityMock;
  private Account account;
  private final String accountName = "AccountName";
  private final String accountType = "AccountType";
  private boolean getMapsStatus = false;

  public class ChooseMapAsyncTaskTMock extends ChooseMapAsyncTask {

    public ChooseMapAsyncTaskTMock(ChooseMapActivity activity, Account account) {
      super(activity, account);
    }

    /**
     * Creates this method to override {@link ChooseMapAsyncTask#getMaps()}.
     * 
     * @return mock the return value of getMaps().
     */
    @Override
    boolean getMaps() {
      return getMapsStatus;
    }

  }

  @Override
  @UsesMocks({ ChooseMapActivity.class })
  protected void setUp() throws Exception {
    super.setUp();
    chooseMapActivityMock = AndroidMock.createMock(ChooseMapActivity.class);
    account = new Account(accountName, accountType);
    AndroidMock.expect(chooseMapActivityMock.getApplicationContext()).andReturn(getContext());
  }

  /**
   * Tests the method {@link ChooseMapAsyncTask#setActivity(ChooseMapActivity)}
   * when the task is completed.
   */
  public void testSetActivity_completed() {
    chooseMapActivityMock.onAsyncTaskCompleted(false, null, null);
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTask chooseMapAsyncTask = new ChooseMapAsyncTask(chooseMapActivityMock, account);
    chooseMapAsyncTask.setCompleted(true);
    chooseMapAsyncTask.setActivity(chooseMapActivityMock);
    AndroidMock.verify(chooseMapActivityMock);
  }

  /**
   * Tests the method {@link ChooseMapAsyncTask#setActivity(ChooseMapActivity)}
   * when the task is not completed.
   */
  public void testSetActivity_notCompleted() {
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTask chooseMapAsyncTask = new ChooseMapAsyncTask(chooseMapActivityMock, account);
    chooseMapAsyncTask.setCompleted(false);
    chooseMapAsyncTask.setActivity(chooseMapActivityMock);
    AndroidMock.verify(chooseMapActivityMock);
  }

  /**
   * Tests the method {@link ChooseMapAsyncTask#setActivity(ChooseMapActivity)}
   * when the activity is null.
   */
  public void testSetActivity_nullActivity() {
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTask chooseMapAsyncTask = new ChooseMapAsyncTask(chooseMapActivityMock, account);
    chooseMapAsyncTask.setCompleted(true);
    chooseMapAsyncTask.setActivity(null);
  }

  /**
   * Tests the method {@link ChooseMapAsyncTask#onPostExecute(Boolean)} when the
   * result is true.
   */
  public void testOnPostExecute_trueResult() {
    chooseMapActivityMock.onAsyncTaskCompleted(true, null, null);
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTask chooseMapAsyncTask = new ChooseMapAsyncTask(chooseMapActivityMock, account);
    chooseMapAsyncTask.onPostExecute(true);
    AndroidMock.verify(chooseMapActivityMock);
  }

  /**
   * Tests the method {@link ChooseMapAsyncTask#onPostExecute(Boolean)} when the
   * result is false.
   */
  public void testOnPostExecute_falseResult() {
    chooseMapActivityMock.onAsyncTaskCompleted(false, null, null);
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTask chooseMapAsyncTask = new ChooseMapAsyncTask(chooseMapActivityMock, account);
    chooseMapAsyncTask.onPostExecute(false);
    AndroidMock.verify(chooseMapActivityMock);
  }

  /**
   * Tests the method {@link ChooseMapAsyncTas#retryUpload()}.
   */
  public void testRetryUpload() throws Exception {
    AndroidMock.replay(chooseMapActivityMock);
    ChooseMapAsyncTaskTMock chooseMapAsyncTaskTMock = new ChooseMapAsyncTaskTMock(
        chooseMapActivityMock, account);
    chooseMapAsyncTaskTMock.setCanRetry(true);
    getMapsStatus = false;
    assertFalse(chooseMapAsyncTaskTMock.retryUpload());
    // Can only retry once.
    assertFalse(chooseMapAsyncTaskTMock.getCanRetry());

    chooseMapAsyncTaskTMock.setCanRetry(true);
    getMapsStatus = true;
    assertTrue(chooseMapAsyncTaskTMock.retryUpload());
    // Can only retry once.
    assertFalse(chooseMapAsyncTaskTMock.getCanRetry());

    chooseMapAsyncTaskTMock.setCanRetry(false);
    getMapsStatus = true;
    assertFalse(chooseMapAsyncTaskTMock.retryUpload());
    AndroidMock.verify(chooseMapActivityMock);
  }

}

@@ -693,61 +693,8 @@
 ock;
-%0Aimport com.google.android.testing.mocking.UsesMocks;
 %0A%0Aim
@@ -997,27 +997,28 @@
  String 
-accountName
+ACCOUNT_NAME
  = %22Acco
@@ -1050,27 +1050,28 @@
  String 
-accountType
+ACCOUNT_TYPE
  = %22Acco
@@ -1539,356 +1539,8 @@
    %7D
-%0A%0A  %7D%0A%0A  @Override%0A  @UsesMocks(%7B ChooseMapActivity.class %7D)%0A  protected void setUp() throws Exception %7B%0A    super.setUp();%0A    chooseMapActivityMock = AndroidMock.createMock(ChooseMapActivity.class);%0A    account = new Account(accountName, accountType);%0A    AndroidMock.expect(chooseMapActivityMock.getApplicationContext()).andReturn(getContext());
 %0A  %7D
@@ -1706,24 +1706,49 @@
 mpleted() %7B%0A
+    createMockObjects();%0A
     chooseMa
@@ -2275,24 +2275,49 @@
 mpleted() %7B%0A
+    createMockObjects();%0A
     AndroidM
@@ -2773,24 +2773,49 @@
 ctivity() %7B%0A
+    createMockObjects();%0A
     AndroidM
@@ -3192,32 +3192,57 @@
 _trueResult() %7B%0A
+    createMockObjects();%0A
     chooseMapAct
@@ -3693,24 +3693,49 @@
 eResult() %7B%0A
+    createMockObjects();%0A
     chooseMa
@@ -4159,16 +4159,41 @@
 ption %7B%0A
+    createMockObjects();%0A
     Andr
@@ -4909,24 +4909,24 @@
 yUpload());%0A
-
     AndroidM
@@ -4963,12 +4963,415 @@
 ck);%0A  %7D
+%0A  %0A  /**%0A   * Initials chooseMapActivityMock and account for test.%0A   */%0A  private void createMockObjects() %7B%0A    chooseMapActivityMock = AndroidMock.createMock(ChooseMapActivity.class);%0A    account = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);%0A    // This is used in the constructor of ChooseMapAsyncTask.%0A    AndroidMock.expect(chooseMapActivityMock.getApplicationContext()).andReturn(getContext());%0A  %7D
 %0A%0A%7D%0A

Change some code for ChooseMapAsyncTaskTest.