| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/network_configuration_updater.h" | 5 #include "chrome/browser/policy/network_configuration_updater.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/cros/mock_network_library.h" | 7 #include "chrome/browser/chromeos/cros/mock_network_library.h" |
| 8 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 8 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using testing::Mock; | 12 using testing::Mock; |
| 13 using testing::Return; | 13 using testing::Return; |
| 14 using testing::_; | 14 using testing::_; |
| 15 | 15 |
| 16 namespace policy { | 16 namespace policy { |
| 17 | 17 |
| 18 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; | 18 static const char kFakeONC[] = "{ \"GUID\": \"1234\" }"; |
| 19 | 19 |
| 20 class NetworkConfigurationUpdaterTest | 20 class NetworkConfigurationUpdaterTest |
| 21 : public testing::TestWithParam<ConfigurationPolicyType> { | 21 : public testing::TestWithParam<const char*> { |
| 22 protected: | 22 protected: |
| 23 virtual void SetUp() OVERRIDE { | 23 virtual void SetUp() OVERRIDE { |
| 24 EXPECT_CALL(network_library_, LoadOncNetworks(_, "", _, _)) | 24 EXPECT_CALL(network_library_, LoadOncNetworks(_, "", _, _)) |
| 25 .WillRepeatedly(Return(true)); | 25 .WillRepeatedly(Return(true)); |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Maps configuration policy type to corresponding ONC source. | 28 // Maps configuration policy name to corresponding ONC source. |
| 29 static chromeos::NetworkUIData::ONCSource TypeToONCSource( | 29 static chromeos::NetworkUIData::ONCSource NameToONCSource(const char* name) { |
| 30 ConfigurationPolicyType type) { | 30 switch (name) { |
| 31 switch (type) { | 31 case key::kDeviceOpenNetworkConfiguration: |
| 32 case kPolicyDeviceOpenNetworkConfiguration: | |
| 33 return chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY; | 32 return chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY; |
| 34 case kPolicyOpenNetworkConfiguration: | 33 case key::kOpenNetworkConfiguration: |
| 35 return chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY; | 34 return chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY; |
| 36 default: | 35 default: |
| 37 return chromeos::NetworkUIData::ONC_SOURCE_NONE; | 36 return chromeos::NetworkUIData::ONC_SOURCE_NONE; |
| 38 } | 37 } |
| 39 } | 38 } |
| 40 | 39 |
| 41 chromeos::MockNetworkLibrary network_library_; | 40 chromeos::MockNetworkLibrary network_library_; |
| 42 MockConfigurationPolicyProvider provider_; | 41 MockConfigurationPolicyProvider provider_; |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdate) { | 44 TEST_P(NetworkConfigurationUpdaterTest, InitialUpdate) { |
| 46 provider_.AddPolicy(GetParam(), Value::CreateStringValue(kFakeONC)); | 45 provider_.AddPolicy(GetParam(), Value::CreateStringValue(kFakeONC)); |
| 47 | 46 |
| 48 EXPECT_CALL(network_library_, | 47 EXPECT_CALL(network_library_, |
| 49 LoadOncNetworks(kFakeONC, "", TypeToONCSource(GetParam()), _)) | 48 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) |
| 50 .WillOnce(Return(true)); | 49 .WillOnce(Return(true)); |
| 51 | 50 |
| 52 NetworkConfigurationUpdater updater(&provider_, &network_library_); | 51 NetworkConfigurationUpdater updater(&provider_, &network_library_); |
| 53 Mock::VerifyAndClearExpectations(&network_library_); | 52 Mock::VerifyAndClearExpectations(&network_library_); |
| 54 } | 53 } |
| 55 | 54 |
| 56 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { | 55 TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) { |
| 57 NetworkConfigurationUpdater updater(&provider_, &network_library_); | 56 NetworkConfigurationUpdater updater(&provider_, &network_library_); |
| 58 | 57 |
| 59 // We should update if policy changes. | 58 // We should update if policy changes. |
| 60 EXPECT_CALL(network_library_, | 59 EXPECT_CALL(network_library_, |
| 61 LoadOncNetworks(kFakeONC, "", TypeToONCSource(GetParam()), _)) | 60 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) |
| 62 .WillOnce(Return(true)); | 61 .WillOnce(Return(true)); |
| 63 provider_.AddPolicy(GetParam(), Value::CreateStringValue(kFakeONC)); | 62 provider_.AddPolicy(GetParam(), Value::CreateStringValue(kFakeONC)); |
| 64 provider_.NotifyPolicyUpdated(); | 63 provider_.NotifyPolicyUpdated(); |
| 65 Mock::VerifyAndClearExpectations(&network_library_); | 64 Mock::VerifyAndClearExpectations(&network_library_); |
| 66 | 65 |
| 67 // No update if the set the same value again. | 66 // No update if the set the same value again. |
| 68 EXPECT_CALL(network_library_, | 67 EXPECT_CALL(network_library_, |
| 69 LoadOncNetworks(kFakeONC, "", TypeToONCSource(GetParam()), _)) | 68 LoadOncNetworks(kFakeONC, "", NameToONCSource(GetParam()), _)) |
| 70 .Times(0); | 69 .Times(0); |
| 71 provider_.NotifyPolicyUpdated(); | 70 provider_.NotifyPolicyUpdated(); |
| 72 Mock::VerifyAndClearExpectations(&network_library_); | 71 Mock::VerifyAndClearExpectations(&network_library_); |
| 73 | 72 |
| 74 // Another update is expected if the policy goes away. | 73 // Another update is expected if the policy goes away. |
| 75 EXPECT_CALL(network_library_, | 74 EXPECT_CALL(network_library_, |
| 76 LoadOncNetworks(NetworkConfigurationUpdater::kEmptyConfiguration, | 75 LoadOncNetworks(NetworkConfigurationUpdater::kEmptyConfiguration, |
| 77 "", TypeToONCSource(GetParam()), _)) | 76 "", NameToONCSource(GetParam()), _)) |
| 78 .WillOnce(Return(true)); | 77 .WillOnce(Return(true)); |
| 79 provider_.RemovePolicy(GetParam()); | 78 provider_.RemovePolicy(GetParam()); |
| 80 provider_.NotifyPolicyUpdated(); | 79 provider_.NotifyPolicyUpdated(); |
| 81 Mock::VerifyAndClearExpectations(&network_library_); | 80 Mock::VerifyAndClearExpectations(&network_library_); |
| 82 } | 81 } |
| 83 | 82 |
| 84 INSTANTIATE_TEST_CASE_P( | 83 INSTANTIATE_TEST_CASE_P( |
| 85 NetworkConfigurationUpdaterTestInstance, | 84 NetworkConfigurationUpdaterTestInstance, |
| 86 NetworkConfigurationUpdaterTest, | 85 NetworkConfigurationUpdaterTest, |
| 87 testing::Values(kPolicyDeviceOpenNetworkConfiguration, | 86 testing::Values(key::kDeviceOpenNetworkConfiguration, |
| 88 kPolicyOpenNetworkConfiguration)); | 87 key::kOpenNetworkConfiguration)); |
| 89 | 88 |
| 90 } // namespace policy | 89 } // namespace policy |
| OLD | NEW |