Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / search_engines / search_host_to_urls_map_unittest.cc
1 // Copyright 2014 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 "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "components/search_engines/search_host_to_urls_map.h"
8 #include "components/search_engines/search_terms_data.h"
9 #include "components/search_engines/template_url.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
13
14 // Basic functionality for the SearchHostToURLsMap tests.
15 class SearchHostToURLsMapTest : public testing::Test {
16  public:
17   SearchHostToURLsMapTest() {}
18
19   void SetUp() override;
20
21  protected:
22   scoped_ptr<SearchHostToURLsMap> provider_map_;
23   scoped_ptr<TemplateURL> t_urls_[2];
24   std::string host_;
25
26   DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest);
27 };
28
29 void SearchHostToURLsMapTest::SetUp() {
30   // Add some entries to the search host map.
31   host_ = "www.unittest.com";
32   TemplateURLData data;
33   data.SetURL("http://" + host_ + "/path1");
34   t_urls_[0].reset(new TemplateURL(data));
35   data.SetURL("http://" + host_ + "/path2");
36   t_urls_[1].reset(new TemplateURL(data));
37   std::vector<TemplateURL*> template_urls;
38   template_urls.push_back(t_urls_[0].get());
39   template_urls.push_back(t_urls_[1].get());
40
41   provider_map_.reset(new SearchHostToURLsMap);
42   provider_map_->Init(template_urls, SearchTermsData());
43 }
44
45 TEST_F(SearchHostToURLsMapTest, Add) {
46   std::string new_host = "example.com";
47   TemplateURLData data;
48   data.SetURL("http://" + new_host + "/");
49   TemplateURL new_t_url(data);
50   provider_map_->Add(&new_t_url, SearchTermsData());
51
52   ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
53 }
54
55 TEST_F(SearchHostToURLsMapTest, Remove) {
56   provider_map_->Remove(t_urls_[0].get());
57
58   const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
59   ASSERT_EQ(t_urls_[1].get(), found_url);
60
61   const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
62   ASSERT_TRUE(urls != NULL);
63
64   int url_count = 0;
65   for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
66     url_count++;
67     ASSERT_EQ(t_urls_[1].get(), *i);
68   }
69   ASSERT_EQ(1, url_count);
70 }
71
72 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForKnownHost) {
73   const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
74   ASSERT_TRUE(found_url == t_urls_[0].get() || found_url == t_urls_[1].get());
75 }
76
77 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
78   const TemplateURL* found_url =
79       provider_map_->GetTemplateURLForHost("a" + host_);
80   ASSERT_TRUE(found_url == NULL);
81 }
82
83 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
84   const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
85   ASSERT_TRUE(urls != NULL);
86
87   bool found_urls[arraysize(t_urls_)] = { false };
88
89   for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
90     const TemplateURL* url = *i;
91     for (size_t i = 0; i < arraysize(found_urls); ++i) {
92       if (url == t_urls_[i].get()) {
93         found_urls[i] = true;
94         break;
95       }
96     }
97   }
98
99   for (size_t i = 0; i < arraysize(found_urls); ++i)
100     ASSERT_TRUE(found_urls[i]);
101 }
102
103 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
104   const SearchHostToURLsMap::TemplateURLSet* urls =
105       provider_map_->GetURLsForHost("a" + host_);
106   ASSERT_TRUE(urls == NULL);
107 }