File: web_data_service_wrapper_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 (98 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webdata_services/web_data_service_wrapper.h"

#include <memory>
#include <utility>

#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "components/os_crypt/async/browser/test_utils.h"
#include "components/search_engines/keyword_table.h"
#include "components/search_engines/keyword_web_data_service.h"
#include "components/search_engines/template_url_data.h"
#include "components/webdata/common/web_data_results.h"
#include "components/webdata/common/web_data_service_consumer.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Helper class that stores request result.
class KeywordsConsumer : public WebDataServiceConsumer {
 public:
  ~KeywordsConsumer() override = default;

  void OnWebDataServiceRequestDone(
      WebDataServiceBase::Handle h,
      std::unique_ptr<WDTypedResult> result) override {
    keywords_ = std::move(result);
  }

  std::unique_ptr<WDTypedResult> keywords_;
};

class WebDataServiceWrapperTest : public testing::Test {
 public:
  WebDataServiceWrapperTest()
      : os_crypt_(os_crypt_async::GetTestOSCryptAsyncForTesting(
            /*is_sync_for_unittests=*/true)) {}

 protected:
  void SetUp() override { ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); }

  // Creates WebDataServiceWrapper under test.
  std::unique_ptr<WebDataServiceWrapper> CreateWebDataServiceWrapper() {
    return std::make_unique<WebDataServiceWrapper>(
        scoped_temp_dir_.GetPath(), "en_US",
        task_environment_.GetMainThreadTaskRunner(), base::DoNothing(),
        os_crypt_.get(),
        /*use_in_memory_autofill_account_database=*/false);
  }

  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};
  std::unique_ptr<os_crypt_async::OSCryptAsync> os_crypt_;
  base::ScopedTempDir scoped_temp_dir_;
};

}  // namespace

// This test ensures that KeywordWebDataService writes all pending operations
// on shutdown. This requires wrapped services to be closed in a proper order
// but also that asynchronous tasks are posted to DB sequence in a proper order
// (the task that closes database should be the last one).
TEST_F(WebDataServiceWrapperTest, ShutdownKeywordWebDataService) {
  TemplateURLData test_keyword;
  test_keyword.SetShortName(u"Foo Bar");
  test_keyword.SetKeyword(u"foo");
  test_keyword.SetURL("http://foo.bar");
  test_keyword.id = 1234;

  // Create WebDataServiceWrapper. Add a test keyword and perform shutdown.
  auto web_data_service_wrapper = CreateWebDataServiceWrapper();
  web_data_service_wrapper->GetKeywordWebData()->AddKeyword(test_keyword);
  web_data_service_wrapper->Shutdown();
  task_environment_.RunUntilIdle();
  web_data_service_wrapper.reset();

  // Create WebDataServiceWrapper again. Verify that the test keyword is present
  // in the database.
  web_data_service_wrapper = CreateWebDataServiceWrapper();
  KeywordsConsumer keywords_consumer;
  web_data_service_wrapper->GetKeywordWebData()->GetKeywords(
      &keywords_consumer);
  task_environment_.RunUntilIdle();
  ASSERT_TRUE(keywords_consumer.keywords_);
  ASSERT_EQ(KEYWORDS_RESULT, keywords_consumer.keywords_->GetType());
  WDKeywordsResult keyword_result =
      reinterpret_cast<const WDResult<WDKeywordsResult>*>(
          keywords_consumer.keywords_.get())
          ->GetValue();
  ASSERT_EQ(1u, keyword_result.keywords.size());
  EXPECT_EQ(test_keyword.short_name(), keyword_result.keywords[0].short_name());
  web_data_service_wrapper->Shutdown();
}