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 OwnerSettingsServiceChromeOS::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 // OwnerSettingsServiceChromeOS::Observer implementation: |
| 45 virtual void OnSetCompleted(bool success) override { |
| 46 CHECK(success); |
| 47 CHECK(!set_requests_.empty()); |
| 48 SetRequest request = set_requests_.front(); |
| 49 set_requests_.pop(); |
| 50 const base::Value* value = provider_->Get(request.first); |
| 51 CHECK(request.second->Equals(value)) |
| 52 << "Incorrect value for setting: " << request.first; |
| 53 if (set_requests_.empty()) |
| 54 loop_.Quit(); |
| 55 } |
| 56 |
| 57 void Set(const std::string& setting, const base::Value& value) { |
| 58 service_->Set(setting, value); |
| 59 set_requests_.push( |
| 60 SetRequest(setting, linked_ptr<base::Value>(value.DeepCopy()))); |
| 61 } |
| 62 |
| 63 void Wait() { loop_.Run(); } |
| 64 |
| 65 private: |
| 66 OwnerSettingsServiceChromeOS* service_; |
| 67 DeviceSettingsProvider* provider_; |
| 68 base::RunLoop loop_; |
| 69 |
| 70 typedef std::pair<std::string, linked_ptr<base::Value>> SetRequest; |
| 71 std::queue<SetRequest> set_requests_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(PrefsChecker); |
| 74 }; |
| 75 |
| 76 } // namespace |
| 77 |
| 78 class OwnerSettingsServiceChromeOSTest : public DeviceSettingsTestBase { |
| 79 public: |
| 80 OwnerSettingsServiceChromeOSTest() |
| 81 : local_state_(TestingBrowserProcess::GetGlobal()), |
| 82 user_data_dir_override_(chrome::DIR_USER_DATA) {} |
| 83 |
| 84 virtual void SetUp() override { |
| 85 DeviceSettingsTestBase::SetUp(); |
| 86 provider_.reset(new DeviceSettingsProvider(base::Bind(&OnPrefChanged), |
| 87 &device_settings_service_)); |
| 88 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey()); |
| 89 InitOwner(device_policy_.policy_data().username(), true); |
| 90 FlushDeviceSettings(); |
| 91 } |
| 92 |
| 93 virtual void TearDown() override { DeviceSettingsTestBase::TearDown(); } |
| 94 |
| 95 void TestSingleSet(OwnerSettingsServiceChromeOS* service, |
| 96 const std::string& setting, |
| 97 const base::Value& in_value) { |
| 98 PrefsChecker checker(service, provider_.get()); |
| 99 checker.Set(setting, in_value); |
| 100 FlushDeviceSettings(); |
| 101 checker.Wait(); |
| 102 } |
| 103 |
| 104 ScopedTestingLocalState local_state_; |
| 105 scoped_ptr<DeviceSettingsProvider> provider_; |
| 106 base::ScopedPathOverride user_data_dir_override_; |
| 107 |
| 108 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsServiceChromeOSTest); |
| 110 }; |
| 111 |
| 112 TEST_F(OwnerSettingsServiceChromeOSTest, SingleSetTest) { |
| 113 OwnerSettingsServiceChromeOS* service = |
| 114 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get()); |
| 115 ASSERT_TRUE(service); |
| 116 ASSERT_TRUE(service->IsOwner()); |
| 117 TestSingleSet(service, kReleaseChannel, base::StringValue("dev-channel")); |
| 118 TestSingleSet(service, kReleaseChannel, base::StringValue("beta-channel")); |
| 119 TestSingleSet(service, kReleaseChannel, base::StringValue("stable-channel")); |
| 120 } |
| 121 |
| 122 TEST_F(OwnerSettingsServiceChromeOSTest, MultipleSetTest) { |
| 123 OwnerSettingsServiceChromeOS* service = |
| 124 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get()); |
| 125 ASSERT_TRUE(service); |
| 126 ASSERT_TRUE(service->IsOwner()); |
| 127 base::FundamentalValue allow_guest(false); |
| 128 base::StringValue release_channel("stable-channel"); |
| 129 base::FundamentalValue show_user_names(true); |
| 130 |
| 131 PrefsChecker checker(service, provider_.get()); |
| 132 |
| 133 checker.Set(kAccountsPrefAllowGuest, allow_guest); |
| 134 checker.Set(kReleaseChannel, release_channel); |
| 135 checker.Set(kAccountsPrefShowUserNamesOnSignIn, show_user_names); |
| 136 |
| 137 FlushDeviceSettings(); |
| 138 checker.Wait(); |
| 139 } |
| 140 |
| 141 } // namespace chromeos |
OLD | NEW |