Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/ui/webui/settings/metrics_reporting_handler_unittest.cc

Issue 2233443002: MD Settings: implement metrics reporting checkbox on desktop (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@desktop-uma
Patch Set: merge + closure Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 using testing::_;
34 using testing::Return;
35
36 class MetricsReportingHandlerTest : public testing::Test {
37 public:
38 MetricsReportingHandlerTest() {
39 // Local state must be set up before |handler_|.
40 TestingBrowserProcess::CreateInstance();
41 local_state_.reset(new ScopedTestingLocalState(
42 TestingBrowserProcess::GetGlobal()));
43
44 handler_.reset(new TestingMetricsReportingHandler);
45 handler_->set_web_ui(&test_web_ui_);
46
47 EXPECT_CALL(provider_, IsInitializationComplete(testing::_)).WillRepeatedly(
48 testing::Return(true));
49 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
50 }
51
52 void SetUp() override {
53 ASSERT_EQ(local_state(), g_browser_process->local_state());
54 EXPECT_TRUE(test_web_ui()->call_data().empty());
55
56 base::ListValue args;
57 args.Append(base::WrapUnique(new base::FundamentalValue(1)));
58 handler()->HandleGetMetricsReporting(&args);
59
60 EXPECT_TRUE(handler()->IsJavascriptAllowed());
61 EXPECT_EQ(1u, test_web_ui()->call_data().size());
62
63 test_web_ui()->ClearTrackedCalls();
64 }
65
66 PrefService* local_state() { return local_state_->Get(); }
67 TestingMetricsReportingHandler* handler() { return handler_.get(); }
68 content::TestWebUI* test_web_ui() { return &test_web_ui_; }
69 policy::PolicyMap* map() { return &map_; }
70 policy::MockConfigurationPolicyProvider* provider() { return &provider_; }
dschuyler 2016/08/10 18:55:16 For test_web_ui(), map(), and provider() can we r
Dan Beam 2016/08/10 19:41:59 we could... but what's the benefit? the google c+
71
72 private:
73 content::TestBrowserThreadBundle thread_bundle_;
74 content::TestWebUI test_web_ui_;
75 std::unique_ptr<ScopedTestingLocalState> local_state_;
76 std::unique_ptr<TestingMetricsReportingHandler> handler_;
77
78 policy::MockConfigurationPolicyProvider provider_;
79 policy::PolicyMap map_;
80 };
81
82 TEST_F(MetricsReportingHandlerTest, PrefChangesNotifyPage) {
83 // Toggle the pref.
84 local_state()->SetBoolean(
85 metrics::prefs::kMetricsReportingEnabled,
86 !local_state()->GetBoolean(metrics::prefs::kMetricsReportingEnabled));
87 EXPECT_EQ(1u, test_web_ui()->call_data().size());
88
89 test_web_ui()->ClearTrackedCalls();
90 handler()->DisallowJavascript();
91
92 // Toggle the pref again, while JavaScript is disabled.
93 local_state()->SetBoolean(
94 metrics::prefs::kMetricsReportingEnabled,
95 !local_state()->GetBoolean(metrics::prefs::kMetricsReportingEnabled));
96 EXPECT_TRUE(test_web_ui()->call_data().empty());
97 }
98
99 TEST_F(MetricsReportingHandlerTest, PolicyChangesNotifyPage) {
100 // Change the policy, check that the page was notified.
101 map()->Set(policy::key::kMetricsReportingEnabled,
102 policy::POLICY_LEVEL_MANDATORY,
103 policy::POLICY_SCOPE_USER,
104 policy::POLICY_SOURCE_CLOUD,
105 base::WrapUnique(new base::FundamentalValue(true)),
106 nullptr);
107 provider()->UpdateChromePolicy(*map());
108 EXPECT_EQ(1u, test_web_ui()->call_data().size());
109
110 test_web_ui()->ClearTrackedCalls();
111 handler()->DisallowJavascript();
112
113 // Policies changing while JavaScript is disabled shouldn't notify the page.
114 map()->Set(policy::key::kMetricsReportingEnabled,
115 policy::POLICY_LEVEL_MANDATORY,
116 policy::POLICY_SCOPE_USER,
117 policy::POLICY_SOURCE_CLOUD,
118 base::WrapUnique(new base::FundamentalValue(false)),
119 nullptr);
120 provider()->UpdateChromePolicy(*map());
121 EXPECT_TRUE(test_web_ui()->call_data().empty());
122 }
123
124 } // namespace settings
125
126 #endif // defined(GOOGLE_CHROME_BUILD) && !defined(OS_CHROMEOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698