Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: chrome/browser/extensions/api/discovery/discovery_api_unittest.cc

Issue 10388192: Initial unit tests for the discovery API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 // This file tests the chrome.alarms extension API.
6
7 #include "base/values.h"
8 #include "chrome/browser/browser_process_impl.h"
9 #include "chrome/browser/extensions/api/discovery/discovery_api.h"
10 #include "chrome/browser/extensions/api/discovery/suggested_link.h"
11 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h"
12 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_facto ry.h"
13 #include "chrome/browser/extensions/extension_function_test_utils.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/testing_browser_process.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace utils = extension_function_test_utils;
21
22 namespace {
23 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList;
24 } // namespace
25
26 namespace extensions {
27
28 class ExtensionDiscoveryTest : public BrowserWithTestWindowTest {
29 public:
30 virtual void SetUp() {
31 BrowserWithTestWindowTest::SetUp();
32 extension_ = utils::CreateEmptyExtensionWithLocation(Extension::LOAD);
33 }
34
35 // Runs a function and returns a pointer to a value, transferring ownership.
36 base::Value* RunFunctionWithExtension(
37 UIThreadExtensionFunction* function, const std::string& args) {
38 function->set_extension(extension_.get());
39 return utils::RunFunctionAndReturnResult(function, args, browser());
40 }
41
42 // Runs a function and ignores the return value.
43 void RunFunction(UIThreadExtensionFunction* function,
44 const std::string& args) {
45 scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args));
46 }
47
48 // Runs a function without argumentsand ignores the return value.
49 void RunFunctionWithoutArguments(UIThreadExtensionFunction* function,
50 const std::string& args) {
51 scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args));
52 }
53
54 // Runs a function, expect an error, and return it in a string.
55 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function,
56 const std::string& args) {
57 function->set_extension(extension_.get());
58 return utils::RunFunctionAndReturnError(function, args, browser());
59 }
60
61 const std::string& GetExtensionId() const {
62 return extension_->id();
63 }
64
65 protected:
66 scoped_refptr<Extension> extension_;
67 };
68
69 TEST_F(ExtensionDiscoveryTest, Suggest) {
70 RunFunction(new DiscoverySuggestFunction(),
71 "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\", "
72 "\"score\": 0.5}]");
73
74 extensions::SuggestedLinksRegistry* registry =
75 extensions::SuggestedLinksRegistryFactory::GetForProfile(
76 browser()->profile());
77
78 const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
79 ASSERT_EQ(1u, links->size());
80 ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
81 ASSERT_EQ("Google", links->at(0)->link_text());
82 ASSERT_DOUBLE_EQ(0.5, links->at(0)->score());
83 }
84
85 TEST_F(ExtensionDiscoveryTest, SuggestWithoutScore) {
86 RunFunction(new DiscoverySuggestFunction(),
87 "[{\"linkUrl\": \"https://amazon.com/\", \"linkText\": \"Amazon\"}]");
Aaron Boodman 2012/05/28 18:32:12 The JSON schema compiler-generated structs will al
beaudoin 2012/05/28 22:46:34 Done.
88
89 extensions::SuggestedLinksRegistry* registry =
90 extensions::SuggestedLinksRegistryFactory::GetForProfile(
91 browser()->profile());
92
93 const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
94 ASSERT_EQ(1u, links->size());
95 ASSERT_EQ("https://amazon.com/", links->at(0)->link_url());
96 ASSERT_EQ("Amazon", links->at(0)->link_text());
97 ASSERT_DOUBLE_EQ(1.0, links->at(0)->score()); // Score should default to 1.
98 }
99
100 TEST_F(ExtensionDiscoveryTest, SuggestTwiceSameUrl) {
101 // Suggesting the same URL a second time should override the first.
102 RunFunction(new DiscoverySuggestFunction(),
103 "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\", "
104 "\"score\": 0.5}]");
105 RunFunction(new DiscoverySuggestFunction(),
106 "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google2\", "
107 "\"score\": 0.1}]");
108
109 extensions::SuggestedLinksRegistry* registry =
110 extensions::SuggestedLinksRegistryFactory::GetForProfile(
111 browser()->profile());
112
113 const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
114 ASSERT_EQ(1u, links->size());
115 ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
116 ASSERT_EQ("Google2", links->at(0)->link_text());
117 ASSERT_DOUBLE_EQ(0.1, links->at(0)->score());
118 }
119
120 TEST_F(ExtensionDiscoveryTest, Remove) {
121 RunFunction(new DiscoverySuggestFunction(),
122 "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\"}]");
123 RunFunction(new DiscoverySuggestFunction(),
124 "[{\"linkUrl\": \"https://amazon.com/\", \"linkText\": \"Amazon\"}]");
125 RunFunction(new DiscoverySuggestFunction(),
126 "[{\"linkUrl\": \"http://www.youtube.com/watch?v=zH5bJSG0DZk\", "
127 "\"linkText\": \"YouTube\"}]");
128 RunFunction(new DiscoveryRemoveSuggestionFunction(),
129 "[\"https://amazon.com/\"]");
130
131 extensions::SuggestedLinksRegistry* registry =
132 extensions::SuggestedLinksRegistryFactory::GetForProfile(
133 browser()->profile());
134
135 const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
136 ASSERT_EQ(2u, links->size());
137 ASSERT_EQ("http://www.google.com", links->at(0)->link_url());
138 ASSERT_EQ("Google", links->at(0)->link_text());
139 ASSERT_DOUBLE_EQ(1.0, links->at(0)->score());
140 ASSERT_EQ("http://www.youtube.com/watch?v=zH5bJSG0DZk",
141 links->at(1)->link_url());
142 ASSERT_EQ("YouTube", links->at(1)->link_text());
143 ASSERT_DOUBLE_EQ(1.0, links->at(1)->score());
144 }
145
146 TEST_F(ExtensionDiscoveryTest, ClearAll) {
147 RunFunction(new DiscoverySuggestFunction(),
148 "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\"}]");
149 RunFunction(new DiscoverySuggestFunction(),
150 "[{\"linkUrl\": \"https://amazon.com/\", \"linkText\": \"Amazon\"}]");
151 RunFunction(new DiscoverySuggestFunction(),
152 "[{\"linkUrl\": \"http://www.youtube.com/watch?v=zH5bJSG0DZk\", "
153 "\"linkText\": \"YouTube\"}]");
154 RunFunction(new DiscoveryClearAllSuggestionsFunction(), "[]");
155
156 extensions::SuggestedLinksRegistry* registry =
157 extensions::SuggestedLinksRegistryFactory::GetForProfile(
158 browser()->profile());
159
160 const SuggestedLinkList* links = registry->GetAll(GetExtensionId());
161 ASSERT_EQ(0u, links->size());
162 }
163
164 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/README.txt ('k') | chrome/browser/extensions/api/discovery/suggested_links_registry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698