Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / template_url_service_unittest.cc
1 // Copyright 2014 The Chromium Authors
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/search_engines/template_url_service.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10
11 #include "base/threading/platform_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 class TemplateURLServiceUnitTest : public testing::Test {
15  public:
16   TemplateURLServiceUnitTest()
17       : template_url_service_(/*initializers=*/nullptr, /*count=*/0) {}
18   TemplateURLService& template_url_service() { return template_url_service_; }
19
20  private:
21   TemplateURLService template_url_service_;
22 };
23
24 TEST_F(TemplateURLServiceUnitTest, SessionToken) {
25   // Subsequent calls always get the same token.
26   std::string token = template_url_service().GetSessionToken();
27   std::string token2 = template_url_service().GetSessionToken();
28   EXPECT_EQ(token, token2);
29   EXPECT_FALSE(token.empty());
30
31   // Calls do not regenerate a token.
32   template_url_service().current_token_ = "PRE-EXISTING TOKEN";
33   token = template_url_service().GetSessionToken();
34   EXPECT_EQ(token, "PRE-EXISTING TOKEN");
35
36   // ... unless the token has expired.
37   template_url_service().current_token_.clear();
38   const base::TimeDelta kSmallDelta = base::Milliseconds(1);
39   template_url_service().token_expiration_time_ =
40       base::TimeTicks::Now() - kSmallDelta;
41   token = template_url_service().GetSessionToken();
42   EXPECT_FALSE(token.empty());
43   EXPECT_EQ(token, template_url_service().current_token_);
44
45   // ... or cleared.
46   template_url_service().current_token_.clear();
47   template_url_service().ClearSessionToken();
48   token = template_url_service().GetSessionToken();
49   EXPECT_FALSE(token.empty());
50   EXPECT_EQ(token, template_url_service().current_token_);
51
52   // The expiration time is always updated.
53   template_url_service().GetSessionToken();
54   base::TimeTicks expiration_time_1 =
55       template_url_service().token_expiration_time_;
56   base::PlatformThread::Sleep(kSmallDelta);
57   template_url_service().GetSessionToken();
58   base::TimeTicks expiration_time_2 =
59       template_url_service().token_expiration_time_;
60   EXPECT_GT(expiration_time_2, expiration_time_1);
61   EXPECT_GE(expiration_time_2, expiration_time_1 + kSmallDelta);
62 }
63
64 TEST_F(TemplateURLServiceUnitTest, GenerateSearchURL) {
65   // Set the default search provider to a custom one.
66   TemplateURLData template_url_data;
67   template_url_data.SetURL("https://www.example.com/?q={searchTerms}");
68   template_url_service().SetUserSelectedDefaultSearchProvider(
69       template_url_service().Add(
70           std::make_unique<TemplateURL>(template_url_data)));
71
72   EXPECT_EQ(
73       "https://www.example.com/?q=foo",
74       template_url_service().GenerateSearchURLForDefaultSearchProvider(u"foo"));
75   EXPECT_EQ(
76       "https://www.example.com/?q=",
77       template_url_service().GenerateSearchURLForDefaultSearchProvider(u""));
78 }
79
80 TEST_F(TemplateURLServiceUnitTest, ExtractSearchMetadata) {
81   TemplateURLData template_url_data;
82   template_url_data.SetURL("https://www.example.com/?q={searchTerms}");
83   template_url_data.search_intent_params = {"gs_ssp", "si"};
84   template_url_service().SetUserSelectedDefaultSearchProvider(
85       template_url_service().Add(
86           std::make_unique<TemplateURL>(template_url_data)));
87
88   GURL input("https://www.example.com/?q=MyQuery&si=my_si&other_param=foobar");
89   auto result = template_url_service().ExtractSearchMetadata(input);
90   ASSERT_TRUE(result.has_value());
91
92   EXPECT_EQ(result->normalized_url,
93             "https://www.example.com/?si=my_si&q=myquery")
94       << "q parameter and si parameter should have been preserved. other_param "
95          "should be discarded.";
96   EXPECT_EQ(result->search_terms, u"myquery");
97 }