/** * Tests that a WebappActivity can be brought forward by firing an Intent with * TabOpenType.BRING_TAB_TO_FRONT. */ @MediumTest @Feature({"Webapps"}) public void testBringTabToFront() throws Exception { // Start the WebappActivity. final WebappActivity firstActivity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); final int webappTabId = firstActivity.getActivityTab().getId(); // Return home. final Context context = getInstrumentation().getTargetContext(); ApplicationTestUtils.fireHomeScreenIntent(context); getInstrumentation().waitForIdleSync(); // Bring the WebappActivity back via an Intent. Intent intent = Tab.createBringTabToFrontIntent(webappTabId); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); // When Chrome is back in the foreground, confirm that the correct Activity was restored. // Because of Android killing Activities willy-nilly, it may not be the same Activity, but // it should have the same Tab ID. getInstrumentation().waitForIdleSync(); ApplicationTestUtils.waitUntilChromeInForeground(); CriteriaHelper.pollInstrumentationThread( new Criteria() { @Override public boolean isSatisfied() { Activity lastActivity = ApplicationStatus.getLastTrackedFocusedActivity(); if (!isWebappActivityReady(lastActivity)) return false; WebappActivity webappActivity = (WebappActivity) lastActivity; return webappActivity.getActivityTab().getId() == webappTabId; } }); }
/** Returns true when the last Activity is a WebappActivity and is ready for testing . */ private boolean isWebappActivityReady(Activity lastActivity) { if (!(lastActivity instanceof WebappActivity)) return false; WebappActivity webappActivity = (WebappActivity) lastActivity; if (webappActivity.getActivityTab() == null) return false; View rootView = webappActivity.findViewById(android.R.id.content); if (!rootView.hasWindowFocus()) return false; return true; }
private <T extends ChromeActivity> void triggerWindowOpenAndWaitForLoad( Class<T> classToWaitFor, String linkHtml, boolean checkContents) throws Exception { final WebappActivity firstActivity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); final int firstWebappId = firstActivity.getActivityTab().getId(); // Load up the test page. new TabLoadObserver(firstActivity.getActivityTab()).fullyLoadUrl(linkHtml); // Do a plain click to make the link open in the main browser via a window.open(). // If the window is opened successfully, javascript on the first page triggers and changes // its URL as a signal for this test. Runnable fgTrigger = new Runnable() { @Override public void run() { ThreadUtils.runOnUiThreadBlocking( new Runnable() { @Override public void run() { View view = firstActivity.findViewById(android.R.id.content).getRootView(); TouchCommon.singleClickView(view); } }); } }; ChromeActivity secondActivity = ActivityUtils.waitForActivity(getInstrumentation(), classToWaitFor, fgTrigger); waitForFullLoad(secondActivity, "Page 4"); if (checkContents) { assertEquals( "New WebContents was not created", SUCCESS_URL, firstActivity.getActivityTab().getUrl()); } assertNotSame( "Wrong Activity in foreground", firstActivity, ApplicationStatus.getLastTrackedFocusedActivity()); // Close the child window to kick the user back to the WebappActivity. JavaScriptUtils.executeJavaScript( secondActivity.getActivityTab().getWebContents(), "window.close()"); CriteriaHelper.pollUiThread( new Criteria() { @Override public boolean isSatisfied() { Activity lastActivity = ApplicationStatus.getLastTrackedFocusedActivity(); if (!isWebappActivityReady(lastActivity)) return false; WebappActivity webappActivity = (WebappActivity) lastActivity; return webappActivity.getActivityTab().getId() == firstWebappId; } }); ApplicationTestUtils.waitUntilChromeInForeground(); }
/** Tests that the WebappActivity gets the next available Tab ID instead of 0. */ @MediumTest @Feature({"Webapps"}) public void testWebappTabIdsProperlyAssigned() throws Exception { SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(TabIdManager.PREF_NEXT_ID, 11684); editor.apply(); final WebappActivity webappActivity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); assertEquals("Wrong Tab ID was used", 11684, webappActivity.getActivityTab().getId()); }
/** Tests that the WebappActivity gets the next available Tab ID instead of 0. */ @MediumTest public void testWebappTabIdsProperlyAssigned() throws Exception { Context context = getInstrumentation().getTargetContext(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(TabIdManager.PREF_NEXT_ID, 11684); editor.apply(); final WebappActivity webappActivity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); assertTrue(isNumberOfRunningActivitiesCorrect(1)); assertEquals("Wrong Tab ID was used", 11684, webappActivity.getActivityTab().getId()); }
@Override public boolean isShowingTopControlsEnabled() { if (!super.isShowingTopControlsEnabled()) return false; String webappStartUrl = mActivity.getWebappInfo().uri().toString(); return shouldShowTopControls(webappStartUrl, mTab.getUrl(), mTab.getSecurityLevel()); }
@Override public void activateContents() { String startUrl = mActivity.getWebappInfo().uri().toString(); // Create an Intent that will be fired toward the WebappLauncherActivity, which in turn // will fire an Intent to launch the correct WebappActivity. On L+ this could probably // be changed to call AppTask.moveToFront(), but for backwards compatibility we relaunch // it the hard way. Intent intent = new Intent(); intent.setAction(WebappLauncherActivity.ACTION_START_WEBAPP); intent.setPackage(mActivity.getPackageName()); mActivity.getWebappInfo().setWebappIntentExtras(intent); intent.putExtra(ShortcutHelper.EXTRA_MAC, ShortcutHelper.getEncodedMac(mActivity, startUrl)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ContextUtils.getApplicationContext().startActivity(intent); }
/** * Brings a live WebappActivity back to the foreground if one exists for the given tab ID. * * @param tabId ID of the Tab to bring back to the foreground. * @return True if a live WebappActivity was found, false otherwise. */ public static boolean bringWebappToFront(int tabId) { if (tabId == Tab.INVALID_TAB_ID) return false; for (WeakReference<Activity> activityRef : ApplicationStatus.getRunningActivities()) { Activity activity = activityRef.get(); if (activity == null || !(activity instanceof WebappActivity)) continue; WebappActivity webappActivity = (WebappActivity) activity; if (webappActivity.getActivityTab() != null && webappActivity.getActivityTab().getId() == tabId) { Tab tab = webappActivity.getActivityTab(); tab.getTabWebContentsDelegateAndroid().activateContents(); return true; } } return false; }
private void runForegroundingTest(boolean viaActivateContents) throws Exception { // Start the WebappActivity. final WebappActivity activity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); assertTrue(isNumberOfRunningActivitiesCorrect(1)); // Return home. final Context context = getInstrumentation().getTargetContext(); ApplicationTestUtils.fireHomeScreenIntent(context); getInstrumentation().waitForIdleSync(); if (viaActivateContents) { // Bring it back via the Tab. activity.getActivityTab().getTabWebContentsDelegateAndroid().activateContents(); } else { // Bring the WebappActivity back via an Intent. int webappTabId = activity.getActivityTab().getId(); Intent intent = Tab.createBringTabToFrontIntent(webappTabId); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } // When Chrome is back in the foreground, confirm that the original Activity was restored. getInstrumentation().waitForIdleSync(); ApplicationTestUtils.waitUntilChromeInForeground(); assertTrue( CriteriaHelper.pollForCriteria( new Criteria() { @Override public boolean isSatisfied() { return activity == ApplicationStatus.getLastTrackedFocusedActivity() && activity.hasWindowFocus(); } })); assertTrue(isNumberOfRunningActivitiesCorrect(1)); }
/** Tests that WebappActivities are started properly. */ @MediumTest @Feature({"Webapps"}) public void testWebappLaunches() throws Exception { final WebappActivity firstActivity = startWebappActivity(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON); final int firstTabId = firstActivity.getActivityTab().getId(); // Firing a different Intent should start a new WebappActivity instance. fireWebappIntent(WEBAPP_2_ID, WEBAPP_2_URL, WEBAPP_2_TITLE, WEBAPP_ICON, true); CriteriaHelper.pollUiThread( new Criteria() { @Override public boolean isSatisfied() { Activity lastActivity = ApplicationStatus.getLastTrackedFocusedActivity(); if (!isWebappActivityReady(lastActivity)) return false; WebappActivity lastWebappActivity = (WebappActivity) lastActivity; return lastWebappActivity.getActivityTab().getId() != firstTabId; } }); // Firing the first Intent should bring back the first WebappActivity instance, or at least // a WebappActivity with the same tab if the other one was killed by Android mid-test. fireWebappIntent(WEBAPP_1_ID, WEBAPP_1_URL, WEBAPP_1_TITLE, WEBAPP_ICON, true); CriteriaHelper.pollUiThread( new Criteria() { @Override public boolean isSatisfied() { Activity lastActivity = ApplicationStatus.getLastTrackedFocusedActivity(); if (!isWebappActivityReady(lastActivity)) return false; WebappActivity lastWebappActivity = (WebappActivity) lastActivity; return lastWebappActivity.getActivityTab().getId() == firstTabId; } }); }