diff options
author | tbarzic@chromium.org <tbarzic@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-23 05:48:57 +0000 |
---|---|---|
committer | tbarzic@chromium.org <tbarzic@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-23 05:48:57 +0000 |
commit | df3d1cc3804697ef31a926c980b4da1d7efddbef (patch) | |
tree | 0c1e4665d1b98d6a7aec8be510f570145063f6ab | |
parent | f41da18890d075d126fbef0fc1f4e5b973d2d802 (diff) | |
download | chromium_src-df3d1cc3804697ef31a926c980b4da1d7efddbef.zip chromium_src-df3d1cc3804697ef31a926c980b4da1d7efddbef.tar.gz chromium_src-df3d1cc3804697ef31a926c980b4da1d7efddbef.tar.bz2 |
Wire up echoPrivate.getUserConsent function to use echo dialog.
NOTE: the function is not yet wired up in the echo extension
BUG=175070
Review URL: https://chromiumcodereview.appspot.com/14365022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@195735 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/extensions/echo_private_api.cc | 93 | ||||
-rw-r--r-- | chrome/browser/chromeos/extensions/echo_private_api.h | 34 | ||||
-rw-r--r-- | chrome/browser/chromeos/extensions/echo_private_apitest.cc | 161 | ||||
-rw-r--r-- | chrome/browser/chromeos/ui/echo_dialog_view.cc | 23 | ||||
-rw-r--r-- | chrome/browser/chromeos/ui/echo_dialog_view.h | 2 |
5 files changed, 236 insertions, 77 deletions
diff --git a/chrome/browser/chromeos/extensions/echo_private_api.cc b/chrome/browser/chromeos/extensions/echo_private_api.cc index 5cf7bed..2ccb905 100644 --- a/chrome/browser/chromeos/extensions/echo_private_api.cc +++ b/chrome/browser/chromeos/extensions/echo_private_api.cc @@ -7,15 +7,17 @@ #include <string> #include "base/bind.h" -#include "base/compiler_specific.h" #include "base/file_util.h" #include "base/location.h" #include "base/stringprintf.h" #include "base/time.h" -#include "base/values.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h" #include "chrome/browser/chromeos/settings/cros_settings.h" #include "chrome/browser/chromeos/system/statistics_provider.h" +#include "chrome/browser/chromeos/ui/echo_dialog_view.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_window.h" #include "chrome/common/extensions/api/echo_private.h" #include "chrome/common/extensions/extension.h" #include "content/public/browser/browser_thread.h" @@ -24,6 +26,15 @@ namespace echo_api = extensions::api::echo_private; using content::BrowserThread; +namespace { + +// URL of "More info" link shown in echo dialog in GetUserConsent function. +const char kMoreInfoLink[] = + "chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html?" + "answer=2677280"; + +} // namespace + EchoPrivateGetRegistrationCodeFunction:: EchoPrivateGetRegistrationCodeFunction() {} @@ -142,22 +153,41 @@ EchoPrivateGetUserConsentFunction::EchoPrivateGetUserConsentFunction() : redeem_offers_allowed_(false) { } +// static +scoped_refptr<EchoPrivateGetUserConsentFunction> +EchoPrivateGetUserConsentFunction::CreateForTest( + const DialogShownTestCallback& dialog_shown_callback) { + scoped_refptr<EchoPrivateGetUserConsentFunction> function( + new EchoPrivateGetUserConsentFunction()); + function->dialog_shown_callback_ = dialog_shown_callback; + return function; +} + EchoPrivateGetUserConsentFunction::~EchoPrivateGetUserConsentFunction() {} bool EchoPrivateGetUserConsentFunction::RunImpl() { - scoped_ptr<echo_api::GetUserConsent::Params> params = - echo_api::GetUserConsent::Params::Create(*args_); - EXTENSION_FUNCTION_VALIDATE(params); - - if (!GURL(params->consent_requester.origin).is_valid()) { - error_ = "Invalid origin."; - return false; - } - CheckRedeemOffersAllowed(); return true; } +void EchoPrivateGetUserConsentFunction::OnAccept() { + Finalize(true); +} + +void EchoPrivateGetUserConsentFunction::OnCancel() { + Finalize(false); +} + +void EchoPrivateGetUserConsentFunction::OnMoreInfoLinkClicked() { + chrome::NavigateParams params(profile(), + GURL(kMoreInfoLink), + content::PAGE_TRANSITION_LINK); + // Open the link in a new window. The echo dialog is modal, so the current + // window is useless until the dialog is closed. + params.disposition = NEW_WINDOW; + chrome::Navigate(¶ms); +} + void EchoPrivateGetUserConsentFunction::CheckRedeemOffersAllowed() { chromeos::CrosSettingsProvider::TrustedStatus status = chromeos::CrosSettings::Get()->PrepareTrustedValues(base::Bind( @@ -177,7 +207,44 @@ void EchoPrivateGetUserConsentFunction::OnRedeemOffersAllowedChecked( bool is_allowed) { redeem_offers_allowed_ = is_allowed; - // TODO(tbarzic): Implement dialogs to be used here. - results_ = echo_api::GetUserConsent::Results::Create(false); + scoped_ptr<echo_api::GetUserConsent::Params> params = + echo_api::GetUserConsent::Params::Create(*args_); + + // Verify that the passed origin URL is valid. + GURL service_origin = GURL(params->consent_requester.origin); + if (!service_origin.is_valid()) { + error_ = "Invalid origin."; + SendResponse(false); + return; + } + + // Add ref to ensure the function stays around until the dialog listener is + // called. The reference is release in |Finalize|. + AddRef(); + + // Create and show the dialog. + chromeos::EchoDialogView* dialog = new chromeos::EchoDialogView(this); + if (redeem_offers_allowed_) { + dialog->InitForEnabledEcho( + UTF8ToUTF16(params->consent_requester.service_name), + UTF8ToUTF16(params->consent_requester.origin)); + } else { + dialog->InitForDisabledEcho(); + } + dialog->Show(GetCurrentBrowser()->window()->GetNativeWindow()); + + // If there is a dialog_shown_callback_, invoke it with the created dialog. + if (!dialog_shown_callback_.is_null()) + dialog_shown_callback_.Run(dialog); +} + +void EchoPrivateGetUserConsentFunction::Finalize(bool consent) { + // Consent should not be true if offers redeeming is disabled. + CHECK(redeem_offers_allowed_ || !consent); + results_ = echo_api::GetUserConsent::Results::Create(consent); SendResponse(true); + + // Release the reference added in |OnRedeemOffersAllowedChecked|, before + // showing the dialog. + Release(); } diff --git a/chrome/browser/chromeos/extensions/echo_private_api.h b/chrome/browser/chromeos/extensions/echo_private_api.h index 41ff0d4..1d44362 100644 --- a/chrome/browser/chromeos/extensions/echo_private_api.h +++ b/chrome/browser/chromeos/extensions/echo_private_api.h @@ -6,8 +6,13 @@ #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_ECHO_PRIVATE_API_H_ #include "base/compiler_specific.h" +#include "chrome/browser/chromeos/ui/echo_dialog_listener.h" #include "chrome/browser/extensions/extension_function.h" +namespace chromeos { +class EchoDialogView; +} + class EchoPrivateGetRegistrationCodeFunction : public SyncExtensionFunction { public: EchoPrivateGetRegistrationCodeFunction(); @@ -58,14 +63,20 @@ class EchoPrivateCheckAllowRedeemOffersFunction // either asks user's consent to verify the device's eligibility for the offer, // or informs the user that the offers redeeming is disabled. // It returns whether the user consent was given. -// -// NOTE: Currently only the first part is implemented, and its result is kept in -// |redeem_offers_allowed_|. The function itself always returns false. -class EchoPrivateGetUserConsentFunction : public AsyncExtensionFunction { +class EchoPrivateGetUserConsentFunction + : public AsyncExtensionFunction, + public chromeos::EchoDialogListener { public: + // Type for the dialog shown callback used in tests. + typedef base::Callback<void(chromeos::EchoDialogView* dialog)> + DialogShownTestCallback; + EchoPrivateGetUserConsentFunction(); - bool redeem_offers_allowed() const { return redeem_offers_allowed_; } + // Creates the function with non-null dialog_shown_callback_. + // To be used in tests. + static scoped_refptr<EchoPrivateGetUserConsentFunction> CreateForTest( + const DialogShownTestCallback& dialog_shown_callback); protected: virtual ~EchoPrivateGetUserConsentFunction(); @@ -73,6 +84,11 @@ class EchoPrivateGetUserConsentFunction : public AsyncExtensionFunction { virtual bool RunImpl() OVERRIDE; private: + // chromeos::EchoDialogListener overrides. + virtual void OnAccept() OVERRIDE; + virtual void OnCancel() OVERRIDE; + virtual void OnMoreInfoLinkClicked() OVERRIDE; + // Checks whether "allow redeem ChromeOS registration offers" setting is // disabled in cros settings. It may be asynchronous if the needed settings // provider is not yet trusted. @@ -80,9 +96,13 @@ class EchoPrivateGetUserConsentFunction : public AsyncExtensionFunction { void CheckRedeemOffersAllowed(); void OnRedeemOffersAllowedChecked(bool is_allowed); - // Used only for test, and only until the rest of functionality is - // implemented. + // Sets result and calls SendResponse. + void Finalize(bool consent); + + // Result of |CheckRedeemOffersAllowed()|. bool redeem_offers_allowed_; + // Callback used in tests. Called after an echo dialog is shown. + DialogShownTestCallback dialog_shown_callback_; DECLARE_EXTENSION_FUNCTION("echoPrivate.getUserConsent", ECHOPRIVATE_GETUSERCONSENT) diff --git a/chrome/browser/chromeos/extensions/echo_private_apitest.cc b/chrome/browser/chromeos/extensions/echo_private_apitest.cc index ee76897..c42271e 100644 --- a/chrome/browser/chromeos/extensions/echo_private_apitest.cc +++ b/chrome/browser/chromeos/extensions/echo_private_apitest.cc @@ -4,21 +4,34 @@ #include "chrome/browser/chromeos/extensions/echo_private_api.h" +#include "base/bind.h" #include "base/command_line.h" -#include "chrome/browser/browser_process.h" +#include "base/message_loop_proxy.h" #include "chrome/browser/chromeos/settings/cros_settings.h" +#include "chrome/browser/chromeos/ui/echo_dialog_view.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_function_test_utils.h" -#include "chrome/browser/policy/browser_policy_connector.h" #include "chrome/common/chrome_switches.h" +#include "testing/gtest/include/gtest/gtest.h" namespace utils = extension_function_test_utils; -namespace { +namespace chromeos { class ExtensionEchoPrivateApiTest : public ExtensionApiTest { public: - ExtensionEchoPrivateApiTest() {} + enum DialogTestAction { + DIALOG_TEST_ACTION_NONE, + DIALOG_TEST_ACTION_ACCEPT, + DIALOG_TEST_ACTION_CANCEL, + }; + + ExtensionEchoPrivateApiTest() + : expected_dialog_buttons_(ui::DIALOG_BUTTON_NONE), + dialog_action_(DIALOG_TEST_ACTION_NONE), + dialog_invocation_count_(0) { + } + virtual ~ExtensionEchoPrivateApiTest() {} virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { @@ -28,6 +41,68 @@ class ExtensionEchoPrivateApiTest : public ExtensionApiTest { // provider. command_line->AppendSwitch(switches::kStubCrosSettings); } + + void RunDefaultGetUserFunctionAndExpectResultEquals(bool expected_result) { + scoped_refptr<EchoPrivateGetUserConsentFunction> function( + EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind( + &ExtensionEchoPrivateApiTest::OnDialogShown, this))); + function->set_has_callback(true); + + scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( + function, + "[{\"serviceName\":\"some_name\",\"origin\":\"http://chromium.org\"}]", + browser())); + + ASSERT_TRUE(result.get()); + ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); + + bool result_as_boolean = false; + ASSERT_TRUE(result->GetAsBoolean(&result_as_boolean)); + + EXPECT_EQ(expected_result, result_as_boolean); + } + + void OnDialogShown(chromeos::EchoDialogView* dialog) { + dialog_invocation_count_++; + ASSERT_LE(dialog_invocation_count_, 1); + + EXPECT_EQ(expected_dialog_buttons_, dialog->GetDialogButtons()); + + // Don't accept the dialog if the dialog buttons don't match expectation. + // Accepting a dialog which should not have accept option may crash the + // test. The test already failed, so it's ok to cancel the dialog. + DialogTestAction dialog_action = dialog_action_; + if (dialog_action == DIALOG_TEST_ACTION_ACCEPT && + expected_dialog_buttons_ != dialog->GetDialogButtons()) { + dialog_action = DIALOG_TEST_ACTION_CANCEL; + } + + // Perform test action on the dialog. + // The dialog should stay around until AcceptWindow or CancelWindow is + // called, so base::Unretained is safe. + if (dialog_action == DIALOG_TEST_ACTION_ACCEPT) { + base::MessageLoopProxy::current()->PostTask( + FROM_HERE, + base::Bind(base::IgnoreResult(&chromeos::EchoDialogView::Accept), + base::Unretained(dialog))); + } else if (dialog_action == DIALOG_TEST_ACTION_CANCEL) { + base::MessageLoopProxy::current()->PostTask( + FROM_HERE, + base::Bind(base::IgnoreResult(&chromeos::EchoDialogView::Cancel), + base::Unretained(dialog))); + } + } + + int dialog_invocation_count() const { + return dialog_invocation_count_; + } + + protected: + int expected_dialog_buttons_; + DialogTestAction dialog_action_; + + private: + int dialog_invocation_count_; }; IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, EchoTest) { @@ -37,8 +112,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, EchoTest) { IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, GetUserConsent_InvalidOrigin) { + expected_dialog_buttons_ = ui::DIALOG_BUTTON_NONE; + dialog_action_ = DIALOG_TEST_ACTION_NONE; + scoped_refptr<EchoPrivateGetUserConsentFunction> function( - new EchoPrivateGetUserConsentFunction()); + EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind( + &ExtensionEchoPrivateApiTest::OnDialogShown, + base::Unretained(this)))); std::string error = utils::RunFunctionAndReturnError( function.get(), @@ -46,29 +126,17 @@ IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, browser()); EXPECT_EQ("Invalid origin.", error); + EXPECT_EQ(0, dialog_invocation_count()); } IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, GetUserConsent_AllowRedeemPrefNotSet) { - scoped_refptr<EchoPrivateGetUserConsentFunction> function( - new EchoPrivateGetUserConsentFunction()); - - function->set_has_callback(true); + expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; + dialog_action_ = DIALOG_TEST_ACTION_ACCEPT; - scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( - function.get(), - "[{\"serviceName\":\"some name\",\"origin\":\"http://chromium.org\"}]", - browser())); - - ASSERT_TRUE(result.get()); - - bool result_as_boolean = false; - EXPECT_TRUE(result->GetAsBoolean(&result_as_boolean)); - EXPECT_FALSE(result_as_boolean); + RunDefaultGetUserFunctionAndExpectResultEquals(true); - EXPECT_EQ(!g_browser_process->browser_policy_connector() - ->IsEnterpriseManaged(), - function->redeem_offers_allowed()); + EXPECT_EQ(1, dialog_invocation_count()); } IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, @@ -76,49 +144,38 @@ IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, chromeos::CrosSettings::Get()->SetBoolean( chromeos::kAllowRedeemChromeOsRegistrationOffers, true); - scoped_refptr<EchoPrivateGetUserConsentFunction> function( - new EchoPrivateGetUserConsentFunction()); + expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; + dialog_action_ = DIALOG_TEST_ACTION_ACCEPT; - function->set_has_callback(true); + RunDefaultGetUserFunctionAndExpectResultEquals(true); - scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( - function.get(), - "[{\"serviceName\":\"some_name\",\"origin\":\"http://chromium.org\"}]", - browser())); + EXPECT_EQ(1, dialog_invocation_count()); +} - ASSERT_TRUE(result.get()); +IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, + GetUserConsent_ConsentDenied) { + chromeos::CrosSettings::Get()->SetBoolean( + chromeos::kAllowRedeemChromeOsRegistrationOffers, true); - bool result_as_boolean = false; - EXPECT_TRUE(result->GetAsBoolean(&result_as_boolean)); - EXPECT_FALSE(result_as_boolean); + expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; + dialog_action_ = DIALOG_TEST_ACTION_CANCEL; - EXPECT_TRUE(function->redeem_offers_allowed()); -} + RunDefaultGetUserFunctionAndExpectResultEquals(false); + EXPECT_EQ(1, dialog_invocation_count()); +} IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, GetUserConsent_AllowRedeemPrefFalse) { chromeos::CrosSettings::Get()->SetBoolean( chromeos::kAllowRedeemChromeOsRegistrationOffers, false); - scoped_refptr<EchoPrivateGetUserConsentFunction> function( - new EchoPrivateGetUserConsentFunction()); - - function->set_has_callback(true); + expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL; + dialog_action_ = DIALOG_TEST_ACTION_CANCEL; - scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( - function.get(), - "[{\"serviceName\":\"some_name\",\"origin\":\"http://chromium.org\"}]", - browser())); - - ASSERT_TRUE(result.get()); + RunDefaultGetUserFunctionAndExpectResultEquals(false); - bool result_as_boolean = false; - EXPECT_TRUE(result->GetAsBoolean(&result_as_boolean)); - EXPECT_FALSE(result_as_boolean); - - EXPECT_FALSE(function->redeem_offers_allowed()); + EXPECT_EQ(1, dialog_invocation_count()); } - -} // namespace +} // namespace chromeos diff --git a/chrome/browser/chromeos/ui/echo_dialog_view.cc b/chrome/browser/chromeos/ui/echo_dialog_view.cc index 489da8b..9a8cc24 100644 --- a/chrome/browser/chromeos/ui/echo_dialog_view.cc +++ b/chrome/browser/chromeos/ui/echo_dialog_view.cc @@ -7,6 +7,7 @@ #include "chrome/browser/chromeos/ui/echo_dialog_listener.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/gfx/font.h" #include "ui/views/controls/styled_label.h" #include "ui/views/widget/widget.h" #include "ui/views/window/dialog_client_view.h" @@ -48,11 +49,20 @@ void EchoDialogView::InitForEnabledEcho(const string16& service_name, link, &offsets); - // TODO(tbarzic): Set style for service_name substring. - label_ = new views::StyledLabel(text, this); + + views::StyledLabel::RangeStyleInfo service_name_style; + service_name_style.font_style = gfx::Font::UNDERLINE; + service_name_style.tooltip = origin; + label_->AddStyleRange( + ui::Range(offsets[0], offsets[0] + service_name.length()), + service_name_style); + + views::StyledLabel::RangeStyleInfo link_style; + link_style.font_style = gfx::Font::NORMAL; + link_style.is_link = true; label_->AddStyleRange(ui::Range(offsets[1], offsets[1] + link.length()), - views:: StyledLabel::RangeStyleInfo::CreateForLink()); + link_style); SetLabelBorderAndBounds(); @@ -71,8 +81,11 @@ void EchoDialogView::InitForDisabledEcho() { IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset); label_ = new views::StyledLabel(text, this); - label_->AddStyleRange(ui::Range(offset, offset + link.length()), - views::StyledLabel::RangeStyleInfo::CreateForLink()); + + views::StyledLabel::RangeStyleInfo link_style; + link_style.font_style = gfx::Font::NORMAL; + link_style.is_link = true; + label_->AddStyleRange(ui::Range(offset, offset + link.length()), link_style); SetLabelBorderAndBounds(); diff --git a/chrome/browser/chromeos/ui/echo_dialog_view.h b/chrome/browser/chromeos/ui/echo_dialog_view.h index 9dc7aa3..924a604 100644 --- a/chrome/browser/chromeos/ui/echo_dialog_view.h +++ b/chrome/browser/chromeos/ui/echo_dialog_view.h @@ -47,6 +47,8 @@ class EchoDialogView : public views::DialogDelegateView, void Show(gfx::NativeWindow parent); private: + friend class ExtensionEchoPrivateApiTest; + // views::DialogDelegate overrides. virtual int GetDialogButtons() const OVERRIDE; virtual int GetDefaultDialogButton() const OVERRIDE; |