Chromium Code Reviews| Index: chrome/browser/extensions/api/discovery/discovery_api_unittest.cc |
| diff --git a/chrome/browser/extensions/api/discovery/discovery_api_unittest.cc b/chrome/browser/extensions/api/discovery/discovery_api_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8b2b4eac964fc608e5f69944905d558ce1783b67 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/discovery/discovery_api_unittest.cc |
| @@ -0,0 +1,164 @@ |
| +// 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/values.h" |
| +#include "chrome/browser/browser_process_impl.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/test/base/browser_with_test_window_test.h" |
| +#include "chrome/test/base/testing_browser_process.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace utils = extension_function_test_utils; |
| + |
| +namespace { |
| +typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList; |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +class ExtensionDiscoveryTest : public BrowserWithTestWindowTest { |
| + public: |
| + virtual void SetUp() { |
| + BrowserWithTestWindowTest::SetUp(); |
| + extension_ = utils::CreateEmptyExtensionWithLocation(Extension::LOAD); |
| + } |
| + |
| + // 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::RunFunctionAndReturnResult(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)); |
| + } |
| + |
| + // Runs a function without argumentsand ignores the return value. |
| + void RunFunctionWithoutArguments(UIThreadExtensionFunction* function, |
| + const std::string& args) { |
| + scoped_ptr<base::Value> result(RunFunctionWithExtension(function, args)); |
| + } |
| + |
| + // Runs a function, expect an error, and return it in a string. |
| + std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
| + const std::string& args) { |
| + function->set_extension(extension_.get()); |
| + return utils::RunFunctionAndReturnError(function, args, browser()); |
| + } |
| + |
| + const std::string& GetExtensionId() const { |
| + return extension_->id(); |
| + } |
| + |
| + protected: |
| + scoped_refptr<Extension> extension_; |
| +}; |
| + |
| +TEST_F(ExtensionDiscoveryTest, Suggest) { |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\", " |
| + "\"score\": 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(), |
| + "[{\"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.
|
| + |
| + 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(), |
| + "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\", " |
| + "\"score\": 0.5}]"); |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google2\", " |
| + "\"score\": 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(), |
| + "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\"}]"); |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"https://amazon.com/\", \"linkText\": \"Amazon\"}]"); |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"http://www.youtube.com/watch?v=zH5bJSG0DZk\", " |
| + "\"linkText\": \"YouTube\"}]"); |
| + RunFunction(new DiscoveryRemoveSuggestionFunction(), |
| + "[\"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(), |
| + "[{\"linkUrl\": \"http://www.google.com\", \"linkText\": \"Google\"}]"); |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"https://amazon.com/\", \"linkText\": \"Amazon\"}]"); |
| + RunFunction(new DiscoverySuggestFunction(), |
| + "[{\"linkUrl\": \"http://www.youtube.com/watch?v=zH5bJSG0DZk\", " |
| + "\"linkText\": \"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 |