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 #include "chrome/browser/chromeos/extensions/echo_private_api.h" 6 7 #include "base/bind.h" 8 #include "base/command_line.h" 9 #include "base/message_loop/message_loop_proxy.h" 10 #include "chrome/browser/chromeos/settings/cros_settings.h" 11 #include "chrome/browser/chromeos/ui/echo_dialog_view.h" 12 #include "chrome/browser/extensions/extension_apitest.h" 13 #include "chrome/browser/extensions/extension_function_test_utils.h" 14 #include "chromeos/chromeos_switches.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 namespace utils = extension_function_test_utils; 18 19 namespace chromeos { 20 21 class ExtensionEchoPrivateApiTest : public ExtensionApiTest { 22 public: 23 enum DialogTestAction { 24 DIALOG_TEST_ACTION_NONE, 25 DIALOG_TEST_ACTION_ACCEPT, 26 DIALOG_TEST_ACTION_CANCEL, 27 }; 28 29 ExtensionEchoPrivateApiTest() 30 : expected_dialog_buttons_(ui::DIALOG_BUTTON_NONE), 31 dialog_action_(DIALOG_TEST_ACTION_NONE), 32 dialog_invocation_count_(0) { 33 } 34 35 virtual ~ExtensionEchoPrivateApiTest() {} 36 37 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 38 ExtensionApiTest::SetUpCommandLine(command_line); 39 40 // Force usage of stub cros settings provider instead of device settings 41 // provider. 42 command_line->AppendSwitch(switches::kStubCrosSettings); 43 } 44 45 void RunDefaultGetUserFunctionAndExpectResultEquals(bool expected_result) { 46 scoped_refptr<EchoPrivateGetUserConsentFunction> function( 47 EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind( 48 &ExtensionEchoPrivateApiTest::OnDialogShown, this))); 49 function->set_has_callback(true); 50 51 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( 52 function.get(), 53 "[{\"serviceName\":\"some_name\",\"origin\":\"http://chromium.org\"}]", 54 browser())); 55 56 ASSERT_TRUE(result.get()); 57 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); 58 59 bool result_as_boolean = false; 60 ASSERT_TRUE(result->GetAsBoolean(&result_as_boolean)); 61 62 EXPECT_EQ(expected_result, result_as_boolean); 63 } 64 65 void OnDialogShown(chromeos::EchoDialogView* dialog) { 66 dialog_invocation_count_++; 67 ASSERT_LE(dialog_invocation_count_, 1); 68 69 EXPECT_EQ(expected_dialog_buttons_, dialog->GetDialogButtons()); 70 71 // Don't accept the dialog if the dialog buttons don't match expectation. 72 // Accepting a dialog which should not have accept option may crash the 73 // test. The test already failed, so it's ok to cancel the dialog. 74 DialogTestAction dialog_action = dialog_action_; 75 if (dialog_action == DIALOG_TEST_ACTION_ACCEPT && 76 expected_dialog_buttons_ != dialog->GetDialogButtons()) { 77 dialog_action = DIALOG_TEST_ACTION_CANCEL; 78 } 79 80 // Perform test action on the dialog. 81 // The dialog should stay around until AcceptWindow or CancelWindow is 82 // called, so base::Unretained is safe. 83 if (dialog_action == DIALOG_TEST_ACTION_ACCEPT) { 84 base::MessageLoopProxy::current()->PostTask( 85 FROM_HERE, 86 base::Bind(base::IgnoreResult(&chromeos::EchoDialogView::Accept), 87 base::Unretained(dialog))); 88 } else if (dialog_action == DIALOG_TEST_ACTION_CANCEL) { 89 base::MessageLoopProxy::current()->PostTask( 90 FROM_HERE, 91 base::Bind(base::IgnoreResult(&chromeos::EchoDialogView::Cancel), 92 base::Unretained(dialog))); 93 } 94 } 95 96 int dialog_invocation_count() const { 97 return dialog_invocation_count_; 98 } 99 100 protected: 101 int expected_dialog_buttons_; 102 DialogTestAction dialog_action_; 103 104 private: 105 int dialog_invocation_count_; 106 }; 107 108 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, EchoTest) { 109 EXPECT_TRUE(RunComponentExtensionTest("echo/component_extension")) 110 << message_; 111 } 112 113 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, 114 GetUserConsent_InvalidOrigin) { 115 expected_dialog_buttons_ = ui::DIALOG_BUTTON_NONE; 116 dialog_action_ = DIALOG_TEST_ACTION_NONE; 117 118 scoped_refptr<EchoPrivateGetUserConsentFunction> function( 119 EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind( 120 &ExtensionEchoPrivateApiTest::OnDialogShown, 121 base::Unretained(this)))); 122 123 std::string error = utils::RunFunctionAndReturnError( 124 function.get(), 125 "[{\"serviceName\":\"some name\",\"origin\":\"invalid\"}]", 126 browser()); 127 128 EXPECT_EQ("Invalid origin.", error); 129 EXPECT_EQ(0, dialog_invocation_count()); 130 } 131 132 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, 133 GetUserConsent_AllowRedeemPrefNotSet) { 134 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; 135 dialog_action_ = DIALOG_TEST_ACTION_ACCEPT; 136 137 RunDefaultGetUserFunctionAndExpectResultEquals(true); 138 139 EXPECT_EQ(1, dialog_invocation_count()); 140 } 141 142 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, 143 GetUserConsent_AllowRedeemPrefTrue) { 144 chromeos::CrosSettings::Get()->SetBoolean( 145 chromeos::kAllowRedeemChromeOsRegistrationOffers, true); 146 147 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; 148 dialog_action_ = DIALOG_TEST_ACTION_ACCEPT; 149 150 RunDefaultGetUserFunctionAndExpectResultEquals(true); 151 152 EXPECT_EQ(1, dialog_invocation_count()); 153 } 154 155 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, 156 GetUserConsent_ConsentDenied) { 157 chromeos::CrosSettings::Get()->SetBoolean( 158 chromeos::kAllowRedeemChromeOsRegistrationOffers, true); 159 160 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK; 161 dialog_action_ = DIALOG_TEST_ACTION_CANCEL; 162 163 RunDefaultGetUserFunctionAndExpectResultEquals(false); 164 165 EXPECT_EQ(1, dialog_invocation_count()); 166 } 167 168 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, 169 GetUserConsent_AllowRedeemPrefFalse) { 170 chromeos::CrosSettings::Get()->SetBoolean( 171 chromeos::kAllowRedeemChromeOsRegistrationOffers, false); 172 173 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL; 174 dialog_action_ = DIALOG_TEST_ACTION_CANCEL; 175 176 RunDefaultGetUserFunctionAndExpectResultEquals(false); 177 178 EXPECT_EQ(1, dialog_invocation_count()); 179 } 180 181 } // namespace chromeos 182