chromium/chrome/browser/web_applications/preinstalled_web_app_utils_unittest.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/web_applications/preinstalled_web_app_utils.h"

#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/web_applications/test/test_file_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/devices/device_data_manager.h"
#include "ui/events/devices/device_data_manager_test_api.h"
#include "ui/events/devices/touchscreen_device.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/components/arc/arc_util.h"
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/crosapi/mojom/crosapi.mojom.h"
#include "chromeos/startup/browser_init_params.h"
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

namespace web_app {

namespace {

ui::TouchscreenDevice CreateTouchDevice(ui::InputDeviceType type,
                                        bool stylus_support) {}

}  // namespace

class PreinstalledWebAppUtilsTest : public testing::Test {};

// ParseConfig() is also tested by PreinstalledWebAppManagerTest.

#if BUILDFLAG(IS_CHROMEOS)

namespace {

std::string BoolParamToString(
    const ::testing::TestParamInfo<bool>& bool_param) {
  return bool_param.param ? "true" : "false";
}

using IsTablet = bool;
using IsArcSupported = bool;

}  // namespace

class PreinstalledWebAppUtilsTabletTest
    : public PreinstalledWebAppUtilsTest,
      public ::testing::WithParamInterface<IsTablet> {
 public:
  PreinstalledWebAppUtilsTabletTest() {
    if (GetParam()) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
      base::CommandLine::ForCurrentProcess()->AppendSwitch(
          ash::switches::kEnableTabletFormFactor);
#else
      auto init_params = crosapi::mojom::BrowserInitParams::New();
      init_params->device_properties = crosapi::mojom::DeviceProperties::New();
      init_params->device_properties->is_tablet_form_factor = true;
      chromeos::BrowserInitParams::SetInitParamsForTests(
          std::move(init_params));
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
    }
  }
  ~PreinstalledWebAppUtilsTabletTest() override = default;

  bool is_tablet() const { return GetParam(); }
};

TEST_P(PreinstalledWebAppUtilsTabletTest, DisableIfTabletFormFactor) {
  std::optional<ExternalInstallOptions> disable_true_options = ParseConfig(R"(
    {
      "app_url": "https://test.org",
      "launch_container": "window",
      "disable_if_tablet_form_factor": true,
      "user_type": ["test"]
    }
  )");
  EXPECT_TRUE(disable_true_options->disable_if_tablet_form_factor);

  std::optional<ExternalInstallOptions> disable_false_options = ParseConfig(R"(
    {
      "app_url": "https://test.org",
      "launch_container": "window",
      "disable_if_tablet_form_factor": false,
      "user_type": ["test"]
    }
  )");
  EXPECT_FALSE(disable_false_options->disable_if_tablet_form_factor);
}

INSTANTIATE_TEST_SUITE_P(All,
                         PreinstalledWebAppUtilsTabletTest,
                         ::testing::Values(true, false),
                         BoolParamToString);

class PreinstalledWebAppUtilsArcTest
    : public PreinstalledWebAppUtilsTest,
      public ::testing::WithParamInterface<IsArcSupported> {
 public:
  PreinstalledWebAppUtilsArcTest() {
    if (GetParam()) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
      base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
          ash::switches::kArcAvailability, "officially-supported");
#else
      auto init_params = crosapi::mojom::BrowserInitParams::New();
      init_params->device_properties = crosapi::mojom::DeviceProperties::New();
      init_params->device_properties->is_arc_available = true;
      chromeos::BrowserInitParams::SetInitParamsForTests(
          std::move(init_params));
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
    }
  }
  ~PreinstalledWebAppUtilsArcTest() override = default;

  bool is_arc_supported() const { return GetParam(); }
};

TEST_P(PreinstalledWebAppUtilsArcTest, DisableIfArcSupported) {
  std::optional<ExternalInstallOptions> disable_true_options = ParseConfig(R"(
    {
      "app_url": "https://test.org",
      "launch_container": "window",
      "disable_if_arc_supported": true,
      "user_type": ["test"]
    }
  )");
  EXPECT_TRUE(disable_true_options->disable_if_arc_supported);

  std::optional<ExternalInstallOptions> disable_false_options = ParseConfig(R"(
    {
      "app_url": "https://test.org",
      "launch_container": "window",
      "disable_if_arc_supported": false,
      "user_type": ["test"]
    }
  )");
  EXPECT_FALSE(disable_false_options->disable_if_arc_supported);
}

INSTANTIATE_TEST_SUITE_P(All,
                         PreinstalledWebAppUtilsArcTest,
                         ::testing::Values(true, false),
                         BoolParamToString);

#endif  // BUILDFLAG(IS_CHROMEOS)

// TODO(crbug.com/40145619): Loading icon.png is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_OfflineManifestValid
#else
#define MAYBE_OfflineManifestValid
#endif
TEST_F(PreinstalledWebAppUtilsTest, MAYBE_OfflineManifestValid) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestName) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestStartUrl) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestScope) {}

// TODO(crbug.com/40145619): Loading icon.png is flaky on Windows.
#if BUILDFLAG(IS_WIN)
#define MAYBE_OfflineManifestDisplay
#else
#define MAYBE_OfflineManifestDisplay
#endif
TEST_F(PreinstalledWebAppUtilsTest, MAYBE_OfflineManifestDisplay) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestIconAnyPngs) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestIconMaskablePngs) {}

TEST_F(PreinstalledWebAppUtilsTest, OfflineManifestThemeColorArgbHex) {}

TEST_F(PreinstalledWebAppUtilsTest, ForceReinstallForMilestone) {}

TEST_F(PreinstalledWebAppUtilsTest, IsReinstallPastMilestoneNeeded) {}

TEST_F(PreinstalledWebAppUtilsTest, OemInstalled) {}

TEST_F(PreinstalledWebAppUtilsTest,
       DisableIfTouchscreenWithStylusNotSupported) {}

TEST_F(PreinstalledWebAppUtilsTest, GateOnFeatureNameOrInstalled) {}

class PreinstalledWebAppUtilsDeviceManagerTest
    : public PreinstalledWebAppUtilsTest {};

TEST_F(PreinstalledWebAppUtilsDeviceManagerTest,
       HasStylusEnabledTouchscreen_Uninitialized) {}

TEST_F(PreinstalledWebAppUtilsDeviceManagerTest,
       HasStylusEnabledTouchscreen_NoTouchscreen) {}

TEST_F(PreinstalledWebAppUtilsDeviceManagerTest,
       HasStylusEnabledTouchscreen_NonStylusTouchscreen) {}

TEST_F(PreinstalledWebAppUtilsDeviceManagerTest,
       HasStylusEnabledTouchscreen_ExternalStylusTouchscreen) {}

TEST_F(PreinstalledWebAppUtilsDeviceManagerTest,
       HasStylusEnabledTouchscreen_InternalStylusTouchscreen) {}

}  // namespace web_app