chromium/chrome/browser/ui/views/frame/webui_tab_strip_field_trial_browsertest.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/frame/webui_tab_strip_field_trial.h"

#include <string_view>
#include <vector>

#include "base/command_line.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/field_trial.h"
#include "base/ranges/algorithm.h"
#include "base/test/mock_entropy_provider.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/variations/active_field_trials.h"
#include "components/variations/synthetic_trials_active_group_id_provider.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace {

void RegisterFakeFieldTrialWithState(base::FeatureList* feature_list,
                                     bool enabled) {}

bool IsInGroup(std::string_view group_name) {}

}  // namespace

class WebUITabStripFieldTrialBrowserTest : public InProcessBrowserTest {};

// Set feature on by field trial, use system tablet mode capability.
class WebUITabStripFieldTrialDefaultTabletModeBrowserTest
    : public WebUITabStripFieldTrialBrowserTest {};

// Verify the synthetic trial is only enrolled if the devices is a
// tablet.
IN_PROC_BROWSER_TEST_F(WebUITabStripFieldTrialDefaultTabletModeBrowserTest,
                       GroupDependsOnTabletMode) {}

// The following tests depend on ash.
#if BUILDFLAG(IS_CHROMEOS_ASH)

// Overrides the device's tablet mode capability, forcing it to appear
// as a tablet.
class WebUITabStripFieldTrialWithTabletModeBrowserTest
    : public WebUITabStripFieldTrialBrowserTest {
 public:
  WebUITabStripFieldTrialWithTabletModeBrowserTest() {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        ash::switches::kAshEnableTabletMode);
  }
};

class WebUITabStripFieldTrialEnabledBrowserTest
    : public WebUITabStripFieldTrialWithTabletModeBrowserTest {
 public:
  WebUITabStripFieldTrialEnabledBrowserTest() {
    RegisterFakeFieldTrialWithState(feature_list(), true);
    InitFeatureList();
    EXPECT_TRUE(base::FeatureList::IsEnabled(features::kWebUITabStrip));
  }
};

// Tablets with WebUITabStrip enabled by field trial should be enrolled
// in the synthetic trial.
IN_PROC_BROWSER_TEST_F(WebUITabStripFieldTrialEnabledBrowserTest, Test) {
  EXPECT_TRUE(IsInGroup("Enabled"));
  EXPECT_FALSE(IsInGroup("Disabled"));
  EXPECT_FALSE(IsInGroup("Default"));
}

class WebUITabStripFieldTrialDisabledBrowserTest
    : public WebUITabStripFieldTrialWithTabletModeBrowserTest {
 public:
  WebUITabStripFieldTrialDisabledBrowserTest() {
    RegisterFakeFieldTrialWithState(feature_list(), false);
    InitFeatureList();
    EXPECT_FALSE(base::FeatureList::IsEnabled(features::kWebUITabStrip));
  }
};

// Tablets with WebUITabStrip disabled by field trial should be enrolled
// in the synthetic trial.
IN_PROC_BROWSER_TEST_F(WebUITabStripFieldTrialDisabledBrowserTest, Test) {
  EXPECT_FALSE(IsInGroup("Enabled"));
  EXPECT_TRUE(IsInGroup("Disabled"));
  EXPECT_FALSE(IsInGroup("Default"));
}

class WebUITabStripFieldTrialCommandLineOverrideBrowserTest
    : public WebUITabStripFieldTrialWithTabletModeBrowserTest {
 public:
  WebUITabStripFieldTrialCommandLineOverrideBrowserTest() {
    feature_list()->InitFromCommandLine("WebUITabStrip", "");
    InitFeatureList();
    EXPECT_TRUE(base::FeatureList::IsEnabled(features::kWebUITabStrip));
  }
};

// If WebUITabStrip is enabled by command line (or about:flags) the
// synthetic field trial shouldn't be registered.
IN_PROC_BROWSER_TEST_F(WebUITabStripFieldTrialCommandLineOverrideBrowserTest,
                       Test) {
  EXPECT_FALSE(IsInGroup("Enabled"));
  EXPECT_FALSE(IsInGroup("Disabled"));
  EXPECT_FALSE(IsInGroup("Default"));
}

#endif  // BUILDFLAG(IS_CHROMEOS_ASH)