Home | History | Annotate | Download | only in browseractions
      1 /*
      2  * Copyright 2017 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 androidx.browser.browseractions;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.app.PendingIntent;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.net.Uri;
     28 import android.os.Bundle;
     29 import android.support.test.InstrumentationRegistry;
     30 import android.support.test.filters.SmallTest;
     31 import android.support.test.runner.AndroidJUnit4;
     32 
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 import java.util.ArrayList;
     37 import java.util.List;
     38 
     39 /** Unit tests for {@link BrowserActionsIntent}. */
     40 @RunWith(AndroidJUnit4.class)
     41 @SmallTest
     42 public final class BrowserActionsIntentTest {
     43     private static final String TEST_URL = "http://www.example.com";
     44     private static final String CUSTOM_ITEM_TITLE = "Share url";
     45     private Uri mUri = Uri.parse(TEST_URL);
     46     private Context mContext = InstrumentationRegistry.getTargetContext();
     47 
     48     /**
     49      * Test whether default {@link BrowserActionsIntent} is populated correctly.
     50      */
     51     @Test
     52     public void testDefaultBrowserActionsIntent() {
     53         BrowserActionsIntent browserActionsIntent =
     54                 new BrowserActionsIntent.Builder(mContext, mUri).build();
     55         Intent intent = browserActionsIntent.getIntent();
     56         assertNotNull(intent);
     57 
     58         assertEquals(BrowserActionsIntent.ACTION_BROWSER_ACTIONS_OPEN, intent.getAction());
     59         assertEquals(mUri, intent.getData());
     60         assertTrue(intent.hasExtra(BrowserActionsIntent.EXTRA_TYPE));
     61         assertEquals(BrowserActionsIntent.URL_TYPE_NONE,
     62                 intent.getIntExtra(BrowserActionsIntent.EXTRA_TYPE, 0));
     63         assertTrue(intent.hasExtra(BrowserActionsIntent.EXTRA_APP_ID));
     64         assertEquals(mContext.getPackageName(), BrowserActionsIntent.getCreatorPackageName(intent));
     65         assertFalse(intent.hasExtra(BrowserActionsIntent.EXTRA_SELECTED_ACTION_PENDING_INTENT));
     66     }
     67 
     68     @Test
     69     /**
     70      * Test whether custom items are set correctly.
     71      */
     72     public void testCustomItem() {
     73         PendingIntent action1 = createCustomItemAction(TEST_URL);
     74         BrowserActionItem customItemWithoutIcon = new BrowserActionItem(CUSTOM_ITEM_TITLE, action1);
     75         PendingIntent action2 = createCustomItemAction(TEST_URL);
     76         BrowserActionItem customItemWithIcon =
     77                 new BrowserActionItem(CUSTOM_ITEM_TITLE, action2, android.R.drawable.ic_menu_share);
     78         ArrayList<BrowserActionItem> customItems = new ArrayList<>();
     79         customItems.add(customItemWithIcon);
     80         customItems.add(customItemWithoutIcon);
     81 
     82         BrowserActionsIntent browserActionsIntent = new BrowserActionsIntent.Builder(mContext, mUri)
     83                 .setCustomItems(customItems)
     84                 .build();
     85         Intent intent = browserActionsIntent.getIntent();
     86         assertTrue(intent.hasExtra(BrowserActionsIntent.EXTRA_MENU_ITEMS));
     87         ArrayList<Bundle> bundles =
     88                 intent.getParcelableArrayListExtra(BrowserActionsIntent.EXTRA_MENU_ITEMS);
     89         assertNotNull(bundles);
     90         List<BrowserActionItem> items = BrowserActionsIntent.parseBrowserActionItems(bundles);
     91         assertEquals(2, items.size());
     92         BrowserActionItem items1 = items.get(0);
     93         assertEquals(CUSTOM_ITEM_TITLE, items1.getTitle());
     94         assertEquals(android.R.drawable.ic_menu_share, items1.getIconId());
     95         assertEquals(action1, items1.getAction());
     96         BrowserActionItem items2 = items.get(1);
     97         assertEquals(CUSTOM_ITEM_TITLE, items2.getTitle());
     98         assertEquals(0, items2.getIconId());
     99         assertEquals(action2, items2.getAction());
    100     }
    101 
    102     static PendingIntent createCustomItemAction(String url) {
    103         Context context = InstrumentationRegistry.getTargetContext();
    104         Intent customIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    105         return PendingIntent.getActivity(context, 0, customIntent, 0);
    106     }
    107 }
    108