File: settings_clear_browsing_data_handler_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (326 lines) | stat: -rw-r--r-- 12,242 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h"

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/template_url_service_factory_test_util.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/toasts/toast_controller.h"
#include "chrome/browser/ui/toasts/toast_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/counters/browsing_data_counter.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace settings {

using ::testing::_;
using ::testing::Optional;

static const char* kTestingDatatypePref = "counter.testing.datatype";

namespace {

class MockBrowsingDataCounter : public browsing_data::BrowsingDataCounter {
 public:
  MockBrowsingDataCounter() {
    ON_CALL(*this, SetBeginTime).WillByDefault([this](base::Time begin_time) {
      browsing_data::BrowsingDataCounter::SetBeginTime(begin_time);
    });
  }
  ~MockBrowsingDataCounter() override = default;

  MOCK_METHOD(void, Count, ());
  MOCK_METHOD(void, SetBeginTime, (base::Time));

  const char* GetPrefName() const override { return kTestingDatatypePref; }
};

}  // namespace

class TestingClearBrowsingDataHandler
    : public settings::ClearBrowsingDataHandler {
 public:
  using settings::ClearBrowsingDataHandler::AllowJavascript;
  using settings::ClearBrowsingDataHandler::set_web_ui;

  TestingClearBrowsingDataHandler(content::WebUI* webui, Profile* profile)
      : ClearBrowsingDataHandler(webui, profile) {
    AddCounter(std::make_unique<MockBrowsingDataCounter>(),
               browsing_data::ClearBrowsingDataTab::BASIC);
    AddCounter(std::make_unique<MockBrowsingDataCounter>(),
               browsing_data::ClearBrowsingDataTab::ADVANCED);
  }

  void HandleRestartCounters(const base::Value::List& args) {
    settings::ClearBrowsingDataHandler::HandleRestartCounters(args);
  }

  MockBrowsingDataCounter* basic_counter() const {
    return static_cast<MockBrowsingDataCounter*>(counters_basic_[0].get());
  }

  MockBrowsingDataCounter* advanced_counter() const {
    return static_cast<MockBrowsingDataCounter*>(counters_advanced_[0].get());
  }

  // Some services initialized in |OnJavascriptAllowed()| don't have test
  // versions, hence are not available in unittests. For this reason we only
  // initialize services needed by the unittests below.
  void OnJavascriptAllowed() override {
    dse_service_observation_.Observe(
        TemplateURLServiceFactory::GetForProfile(profile_));
  }
};

class ClearBrowsingDataHandlerUnitTest : public testing::Test {
 public:
  void SetUp() override;
  void TearDown() override;
  void VerifySearchHistoryWebUIUpdate(
      const bool expected_is_non_google_dse,
      const std::u16string& expected_non_google_search_history_strin);
  TemplateURL* AddSearchEngine(const std::u16string& short_name,
                               const GURL& searchable_url,
                               int prepopulate_id,
                               bool set_default);

 protected:
  content::BrowserTaskEnvironment browser_task_environment_;
  std::unique_ptr<TestBrowserWindow> browser_window_;
  std::unique_ptr<Browser> browser_;
  std::unique_ptr<TestingProfileManager> testing_profile_manager;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<content::WebContents> web_contents_;
  content::TestWebUI test_web_ui_;
  base::test::ScopedFeatureList feature_list_;
  std::unique_ptr<TestingClearBrowsingDataHandler> handler_;
  std::unique_ptr<TemplateURLServiceFactoryTestUtil> dse_factory_util_;
  raw_ptr<TemplateURLService> template_url_service;

  const content::TestWebUI::CallData& GetCallData() {
    return *test_web_ui_.call_data().back();
  }

  Browser* browser() { return browser_.get(); }
};

