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
|
// Copyright (c) 2011 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 "chrome/browser/autocomplete/autocomplete_popup_model.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"
class AutoCompletePopupModelTest : public testing::Test {
public:
AutoCompletePopupModelTest() {
}
virtual void SetUp();
virtual void TearDown();
protected:
AutocompleteMatch CreateMatch(const string16& keyword,
const string16& query_string,
AutocompleteMatch::Type type);
void RunTest(const char* input, AutocompleteMatch::Type type,
const char* keyword);
scoped_ptr<TestingProfile> profile_;
scoped_ptr<AutocompletePopupModel> model_;
scoped_ptr<AutocompleteEditModel> edit_model_;
};
void AutoCompletePopupModelTest::SetUp() {
profile_.reset(new TestingProfile());
profile_->CreateTemplateURLService();
edit_model_.reset(new AutocompleteEditModel(NULL, NULL, profile_.get()));
model_.reset(new AutocompletePopupModel(NULL, edit_model_.get()));
TemplateURLService* turl_model =
TemplateURLServiceFactory::GetForProfile(profile_.get());
turl_model->Load();
// Reset the default TemplateURL.
TemplateURL* default_t_url = new TemplateURL();
default_t_url->set_keyword(ASCIIToUTF16("t"));
default_t_url->SetURL("http://defaultturl/{searchTerms}", 0, 0);
turl_model->Add(default_t_url);
turl_model->SetDefaultSearchProvider(default_t_url);
ASSERT_NE(0, default_t_url->id());
// Create another TemplateURL for KeywordProvider.
TemplateURL* keyword_t_url = new TemplateURL();
keyword_t_url->set_short_name(ASCIIToUTF16("k"));
keyword_t_url->set_keyword(ASCIIToUTF16("k"));
keyword_t_url->SetURL("http://keyword/{searchTerms}", 0, 0);
turl_model->Add(keyword_t_url);
ASSERT_NE(0, keyword_t_url->id());
}
void AutoCompletePopupModelTest::TearDown() {
profile_.reset();
model_.reset();
edit_model_.reset();
}
AutocompleteMatch AutoCompletePopupModelTest::CreateMatch(
const string16& keyword,
const string16& query_string,
AutocompleteMatch::Type type) {
AutocompleteMatch match(NULL, 0, false, type);
match.contents = query_string;
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_.get());
if (!keyword.empty()) {
const TemplateURL* template_url =
template_url_service->GetTemplateURLForKeyword(keyword);
match.template_url =
TemplateURL::SupportsReplacement(template_url) ? template_url : NULL;
}
if (match.template_url)
match.fill_into_edit = match.template_url->keyword() + char16(' ');
else
match.template_url = template_url_service->GetDefaultSearchProvider();
match.fill_into_edit.append(query_string);
match.transition = keyword.empty() ?
content::PAGE_TRANSITION_GENERATED : content::PAGE_TRANSITION_KEYWORD;
return match;
}
void AutoCompletePopupModelTest::RunTest(const char* input,
AutocompleteMatch::Type type,
const char* keyword) {
string16 keyword16(ASCIIToUTF16(keyword));
string16 detected_keyword;
EXPECT_FALSE(model_->GetKeywordForMatch(
CreateMatch(keyword16, ASCIIToUTF16(input), type), &detected_keyword));
EXPECT_EQ(keyword16, detected_keyword);
}
TEST_F(AutoCompletePopupModelTest, GetKeywordForMatch) {
string16 keyword;
// Possible matches when the input is "tfoo"
RunTest("tfoo", AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, "");
RunTest("tfoo", AutocompleteMatch::SEARCH_HISTORY, "");
RunTest("tfoo", AutocompleteMatch::SEARCH_SUGGEST, "");
// Possible matches when the input is "t foo"
RunTest("foo", AutocompleteMatch::SEARCH_HISTORY, "t");
RunTest("foo", AutocompleteMatch::SEARCH_OTHER_ENGINE, "t");
// Possible matches when the input is "k foo"
RunTest("foo", AutocompleteMatch::SEARCH_HISTORY, "k");
RunTest("foo", AutocompleteMatch::SEARCH_OTHER_ENGINE, "k");
}
|