Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / echo_private_apitest.cc
blobceeae51210201a12e530f57083ad230c19d21663
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.
5 #include "chrome/browser/chromeos/extensions/echo_private_api.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/thread_task_runner_handle.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"
17 namespace utils = extension_function_test_utils;
19 namespace chromeos {
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,
29 ExtensionEchoPrivateApiTest()
30 : expected_dialog_buttons_(ui::DIALOG_BUTTON_NONE),
31 dialog_action_(DIALOG_TEST_ACTION_NONE),
32 dialog_invocation_count_(0) {
35 ~ExtensionEchoPrivateApiTest() override {}
37 void SetUpCommandLine(base::CommandLine* command_line) override {
38 ExtensionApiTest::SetUpCommandLine(command_line);
40 // Force usage of stub cros settings provider instead of device settings
41 // provider.
42 command_line->AppendSwitch(switches::kStubCrosSettings);
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);
51 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
52 function.get(),
53 "[{\"serviceName\":\"some_name\",\"origin\":\"http://chromium.org\"}]",
54 browser()));
56 ASSERT_TRUE(result.get());
57 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType());
59 bool result_as_boolean = false;
60 ASSERT_TRUE(result->GetAsBoolean(&result_as_boolean));
62 EXPECT_EQ(expected_result, result_as_boolean);
65 void OnDialogShown(chromeos::EchoDialogView* dialog) {
66 dialog_invocation_count_++;
67 ASSERT_LE(dialog_invocation_count_, 1);
69 EXPECT_EQ(expected_dialog_buttons_, dialog->GetDialogButtons());
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;
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::ThreadTaskRunnerHandle::Get()->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::ThreadTaskRunnerHandle::Get()->PostTask(
90 FROM_HERE,
91 base::Bind(base::IgnoreResult(&chromeos::EchoDialogView::Cancel),
92 base::Unretained(dialog)));
96 int dialog_invocation_count() const {
97 return dialog_invocation_count_;
100 protected:
101 int expected_dialog_buttons_;
102 DialogTestAction dialog_action_;
104 private:
105 int dialog_invocation_count_;
108 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest, EchoTest) {
109 EXPECT_TRUE(RunComponentExtensionTest("echo/component_extension"))
110 << message_;
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;
118 scoped_refptr<EchoPrivateGetUserConsentFunction> function(
119 EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind(
120 &ExtensionEchoPrivateApiTest::OnDialogShown,
121 base::Unretained(this))));
123 std::string error = utils::RunFunctionAndReturnError(
124 function.get(),
125 "[{\"serviceName\":\"some name\",\"origin\":\"invalid\"}]",
126 browser());
128 EXPECT_EQ("Invalid origin.", error);
129 EXPECT_EQ(0, dialog_invocation_count());
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;
137 RunDefaultGetUserFunctionAndExpectResultEquals(true);
139 EXPECT_EQ(1, dialog_invocation_count());
142 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
143 GetUserConsent_AllowRedeemPrefTrue) {
144 chromeos::CrosSettings::Get()->SetBoolean(
145 chromeos::kAllowRedeemChromeOsRegistrationOffers, true);
147 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
148 dialog_action_ = DIALOG_TEST_ACTION_ACCEPT;
150 RunDefaultGetUserFunctionAndExpectResultEquals(true);
152 EXPECT_EQ(1, dialog_invocation_count());
155 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
156 GetUserConsent_ConsentDenied) {
157 chromeos::CrosSettings::Get()->SetBoolean(
158 chromeos::kAllowRedeemChromeOsRegistrationOffers, true);
160 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
161 dialog_action_ = DIALOG_TEST_ACTION_CANCEL;
163 RunDefaultGetUserFunctionAndExpectResultEquals(false);
165 EXPECT_EQ(1, dialog_invocation_count());
168 IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
169 GetUserConsent_AllowRedeemPrefFalse) {
170 chromeos::CrosSettings::Get()->SetBoolean(
171 chromeos::kAllowRedeemChromeOsRegistrationOffers, false);
173 expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL;
174 dialog_action_ = DIALOG_TEST_ACTION_CANCEL;
176 RunDefaultGetUserFunctionAndExpectResultEquals(false);
178 EXPECT_EQ(1, dialog_invocation_count());
181 } // namespace chromeos