void ClearBrowsingDataHandlerUnitTest::SetUp() {
  feature_list_.InitWithFeatures({toast_features::kClearBrowsingDataToast}, {});

  testing_profile_manager = std::make_unique<TestingProfileManager>(
      TestingBrowserProcess::GetGlobal());
  ASSERT_TRUE(testing_profile_manager->SetUp());

  TestingProfile::Builder builder;
  profile_ = builder.Build();

  profile_->GetTestingPrefService()->registry()->RegisterBooleanPref(
      kTestingDatatypePref, true);

  browser_window_ = std::make_unique<TestBrowserWindow>();
  Browser::CreateParams params(profile_.get(), /*user_gesture*/ true);
  params.type = Browser::TYPE_NORMAL;
  params.window = browser_window_.get();
  browser_.reset(Browser::Create(params));

  std::unique_ptr<tabs::TabModel> tab_model = std::make_unique<tabs::TabModel>(
      content::WebContents::Create(
          content::WebContents::CreateParams(profile_.get())),
      browser()->GetTabStripModel());
  browser()->GetTabStripModel()->AppendTab(std::move(tab_model), true);

  test_web_ui_.set_web_contents(
      browser()->GetTabStripModel()->GetActiveWebContents());
  test_web_ui_.ClearTrackedCalls();

  dse_factory_util_ =
      std::make_unique<TemplateURLServiceFactoryTestUtil>(profile_.get());
  dse_factory_util_->VerifyLoad();
  template_url_service =
      TemplateURLServiceFactory::GetForProfile(profile_.get());

  handler_ = std::make_unique<TestingClearBrowsingDataHandler>(&test_web_ui_,
                                                               profile_.get());
  handler_->set_web_ui(&test_web_ui_);
  handler_->RegisterMessages();
  handler_->AllowJavascript();

  browser_task_environment_.RunUntilIdle();
}

void ClearBrowsingDataHandlerUnitTest::TearDown() {
  dse_factory_util_.reset();
  browser_->tab_strip_model()->CloseAllTabs();
  browser_ = nullptr;
  browser_window_ = nullptr;
}

void ClearBrowsingDataHandlerUnitTest::VerifySearchHistoryWebUIUpdate(
    const bool expected_is_non_google_dse,
    const std::u16string& expected_non_google_search_history_string) {
  // Verify the latest update if multiple, so iterate from the end.
  const std::vector<std::unique_ptr<content::TestWebUI::CallData>>& call_data =
      test_web_ui_.call_data();
  for (int i = call_data.size() - 1; i >= 0; --i) {
    const content::TestWebUI::CallData& data = *(call_data[i]);
    if (data.function_name() != "cr.webUIListenerCallback") {
      continue;
    }
    const std::string* event = data.arg1()->GetIfString();
    if (!event || *event != "update-sync-state") {
      continue;
    }
    const base::Value::Dict* arg2_dict = data.arg2()->GetIfDict();
    if (!arg2_dict) {
      continue;
    }
    ASSERT_THAT(arg2_dict->FindBool("isNonGoogleDse"),
                Optional(expected_is_non_google_dse));
    if (expected_is_non_google_dse) {
      std::u16string actual_non_google_search_history_string =
          base::UTF8ToUTF16(
              *arg2_dict->FindString("nonGoogleSearchHistoryString"));
      ASSERT_EQ(expected_non_google_search_history_string,
                actual_non_google_search_history_string);
    }
    return;
  }
  NOTREACHED();
}

TemplateURL* ClearBrowsingDataHandlerUnitTest::AddSearchEngine(
    const std::u16string& short_name,
    const GURL& searchable_url,
    int prepopulate_id,
    bool set_default) {
  TemplateURLData data;
  data.SetShortName(short_name);
  data.SetKeyword(short_name);
  data.SetURL(searchable_url.possibly_invalid_spec());
  data.favicon_url = TemplateURL::GenerateFaviconURL(searchable_url);
  data.prepopulate_id = prepopulate_id;
  TemplateURL* url =
      template_url_service->Add(std::make_unique<TemplateURL>(data));
  if (set_default) {
    template_url_service->SetUserSelectedDefaultSearchProvider(url);
  }
  return url;
}

