/*
 * 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.services;

import com.google.android.maps.mytracks.R;
import com.google.android.testing.mocking.UsesMocks;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.test.ServiceTestCase;

import org.easymock.EasyMock;

/**
 * Tests {@link ControlRecordingService}.
 * 
 * @author Youtao Liu
 */
public class ControlRecordingServiceTest extends ServiceTestCase<ControlRecordingService> {

  private Context context;
  private ControlRecordingService controlRecordingService;

  public ControlRecordingServiceTest() {
    super(ControlRecordingService.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    context = getContext();
  }

  /**
   * Tests the start of {@link ControlRecordingService} and tests the method
   * {@link ControlRecordingService#onHandleIntent(Intent, ITrackRecordingService)}
   * to start a track recording.
   */
  @UsesMocks(ITrackRecordingService.class)
  public void testStartRecording() {
    Intent intent = new Intent(context, ControlRecordingService.class);
    intent.setAction(context.getString(R.string.track_action_start));
    controlRecordingService = getService();
    assertNull(controlRecordingService);
    startService(intent);
    controlRecordingService = getService();
    assertNotNull(controlRecordingService);

    ITrackRecordingService iTrackRecordingServiceMock = EasyMock
        .createStrictMock(ITrackRecordingService.class);
    try {
      EasyMock.expect(iTrackRecordingServiceMock.startNewTrack()).andReturn(1L);
      EasyMock.replay(iTrackRecordingServiceMock);
      controlRecordingService.onHandleIntent(intent, iTrackRecordingServiceMock);
      EasyMock.verify(iTrackRecordingServiceMock);
    } catch (RemoteException e) {
      fail();
    }
  }

  /**
   * Tests the method
   * {@link ControlRecordingService#onHandleIntent(Intent, ITrackRecordingService)}
   * to stop a track recording.
   */
  @UsesMocks(ITrackRecordingService.class)
  public void testStopRecording() {
    Intent intent = new Intent(context, ControlRecordingService.class);
    intent.setAction(context.getString(R.string.track_action_end));
    startService(intent);
    controlRecordingService = getService();
    ITrackRecordingService iTrackRecordingServiceMock = EasyMock
        .createStrictMock(ITrackRecordingService.class);
    try {
      iTrackRecordingServiceMock.endCurrentTrack();
      EasyMock.replay(iTrackRecordingServiceMock);
      controlRecordingService.onHandleIntent(intent, iTrackRecordingServiceMock);
      EasyMock.verify(iTrackRecordingServiceMock);
    } catch (RemoteException e) {
      fail();
    }
  }

}

@@ -1646,45 +1646,20 @@
     
-Intent intent = new Intent(context, C
+assertNull(c
 ontr
@@ -1680,239 +1680,106 @@
 vice
-.class
 );%0A    
-i
+I
 ntent
-.setAction(context.getString(R.string.track_action_start));%0A    controlRecordingService = getService();%0A    assertNull(controlRecordingService);%0A    startService(intent);%0A    controlRecordingService = getService(
+ intent = startControlRecordingService(context.getString(R.string.track_action_start)
 );%0A 
@@ -2531,28 +2531,13 @@
 t = 
-new Intent(context, 
+start
 Cont
@@ -2559,37 +2559,8 @@
 vice
-.class);%0A    intent.setAction
 (con
@@ -2611,73 +2611,8 @@
     
-startService(intent);%0A    controlRecordingService = getService();
 %0A   
@@ -3016,29 +3016,29 @@
 %7B%0A      fail();%0A
-
     %7D%0A  %7D%0A%0A%7D%0A
@@ -3033,12 +3033,411 @@
    %7D%0A  %7D
+%0A  %0A  /**%0A   * Starts a ControlRecordingService with a specified action.%0A   * %0A   * @param action the action string in the start intent%0A   */%0A  private Intent startControlRecordingService(String action) %7B%0A    Intent intent = new Intent(context, ControlRecordingService.class);%0A    intent.setAction(action);%0A    startService(intent);%0A    controlRecordingService = getService();%0A    return intent;%0A  %7D
 %0A%0A%7D%0A

Change code style of ControlRecordingServiceTest.