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

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

Issue 1799913003: Settings People Revamp: Easy Unlock: Add basic UI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: test fixes Created 4 years, 9 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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/ui/webui/settings/chromeos/easy_unlock_settings_handler .h" 5 #include "chrome/browser/ui/webui/settings/chromeos/easy_unlock_settings_handler .h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/signin/easy_unlock_service.h" 8 #include "chrome/browser/signin/easy_unlock_service.h"
9 #include "chrome/browser/signin/easy_unlock_service_factory.h" 9 #include "chrome/browser/signin/easy_unlock_service_factory.h"
10 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/browser/web_ui_data_source.h" 11 #include "content/public/browser/web_ui_data_source.h"
12 #include "content/public/test/test_browser_thread_bundle.h" 12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "content/public/test/test_web_ui.h" 13 #include "content/public/test/test_web_ui.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace chromeos { 16 namespace chromeos {
17 namespace settings { 17 namespace settings {
18 18
19 namespace { 19 namespace {
20 20
21 class FakeEasyUnlockService : public EasyUnlockService { 21 class FakeEasyUnlockService : public EasyUnlockService {
22 public: 22 public:
23 explicit FakeEasyUnlockService(Profile* profile) 23 explicit FakeEasyUnlockService(Profile* profile)
24 : EasyUnlockService(profile), turn_off_status_(IDLE), is_allowed_(true) {} 24 : EasyUnlockService(profile),
25 turn_off_status_(IDLE),
26 is_allowed_(true),
27 is_enabled_(false) {}
25 28
26 TurnOffFlowStatus GetTurnOffFlowStatus() const override { 29 TurnOffFlowStatus GetTurnOffFlowStatus() const override {
27 return turn_off_status_; 30 return turn_off_status_;
28 } 31 }
29 32
30 bool IsAllowed() const override { return is_allowed_; } 33 bool IsAllowed() const override { return is_allowed_; }
34 void set_is_allowed(bool is_allowed) { is_allowed_ = is_allowed; }
31 35
32 void set_is_allowed(bool is_allowed) { is_allowed_ = is_allowed; } 36 bool IsEnabled() const override { return is_enabled_; }
37 void set_is_enabled(bool is_enabled) { is_enabled_ = is_enabled; }
33 38
34 void RunTurnOffFlow() override { 39 void RunTurnOffFlow() override {
35 turn_off_status_ = PENDING; 40 turn_off_status_ = PENDING;
36 NotifyTurnOffOperationStatusChanged(); 41 NotifyTurnOffOperationStatusChanged();
37 } 42 }
38 43
39 void ResetTurnOffFlow() override { 44 void ResetTurnOffFlow() override {
40 turn_off_status_ = IDLE; 45 turn_off_status_ = IDLE;
41 NotifyTurnOffOperationStatusChanged(); 46 NotifyTurnOffOperationStatusChanged();
42 } 47 }
(...skipping 26 matching lines...) Expand all
69 void SetAutoPairingResult(bool success, const std::string& error) override {} 74 void SetAutoPairingResult(bool success, const std::string& error) override {}
70 75
71 void InitializeInternal() override {} 76 void InitializeInternal() override {}
72 void ShutdownInternal() override {} 77 void ShutdownInternal() override {}
73 bool IsAllowedInternal() const override { return false; } 78 bool IsAllowedInternal() const override { return false; }
74 void OnWillFinalizeUnlock(bool success) override {} 79 void OnWillFinalizeUnlock(bool success) override {}
75 void OnSuspendDoneInternal() override {} 80 void OnSuspendDoneInternal() override {}
76 81
77 TurnOffFlowStatus turn_off_status_; 82 TurnOffFlowStatus turn_off_status_;
78 bool is_allowed_; 83 bool is_allowed_;
84 bool is_enabled_;
79 }; 85 };
80 86
81 class TestEasyUnlockSettingsHandler : public EasyUnlockSettingsHandler { 87 class TestEasyUnlockSettingsHandler : public EasyUnlockSettingsHandler {
82 public: 88 public:
83 explicit TestEasyUnlockSettingsHandler(Profile* profile) 89 explicit TestEasyUnlockSettingsHandler(Profile* profile)
84 : EasyUnlockSettingsHandler(profile) {} 90 : EasyUnlockSettingsHandler(profile) {}
85 91
86 using EasyUnlockSettingsHandler::set_web_ui; 92 using EasyUnlockSettingsHandler::set_web_ui;
87 }; 93 };
88 94
(...skipping 16 matching lines...) Expand all
105 profile_ = builder.Build(); 111 profile_ = builder.Build();
106 } 112 }
107 113
108 Profile* profile() { return profile_.get(); } 114 Profile* profile() { return profile_.get(); }
109 content::TestWebUI* web_ui() { return &web_ui_; } 115 content::TestWebUI* web_ui() { return &web_ui_; }
110 FakeEasyUnlockService* fake_easy_unlock_service() { 116 FakeEasyUnlockService* fake_easy_unlock_service() {
111 return static_cast<FakeEasyUnlockService*>( 117 return static_cast<FakeEasyUnlockService*>(
112 EasyUnlockService::Get(profile_.get())); 118 EasyUnlockService::Get(profile_.get()));
113 } 119 }
114 120
121 void VerifyEnabledStatusCallback(size_t expected_total_calls,
122 bool expected_status) {
123 std::string event;
124 bool status;
125
126 EXPECT_EQ(expected_total_calls, web_ui_.call_data().size());
127
128 const content::TestWebUI::CallData& data = *web_ui_.call_data().back();
129 EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
130 ASSERT_TRUE(data.arg1()->GetAsString(&event));
131 EXPECT_EQ("easy-unlock-enabled-status", event);
132 ASSERT_TRUE(data.arg2()->GetAsBoolean(&status));
133
134 EXPECT_EQ(expected_status, status);
135 }
136
115 void VerifyTurnOffStatusCallback(size_t expected_total_calls, 137 void VerifyTurnOffStatusCallback(size_t expected_total_calls,
116 const std::string& expected_status) { 138 const std::string& expected_status) {
117 std::string event; 139 std::string event;
118 std::string status; 140 std::string status;
119 141
120 EXPECT_EQ(expected_total_calls, web_ui_.call_data().size()); 142 EXPECT_EQ(expected_total_calls, web_ui_.call_data().size());
121 143
122 const content::TestWebUI::CallData& data = *web_ui_.call_data().back(); 144 const content::TestWebUI::CallData& data = *web_ui_.call_data().back();
123 EXPECT_EQ("cr.webUIListenerCallback", data.function_name()); 145 EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
124 ASSERT_TRUE(data.arg1()->GetAsString(&event)); 146 ASSERT_TRUE(data.arg1()->GetAsString(&event));
(...skipping 16 matching lines...) Expand all
141 content::WebUIDataSource::Add(profile(), data_source); 163 content::WebUIDataSource::Add(profile(), data_source);
142 handler.reset( 164 handler.reset(
143 EasyUnlockSettingsHandler::Create(data_source, profile())); 165 EasyUnlockSettingsHandler::Create(data_source, profile()));
144 EXPECT_TRUE(handler.get()); 166 EXPECT_TRUE(handler.get());
145 167
146 fake_easy_unlock_service()->set_is_allowed(false); 168 fake_easy_unlock_service()->set_is_allowed(false);
147 handler.reset(EasyUnlockSettingsHandler::Create(data_source, profile())); 169 handler.reset(EasyUnlockSettingsHandler::Create(data_source, profile()));
148 EXPECT_FALSE(handler.get()); 170 EXPECT_FALSE(handler.get());
149 } 171 }
150 172
173 TEST_F(EasyUnlockSettingsHandlerTest, EnabledStatus) {
174 scoped_ptr<EasyUnlockSettingsHandler> handler;
175 handler.reset(new TestEasyUnlockSettingsHandler(profile()));
176 handler->set_web_ui(web_ui());
177
178 // Test the C++ -> JS push path.
179 handler->SendEnabledStatus();
180 VerifyEnabledStatusCallback(1U, false);
181
182 fake_easy_unlock_service()->set_is_enabled(true);
183 handler->SendEnabledStatus();
184 VerifyEnabledStatusCallback(2U, true);
185
186 // Test the JS -> C++ -> JS callback path.
187 base::ListValue list_args;
188 list_args.Append(new base::StringValue("test-callback-id"));
189 handler->HandleGetEnabledStatus(&list_args);
190
191 std::string callback_id;
192 bool enabled_status;
193
194 EXPECT_EQ(3U, web_ui()->call_data().size());
195
196 const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
197 EXPECT_EQ("cr.webUIResponse", data.function_name());
198
199 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
200 EXPECT_EQ("test-callback-id", callback_id);
201
202 ASSERT_TRUE(data.arg2()->GetAsBoolean(&enabled_status));
203 EXPECT_TRUE(enabled_status);
204 }
205
151 TEST_F(EasyUnlockSettingsHandlerTest, TurnOffStatus) { 206 TEST_F(EasyUnlockSettingsHandlerTest, TurnOffStatus) {
152 scoped_ptr<EasyUnlockSettingsHandler> handler; 207 scoped_ptr<EasyUnlockSettingsHandler> handler;
153 handler.reset(new TestEasyUnlockSettingsHandler(profile())); 208 handler.reset(new TestEasyUnlockSettingsHandler(profile()));
154 handler->set_web_ui(web_ui()); 209 handler->set_web_ui(web_ui());
155 210
211 // Send an initial status query to turn on service observer.
212 base::ListValue list_args;
213 list_args.Append(new base::StringValue("test-callback-id"));
214 handler->HandleGetEnabledStatus(&list_args);
215 EXPECT_EQ(1U, web_ui()->call_data().size());
216
156 handler->HandleGetTurnOffFlowStatus(nullptr); 217 handler->HandleGetTurnOffFlowStatus(nullptr);
157 VerifyTurnOffStatusCallback(1U, "idle"); 218 VerifyTurnOffStatusCallback(2U, "idle");
158 219
159 handler->HandleRequestTurnOff(nullptr); 220 handler->HandleRequestTurnOff(nullptr);
160 VerifyTurnOffStatusCallback(2U, "pending"); 221 VerifyTurnOffStatusCallback(3U, "pending");
161 222
162 handler->HandleGetTurnOffFlowStatus(nullptr); 223 handler->HandleGetTurnOffFlowStatus(nullptr);
163 VerifyTurnOffStatusCallback(3U, "pending"); 224 VerifyTurnOffStatusCallback(4U, "pending");
164 225
165 handler->HandlePageDismissed(nullptr); 226 handler->HandlePageDismissed(nullptr);
166 VerifyTurnOffStatusCallback(4U, "idle"); 227 VerifyTurnOffStatusCallback(5U, "idle");
167 228
168 fake_easy_unlock_service()->SetTurnOffFailForTest(); 229 fake_easy_unlock_service()->SetTurnOffFailForTest();
169 VerifyTurnOffStatusCallback(5U, "server-error"); 230 VerifyTurnOffStatusCallback(6U, "server-error");
170 231
171 handler->HandleGetTurnOffFlowStatus(nullptr); 232 handler->HandleGetTurnOffFlowStatus(nullptr);
172 VerifyTurnOffStatusCallback(6U, "server-error"); 233 VerifyTurnOffStatusCallback(7U, "server-error");
173 } 234 }
174 235
175 } // namespace settings 236 } // namespace settings
176 } // namespace chromeos 237 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698