| OLD | NEW |
| (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/json/json_writer.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/browser_process_impl.h" |
| 10 #include "chrome/browser/extensions/api/discovery/discovery_api.h" |
| 11 #include "chrome/browser/extensions/api/discovery/suggested_link.h" |
| 12 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h" |
| 13 #include "chrome/browser/extensions/api/discovery/suggested_links_registry_facto
ry.h" |
| 14 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/common/extensions/api/experimental_discovery.h" |
| 18 #include "chrome/test/base/browser_with_test_window_test.h" |
| 19 #include "chrome/test/base/testing_browser_process.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace utils = extension_function_test_utils; |
| 23 |
| 24 namespace { |
| 25 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList; |
| 26 typedef extensions::api::experimental_discovery::SuggestDetails SuggestDetails; |
| 27 |
| 28 // Converts suggest details used as parameter into a JSON string. |
| 29 std::string SuggestDetailsParamToJSON(const SuggestDetails& suggest_details) { |
| 30 std::string result; |
| 31 scoped_ptr<base::DictionaryValue> value = suggest_details.ToValue(); |
| 32 base::ListValue params; |
| 33 params.Append(value.release()); // |params| takes ownership of |value|. |
| 34 base::JSONWriter::Write(¶ms, &result); |
| 35 return result; |
| 36 } |
| 37 |
| 38 // Converts the parameters to the API Suggest method to JSON (without score). |
| 39 std::string UnscoredSuggestParamsToJSON(const std::string& link_url, |
| 40 const std::string& link_text) { |
| 41 SuggestDetails suggest_details; |
| 42 suggest_details.link_url = link_url; |
| 43 suggest_details.link_text = link_text; |
| 44 return SuggestDetailsParamToJSON(suggest_details); |
| 45 } |
| 46 |
| 47 |
| 48 // Converts the parameters to the API Suggest method to JSON. |
| 49 std::string SuggestParamsToJSON(const std::string& link_url, |
| 50 const std::string& link_text, |
| 51 double score) { |
| 52 SuggestDetails suggest_details; |
| 53 suggest_details.score.reset(new double(score)); |
| 54 suggest_details.link_url = link_url; |
| 55 suggest_details.link_text = link_text; |
| 56 return SuggestDetailsParamToJSON(suggest_details); |
| 57 } |
| 58 |
| 59 // Converts the parameters to the API RemoveSuggestion method to JSON. |
| 60 std::string RemoveSuggestionParamsToJSON(const std::string& link_url) { |
| 61 std::string result; |
| 62 base::ListValue params; |
| 63 params.Append(base::Value::CreateStringValue(link_url)); |
| 64 base::JSONWriter::Write(¶ms, &result); |
| 65 return result; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 namespace extensions { |
| 71 |
| 72 class ExtensionDiscoveryTest : public BrowserWithTestWindowTest { |
| 73 public: |
| 74 virtual void SetUp() { |
| 75 BrowserWithTestWindowTest::SetUp(); |
| 76 extension_ = utils::CreateEmptyExtensionWithLocation(Extension::LOAD); |
| 77 } |
| 78 |
| 79 // Runs a function and returns a pointer to a value, transferring ownership. |
| 80 base::Value* RunFunctionWithExtension( |
| 81 UIThreadExtensionFunction* function, const std::string& args) { |
| 82 function->set_extension(extension_.get()); |
| 83 return utils::RunFunctionAndReturnResult(function, args, browser()); |
| 84 } |
| 85 |
| 86 // Runs a function and ignores the return value. |
| 87 void RunFunction(UIThreadExtensionFunction* function, |
| 88 const std::string& args) { |
| 89 scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args)); |
| 90 } |
| 91 |
| 92 // Runs a function without argumentsand ignores the return value. |
| 93 void RunFunctionWithoutArguments(UIThreadExtensionFunction* function, |
| 94 const std::string& args) { |
| 95 scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args)); |
| 96 } |
| 97 |
| 98 // Runs a function, expect an error, and return it in a string. |
| 99 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| 100 const std::string& args) { |
| 101 function->set_extension(extension_.get()); |
| 102 return utils::RunFunctionAndReturnError(function, args, browser()); |
| 103 } |
| 104 |
| 105 const std::string& GetExtensionId() const { |
| 106 return extension_->id(); |
| 107 } |
| 108 |
| 109 protected: |
| 110 scoped_refptr<Extension> extension_; |
| 111 }; |
| 112 |
| 113 TEST_F(ExtensionDiscoveryTest, Suggest) { |
| 114 RunFunction(new DiscoverySuggestFunction(), |
| 115 SuggestParamsToJSON("http://www.google.com", "Google", 0.5)); |
| 116 |
| 117 extensions::SuggestedLinksRegistry* registry = |
| 118 extensions::SuggestedLinksRegistryFactory::GetForProfile( |
| 119 browser()->profile()); |
| 120 |
| 121 const SuggestedLinkList* links = registry->GetAll(GetExtensionId()); |
| 122 ASSERT_EQ(1u, links->size()); |
| 123 ASSERT_EQ("http://www.google.com", links->at(0)->link_url()); |
| 124 ASSERT_EQ("Google", links->at(0)->link_text()); |
| 125 ASSERT_DOUBLE_EQ(0.5, links->at(0)->score()); |
| 126 } |
| 127 |
| 128 TEST_F(ExtensionDiscoveryTest, SuggestWithoutScore) { |
| 129 RunFunction(new DiscoverySuggestFunction(), |
| 130 UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon")); |
| 131 |
| 132 extensions::SuggestedLinksRegistry* registry = |
| 133 extensions::SuggestedLinksRegistryFactory::GetForProfile( |
| 134 browser()->profile()); |
| 135 |
| 136 const SuggestedLinkList* links = registry->GetAll(GetExtensionId()); |
| 137 ASSERT_EQ(1u, links->size()); |
| 138 ASSERT_EQ("https://amazon.com/", links->at(0)->link_url()); |
| 139 ASSERT_EQ("Amazon", links->at(0)->link_text()); |
| 140 ASSERT_DOUBLE_EQ(1.0, links->at(0)->score()); // Score should default to 1. |
| 141 } |
| 142 |
| 143 TEST_F(ExtensionDiscoveryTest, SuggestTwiceSameUrl) { |
| 144 // Suggesting the same URL a second time should override the first. |
| 145 RunFunction(new DiscoverySuggestFunction(), |
| 146 SuggestParamsToJSON("http://www.google.com", "Google", 0.5)); |
| 147 RunFunction(new DiscoverySuggestFunction(), |
| 148 SuggestParamsToJSON("http://www.google.com", "Google2", 0.1)); |
| 149 |
| 150 extensions::SuggestedLinksRegistry* registry = |
| 151 extensions::SuggestedLinksRegistryFactory::GetForProfile( |
| 152 browser()->profile()); |
| 153 |
| 154 const SuggestedLinkList* links = registry->GetAll(GetExtensionId()); |
| 155 ASSERT_EQ(1u, links->size()); |
| 156 ASSERT_EQ("http://www.google.com", links->at(0)->link_url()); |
| 157 ASSERT_EQ("Google2", links->at(0)->link_text()); |
| 158 ASSERT_DOUBLE_EQ(0.1, links->at(0)->score()); |
| 159 } |
| 160 |
| 161 TEST_F(ExtensionDiscoveryTest, Remove) { |
| 162 RunFunction(new DiscoverySuggestFunction(), |
| 163 UnscoredSuggestParamsToJSON("http://www.google.com", "Google")); |
| 164 RunFunction(new DiscoverySuggestFunction(), |
| 165 UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon")); |
| 166 RunFunction(new DiscoverySuggestFunction(), |
| 167 UnscoredSuggestParamsToJSON("http://www.youtube.com/watch?v=zH5bJSG0DZk", |
| 168 "YouTube")); |
| 169 RunFunction(new DiscoveryRemoveSuggestionFunction(), |
| 170 RemoveSuggestionParamsToJSON("https://amazon.com/")); |
| 171 |
| 172 extensions::SuggestedLinksRegistry* registry = |
| 173 extensions::SuggestedLinksRegistryFactory::GetForProfile( |
| 174 browser()->profile()); |
| 175 |
| 176 const SuggestedLinkList* links = registry->GetAll(GetExtensionId()); |
| 177 ASSERT_EQ(2u, links->size()); |
| 178 ASSERT_EQ("http://www.google.com", links->at(0)->link_url()); |
| 179 ASSERT_EQ("Google", links->at(0)->link_text()); |
| 180 ASSERT_DOUBLE_EQ(1.0, links->at(0)->score()); |
| 181 ASSERT_EQ("http://www.youtube.com/watch?v=zH5bJSG0DZk", |
| 182 links->at(1)->link_url()); |
| 183 ASSERT_EQ("YouTube", links->at(1)->link_text()); |
| 184 ASSERT_DOUBLE_EQ(1.0, links->at(1)->score()); |
| 185 } |
| 186 |
| 187 TEST_F(ExtensionDiscoveryTest, ClearAll) { |
| 188 RunFunction(new DiscoverySuggestFunction(), |
| 189 UnscoredSuggestParamsToJSON("http://www.google.com", "Google")); |
| 190 RunFunction(new DiscoverySuggestFunction(), |
| 191 UnscoredSuggestParamsToJSON("https://amazon.com/", "Amazon")); |
| 192 RunFunction(new DiscoverySuggestFunction(), |
| 193 UnscoredSuggestParamsToJSON("http://www.youtube.com/watch?v=zH5bJSG0DZk", |
| 194 "YouTube")); |
| 195 RunFunction(new DiscoveryClearAllSuggestionsFunction(), "[]"); |
| 196 |
| 197 extensions::SuggestedLinksRegistry* registry = |
| 198 extensions::SuggestedLinksRegistryFactory::GetForProfile( |
| 199 browser()->profile()); |
| 200 |
| 201 const SuggestedLinkList* links = registry->GetAll(GetExtensionId()); |
| 202 ASSERT_EQ(0u, links->size()); |
| 203 } |
| 204 |
| 205 } // namespace extensions |
| OLD | NEW |