Commit 8f0e2b8a authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Add ability to start shell / browsertests in incognito mode

For testing that translate works in incognito mode I'll need a
browsertest where the Profile is actually *in* incognito mode. This CL
adds the ability to start the WebLayer shell and browsertests in
incognito.

There was a TODO that mentioned adding an incognito Profile to the
regular Profile in WebLayer Shell, but this CL instead takes a more
straightforward approach of just having the existing Profile be created
in incognito mode if desired. That is sufficient for the upcoming use
case.

Bug: 1072334
Change-Id: Ib56c1177e2e6f6c953f6bb985d532961a2e01245
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2198970
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770037}
parent 70f56a1e
......@@ -127,6 +127,8 @@ class BrowserTestBase : public testing::Test {
return embedded_test_server_.get();
}
bool set_up_called() { return set_up_called_; }
#if defined(OS_POSIX)
// This is only needed by a test that raises SIGTERM to ensure that a specific
// codepath is taken.
......
......@@ -14,6 +14,7 @@ import android.widget.RelativeLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import org.chromium.base.CommandLine;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.native_test.NativeBrowserTest;
......@@ -76,7 +77,10 @@ public class WebLayerBrowserTestsActivity extends NativeBrowserTestActivity {
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Fragment fragment = WebLayer.createBrowserFragment("BrowserTestProfile");
CommandLine commandLine = CommandLine.getInstance();
String path = (commandLine.hasSwitch("start-in-incognito")) ? null : "BrowserTestProfile";
Fragment fragment = WebLayer.createBrowserFragment(path);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(viewId, fragment);
......
......@@ -32,6 +32,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import org.chromium.base.CommandLine;
import org.chromium.base.IntentUtils;
import org.chromium.weblayer.Browser;
import org.chromium.weblayer.ContextMenuParams;
......@@ -58,7 +59,7 @@ import java.util.List;
* Activity for managing the Demo Shell.
*/
public class WebLayerShellActivity extends FragmentActivity {
private static final String PROFILE_NAME = "DefaultProfile";
private static final String NON_INCOGNITO_PROFILE_NAME = "DefaultProfile";
private static class ContextMenuCreator
implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
......@@ -133,6 +134,7 @@ public class WebLayerShellActivity extends FragmentActivity {
private List<Tab> mPreviousTabList = new ArrayList<>();
private Runnable mExitFullscreenRunnable;
private View mBottomView;
private boolean mInIncognitoMode;
@Override
protected void onCreate(final Bundle savedInstanceState) {
......@@ -205,8 +207,11 @@ public class WebLayerShellActivity extends FragmentActivity {
}
if (item.getItemId() == R.id.site_settings_menu_id) {
Intent intent =
SiteSettingsActivity.createIntentForCategoryList(this, PROFILE_NAME);
// TODO(crbug.com/1083233): Figure out the right long-term behavior here.
if (mInIncognitoMode) return true;
Intent intent = SiteSettingsActivity.createIntentForCategoryList(
this, NON_INCOGNITO_PROFILE_NAME);
IntentUtils.safeStartActivity(this, intent);
return true;
}
......@@ -427,7 +432,14 @@ public class WebLayerShellActivity extends FragmentActivity {
}
}
Fragment fragment = WebLayer.createBrowserFragment(PROFILE_NAME);
if (CommandLine.isInitialized()
&& CommandLine.getInstance().hasSwitch("start-in-incognito")) {
mInIncognitoMode = true;
}
String profileName = mInIncognitoMode ? null : NON_INCOGNITO_PROFILE_NAME;
Fragment fragment = WebLayer.createBrowserFragment(profileName);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(mMainViewId, fragment);
......
......@@ -77,9 +77,11 @@ class MainDelegateImpl : public MainDelegate {
private:
#if !defined(OS_ANDROID)
void InitializeProfile() {
profile_ = Profile::Create("web_shell");
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string profile_name =
command_line->HasSwitch(switches::kStartInIncognito) ? "" : "web_shell";
// TODO: create an incognito profile as well.
profile_ = Profile::Create(profile_name);
}
void DestroyProfile() { profile_.reset(); }
......
......@@ -10,5 +10,8 @@ namespace switches {
// Stops new Shell objects from navigating to a default url.
const char kNoInitialNavigation[] = "no-initial-navigation";
// Starts the shell with the profile in incognito mode.
const char kStartInIncognito[] = "start-in-incognito";
} // namespace switches
} // namespace weblayer
......@@ -10,6 +10,8 @@ namespace switches {
extern const char kNoInitialNavigation[];
extern const char kStartInIncognito[];
} // namespace switches
} // namespace weblayer
......
......@@ -29,6 +29,10 @@ WebLayerBrowserTest::~WebLayerBrowserTest() = default;
void WebLayerBrowserTest::SetUp() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kNoInitialNavigation);
if (start_in_incognito_mode_)
command_line->AppendSwitch(switches::kStartInIncognito);
SetUpCommandLine(command_line);
content::BrowserTestBase::SetUp();
}
......@@ -51,6 +55,11 @@ void WebLayerBrowserTest::PostRunTestOnMainThread() {
Shell::CloseAllWindows();
}
void WebLayerBrowserTest::SetShellStartsInIncognitoMode() {
DCHECK(!set_up_called());
start_in_incognito_mode_ = true;
}
ProfileImpl* WebLayerBrowserTest::GetProfile() {
return static_cast<TabImpl*>(shell_->tab())->profile();
}
......
......@@ -24,6 +24,10 @@ class WebLayerBrowserTest : public content::BrowserTestBase {
void PreRunTestOnMainThread() override;
void PostRunTestOnMainThread() override;
// Configures this object such that when it starts the shell it does so in
// incognito mode. Must be invoked before SetUp() has been called.
void SetShellStartsInIncognitoMode();
// Returns the window for the test.
Shell* shell() const { return shell_; }
......@@ -31,6 +35,7 @@ class WebLayerBrowserTest : public content::BrowserTestBase {
private:
Shell* shell_ = nullptr;
bool start_in_incognito_mode_ = false;
base::test::ScopedFeatureList feature_list_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment