summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines/search_host_to_urls_map_unittest.cc
blob: 5e682aac285ef3e629c6e2d419e74c21aa8b5886 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/search_engines/search_host_to_urls_map.h"
#include "chrome/browser/search_engines/search_terms_data.h"
#include "chrome/browser/search_engines/template_url.h"
#include "testing/gtest/include/gtest/gtest.h"

typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;

// Basic functionality for the SearchHostToURLsMap tests.
class SearchHostToURLsMapTest : public testing::Test {
 public:
  SearchHostToURLsMapTest() {}

  virtual void SetUp();

 protected:
  scoped_ptr<SearchHostToURLsMap> provider_map_;
  scoped_ptr<TemplateURL> t_urls_[2];
  std::string host_;

  DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest);
};

void SearchHostToURLsMapTest::SetUp() {
  // Add some entries to the search host map.
  host_ = "www.unittest.com";
  TemplateURLData data;
  data.SetURL("http://" + host_ + "/path1");
  t_urls_[0].reset(new TemplateURL(NULL, data));
  data.SetURL("http://" + host_ + "/path2");
  t_urls_[1].reset(new TemplateURL(NULL, data));
  std::vector<TemplateURL*> template_urls;
  template_urls.push_back(t_urls_[0].get());
  template_urls.push_back(t_urls_[1].get());

  provider_map_.reset(new SearchHostToURLsMap);
  UIThreadSearchTermsData search_terms_data(NULL);
  provider_map_->Init(template_urls, search_terms_data);
}

TEST_F(SearchHostToURLsMapTest, Add) {
  std::string new_host = "example.com";
  TemplateURLData data;
  data.SetURL("http://" + new_host + "/");
  TemplateURL new_t_url(NULL, data);
  UIThreadSearchTermsData search_terms_data(NULL);
  provider_map_->Add(&new_t_url, search_terms_data);

  ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
}

TEST_F(SearchHostToURLsMapTest, Remove) {
  UIThreadSearchTermsData search_terms_data(NULL);
  provider_map_->Remove(t_urls_[0].get(), search_terms_data);

  const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
  ASSERT_EQ(t_urls_[1].get(), found_url);

  const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
  ASSERT_TRUE(urls != NULL);

  int url_count = 0;
  for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
    url_count++;
    ASSERT_EQ(t_urls_[1].get(), *i);
  }
  ASSERT_EQ(1, url_count);
}

TEST_F(SearchHostToURLsMapTest, GetTemplateURLForKnownHost) {
  const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
  ASSERT_TRUE(found_url == t_urls_[0].get() || found_url == t_urls_[1].get());
}

TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
  const TemplateURL* found_url =
      provider_map_->GetTemplateURLForHost("a" + host_);
  ASSERT_TRUE(found_url == NULL);
}

TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
  const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
  ASSERT_TRUE(urls != NULL);

  bool found_urls[arraysize(t_urls_)] = { false };

  for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
    const TemplateURL* url = *i;
    for (size_t i = 0; i < arraysize(found_urls); ++i) {
      if (url == t_urls_[i].get()) {
        found_urls[i] = true;
        break;
      }
    }
  }

  for (size_t i = 0; i < arraysize(found_urls); ++i)
    ASSERT_TRUE(found_urls[i]);
}

TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
  const SearchHostToURLsMap::TemplateURLSet* urls =
      provider_map_->GetURLsForHost("a" + host_);
  ASSERT_TRUE(urls == NULL);
}