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