| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #if defined(GOOGLE_CHROME_BUILD) && !defined(OS_CHROMEOS) |
| 6 |
| 7 #include "chrome/browser/ui/webui/settings/metrics_reporting_handler.h" |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/test/base/scoped_testing_local_state.h" |
| 12 #include "chrome/test/base/testing_browser_process.h" |
| 13 #include "components/metrics/metrics_pref_names.h" |
| 14 #include "components/policy/core/browser/browser_policy_connector.h" |
| 15 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 16 #include "components/policy/core/common/policy_map.h" |
| 17 #include "components/policy/core/common/policy_types.h" |
| 18 #include "components/prefs/pref_service.h" |
| 19 #include "content/public/browser/web_ui.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "content/public/test/test_web_ui.h" |
| 22 #include "policy/policy_constants.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace settings { |
| 26 |
| 27 class TestingMetricsReportingHandler : public MetricsReportingHandler { |
| 28 public: |
| 29 using MetricsReportingHandler::set_web_ui; |
| 30 using MetricsReportingHandler::HandleGetMetricsReporting; |
| 31 }; |
| 32 |
| 33 class MetricsReportingHandlerTest : public testing::Test { |
| 34 public: |
| 35 MetricsReportingHandlerTest() { |
| 36 // Local state must be set up before |handler_|. |
| 37 TestingBrowserProcess::CreateInstance(); |
| 38 local_state_.reset(new ScopedTestingLocalState( |
| 39 TestingBrowserProcess::GetGlobal())); |
| 40 |
| 41 handler_.reset(new TestingMetricsReportingHandler); |
| 42 handler_->set_web_ui(&test_web_ui_); |
| 43 |
| 44 EXPECT_CALL(provider_, IsInitializationComplete(testing::_)).WillRepeatedly( |
| 45 testing::Return(true)); |
| 46 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 47 } |
| 48 |
| 49 void SetUp() override { |
| 50 ASSERT_EQ(local_state(), g_browser_process->local_state()); |
| 51 EXPECT_TRUE(test_web_ui()->call_data().empty()); |
| 52 |
| 53 base::ListValue args; |
| 54 args.Append(base::WrapUnique(new base::FundamentalValue(1))); |
| 55 handler()->HandleGetMetricsReporting(&args); |
| 56 |
| 57 EXPECT_TRUE(handler()->IsJavascriptAllowed()); |
| 58 EXPECT_EQ(1u, test_web_ui()->call_data().size()); |
| 59 |
| 60 test_web_ui()->ClearTrackedCalls(); |
| 61 } |
| 62 |
| 63 PrefService* local_state() { return local_state_->Get(); } |
| 64 TestingMetricsReportingHandler* handler() { return handler_.get(); } |
| 65 content::TestWebUI* test_web_ui() { return &test_web_ui_; } |
| 66 policy::PolicyMap* map() { return &map_; } |
| 67 policy::MockConfigurationPolicyProvider* provider() { return &provider_; } |
| 68 |
| 69 private: |
| 70 content::TestBrowserThreadBundle thread_bundle_; |
| 71 content::TestWebUI test_web_ui_; |
| 72 std::unique_ptr<ScopedTestingLocalState> local_state_; |
| 73 std::unique_ptr<TestingMetricsReportingHandler> handler_; |
| 74 |
| 75 policy::MockConfigurationPolicyProvider provider_; |
| 76 policy::PolicyMap map_; |
| 77 }; |
| 78 |
| 79 TEST_F(MetricsReportingHandlerTest, PrefChangesNotifyPage) { |
| 80 // Toggle the pref. |
| 81 local_state()->SetBoolean( |
| 82 metrics::prefs::kMetricsReportingEnabled, |
| 83 !local_state()->GetBoolean(metrics::prefs::kMetricsReportingEnabled)); |
| 84 EXPECT_EQ(1u, test_web_ui()->call_data().size()); |
| 85 |
| 86 test_web_ui()->ClearTrackedCalls(); |
| 87 handler()->DisallowJavascript(); |
| 88 |
| 89 // Toggle the pref again, while JavaScript is disabled. |
| 90 local_state()->SetBoolean( |
| 91 metrics::prefs::kMetricsReportingEnabled, |
| 92 !local_state()->GetBoolean(metrics::prefs::kMetricsReportingEnabled)); |
| 93 EXPECT_TRUE(test_web_ui()->call_data().empty()); |
| 94 } |
| 95 |
| 96 TEST_F(MetricsReportingHandlerTest, PolicyChangesNotifyPage) { |
| 97 // Change the policy, check that the page was notified. |
| 98 map()->Set(policy::key::kMetricsReportingEnabled, |
| 99 policy::POLICY_LEVEL_MANDATORY, |
| 100 policy::POLICY_SCOPE_USER, |
| 101 policy::POLICY_SOURCE_CLOUD, |
| 102 base::WrapUnique(new base::FundamentalValue(true)), |
| 103 nullptr); |
| 104 provider()->UpdateChromePolicy(*map()); |
| 105 EXPECT_EQ(1u, test_web_ui()->call_data().size()); |
| 106 |
| 107 test_web_ui()->ClearTrackedCalls(); |
| 108 handler()->DisallowJavascript(); |
| 109 |
| 110 // Policies changing while JavaScript is disabled shouldn't notify the page. |
| 111 map()->Set(policy::key::kMetricsReportingEnabled, |
| 112 policy::POLICY_LEVEL_MANDATORY, |
| 113 policy::POLICY_SCOPE_USER, |
| 114 policy::POLICY_SOURCE_CLOUD, |
| 115 base::WrapUnique(new base::FundamentalValue(false)), |
| 116 nullptr); |
| 117 provider()->UpdateChromePolicy(*map()); |
| 118 EXPECT_TRUE(test_web_ui()->call_data().empty()); |
| 119 } |
| 120 |
| 121 } // namespace settings |
| 122 |
| 123 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_CHROMEOS) |
| OLD | NEW |