chromium/chrome/browser/device_api/managed_configuration_api_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/device_api/managed_configuration_api.h"

#include <optional>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/test/gtest_tags.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/device_api/managed_configuration_api_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/test/guest_session_mixin.h"
#endif

Eq;

namespace {

constexpr char kOrigin[] =;
constexpr char kConfigurationUrl1[] =;
constexpr char kConfigurationUrl2[] =;
constexpr char kConfigurationHash1[] =;
constexpr char kConfigurationHash2[] =;
constexpr char kConfigurationData1[] =;
constexpr char kConfigurationData2[] =;
constexpr char kConfigurationData3[] =;
constexpr char kKey1[] =;
constexpr char kKey2[] =;
constexpr char kKey3[] =;
constexpr char kValue1[] =;
constexpr char kValue2[] =;
constexpr char kValue12[] =;
constexpr char kValue22[] =;

struct ResponseTemplate {};

std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
    std::map<std::string, ResponseTemplate> templates,
    const net::test_server::HttpRequest& request) {}

bool DictValueEquals(std::optional<base::Value::Dict> value,
                     const std::map<std::string, std::string>& expected) {}

class ManagedConfigurationAPITestBase : public MixinBasedInProcessBrowserTest {};

}  // namespace

class ManagedConfigurationAPITest : public ManagedConfigurationAPITestBase,
                                    public ManagedConfigurationAPI::Observer {};

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest,
                       PRE_DataIsDownloadedAndPersists) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest,
                       DataIsDownloadedAndPersists) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest, AppRemovedFromPolicyList) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest, UnknownKeys) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest, DataUpdates) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest,
                       PolicyUpdateWhileDownloadingDifferentHash) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest,
                       PolicyUpdateWhileDownloadingSameHash) {}

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPITest,
                       NonDictionaryConfiguration) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)

// Test the API behavior in the Guest Session.
class ManagedConfigurationAPIGuestTest
    : public ManagedConfigurationAPITestBase {
 public:
  ManagedConfigurationAPIGuestTest() {
    // Suppress the InProcessBrowserTest's default behavior of opening
    // about://blank pages and let the standard startup code open the
    // chrome://newtab page. The reason is that the navigator.managed API
    // doesn't work on about://blank pages.
    set_open_about_blank_on_browser_launch(false);
  }

  ~ManagedConfigurationAPIGuestTest() override = default;
  ManagedConfigurationAPIGuestTest(const ManagedConfigurationAPIGuestTest&) =
      delete;
  ManagedConfigurationAPIGuestTest& operator=(
      const ManagedConfigurationAPIGuestTest&) = delete;

 protected:
  // Returns the result of navigator.managed.getManagedConfiguration().
  content::EvalJsResult GetValuesFromJsApi(
      const std::vector<std::string>& keys) {
    content::WebContents* tab =
        browser()->tab_strip_model()->GetActiveWebContents();
    if (!tab) {
      ADD_FAILURE() << "No tab active";
      return {base::Value(), std::string()};
    }
    base::Value::List keys_value;
    for (const auto& key : keys) {
      keys_value.Append(key);
    }
    return content::EvalJs(
        tab, content::JsReplace("navigator.managed.getManagedConfiguration($1)",
                                base::Value(std::move(keys_value))));
  }

 private:
  ash::GuestSessionMixin guest_session_{&mixin_host_};
};

IN_PROC_BROWSER_TEST_F(ManagedConfigurationAPIGuestTest, Disabled) {
  EXPECT_EQ(api(), nullptr);
  // The JS API should return an error (but not cause a crash - it's a
  // regression test for b/231283325).
  EXPECT_THAT(
      GetValuesFromJsApi({kKey1}),
      testing::Field("error", &content::EvalJsResult::error,
                     Eq("a JavaScript error: \"NotAllowedError: This API is "
                        "available only for managed apps.\"\n")));
}

#endif  // BUILDFLAG(IS_CHROMEOS_ASH)