/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * 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 org.uribeacon.scan.compat;

import static android.content.Context.ALARM_SERVICE;
import static android.content.Context.BLUETOOTH_SERVICE;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;

import android.app.AlarmManager;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.test.AndroidTestCase;

import org.uribeacon.scan.compat.BluetoothLeScannerCompat;
import org.uribeacon.scan.compat.BluetoothLeScannerCompatProvider;
import org.uribeacon.scan.compat.JbBluetoothLeScannerCompat;

/**
 * Unit tests for the {@link org.uribeacon.scan.compat.BluetoothLeScannerCompatProvider} singleton provider.
 */
public class BluetoothLeScannerCompatProviderTest extends AndroidTestCase {

  private Context context;
  private BluetoothManager bluetoothManager;
  private AlarmManager alarmManager;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    bluetoothManager = (BluetoothManager) getContext().getSystemService(BLUETOOTH_SERVICE);
    alarmManager = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
    context = new ContextWrapper(getContext()) {
      @Override
      public Object getSystemService(String name) {
        return BLUETOOTH_SERVICE.equals(name) ? bluetoothManager
            : ALARM_SERVICE.equals(name) ? alarmManager
            : super.getSystemService(name);
      }
    };
  }

  /**
   * Make sure that calling the static getter more than once returns the same scanner instance.
   */
  public void testSingleton() {
    BluetoothLeScannerCompat scanner1 =
        BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(context);
    assertNotNull(scanner1);
    BluetoothLeScannerCompat scanner2 =
        BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(context);
    assertTrue(scanner1 == scanner2);  // Test reference equivalence with '=='
  }

  /**
   * Check "L" support.
   * <P>
   * This test will not run if the LBluetoothLeScannerCompat class isn't linked.
   */
  public void testAdvancedAndroidVersion() {
    String lCompat =
        "LBluetoothLeScannerCompat";

    try {
      Class.forName(lCompat);
    } catch (ClassNotFoundException e) {
      // Class not linked; this test cannot run.
      return;
    }

    assertEquals(lCompat, BluetoothLeScannerCompatProvider
            .getBluetoothLeScannerCompat(context, true).getClass());
  }

  /**
   * Make sure we don't try to instantiate the scanner if either manager (bt or alarms) is missing.
   */
  public void testManagersRequired() {
    final boolean canUseNativeApi = true;

    bluetoothManager = null;
    alarmManager = null;
    assertNull(BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(
        context, canUseNativeApi));

    bluetoothManager = (BluetoothManager) getContext().getSystemService(BLUETOOTH_SERVICE);
    alarmManager = null;
    assertNull(BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(
        context, canUseNativeApi));

    bluetoothManager = null;
    alarmManager = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
    assertNull(BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(
        context, canUseNativeApi));

    bluetoothManager = (BluetoothManager) getContext().getSystemService(BLUETOOTH_SERVICE);
    alarmManager = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
    assertNotNull(BluetoothLeScannerCompatProvider.getBluetoothLeScannerCompat(
        context, canUseNativeApi));
  }
}

@@ -962,24 +962,49 @@
 extWrapper;%0A
+import android.os.Build;%0A
 import andro
@@ -3426,32 +3426,253 @@
 seNativeApi));%0A%0A
+    // The alarm manager is not mandatory for creating a bluetooth scanner on%0A    // LOLLIPOP (at least not if the right hardware features are supported.)%0A    if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.LOLLIPOP) %7B%0A  
     bluetoothMan
@@ -3739,32 +3739,34 @@
 TOOTH_SERVICE);%0A
+  
     alarmManager
@@ -3770,32 +3770,34 @@
 ger = null;%0A    
+  
 assertNull(Bluet
@@ -3853,32 +3853,34 @@
 Compat(%0A        
+ 
+ 
 context, canUseN
@@ -3882,32 +3882,38 @@
 nUseNativeApi));
+%0A    %7D
 %0A%0A    bluetoothM

Fix a unittest on LOLLIPOP.