OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <queue> |
| 6 #include <utility> |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/linked_ptr.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/test/scoped_path_override.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h" |
| 15 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact
ory.h" |
| 16 #include "chrome/browser/chromeos/settings/device_settings_provider.h" |
| 17 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
| 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/test/base/scoped_testing_local_state.h" |
| 20 #include "chrome/test/base/testing_browser_process.h" |
| 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "chromeos/settings/cros_settings_names.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 namespace { |
| 28 |
| 29 void OnPrefChanged(const std::string& /* setting */) { |
| 30 } |
| 31 |
| 32 class PrefsChecker : public ownership::OwnerSettingsService::Observer { |
| 33 public: |
| 34 PrefsChecker(OwnerSettingsServiceChromeOS* service, |
| 35 DeviceSettingsProvider* provider) |
| 36 : service_(service), provider_(provider) { |
| 37 CHECK(service_); |
| 38 CHECK(provider_); |
| 39 service_->AddObserver(this); |
| 40 } |
| 41 |
| 42 virtual ~PrefsChecker() { service_->RemoveObserver(this); } |
| 43 |
| 44 // OwnerSettingsService::Observer implementation: |
| 45 virtual void OnSignedPolicyStored(bool success) override { |
| 46 CHECK(success); |
| 47 |
| 48 if (service_->has_pending_changes()) |
| 49 return; |
| 50 |
| 51 while (!set_requests_.empty()) { |
| 52 SetRequest request = set_requests_.front(); |
| 53 set_requests_.pop(); |
| 54 const base::Value* value = provider_->Get(request.first); |
| 55 ASSERT_TRUE(request.second->Equals(value)); |
| 56 } |
| 57 loop_.Quit(); |
| 58 } |
| 59 |
| 60 void Set(const std::string& setting, const base::Value& value) { |
| 61 service_->Set(setting, value); |
| 62 set_requests_.push( |
| 63 SetRequest(setting, linked_ptr<base::Value>(value.DeepCopy()))); |
| 64 } |
| 65 |
| 66 void Wait() { loop_.Run(); } |
| 67 |
| 68 private: |
| 69 OwnerSettingsServiceChromeOS* service_; |
| 70 DeviceSettingsProvider* provider_; |
| 71 base::RunLoop loop_; |
| 72 |
| 73 typedef std::pair<std::string, linked_ptr<base::Value>> SetRequest; |
| 74 std::queue<SetRequest> set_requests_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(PrefsChecker); |
| 77 }; |
| 78 |
| 79 } // namespace |
| 80 |
| 81 class OwnerSettingsServiceChromeOSTest : public DeviceSettingsTestBase { |
| 82 public: |
| 83 OwnerSettingsServiceChromeOSTest() |
| 84 : local_state_(TestingBrowserProcess::GetGlobal()), |
| 85 user_data_dir_override_(chrome::DIR_USER_DATA) {} |
| 86 |
| 87 virtual void SetUp() override { |
| 88 DeviceSettingsTestBase::SetUp(); |
| 89 provider_.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged), |
| 90 &device_settings_service_)); |
| 91 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey()); |
| 92 InitOwner(device_policy_.policy_data().username(), true); |
| 93 FlushDeviceSettings(); |
| 94 } |
| 95 |
| 96 virtual void TearDown() override { DeviceSettingsTestBase::TearDown(); } |
| 97 |
| 98 void TestSingleSet(OwnerSettingsServiceChromeOS* service, |
| 99 const std::string& setting, |
| 100 const base::Value& in_value) { |
| 101 PrefsChecker checker(service, provider_.get()); |
| 102 checker.Set(setting, in_value); |
| 103 FlushDeviceSettings(); |
| 104 checker.Wait(); |
| 105 } |
| 106 |
| 107 ScopedTestingLocalState local_state_; |
| 108 scoped_ptr<DeviceSettingsProvider> provider_; |
| 109 base::ScopedPathOverride user_data_dir_override_; |
| 110 |
| 111 private: |
| 112 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSTest); |
| 113 }; |
| 114 |
| 115 TEST_F(OwnerSettingsServiceChromeOSTest, SingleSetTest) { |
| 116 OwnerSettingsServiceChromeOS* service = |
| 117 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get()); |
| 118 ASSERT_TRUE(service); |
| 119 ASSERT_TRUE(service->IsOwner()); |
| 120 TestSingleSet(service, kReleaseChannel, base::StringValue("dev-channel")); |
| 121 TestSingleSet(service, kReleaseChannel, base::StringValue("beta-channel")); |
| 122 TestSingleSet(service, kReleaseChannel, base::StringValue("stable-channel")); |
| 123 } |
| 124 |
| 125 TEST_F(OwnerSettingsServiceChromeOSTest, MultipleSetTest) { |
| 126 OwnerSettingsServiceChromeOS* service = |
| 127 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get()); |
| 128 ASSERT_TRUE(service); |
| 129 ASSERT_TRUE(service->IsOwner()); |
| 130 base::FundamentalValue allow_guest(false); |
| 131 base::StringValue release_channel("stable-channel"); |
| 132 base::FundamentalValue show_user_names(true); |
| 133 |
| 134 PrefsChecker checker(service, provider_.get()); |
| 135 |
| 136 checker.Set(kAccountsPrefAllowGuest, allow_guest); |
| 137 checker.Set(kReleaseChannel, release_channel); |
| 138 checker.Set(kAccountsPrefShowUserNamesOnSignIn, show_user_names); |
| 139 |
| 140 FlushDeviceSettings(); |
| 141 checker.Wait(); |
| 142 } |
| 143 |
| 144 } // namespace chromeos |
OLD | NEW |