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
180
181
182
183
184
185
186
187
188
189
190
|
// 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.
// This file tests the chrome.alarms extension API.
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/discovery/discovery_api.h"
#include "chrome/browser/extensions/api/discovery/suggested_link.h"
#include "chrome/browser/extensions/api/discovery/suggested_links_registry.h"
#include "chrome/browser/extensions/api/discovery/suggested_links_registry_factory.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/api/experimental_discovery.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace utils = extension_function_test_utils;
namespace {
typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList;
typedef extensions::api::experimental_discovery::SuggestDetails SuggestDetails;
// Converts suggest details used as parameter into a JSON string.
std::string SuggestDetailsParamToJSON(const SuggestDetails& suggest_details) {
std::string result;
scoped_ptr<base::DictionaryValue> value = suggest_details.ToValue();
base::ListValue params;
params.Append(value.release()); // |params| takes ownership of |value|.
base::JSONWriter::Write(¶ms, &result);
return result;
}
// Converts the parameters to the API Suggest method to JSON (without score).
std::string UnscoredSuggestParamsToJSON(const std::string& link_url,
const std::string& link_text) {
SuggestDetails suggest_details;
suggest_details.link_url = link_url;
suggest_details.link_text = link_text;
return SuggestDetailsParamToJSON(suggest_details);
}
// Converts the parameters to the API Suggest method to JSON.
std::string SuggestParamsToJSON(const std::string& link_url,
const std::string& link_text,
double score) {
SuggestDetails suggest_details;
suggest_details.score.reset(new double(score));
suggest_details.link_url = link_url;
suggest_details.link_text = link_text;
return SuggestDetailsParamToJSON(suggest_details);
}
// Converts the parameters to the API RemoveSuggestion method to JSON.
std::string RemoveSuggestionParamsToJSON(const std::string& link_url) {
std::string result;
base::ListValue params;
params.Append(new base::StringValue(link_url));
base::JSONWriter::Write(¶ms, &result);
return result;
}
} // namespace
namespace extensions {
class ExtensionDiscoveryTest : public BrowserWithTestWindowTest {
public:
virtual void SetUp() {
BrowserWithTestWindowTest::SetUp();
extension_ = utils::CreateEmptyExtensionWithLocation(Manifest::UNPACKED);
}
// Runs a function and returns a pointer to a value, transferring ownership.
base::Value* RunFunctionWithExtension(
UIThreadExtensionFunction* function, const std::string& args) {
function->set_extension(extension_.get());
return utils::RunFunctionAndReturnSingleResult(function, args, browser());
}
// Runs a function and ignores the return value.
void RunFunction(UIThreadExtensionFunction* function,
const std::string& args) {
scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args));
}
const std::string& GetExtensionId() const {
return extension_->id();
}
protected:
scoped_refptr<Extension> extension_;
};
TEST_F(ExtensionDiscoveryTest, Suggest) {
RunFunction(new DiscoverySuggestFunction(),
SuggestParamsToJSON("http://www.google.com", "Google", 0.5));
extensions::SuggestedLinksRegistry* registry =
extensions::SuggestedLinksRegistryFactory::GetForProfile(
browser()->profile());
const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
ASSERT_EQ(1u, links->size());
ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
ASSERT_EQ("Google", links->at(0)->link_text());
ASSERT_DOUBLE_EQ(0.5, links->at(0)->score());
}
TEST_F(ExtensionDiscoveryTest, SuggestWithoutScore) {
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon"));
extensions::SuggestedLinksRegistry* registry =
extensions::SuggestedLinksRegistryFactory::GetForProfile(
browser()->profile());
const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
ASSERT_EQ(1u, links->size());
ASSERT_EQ("https://amazon.com/", links->at(0)->link_url());
ASSERT_EQ("Amazon", links->at(0)->link_text());
ASSERT_DOUBLE_EQ(1.0, links->at(0)->score()); // Score should default to 1.
}
TEST_F(ExtensionDiscoveryTest, SuggestTwiceSameUrl) {
// Suggesting the same URL a second time should override the first.
RunFunction(new DiscoverySuggestFunction(),
SuggestParamsToJSON("http://www.google.com", "Google", 0.5));
RunFunction(new DiscoverySuggestFunction(),
SuggestParamsToJSON("http://www.google.com", "Google2", 0.1));
extensions::SuggestedLinksRegistry* registry =
extensions::SuggestedLinksRegistryFactory::GetForProfile(
browser()->profile());
const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
ASSERT_EQ(1u, links->size());
ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
ASSERT_EQ("Google2", links->at(0)->link_text());
ASSERT_DOUBLE_EQ(0.1, links->at(0)->score());
}
TEST_F(ExtensionDiscoveryTest, Remove) {
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("http://www.google.com", "Google"));
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon"));
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("http://www.youtube.com/watch?v=zH5bJSG0DZk",
"YouTube"));
RunFunction(new DiscoveryRemoveSuggestionFunction(),
RemoveSuggestionParamsToJSON("https://amazon.com/"));
extensions::SuggestedLinksRegistry* registry =
extensions::SuggestedLinksRegistryFactory::GetForProfile(
browser()->profile());
const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
ASSERT_EQ(2u, links->size());
ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
ASSERT_EQ("Google", links->at(0)->link_text());
ASSERT_DOUBLE_EQ(1.0, links->at(0)->score());
ASSERT_EQ("http://www.youtube.com/watch?v=zH5bJSG0DZk",
links->at(1)->link_url());
ASSERT_EQ("YouTube", links->at(1)->link_text());
ASSERT_DOUBLE_EQ(1.0, links->at(1)->score());
}
TEST_F(ExtensionDiscoveryTest, ClearAll) {
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("http://www.google.com", "Google"));
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon"));
RunFunction(new DiscoverySuggestFunction(),
UnscoredSuggestParamsToJSON("http://www.youtube.com/watch?v=zH5bJSG0DZk",
"YouTube"));
RunFunction(new DiscoveryClearAllSuggestionsFunction(), "[]");
extensions::SuggestedLinksRegistry* registry =
extensions::SuggestedLinksRegistryFactory::GetForProfile(
browser()->profile());
const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
ASSERT_EQ(0u, links->size());
}
} // namespace extensions
|