@SmallTest
  public void testOnRequestPurchaseResponse() throws Exception {
    final String testItemId = TransactionTest.TRANSACTION_1.productId;
    final ResponseCode testResponse = ResponseCode.RESULT_OK;
    final Set<Boolean> flags = new HashSet<Boolean>();
    final IBillingObserver observer =
        new IBillingObserver() {

          public void onTransactionsRestored() {}

          public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {}

          public void onBillingChecked(boolean supported) {}

          public void onRequestPurchaseResponse(String itemId, ResponseCode response) {
            flags.add(true);
            assertEquals(testItemId, itemId);
            assertEquals(testResponse, response);
          }

          @Override
          public void onPurchaseStateChanged(String itemId, PurchaseState state) {}
        };
    BillingController.registerObserver(observer);
    BillingController.onRequestPurchaseResponse(testItemId, testResponse);
    assertEquals(flags.size(), 1);
    BillingController.unregisterObserver(observer);
  }
 @MediumTest
 public void testGetTransactions() throws Exception {
   final List<Transaction> transactions0 = BillingController.getTransactions(getContext());
   assertEquals(transactions0.size(), 0);
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1);
   final List<Transaction> transactions1 = BillingController.getTransactions(getContext());
   assertEquals(transactions1.size(), 1);
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2_REFUNDED);
   final List<Transaction> transactions2 = BillingController.getTransactions(getContext());
   assertEquals(transactions2.size(), 2);
 }
 @MediumTest
 public void testIsPurchased() throws Exception {
   assertFalse(
       BillingController.isPurchased(getContext(), TransactionTest.TRANSACTION_1.productId));
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1);
   assertTrue(
       BillingController.isPurchased(getContext(), TransactionTest.TRANSACTION_1.productId));
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1_REFUNDED);
   assertTrue(
       BillingController.isPurchased(getContext(), TransactionTest.TRANSACTION_1.productId));
 }
 private void runRequest(BillingRequest request) {
   try {
     final long requestId = request.run(mService);
     BillingController.onRequestSent(requestId, request);
   } catch (RemoteException e) {
     Log.w(this.getClass().getSimpleName(), "Remote billing service crashed");
     // TODO: Retry?
   }
 }
  @SmallTest
  public void testOnTransactionRestored() throws Exception {
    final Set<Boolean> flags = new HashSet<Boolean>();
    final IBillingObserver observer =
        new IBillingObserver() {
          public void onTransactionsRestored() {
            flags.add(true);
          }

          public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) {}

          public void onBillingChecked(boolean supported) {}

          public void onRequestPurchaseResponse(String itemId, ResponseCode response) {}

          public void onPurchaseStateChanged(String itemId, PurchaseState state) {}
        };
    BillingController.registerObserver(observer);
    BillingController.onTransactionsRestored();
    assertEquals(flags.size(), 1);
    BillingController.unregisterObserver(observer);
  }
 @MediumTest
 public void testCountPurchases() throws Exception {
   assertEquals(
       BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 0);
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1);
   assertEquals(
       BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_1_REFUNDED);
   assertEquals(
       BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
   BillingController.storeTransaction(getContext(), TransactionTest.TRANSACTION_2);
   assertEquals(
       BillingController.countPurchases(getContext(), TransactionTest.TRANSACTION_1.productId), 1);
 }
  @Override
  public void onCreate() {
    ACRA.init(this);

    App.init(this);

    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    CalculatorPreferences.setDefaultValues(preferences);

    preferences.registerOnSharedPreferenceChangeListener(this);

    setTheme(preferences);

    super.onCreate();

    final AndroidCalculator calculator = new AndroidCalculator(this);

    final EditorTextProcessor editorTextProcessor = new EditorTextProcessor();

    Locator.getInstance()
        .init(
            calculator,
            new AndroidCalculatorEngine(this),
            new AndroidCalculatorClipboard(this),
            new AndroidCalculatorNotifier(this),
            new AndroidCalculatorHistory(this, calculator),
            new AndroidCalculatorLogger(),
            new AndroidCalculatorPreferenceService(this),
            new AndroidCalculatorKeyboard(this, new CalculatorKeyboardImpl(calculator)),
            new AndroidCalculatorPlotter(this, new CalculatorPlotterImpl(calculator)),
            editorTextProcessor);

    editorTextProcessor.init(this);

    listeners.add(new CalculatorActivityLauncher());
    for (CalculatorEventListener listener : listeners) {
      calculator.addCalculatorEventListener(listener);
    }

    calculator.addCalculatorEventListener(broadcaster);

    Locator.getInstance().getCalculator().init();

    BillingDB.init(CalculatorApplication.this);

    if (withAds) {
      AdsController.getInstance()
          .init(
              this,
              ADMOB_USER_ID,
              AD_FREE_PRODUCT_ID,
              new BillingController.IConfiguration() {

                @Override
                public byte[] getObfuscationSalt() {
                  return new byte[] {
                    81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7,
                    -11, 53, 126, 82
                  };
                }

                @Override
                public String getPublicKey() {
                  return CalculatorSecurity.getPK();
                }
              });
    }

    BillingController.registerObserver(new DefaultBillingObserver(this, null));

    // init billing controller
    new Thread(
            new Runnable() {
              @Override
              public void run() {
                BillingController.checkBillingSupported(CalculatorApplication.this);
                AdsController.getInstance().isAdFree(CalculatorApplication.this);

                try {
                  // prepare engine
                  Locator.getInstance().getEngine().getMathEngine0().evaluate("1+1");
                  Locator.getInstance().getEngine().getMathEngine0().evaluate("1*1");
                } catch (Throwable e) {
                  Log.e(TAG, e.getMessage(), e);
                }
              }
            })
        .start();

    Locator.getInstance().getLogger().debug(TAG, "Application started!");
    Locator.getInstance().getNotifier().showDebugMessage(TAG, "Application started!");
  }
 @SmallTest
 public void testCheckBillingSupported() throws Exception {
   BillingController.checkBillingSupported(getContext());
 }