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_driver/fake_sync_service.h" | |
18 #include "components/version_info/version_info.h" | |
19 #include "net/http/http_status_code.h" | |
20 #include "net/url_request/url_request_test_util.h" | |
21 #include "sync/internal_api/public/base/model_type.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 data_types_; | |
37 } | |
38 | |
39 bool IsUsingSecondaryPassphrase() const override { | |
40 return passphrase_; | |
41 } | |
42 | |
43 // Setters. ------------------------------------------------------------------ | |
44 void SetSyncActive(bool active) { | |
45 sync_active_ = active; | |
46 } | |
47 | |
48 void SetActiveDataTypes(syncer::ModelTypeSet data_types) { | |
49 data_types_ = data_types; | |
50 } | |
51 | |
52 void SetUsingSecondaryPassphrase(bool passphrase) { | |
53 passphrase_ = passphrase; | |
54 } | |
55 | |
56 private: | |
57 syncer::ModelTypeSet data_types_; | |
58 bool 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 Expect(bool expected_test_case_result) { | |
90 got_result_ = false; | |
msarda
2016/05/19 13:35:44
Nit: Can this be a local variable instead of a mem
msramek
2016/05/19 19:47:39
Done.
| |
91 | |
92 ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( | |
93 sync_service_.get(), | |
94 history_service_.get(), | |
95 version_info::Channel::STABLE, | |
96 "User agent is not important for this test", | |
97 base::Bind(&HistoryNoticeUtilsTest::Callback, base::Unretained(this))); | |
98 | |
99 if (!got_result_) { | |
100 run_loop_.reset(new base::RunLoop()); | |
101 run_loop_->Run(); | |
102 } | |
103 | |
104 EXPECT_EQ(expected_test_case_result, result_); | |
105 } | |
106 | |
107 private: | |
108 void Callback(bool result) { | |
109 got_result_ = true; | |
110 result_ = result; | |
111 | |
112 if (run_loop_) | |
113 run_loop_->Quit(); | |
114 } | |
115 | |
116 FakeProfileOAuth2TokenService oauth2_token_service_; | |
117 AccountTrackerService account_tracker_; | |
118 TestSigninClient signin_client_; | |
119 FakeSigninManagerBase signin_manager_; | |
120 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
121 std::unique_ptr<TestSyncService> sync_service_; | |
122 std::unique_ptr<history::FakeWebHistoryService> history_service_; | |
123 | |
124 std::unique_ptr<base::RunLoop> run_loop_; | |
125 bool result_; | |
126 bool got_result_; | |
127 | |
128 base::MessageLoop message_loop_; | |
129 }; | |
130 | |
131 TEST_F(HistoryNoticeUtilsTest, NotSyncing) { | |
132 Expect(false); | |
133 } | |
134 | |
135 TEST_F(HistoryNoticeUtilsTest, SyncingWithWrongParameters) { | |
136 sync_service()->SetSyncActive(true); | |
137 | |
138 // Regardless of the state of the web history... | |
139 history_service()->SetWebAndAppActivityEnabled(true); | |
140 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
141 | |
142 // ...the response is false if there's custom passphrase... | |
143 sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All()); | |
144 sync_service()->SetUsingSecondaryPassphrase(true); | |
145 Expect(false); | |
146 | |
147 // ...or even if there's no custom passphrase, but we're not syncing history. | |
148 syncer::ModelTypeSet only_passwords(syncer::PASSWORDS); | |
149 sync_service()->SetActiveDataTypes(only_passwords); | |
150 sync_service()->SetUsingSecondaryPassphrase(false); | |
151 Expect(false); | |
152 } | |
153 | |
154 TEST_F(HistoryNoticeUtilsTest, WebHistoryStates) { | |
155 // If we're syncing... | |
msarda
2016/05/19 13:35:44
Do not use "we" in comments. (What is we here? Chr
msramek
2016/05/19 19:47:39
Done. I agree with you on this, but it's not that
| |
156 sync_service()->SetSyncActive(true); | |
157 sync_service()->SetActiveDataTypes(syncer::ModelTypeSet::All()); | |
158 | |
159 // ...the result is true if both web history queries return true... | |
160 history_service()->SetWebAndAppActivityEnabled(true); | |
161 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
162 Expect(true); | |
163 | |
164 // ...but not otherwise. | |
165 history_service()->SetOtherFormsOfBrowsingHistoryPresent(false); | |
166 Expect(false); | |
167 history_service()->SetWebAndAppActivityEnabled(false); | |
168 Expect(false); | |
169 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
170 Expect(false); | |
171 | |
172 // Invalid responses from the web history are interpreted as false. | |
173 history_service()->SetWebAndAppActivityEnabled(true); | |
174 history_service()->SetOtherFormsOfBrowsingHistoryPresent(true); | |
175 history_service()->SetupFakeResponse(true, net::HTTP_INTERNAL_SERVER_ERROR); | |
176 Expect(false); | |
177 history_service()->SetupFakeResponse(false, net::HTTP_OK); | |
178 Expect(false); | |
msarda
2016/05/19 13:35:44
When I first read the code, I though this expect w
msramek
2016/05/19 19:47:39
Done.
But I would argue that shorter was more rea
| |
179 } | |
180 | |
181 } // namespace browsing_data_ui | |
OLD | NEW |