Commit 737646f8 authored by zhaoqin@google.com's avatar zhaoqin@google.com

Revert 148720 - [Web Intents] Localization of intent titles for CWS suggestions


R=mpcomplete@chromium.org,gbillock@chromium.org
BUG=130432
TEST=CWSIntentsRegistryTest.LocalizeMatchingLocale:ExtensionL10nUtil.GetAllFallbackLocales:ExtensionL10nUtil.LoadMessageCatalogs*


Review URL: https://chromiumcodereview.appspot.com/10790087

TBR=groby@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10821068

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148751 0039d316-1c4b-4281-b951-d872f2087c98
parent e3a682b3
......@@ -10,8 +10,6 @@
#include "base/stl_util.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/extensions/extension_l10n_util.h"
#include "chrome/common/extensions/message_bundle.h"
#include "chrome/browser/intents/api_key.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/webdata/web_data_service.h"
......@@ -40,17 +38,6 @@ const char kMaxSuggestions[] = "15";
const char kCWSIntentServiceURL[] =
"https://www.googleapis.com/chromewebstore/v1.1b/items/intent";
// Determines if a string is a candidate for localization.
bool ShouldLocalize(const std::string& value) {
std::string::size_type index = 0;
index = value.find(extensions::MessageBundle::kMessageBegin);
if (index == std::string::npos)
return false;
index = value.find(extensions::MessageBundle::kMessageEnd, index);
return (index != std::string::npos);
}
// Parses a JSON |response| from the CWS into a list of suggested extensions,
// stored in |intents|. |intents| must not be NULL.
void ParseResponse(const std::string& response,
......@@ -105,38 +92,6 @@ void ParseResponse(const std::string& response,
continue;
info.icon_url = GURL(url_string);
// Need to parse CWS reply, since it is not pre-l10n'd.
ListValue* all_locales = NULL;
if (ShouldLocalize(UTF16ToUTF8(info.name)) &&
item->GetList("locale_data", &all_locales)) {
std::map<std::string, std::string> localized_title;
for (ListValue::const_iterator locale_iter(all_locales->begin());
locale_iter != all_locales->end(); ++locale_iter) {
DictionaryValue* locale = static_cast<DictionaryValue*>(*locale_iter);
std::string locale_id, title;
if (!locale->GetString("locale_string", &locale_id) ||
!locale->GetString("title", &title))
continue;
localized_title[locale_id] = title;
}
std::vector<std::string> valid_locales;
extension_l10n_util::GetAllFallbackLocales(
extension_l10n_util::CurrentLocaleOrDefault(),
"all",
&valid_locales);
for (std::vector<std::string>::iterator iter = valid_locales.begin();
iter != valid_locales.end(); ++iter) {
if (localized_title.count(*iter)) {
info.name = UTF8ToUTF16(localized_title[*iter]);
break;
}
}
}
intents->push_back(info);
}
}
......
......@@ -17,8 +17,6 @@ namespace net {
class URLRequestContextGetter;
}
class CWSIntentsRegistryForTest;
// Handles storing and retrieving of web intents in the web database.
// The registry provides filtering logic to retrieve specific types of intents.
class CWSIntentsRegistry : public ProfileKeyedService,
......@@ -58,7 +56,8 @@ class CWSIntentsRegistry : public ProfileKeyedService,
// Make sure that only CWSIntentsRegistryFactory can create an instance of
// CWSIntentsRegistry.
friend class CWSIntentsRegistryFactory;
friend class ::CWSIntentsRegistryForTest;
FRIEND_TEST_ALL_PREFIXES(CWSIntentsRegistryTest, ValidQuery);
FRIEND_TEST_ALL_PREFIXES(CWSIntentsRegistryTest, InvalidQuery);
struct IntentsQuery;
......
......@@ -8,29 +8,10 @@
#include "base/bind_helpers.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/extensions/extension_l10n_util.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
// Create a CWSIntentsRegistry proxy for testing purposes.
// Needs to be non-anonymous so it can be friended.
class CWSIntentsRegistryForTest {
public:
CWSIntentsRegistryForTest() : registry_(NULL) {}
~CWSIntentsRegistryForTest() { delete registry_; }
void reset(net::URLRequestContextGetter* context) {
delete registry_;
registry_ = new CWSIntentsRegistry(context);
}
CWSIntentsRegistry* operator->() { return registry_; }
private:
CWSIntentsRegistry* registry_;
};
namespace {
const char kCWSResponseInvalid[] =
......@@ -73,66 +54,10 @@ const char kCWSResponseValid[] =
" \"family_safe\":true,"
" \"icon_url\":\"http://qa-lighthouse.sandbox.google.com/image/"
"QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ\"}]}";
const char kCWSResponseValidL10n[] =
"{\"kind\":\"chromewebstore#itemList\","
" \"total_items\":1,"
" \"start_index\":0,"
" \"items\":[ "
" {\"kind\":\"chromewebstore#item\","
" \"id\":\"nhkckhebbbncbkefhcpcgepcgfaclehe\","
" \"type\":\"APPLICATION\","
" \"num_ratings\":0,"
" \"average_rating\":0.0,"
" \"manifest\":\"{\\n\\\"update_url\\\":\\"
"\"http://0.tbhome_staging.dserver.download-qa.td.borg.google.com/"
"service/update2/crx\\\",\\n \\\"name\\\": \\\"__MSG_name__\\\""
",\\n \\\"description\\\": \\\"Do stuff\\\",\\n \\\"version\\\": "
"\\\"1.2.19\\\",\\n \\\"app\\\": {\\n \\\"urls\\\": [ \\n ],"
"\\n \\\"launch\\\": {\\n \\\"web_url\\\": \\"
"\"http://siddharthasaha.net/\\\"\\n }\\n },\\n \\\"icons\\\": "
"{\\n \\\"128\\\": \\\"icon128.png\\\"\\n },\\n \\\"permissions\\\":"
" [\\n \\\"unlimitedStorage\\\",\\n \\\"notifications\\\"\\n ],\\n"
" \\\"intents\\\": {\\n \\\"http://webintents.org/edit\\\" : {\\n "
"\\\"type\\\" : [\\\"image/png\\\", \\\"image/jpg\\\"],\\n \\\"path\\"
"\" : \\\"//services/edit\\\",\\n \\\"title\\\" : "
"\\\"Sample Editing Intent\\\",\\n \\\"disposition\\\" : \\\"inline\\"
"\"\\n },\\n \\\"http://webintents.org/share\\\" : "
"{\\n \\\"type\\\" : [\\\"text/plain\\\", \\\"image/jpg\\\"],"
"\\n \\\"path\\\" : \\\"//services/share\\\",\\n \\\"title\\\" : "
"\\\"Sample sharing Intent\\\",\\n \\\"disposition\\\" : "
"\\\"inline\\\"\\n }\\n }\\n}\\n\","
" \"family_safe\":true,"
" \"icon_url\":\"http://qa-lighthouse.sandbox.google.com/image/"
"QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ\","
"\"locale_data\": [";
const char kCWSResponseValidL10nPostfix[] =
"]}]}";
const char kLocaleDataEn[] =
"{"
" \"locale_string\": \"en\","
" \"title\": \"Localized EN\""
"}";
const char kLocaleDataDe[] =
"{"
" \"locale_string\": \"de\","
" \"title\": \"Localized DE\""
"}";
const char kLocaleDataAll[] =
"{"
" \"locale_string\": \"all\","
" \"title\": \"Localized ALL\""
"}";
const char kValidIconURL[] =
const char kValidIconURL[]=
"http://qa-lighthouse.sandbox.google.com/image/"
"QzPnRCYCBbBGI99ZkGxkp-NNJ488IkkiTyCgynFEeDTJHcw4tHl3csmjTQ";
const char kValidManifest[] =
const char kValidManifest[]=
"{\n\"update_url\":\"http://0.tbhome_staging.dserver.download-qa.td.borg."
"google.com/service/update2/crx\",\n \"name\": \"Sidd's Intent App\",\n"
" \"description\": \"Do stuff\",\n \"version\": \"1.2.19\",\n \"app\":"
......@@ -148,22 +73,9 @@ const char kValidManifest[] =
"\"//services/share\",\n \"title\" : \"Sample sharing Intent\",\n"
" \"disposition\" : \"inline\"\n }\n }\n}\n";
const char kEditAction[] = "http://webintents.org/edit";
const char kImagePngType[] = "image/png";
class ScopedLocale {
public:
ScopedLocale() : locale_(extension_l10n_util::CurrentLocaleOrDefault()) {}
~ScopedLocale() { extension_l10n_util::SetProcessLocale(locale_); }
std::string locale_;
};
class CWSIntentsRegistryTest : public testing::Test {
public:
virtual void SetUp() {
scoped_refptr<TestURLRequestContextGetter> context_getter_(
new TestURLRequestContextGetter(ui_loop_.message_loop_proxy()));
registry_.reset(context_getter_);
CWSIntentsRegistryTest() : test_factory_(NULL) {
}
virtual void TearDown() {
......@@ -180,34 +92,35 @@ class CWSIntentsRegistryTest : public testing::Test {
extensions_ = extensions;
}
void RunRequest(const std::string& action, const std::string& mime,
void SetFakeResponse(const std::string& action, const std::string& mime,
const std::string& response) {
extensions_.clear();
net::FakeURLFetcherFactory test_factory;
test_factory.SetFakeResponse(
test_factory_.SetFakeResponse(
CWSIntentsRegistry::BuildQueryURL(
ASCIIToUTF16(action),ASCIIToUTF16(mime)).spec(),
response, true);
registry_->GetIntentServices(ASCIIToUTF16(action),
ASCIIToUTF16(mime),
base::Bind(&CWSIntentsRegistryTest::Callback,
base::Unretained(this)));
WaitForResults();
}
CWSIntentsRegistry::IntentExtensionList extensions_;
net::FakeURLFetcherFactory test_factory_;
protected:
CWSIntentsRegistryForTest registry_;
CWSIntentsRegistry::IntentExtensionList extensions_;
MessageLoop ui_loop_;
};
} // namespace
TEST_F(CWSIntentsRegistryTest, ValidQuery) {
RunRequest(kEditAction, kImagePngType,kCWSResponseValid);
const scoped_refptr<TestURLRequestContextGetter> context_getter(
new TestURLRequestContextGetter(ui_loop_.message_loop_proxy()));
SetFakeResponse("http://webintents.org/edit", "*/png", kCWSResponseValid);
CWSIntentsRegistry registry(context_getter);
registry.GetIntentServices(ASCIIToUTF16("http://webintents.org/edit"),
ASCIIToUTF16("*/png"),
base::Bind(&CWSIntentsRegistryTest::Callback,
base::Unretained(this)));
WaitForResults();
ASSERT_EQ(1UL, extensions_.size());
EXPECT_EQ(0, extensions_[0].num_ratings);
......@@ -221,7 +134,17 @@ TEST_F(CWSIntentsRegistryTest, ValidQuery) {
}
TEST_F(CWSIntentsRegistryTest, InvalidQuery) {
RunRequest("foo", "foo",kCWSResponseInvalid);
const scoped_refptr<TestURLRequestContextGetter> context_getter(
new TestURLRequestContextGetter(ui_loop_.message_loop_proxy()));
SetFakeResponse("foo", "foo", kCWSResponseInvalid);
CWSIntentsRegistry registry(context_getter);
registry.GetIntentServices(ASCIIToUTF16("foo"),
ASCIIToUTF16("foo"),
base::Bind(&CWSIntentsRegistryTest::Callback,
base::Unretained(this)));
WaitForResults();
EXPECT_EQ(0UL, extensions_.size());
}
......@@ -234,48 +157,3 @@ TEST_F(CWSIntentsRegistryTest, BuildQueryURL) {
EXPECT_EQ(kExpectedURL, url.spec().substr(0, kExpectedURL.size()));
}
// Test for match to the application locale - i.e. if running in "en",
// registry will use locale_data for "en" key, with or without "all"
// locale_data present.
TEST_F(CWSIntentsRegistryTest, LocalizeMatchingLocale) {
ScopedLocale restoreLocaleOnExit;
std::string response = kCWSResponseValidL10n;
response += kLocaleDataEn + std::string(",");
response += kLocaleDataAll + std::string(",");
response += kLocaleDataDe;
response += kCWSResponseValidL10nPostfix;
// Picks the proper locale_data based on application locale.
extension_l10n_util::SetProcessLocale("en");
RunRequest(kEditAction, kImagePngType, response);
ASSERT_EQ(1UL, extensions_.size());
EXPECT_EQ(std::string("Localized EN"),
UTF16ToUTF8(extensions_[0].name));
extension_l10n_util::SetProcessLocale("de");
RunRequest(kEditAction, kImagePngType, response);
ASSERT_EQ(1UL, extensions_.size());
EXPECT_EQ(std::string("Localized DE"),
UTF16ToUTF8(extensions_[0].name));
// Falls back to locale_data for "All" if unknown application locale.
extension_l10n_util::SetProcessLocale("fr");
RunRequest(kEditAction, kImagePngType, std::string(kCWSResponseValidL10n) +
kLocaleDataAll + kCWSResponseValidL10nPostfix);
ASSERT_EQ(1UL, extensions_.size());
EXPECT_EQ(std::string("Localized ALL"),
UTF16ToUTF8(extensions_[0].name));
// Keeps original content if unknown application locale and no "all"
// localization data exists.
response = kCWSResponseValidL10n;
response += kLocaleDataEn;
response += kCWSResponseValidL10nPostfix;
extension_l10n_util::SetProcessLocale("de");
RunRequest(kEditAction, kImagePngType, response);
ASSERT_EQ(1UL, extensions_.size());
EXPECT_EQ(std::string("__MSG_name__"),
UTF16ToUTF8(extensions_[0].name));
}
......@@ -237,15 +237,6 @@ void GetAllLocales(std::set<std::string>* all_locales) {
}
}
void GetAllFallbackLocales(const std::string& application_locale,
const std::string& default_locale,
std::vector<std::string>* all_fallback_locales) {
DCHECK(all_fallback_locales);
if (!application_locale.empty() && application_locale != default_locale)
l10n_util::GetParentLocales(application_locale, all_fallback_locales);
all_fallback_locales->push_back(default_locale);
}
bool GetValidLocales(const FilePath& locale_path,
std::set<std::string>* valid_locales,
std::string* error) {
......@@ -307,9 +298,11 @@ extensions::MessageBundle* LoadMessageCatalogs(
const std::string& application_locale,
const std::set<std::string>& valid_locales,
std::string* error) {
// Order locales to load as current_locale, first_parent, ..., default_locale.
std::vector<std::string> all_fallback_locales;
GetAllFallbackLocales(application_locale, default_locale,
&all_fallback_locales);
if (!application_locale.empty() && application_locale != default_locale)
l10n_util::GetParentLocales(application_locale, &all_fallback_locales);
all_fallback_locales.push_back(default_locale);
std::vector<linked_ptr<DictionaryValue> > catalogs;
for (size_t i = 0; i < all_fallback_locales.size(); ++i) {
......
......@@ -68,13 +68,6 @@ std::string CurrentLocaleOrDefault();
// proper fallback.
void GetAllLocales(std::set<std::string>* all_locales);
// Provides a vector of all fallback locales for message localization.
// The vector is ordered by priority of locale - |application_locale|,
// first_parent, ..., |default_locale|.
void GetAllFallbackLocales(const std::string& application_locale,
const std::string& default_locale,
std::vector<std::string>* all_fallback_locales);
// Adds valid locales to the extension.
// 1. Do nothing if _locales directory is missing (not an error).
// 2. Get list of Chrome locales.
......
......@@ -617,14 +617,4 @@ TEST(ExtensionL10nUtil, ShouldRelocalizeManifestDifferentCurrentLocale) {
EXPECT_TRUE(extension_l10n_util::ShouldRelocalizeManifest(info));
}
TEST(ExtensionL10nUtil, GetAllFallbackLocales) {
std::vector<std::string> fallback_locales;
extension_l10n_util::GetAllFallbackLocales("en_US", "all", &fallback_locales);
ASSERT_EQ(3U, fallback_locales.size());
CHECK_EQ("en_US", fallback_locales[0]);
CHECK_EQ("en", fallback_locales[1]);
CHECK_EQ("all", fallback_locales[2]);
}
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment