summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete/history_contents_provider_unittest.cc
blob: d84ec788d3c95261c0b17712cf3f0b89e28cabe8 (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
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
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/autocomplete/autocomplete.h"
#include "chrome/browser/autocomplete/history_contents_provider.h"
#include "chrome/browser/history/history.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

struct TestEntry {
  const char* url;
  const wchar_t* title;
  const wchar_t* body;
} test_entries[] = {
  {"http://www.google.com/1", L"PAGEONE 1",   L"FOO some body text"},
  {"http://www.google.com/2", L"PAGEONE 2",   L"FOO some more blah blah"},
  {"http://www.google.com/3", L"PAGETHREE 3", L"BAR some hello world for you"},
};

// For comparing TestEntry.url with wide strings generated by the autocomplete
// code
bool UrlIs(const char* url, const std::wstring& str) {
  return WideToUTF8(str) == std::string(url);
}

class HistoryContentsProviderTest : public testing::Test,
                                    public ACProviderListener {
 public:

  void RunQuery(const AutocompleteInput& input,
                bool minimal_changes,
                bool synchronous_only) {
    provider_->Start(input, minimal_changes, synchronous_only);

    // When we're waiting for asynchronous messages, we have to spin the message
    // loop. This will be exited in the OnProviderUpdate function when complete.
    if (!synchronous_only)
      MessageLoop::current()->Run();
  }

  const ACMatches& matches() const { return provider_->matches(); }

 private:
  // testing::Test
  virtual void SetUp() {
    PathService::Get(base::DIR_TEMP, &history_dir_);
    file_util::AppendToPath(&history_dir_, L"HistoryContentProviderTest");
    file_util::Delete(history_dir_, true);  // Normally won't exist.
    file_util::CreateDirectoryW(history_dir_);

    history_service_ = new HistoryService;
    history_service_->Init(history_dir_);

    // Populate history.
    for (int i = 0; i < arraysize(test_entries); i++) {
      // We need the ID scope and page ID so that the visit tracker can find it.
      // We just use the index for the page ID below.
      const void* id_scope = reinterpret_cast<void*>(1);
      GURL url(test_entries[i].url);

      // Add everything in order of time. We don't want to have a time that
      // is "right now" or it will nondeterministically appear in the results.
      Time t = Time::Now() - TimeDelta::FromDays(arraysize(test_entries) + i);

      history_service_->AddPage(url, t, id_scope, i, GURL(),
          PageTransition::LINK, HistoryService::RedirectList());
      history_service_->SetPageTitle(url, test_entries[i].title);
      history_service_->SetPageContents(url, test_entries[i].body);
    }

    provider_ = new HistoryContentsProvider(this, history_service_);
  }

  virtual void TearDown() {
    history_service_->SetOnBackendDestroyTask(new MessageLoop::QuitTask);
    history_service_->Cleanup();
    provider_ = NULL;
    history_service_ = NULL;

    // Wait for history thread to complete (the QuitTask will cause it to exit
    // on destruction). Note: if this never terminates, somebody is probably
    // leaking a reference to the history backend, so it never calls our
    // destroy task.
    MessageLoop::current()->Run();

    file_util::Delete(history_dir_, true);
  }

  // ACProviderListener
  virtual void OnProviderUpdate(bool updated_matches) {
    // When we quit, the test will get back control.
    MessageLoop::current()->Quit();
  }

  std::wstring history_dir_;

  scoped_refptr<HistoryContentsProvider> provider_;
  scoped_refptr<HistoryService> history_service_;
};

}  // namespace

TEST_F(HistoryContentsProviderTest, Body) {
  AutocompleteInput input(L"FOO", std::wstring(), true);
  RunQuery(input, false, false);

  // The results should be the first two pages, in decreasing order.
  const ACMatches& m = matches();
  ASSERT_EQ(2, m.size());
  EXPECT_TRUE(UrlIs(test_entries[1].url, m[0].destination_url));
  EXPECT_STREQ(test_entries[1].title, m[0].description.c_str());
  EXPECT_TRUE(UrlIs(test_entries[0].url, m[1].destination_url));
  EXPECT_STREQ(test_entries[0].title, m[1].description.c_str());
}

TEST_F(HistoryContentsProviderTest, Title) {
  AutocompleteInput input(L"PAGEONE", std::wstring(), true);
  RunQuery(input, false, false);

  // The results should be the first two pages.
  const ACMatches& m = matches();
  ASSERT_EQ(2, m.size());
  EXPECT_TRUE(UrlIs(test_entries[1].url, m[0].destination_url));
  EXPECT_STREQ(test_entries[1].title, m[0].description.c_str());
  EXPECT_TRUE(UrlIs(test_entries[0].url, m[1].destination_url));
  EXPECT_STREQ(test_entries[0].title, m[1].description.c_str());
}

// The "minimal changes" flag should mean that we don't re-query the DB.
TEST_F(HistoryContentsProviderTest, MinimalChanges) {
  AutocompleteInput input(L"PAGEONE", std::wstring(), true);

  // A minimal changes request when there have been no real queries should
  // give us no results.
  RunQuery(input, true, true);
  const ACMatches& m1 = matches();
  EXPECT_EQ(0, m1.size());

  // Now do a "regular" query to get the results.
  RunQuery(input, false, false);
  const ACMatches& m2 = matches();
  EXPECT_EQ(2, m2.size());

  // Now do a minimal one where we want synchronous results, and the results
  // should still be there.
  RunQuery(input, true, true);
  const ACMatches& m3 = matches();
  EXPECT_EQ(2, m3.size());
}