Home | History | Annotate | Download | only in managedprovisioning
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.managedprovisioning;
     18 
     19 import android.accounts.Account;
     20 import android.content.ComponentName;
     21 import android.content.Intent;
     22 import android.os.BaseBundle;
     23 import android.os.Bundle;
     24 import android.os.PersistableBundle;
     25 import android.test.AndroidTestCase;
     26 import android.test.suitebuilder.annotation.SmallTest;
     27 
     28 import java.util.Set;
     29 
     30 public class IntentStoreTest extends AndroidTestCase {
     31 
     32     private static final String SAMPLE_INTENT_STORE_NAME = "sample_intent_store_name";
     33 
     34     private static final ComponentName SAMPLE_COMPONENT_NAME
     35             = new ComponentName("package.sample", "class.sample");
     36 
     37     @Override
     38     public void tearDown() {
     39         createIntentStore().clear();
     40     }
     41 
     42     @SmallTest
     43     public void testAction() throws Exception {
     44         testIntentCanBeRecovered(new Intent("action.test"));
     45     }
     46 
     47     @SmallTest
     48     public void testComponent() throws Exception {
     49         testIntentCanBeRecovered(new Intent().setComponent(SAMPLE_COMPONENT_NAME));
     50     }
     51 
     52     @SmallTest
     53     public void testExtraStrings() throws Exception {
     54         testIntentCanBeRecovered(new Intent().putExtra("a", "b").putExtra("c", "d"));
     55     }
     56 
     57     @SmallTest
     58     public void testExtraInt() throws Exception {
     59         testIntentCanBeRecovered(new Intent().putExtra("a", 42));
     60     }
     61 
     62     @SmallTest
     63     public void testExtraLong() throws Exception {
     64         testIntentCanBeRecovered(new Intent().putExtra("a", 12345678910L));
     65     }
     66 
     67     @SmallTest
     68     public void testExtraBoolean() throws Exception {
     69         testIntentCanBeRecovered(new Intent().putExtra("a", true));
     70     }
     71 
     72     @SmallTest
     73     public void testExtraComponentName() throws Exception {
     74         testIntentCanBeRecovered(new Intent().putExtra("a", SAMPLE_COMPONENT_NAME));
     75     }
     76 
     77     @SmallTest
     78     public void testExtraAccount() throws Exception {
     79         testIntentCanBeRecovered(new Intent().putExtra("a",
     80                 new Account("sample.type", "sample.name")));
     81     }
     82 
     83     @SmallTest
     84     public void testPersistableBundle() throws Exception {
     85         PersistableBundle pBundle = new PersistableBundle();
     86         pBundle.putString("a", "b");
     87         pBundle.putBoolean("c", false);
     88         Intent firstIntent = new Intent().putExtra("a", pBundle);
     89         // Utils.intentEquals() method doesn't support embedded Bundle, so we can't use it.
     90         IntentStore intentStore = createIntentStore();
     91         intentStore.save(firstIntent);
     92         Intent newIntent = intentStore.load();
     93         if (!TestUtils.bundleEquals(pBundle, (PersistableBundle) newIntent.getExtra("a"))) {
     94             TestUtils.failIntentsNotEqual(firstIntent, newIntent);
     95         }
     96     }
     97 
     98     /**
     99      * Tests that the intent can be saved, recovered,
    100      * and that the recovered intent is equal to the original one.
    101      */
    102     private void testIntentCanBeRecovered(Intent intent) throws Exception {
    103         createIntentStore().save(intent);
    104         // Use a different intent store to make sure that the intent store doesn't just save the
    105         // intent in memmory.
    106         IntentStore loadingIntentStore = createIntentStore();
    107         TestUtils.assertIntentEquals(intent, loadingIntentStore.load());
    108     }
    109 
    110     private IntentStore createIntentStore() {
    111         return new IntentStore(getContext(), SAMPLE_INTENT_STORE_NAME);
    112     }
    113 }
    114