1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/shelf_prefs.h"
#include "base/json/json_writer.h"
#include "base/notreached.h"
#include "base/values.h"
#include "chrome/browser/sync/test/integration/preferences_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/data_type.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using preferences_helper::ChangeStringPref;
using preferences_helper::GetPrefs;
using testing::Eq;
class SingleClientOsPreferencesSyncTest : public SyncTest {
public:
SingleClientOsPreferencesSyncTest() : SyncTest(SINGLE_CLIENT) {}
~SingleClientOsPreferencesSyncTest() override = default;
protected:
static std::string ConvertToSyncedPrefValue(const base::Value& value) {
std::string result;
bool success = base::JSONWriter::Write(value, &result);
DCHECK(success);
return result;
}
sync_pb::PreferenceSpecifics* GetPreferenceSpecifics(
syncer::DataType data_type,
sync_pb::EntitySpecifics& specifics) {
switch (data_type) {
case syncer::DataType::PREFERENCES:
return specifics.mutable_preference();
case syncer::DataType::PRIORITY_PREFERENCES:
return specifics.mutable_priority_preference()->mutable_preference();
case syncer::DataType::OS_PREFERENCES:
return specifics.mutable_os_preference()->mutable_preference();
case syncer::DataType::OS_PRIORITY_PREFERENCES:
return specifics.mutable_os_priority_preference()->mutable_preference();
default:
NOTREACHED();
}
}
void InjectPreferenceToFakeServer(syncer::DataType data_type,
const char* name,
const base::Value& value) {
sync_pb::EntitySpecifics specifics;
sync_pb::PreferenceSpecifics* preference_specifics =
GetPreferenceSpecifics(data_type, specifics);
preference_specifics->set_name(name);
preference_specifics->set_value(ConvertToSyncedPrefValue(value));
GetFakeServer()->InjectEntity(
syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
/*non_unique_name=*/name,
/*client_tag=*/name, specifics,
/*creation_time=*/0, /*last_modified_time=*/0));
}
const char* const kOsPreferenceKey = ash::prefs::kShelfAutoHideBehavior;
const base::Value kOsPreferenceNewValue =
base::Value(ash::kShelfAutoHideBehaviorAlways);
const char* const kOsPriorityPreferenceKey = ash::prefs::kTapToClickEnabled;
const base::Value kOsPriorityPreferenceNewValue = base::Value(false);
};
IN_PROC_BROWSER_TEST_F(SingleClientOsPreferencesSyncTest, Sanity) {
ASSERT_TRUE(SetupSync());
// Shelf alignment is a Chrome OS only preference.
ChangeStringPref(/*index=*/0, ash::prefs::kShelfAlignment,
ash::kShelfAlignmentRight);
EXPECT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());
EXPECT_THAT(GetPrefs(/*index=*/0)->GetString(ash::prefs::kShelfAlignment),
Eq(ash::kShelfAlignmentRight));
}
IN_PROC_BROWSER_TEST_F(SingleClientOsPreferencesSyncTest,
OSPreferencesAreUploaded) {
ASSERT_TRUE(SetupClients());
ASSERT_NE(GetPrefs(0)->GetValue(kOsPreferenceKey), kOsPreferenceNewValue);
ASSERT_NE(GetPrefs(0)->GetValue(kOsPriorityPreferenceKey),
kOsPriorityPreferenceNewValue);
ASSERT_TRUE(SetupSync());
GetPrefs(/*index=*/0)->Set(kOsPreferenceKey, kOsPreferenceNewValue);
GetPrefs(/*index=*/0)
->Set(kOsPriorityPreferenceKey, kOsPriorityPreferenceNewValue);
EXPECT_TRUE(FakeServerPrefMatchesValueChecker(
syncer::DataType::OS_PREFERENCES, kOsPreferenceKey,
ConvertToSyncedPrefValue(kOsPreferenceNewValue))
.Wait());
EXPECT_TRUE(FakeServerPrefMatchesValueChecker(
syncer::DataType::OS_PRIORITY_PREFERENCES,
kOsPriorityPreferenceKey,
ConvertToSyncedPrefValue(kOsPriorityPreferenceNewValue))
.Wait());
}
// OS preferences are not getting synced from the browser prefs on the new
// clients.
IN_PROC_BROWSER_TEST_F(SingleClientOsPreferencesSyncTest,
DontReceiveSyncedOSPrefsFromOldClients) {
InjectPreferenceToFakeServer(syncer::PREFERENCES, kOsPreferenceKey,
kOsPreferenceNewValue);
InjectPreferenceToFakeServer(syncer::PRIORITY_PREFERENCES,
kOsPriorityPreferenceKey,
kOsPriorityPreferenceNewValue);
ASSERT_TRUE(SetupSync());
EXPECT_TRUE(GetPrefs(/*index=*/0)
->FindPreference(kOsPreferenceKey)
->IsDefaultValue());
EXPECT_TRUE(GetPrefs(/*index=*/0)
->FindPreference(kOsPriorityPreferenceKey)
->IsDefaultValue());
}
} // namespace
|