Home | History | Annotate | Download | only in browseractions
      1 /*
      2  * Copyright 2018 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.assertNotNull;
     21 
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.net.Uri;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.rule.ActivityTestRule;
     27 import android.support.test.runner.AndroidJUnit4;
     28 import android.view.View;
     29 import android.widget.ListView;
     30 import android.widget.TextView;
     31 
     32 import androidx.browser.R;
     33 import androidx.browser.customtabs.TestActivity;
     34 
     35 import org.junit.Before;
     36 import org.junit.Rule;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 
     40 import java.util.ArrayList;
     41 import java.util.List;
     42 import java.util.concurrent.CountDownLatch;
     43 import java.util.concurrent.TimeUnit;
     44 
     45 /** Unit tests for {@link BrowserActionsFallbackMenuUi}. */
     46 @RunWith(AndroidJUnit4.class)
     47 @SmallTest
     48 public class BrowserActionsFallbackMenuUiTest {
     49     private static final String TEST_URL = "http://www.example.com";
     50     private static final String CUSTOM_ITEM_TITLE_1 = "Open url";
     51     private static final String CUSTOM_ITEM_TITLE_2 = "Share url";
     52     @Rule
     53     public final ActivityTestRule<TestActivity> mActivityTestRule =
     54             new ActivityTestRule<>(TestActivity.class);
     55     private Context mContext;
     56     private List<BrowserActionItem> mMenuItems;
     57 
     58     @Before
     59     public void setup() {
     60         mContext = mActivityTestRule.getActivity();
     61         mMenuItems = createMenuItems();
     62     }
     63 
     64     private List<BrowserActionItem> createMenuItems() {
     65         List<BrowserActionItem> menuItems = new ArrayList<>();
     66         BrowserActionItem menuItem1 = new BrowserActionItem(
     67                 CUSTOM_ITEM_TITLE_1, BrowserActionsIntentTest.createCustomItemAction(TEST_URL));
     68         BrowserActionItem menuItem2 = new BrowserActionItem(
     69                 CUSTOM_ITEM_TITLE_2, BrowserActionsIntentTest.createCustomItemAction(TEST_URL));
     70         menuItems.add(menuItem1);
     71         menuItems.add(menuItem2);
     72         return menuItems;
     73     }
     74 
     75     /**
     76      * Test whether {@link BrowserActionsFallbackMenuDialog} is opened if not provider is available.
     77      */
     78     @Test
     79     public void testBrowserActionsFallbackDialogOpened() throws InterruptedException {
     80         final CountDownLatch signal = new CountDownLatch(1);
     81         final BrowserActionsIntent.BrowserActionsFallDialogListener listener =
     82                 new BrowserActionsIntent.BrowserActionsFallDialogListener() {
     83                     @Override
     84                     public void onDialogShown() {
     85                         signal.countDown();
     86                     }
     87                 };
     88         BrowserActionsIntent.setDialogShownListenter(listener);
     89         mActivityTestRule.getActivity().runOnUiThread(new Runnable() {
     90             @Override
     91             public void run() {
     92                 BrowserActionsIntent browserActionsIntent =
     93                         new BrowserActionsIntent.Builder(mContext, Uri.parse(TEST_URL)).build();
     94                 Intent intent = browserActionsIntent.getIntent();
     95                 BrowserActionsIntent.launchIntent(mContext, intent, null);
     96             }
     97         });
     98         signal.await(5L, TimeUnit.SECONDS);
     99     }
    100 
    101     /**
    102      * Test whether {@link BrowserActionsFallbackMenuUi} is inflated correctly.
    103      */
    104     @Test
    105     public void testBrowserActionsFallbackMenuShownCorrectly() throws InterruptedException {
    106         final CountDownLatch signal = new CountDownLatch(1);
    107         final BrowserActionsFallbackMenuUi.BrowserActionsFallMenuUiListener listener =
    108                 new BrowserActionsFallbackMenuUi.BrowserActionsFallMenuUiListener() {
    109                     @Override
    110                     public void onMenuShown(View contentView) {
    111                         signal.countDown();
    112                         assertDialogInflatedCorrectly(contentView);
    113                     }
    114                 };
    115         mActivityTestRule.getActivity().runOnUiThread(new Runnable() {
    116             @Override
    117             public void run() {
    118                 BrowserActionsFallbackMenuUi menuUi =
    119                         new BrowserActionsFallbackMenuUi(mContext, Uri.parse(TEST_URL), mMenuItems);
    120                 menuUi.setMenuUiListener(listener);
    121                 menuUi.displayMenu();
    122             }
    123         });
    124         signal.await(5L, TimeUnit.SECONDS);
    125     }
    126 
    127     private void assertDialogInflatedCorrectly(View contentView) {
    128         assertNotNull(contentView);
    129         TextView urlTextView =
    130                 (TextView) contentView.findViewById(R.id.browser_actions_header_text);
    131         assertNotNull(urlTextView);
    132         assertEquals(TEST_URL, urlTextView.getText());
    133         ListView menuListView =
    134                 (ListView) contentView.findViewById(R.id.browser_actions_menu_items);
    135         assertNotNull(menuListView);
    136         assertEquals(2, menuListView.getCount());
    137         TextView menuItemTitleView1 = (TextView) menuListView.getChildAt(0).findViewById(
    138                 R.id.browser_actions_menu_item_text);
    139         assertEquals(CUSTOM_ITEM_TITLE_1, menuItemTitleView1.getText());
    140         TextView menuItemTitleView2 = (TextView) menuListView.getChildAt(1).findViewById(
    141                 R.id.browser_actions_menu_item_text);
    142         assertEquals(CUSTOM_ITEM_TITLE_2, menuItemTitleView2.getText());
    143     }
    144 }
    145