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 #include "components/browsing_data_ui/history_notice_utils.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/run_loop.h" | |
12 #include "components/history/core/test/fake_web_history_service.h" | |
13 #include "components/signin/core/browser/account_tracker_service.h" | |
14 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
15 #include "components/signin/core/browser/fake_signin_manager.h" | |
16 #include "components/signin/core/browser/test_signin_client.h" | |
17 #include "components/sync/base/model_type.h" | |
18 #include "components/sync/driver/fake_sync_service.h" | |
19 #include "components/version_info/version_info.h" | |
20 #include "net/http/http_status_code.h" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 namespace browsing_data_ui { | |
25 | |
26 namespace { | |
27 | |
28 class TestSyncService : public sync_driver::FakeSyncService { | |
29 public: | |
30 // Getters (FakeSyncService implementation). --------------------------------- | |
31 bool IsSyncActive() const override { | |
32 return sync_active_; | |
33 } | |
34 | |
35 syncer::ModelTypeSet GetActiveDataTypes() const override { | |
36 return active_data_types_; | |
37 } | |
38 | |
39 bool IsUsingSecondaryPassphrase() const override { | |
40 return using_secondary_passphrase_; | |
41 } | |
42 | |
43 // Setters. ------------------------------------------------------------------ | |
44 void set_sync_active(bool active) { | |
45 sync_active_ = active; | |
46 } | |
47 | |
48 void set_active_data_types(syncer::ModelTypeSet data_types) { | |
49 active_data_types_ = data_types; | |
50 } | |
51 | |
52 void set_using_secondary_passphrase(bool passphrase) { | |
53 using_secondary_passphrase_ = passphrase; | |
54 } | |
55 | |
56 private: | |
57 syncer::ModelTypeSet active_data_types_; | |
58 bool using_secondary_passphrase_ = false; | |
59 bool sync_active_ = false; | |
60 }; | |
61 | |
62 } // namespace | |
63 | |
64 | |
65 class HistoryNoticeUtilsTest : public ::testing::Test { | |
66 public: | |
67 HistoryNoticeUtilsTest() | |
68 : signin_client_(nullptr), | |
69 signin_manager_(&signin_client_, &account_tracker_) { | |
70 } | |
71 | |
72 void SetUp() override { | |
73 sync_service_.reset(new TestSyncService()); | |
74 history_service_.reset(new history::FakeWebHistoryService( | |
75 &oauth2_token_service_, | |
76 &signin_manager_, | |
77 url_request_context_)); | |
78 history_service_->SetupFakeResponse(true /* success */, net::HTTP_OK); | |
79 } | |
80 | |
81 TestSyncService* sync_service() { | |
82 return sync_service_.get(); | |
83 } | |
84 | |
85 history::FakeWebHistoryService* history_service() { | |
86 return history_service_.get(); | |
87 } | |
88 | |
89 void ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult( | |
90 bool expected_test_case_result) { | |
91 bool got_result = false; | |
92 | |
93 ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( | |
94 sync_service_.get(), | |
95 history_service_.get(), | |
96 version_info::Channel::STABLE, | |
97 base::Bind( | |
98 &HistoryNoticeUtilsTest::Callback, | |
99 base::Unretained(this), | |
100 base::Unretained(&got_result))); | |
101 | |
102 if (!got_result) { | |
103 run_loop_.reset(new base::RunLoop()); | |
104 run_loop_->Run(); | |
105 } | |
106 | |
107 // Process the DeleteSoon() called on MergeBooleanCallbacks, otherwise | |
108 // this it will be considered to be leaked. | |
109 base::RunLoop().RunUntilIdle(); | |
110 | |
111 EXPECT_EQ(expected_test_case_result, result_); | |
112 } | |
113 | |
114 private: | |
115 void Callback(bool* got_result, bool result) { | |
116 *got_result = true; | |
117 result_ = result; | |
118 | |
119 if (run_loop_) | |
120 run_loop_->Quit(); | |
121 } | |
122 | |
123 FakeProfileOAuth2TokenService oauth2_token_service_; | |
124 AccountTrackerService account_tracker_; | |
125 TestSigninClient signin_client_; | |
126 FakeSigninManagerBase signin_manager_; | |
127 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
128 std::unique_ptr<TestSyncService> sync_service_; | |
129 std::unique_ptr<history::FakeWebHistoryService> history_service_; | |
130 | |
131 std::unique_ptr<base::RunLoop> run_loop_; | |
132 bool result_; | |
133 | |
134 base::MessageLoop message_loop_; | |
135 }; | |
136 | |
137 TEST_F(HistoryNoticeUtilsTest, NotSyncing) { | |
138 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
139 } | |
140 | |
141 TEST_F(HistoryNoticeUtilsTest, SyncingWithWrongParameters) { | |
142 sync_service()->set_sync_active(true); | |
143 | |
144 // Regardless of the state of the web history... | |
145 history_service()->SetWebAndAppActivityEnabled(true); | |
146 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
147 | |
148 // ...the response is false if there's custom passphrase... | |
149 sync_service()->set_active_data_types(syncer::ModelTypeSet::All()); | |
150 sync_service()->set_using_secondary_passphrase(true); | |
151 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
152 | |
153 // ...or even if there's no custom passphrase, but we're not syncing history. | |
154 syncer::ModelTypeSet only_passwords(syncer::PASSWORDS); | |
155 sync_service()->set_active_data_types(only_passwords); | |
156 sync_service()->set_using_secondary_passphrase(false); | |
157 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
158 } | |
159 | |
160 TEST_F(HistoryNoticeUtilsTest, WebHistoryStates) { | |
161 // If history Sync is active... | |
162 sync_service()->set_sync_active(true); | |
163 sync_service()->set_active_data_types(syncer::ModelTypeSet::All()); | |
164 | |
165 // ...the result is true if both web history queries return true... | |
166 history_service()->SetWebAndAppActivityEnabled(true); | |
167 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
168 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(true); | |
169 | |
170 // ...but not otherwise. | |
171 history_service()->SetOtherFormsOfBrowsingHistoryPresent(false); | |
172 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
173 history_service()->SetWebAndAppActivityEnabled(false); | |
174 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
175 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
176 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
177 | |
178 // Invalid responses from the web history are interpreted as false. | |
179 history_service()->SetWebAndAppActivityEnabled(true); | |
180 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
181 history_service()->SetupFakeResponse(true, net::HTTP_INTERNAL_SERVER_ERROR); | |
182 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
183 history_service()->SetupFakeResponse(false, net::HTTP_OK); | |
184 ExpectShouldPopupDialogAboutOtherFormsOfBrowsingHistoryWithResult(false); | |
185 } | |
186 | |
187 } // namespace browsing_data_ui | |
OLD | NEW |