Commit 56095d32 authored by Guillaume Jenkins's avatar Guillaume Jenkins Committed by Commit Bot

[iOS Enterprise] Policy EG Tests helper API

Adds simple helpers for policy EG tests to set policy values.
Values get serialized as JSON to be sent over IPC to the app interface.

This CL only adds overloads for boolean, base::Value and json-encoded
NSString*, but integer, std::string, arrays and dictionaries will be
added as needed.

Bug: 1065522
Change-Id: I750cdc3c4456baba91510422a13acb5dea3ea501
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2125039Reviewed-by: default avatarTina Wang <tinazwang@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarsebsg <sebsg@chromium.org>
Commit-Queue: Guillaume Jenkins <gujen@google.com>
Cr-Commit-Position: refs/heads/master@{#756742}
parent 37f1c700
......@@ -106,6 +106,7 @@ source_set("eg2_tests") {
deps = [
"//base",
"//components/policy:generated",
"//components/policy/core/common:common_constants",
"//components/strings",
"//ios/chrome/browser:pref_names",
......
......@@ -14,11 +14,9 @@
// namespace.
+ (NSString*)valueForPlatformPolicy:(NSString*)policyKey;
// Sets the |SearchSuggestEnabled| policy to the given value.
// TODO(crbug.com/1024115): This should be replaced with a more generic API that
// can set arbitrarily complex policy data. This suggest-specific API only
// exists to allow us to write an example policy EG2 test.
+ (void)setSuggestPolicyEnabled:(BOOL)enabled;
// Sets the value of the policy with the |policyKey| key to the given value. The
// value must be serialized to JSON.
+ (void)setPolicyValue:(NSString*)jsonValue forKey:(NSString*)policyKey;
@end
......
......@@ -24,9 +24,10 @@
namespace {
// Returns a JSON-encoded string representing the given |pref|. If |pref| is
// nullptr, returns a string representing a base::Value of type NONE.
NSString* SerializedValue(const base::Value* value) {
// Returns a JSON-encoded string representing the given |base::Value|. If
// |value| is nullptr, returns a string representing a |base::Value| of type
// NONE.
NSString* SerializeValue(const base::Value* value) {
base::Value none_value(base::Value::Type::NONE);
if (!value) {
......@@ -40,6 +41,19 @@ NSString* SerializedValue(const base::Value* value) {
return base::SysUTF8ToNSString(serialized_value);
}
// Takes a JSON-encoded string representing a |base::Value|, and deserializes
// into a |base::Value| pointer. If nullptr is given, returns a pointer to a
// |base::Value| of type NONE.
std::unique_ptr<base::Value> DeserializeValue(NSString* json_value) {
if (!json_value) {
return std::make_unique<base::Value>(base::Value::Type::NONE);
}
JSONStringValueDeserializer deserializer(
base::SysNSStringToUTF8((json_value)));
return deserializer.Deserialize(/*error_code=*/nullptr,
/*error_message=*/nullptr);
}
}
@implementation PolicyAppInterface
......@@ -50,26 +64,27 @@ NSString* SerializedValue(const base::Value* value) {
BrowserPolicyConnectorIOS* connector =
GetApplicationContext()->GetBrowserPolicyConnector();
if (!connector) {
return SerializedValue(nullptr);
return SerializeValue(nullptr);
}
const policy::ConfigurationPolicyProvider* platformProvider =
connector->GetPlatformProvider();
if (!platformProvider) {
return SerializedValue(nullptr);
return SerializeValue(nullptr);
}
const policy::PolicyBundle& policyBundle = platformProvider->policies();
const policy::PolicyMap& policyMap = policyBundle.Get(
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, ""));
return SerializedValue(policyMap.GetValue(key));
return SerializeValue(policyMap.GetValue(key));
}
+ (void)setSuggestPolicyEnabled:(BOOL)enabled {
+ (void)setPolicyValue:(NSString*)jsonValue forKey:(NSString*)policyKey {
std::unique_ptr<base::Value> value = DeserializeValue(jsonValue);
policy::PolicyMap values;
values.Set(policy::key::kSearchSuggestEnabled, policy::POLICY_LEVEL_MANDATORY,
values.Set(base::SysNSStringToUTF8(policyKey), policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM,
std::make_unique<base::Value>(enabled), nullptr);
std::move(value), /*external_data_fetcher=*/nullptr);
GetTestPlatformPolicyProvider()->UpdateChromePolicy(values);
}
......
......@@ -4,6 +4,11 @@
#include "ios/testing/earl_grey/earl_grey_test.h"
#include <memory>
#include "base/json/json_string_value_serializer.h"
#include "base/strings/sys_string_conversions.h"
#include "components/policy/policy_constants.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/chrome_switches.h"
#import "ios/chrome/browser/policy/policy_app_interface.h"
......@@ -17,6 +22,51 @@
#error "This file requires ARC support."
#endif
namespace {
// Returns a JSON-encoded string representing the given |base::Value|. If
// |value| is nullptr, returns a string representing a |base::Value| of type
// NONE.
NSString* SerializeValue(const base::Value value) {
std::string serialized_value;
JSONStringValueSerializer serializer(&serialized_value);
serializer.Serialize(std::move(value));
return base::SysUTF8ToNSString(serialized_value);
}
// Sets the value of the policy with the |policy_key| key to the given value.
// The value must be serialized as a JSON string.
// Prefer using the other type-specific helpers instead of this generic helper
// if possible.
void SetPolicy(NSString* json_value, const std::string& policy_key) {
[PolicyAppInterface setPolicyValue:json_value
forKey:base::SysUTF8ToNSString(policy_key)];
}
// Sets the value of the policy with the |policy_key| key to the given value.
// The value must be wrapped in a |base::Value|.
// Prefer using the other type-specific helpers instead of this generic helper
// if possible.
void SetPolicy(base::Value value, const std::string& policy_key) {
SetPolicy(SerializeValue(std::move(value)), policy_key);
}
// Sets the value of the policy with the |policy_key| key to the given boolean
// value.
void SetPolicy(bool enabled, const std::string& policy_key) {
SetPolicy(base::Value(enabled), policy_key);
}
// TODO(crbug.com/1065522): Add helpers as needed for:
// - INTEGER
// - STRING
// - LIST (and subtypes, e.g. int list, string list, etc)
// - DICTIONARY (and subtypes, e.g. int list, string list, etc)
// - Deleting a policy value
// - Setting multiple policies at once
} // namespace
// Test case to verify that enterprise policies are set and respected.
@interface PolicyTestCase : ChromeTestCase
@end
......@@ -54,12 +104,12 @@
@"Unexpected default value");
// Force the preference off via policy.
[PolicyAppInterface setSuggestPolicyEnabled:NO];
SetPolicy(false, policy::key::kSearchSuggestEnabled);
GREYAssertFalse([ChromeEarlGrey userBooleanPref:prefs::kSearchSuggestEnabled],
@"Search suggest preference was unexpectedly true");
// Force the preference on via policy.
[PolicyAppInterface setSuggestPolicyEnabled:YES];
SetPolicy(true, policy::key::kSearchSuggestEnabled);
GREYAssertTrue([ChromeEarlGrey userBooleanPref:prefs::kSearchSuggestEnabled],
@"Search suggest preference was unexpectedly false");
}
......
......@@ -9,6 +9,7 @@
#include "base/json/json_string_value_serializer.h"
#include "base/strings/sys_string_conversions.h"
#import "components/policy/core/common/policy_loader_ios_constants.h"
#include "components/policy/policy_constants.h"
#include "ios/chrome/browser/chrome_switches.h"
#import "ios/chrome/browser/policy/policy_app_interface.h"
#include "ios/chrome/browser/pref_names.h"
......@@ -83,14 +84,14 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data) {
// Tests the values of policies that were explicitly set.
- (void)testPolicyExplicitlySet {
std::unique_ptr<base::Value> searchValue =
GetPlatformPolicy("DefaultSearchProviderName");
GetPlatformPolicy(policy::key::kDefaultSearchProviderName);
GREYAssertTrue(searchValue && searchValue->is_string(),
@"searchValue was not of type string");
GREYAssertEqual(searchValue->GetString(), "Test",
@"searchValue had an unexpected value");
std::unique_ptr<base::Value> suggestValue =
GetPlatformPolicy("SearchSuggestEnabled");
GetPlatformPolicy(policy::key::kSearchSuggestEnabled);
GREYAssertTrue(suggestValue && suggestValue->is_bool(),
@"suggestValue was not of type bool");
GREYAssertFalse(suggestValue->GetBool(),
......@@ -101,7 +102,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data) {
// set.
- (void)testPolicyNotSet {
std::unique_ptr<base::Value> blocklistValue =
GetPlatformPolicy("URLBlacklist");
GetPlatformPolicy(policy::key::kURLBlacklist);
GREYAssertTrue(blocklistValue && blocklistValue->is_none(),
@"blocklistValue was unexpectedly present");
}
......@@ -147,7 +148,7 @@ AppLaunchConfiguration GenerateAppLaunchConfiguration(std::string policy_data) {
// set.
- (void)testLoadPolicyKeyNotSet {
std::unique_ptr<base::Value> searchValue =
GetPlatformPolicy("DefaultSearchProviderName");
GetPlatformPolicy(policy::key::kDefaultSearchProviderName);
GREYAssertTrue(searchValue && searchValue->is_none(),
@"searchValue was unexpectedly present");
}
......
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