/*
 * Copyright 2013 Prateek Srivastava (@f2prateek)
 *
 * 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.f2prateek.dfg;

import android.support.v4.view.ViewPager;
import com.f2prateek.dfg.model.DeviceProvider;
import com.f2prateek.dfg.ui.MainActivity;
import com.squareup.spoon.Spoon;
import java.util.Random;

import static org.fest.assertions.api.ANDROID.assertThat;

/** Tests for displaying a specific {@link MainActivity} item */
public class MainActivityTest extends ActivityTest<MainActivity> {

  private ViewPager pager;

  public MainActivityTest() {
    super(MainActivity.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    pager = (ViewPager) activity.findViewById(R.id.pager);
  }

  public void testAllDevicesShown() throws Exception {
    final String tag_prefix = "pager_item_";
    assertThat(activity).isNotNull();
    assertThat(pager).isNotNull();
    assertThat(pager.getAdapter()).isNotNull().hasCount(DeviceProvider.getDevices().size());

    for (int i = 0; i < pager.getAdapter().getCount(); i++) {
      final int count = i;
      instrumentation.runOnMainSync(new Runnable() {
        @Override
        public void run() {
          pager.setCurrentItem(count);
        }
      });
      instrumentation.waitForIdleSync();
      Spoon.screenshot(activity, tag_prefix + count);
    }
  }

  public void testDeviceIsSaved() throws Exception {
    Spoon.screenshot(activity, "original_default_device");
    final int device = new Random().nextInt(pager.getAdapter().getCount());
    instrumentation.runOnMainSync(new Runnable() {
      @Override
      public void run() {
        pager.setCurrentItem(device);
      }
    });
    //solo.clickOnActionBarItem(R.id.menu_default_device);
    activity.finish();
    activity = getActivity();
    //assertThat(pager).hasCurrentItem(device);
    Spoon.screenshot(activity, "updated_default_device");
  }

  public void testAboutDialogIsShown() throws Exception {
    Spoon.screenshot(activity, "initial_state");
    //solo.clickOnActionBarItem(R.id.menu_about);
    Spoon.screenshot(activity, "about_dialog");
  }
}

@@ -674,24 +674,63 @@
 .ViewPager;%0A
+import com.f2prateek.dfg.model.Device;%0A
 import com.f
@@ -760,24 +760,24 @@
 ceProvider;%0A
-
 import com.f
@@ -839,16 +839,45 @@
 .Spoon;%0A
+import java.util.Comparator;%0A
 import j
@@ -951,16 +951,77 @@
 ertThat;
+%0Aimport static org.fest.assertions.api.Assertions.assertThat;
 %0A%0A/** Te
@@ -1312,24 +1312,24 @@
 er.setUp();%0A
-
     pager = 
@@ -1372,32 +1372,449 @@
 id.pager);%0A  %7D%0A%0A
+  // TODO : move to unit test%0A  public void testUniqueIds() throws Exception %7B%0A    assertThat(DeviceProvider.getDevices()).hasSize(12); // TODO : required?%0A    assertThat(DeviceProvider.getDevices()).usingElementComparator(new Comparator%3CDevice%3E() %7B%0A      @Override public int compare(Device lhs, Device rhs) %7B%0A        return lhs.getId().compareToIgnoreCase(rhs.getId());%0A      %7D%0A    %7D).doesNotHaveDuplicates();%0A  %7D%0A%0A
   public void te

Add some tests to assert uniqueness