TEST_F(ClearBrowsingDataHandlerUnitTest,
       ClearBrowsingData_EmmitsDeleteMetrics) {
  base::HistogramTester histogram_tester;
  base::Value::List args;

  args.Append("fooCallback");
  args.Append(base::Value::List());
  args.Append(1);

  test_web_ui_.HandleReceivedMessage("clearBrowsingData", args);

  const content::TestWebUI::CallData& call_data = GetCallData();
  ASSERT_EQ(3u, call_data.args().size());

  histogram_tester.ExpectBucketCount(
      "Privacy.DeleteBrowsingData.Action",
      browsing_data::DeleteBrowsingDataAction::kClearBrowsingDataDialog, 1);
}

TEST_F(ClearBrowsingDataHandlerUnitTest, ClearBrowsingData_ShowsToast) {
  EXPECT_FALSE(browser()->GetFeatures().toast_controller()->IsShowingToast());

  base::Value::List args;
  args.Append("fooCallback");
  args.Append(base::Value::List());
  args.Append(1);
  test_web_ui_.HandleReceivedMessage("clearBrowsingData", args);

  EXPECT_TRUE(browser()->GetFeatures().toast_controller()->IsShowingToast());
}

TEST_F(ClearBrowsingDataHandlerUnitTest, UpdateSyncState_GoogleDse) {
  handler_->UpdateSyncState();
  VerifySearchHistoryWebUIUpdate(false, u"");
}

TEST_F(ClearBrowsingDataHandlerUnitTest,
       UpdateSyncState_NonGoogleDsePrepopulated) {
  // Prepopulated search engines have an ID > 0.
  AddSearchEngine(u"SomeSE", GURL("https://somese.com?q={searchTerms}"), 1001,
                  true);

  // DSE changes should update the handler, no need to call |UpdateSyncState()|.
  VerifySearchHistoryWebUIUpdate(
      true, l10n_util::GetStringFUTF16(
                IDS_SETTINGS_CLEAR_NON_GOOGLE_SEARCH_HISTORY_PREPOPULATED_DSE,
                u"SomeSE"));
}

TEST_F(ClearBrowsingDataHandlerUnitTest,
       UpdateSyncState_NonGoogleDseNotPrepopulated) {
  // Custom search engines have a prepopulated ID of 0.
  AddSearchEngine(u"SomeSE", GURL("https://somese.com?q={searchTerms}"), 0,
                  true);

  // DSE changes should update the handler, no need to call |UpdateSyncState()|.
  VerifySearchHistoryWebUIUpdate(
      true,
      l10n_util::GetStringUTF16(
          IDS_SETTINGS_CLEAR_NON_GOOGLE_SEARCH_HISTORY_NON_PREPOPULATED_DSE));
}

TEST_F(ClearBrowsingDataHandlerUnitTest, HandleRestartCounters) {
  base::Value::List basic_args;
  basic_args.Append(true /* basic */);
  basic_args.Append(static_cast<int>(browsing_data::TimePeriod::LAST_HOUR));

  EXPECT_CALL(*(handler_->basic_counter()), Count());
  EXPECT_CALL(*(handler_->basic_counter()), SetBeginTime(_));

  EXPECT_CALL(*(handler_->advanced_counter()), Count()).Times(0);
  EXPECT_CALL(*(handler_->advanced_counter()), SetBeginTime(_)).Times(0);

  handler_->HandleRestartCounters(basic_args);

  // Test a different combination of parameters.
  testing::Mock::VerifyAndClearExpectations(handler_->basic_counter());
  testing::Mock::VerifyAndClearExpectations(handler_->advanced_counter());

  base::Value::List advanced_args;
  advanced_args.Append(false /* basic */);
  advanced_args.Append(static_cast<int>(browsing_data::TimePeriod::ALL_TIME));

  EXPECT_CALL(*(handler_->basic_counter()), Count()).Times(0);
  EXPECT_CALL(*(handler_->basic_counter()), SetBeginTime(_)).Times(0);

  EXPECT_CALL(*(handler_->advanced_counter()), Count());
  EXPECT_CALL(*(handler_->advanced_counter()),
              SetBeginTime(browsing_data::CalculateBeginDeleteTime(
                  browsing_data::TimePeriod::ALL_TIME)));

  handler_->HandleRestartCounters(advanced_args);
}

}  // namespace settings