summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/echo_private_apitest.cc
blob: 4a6b1b3e9f11400f3dbf0eed75eb382b0338e51d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 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.

#include "chrome/browser/chromeos/extensions/echo_private_api.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop/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 "chromeos/chromeos_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace utils = extension_function_test_utils;

namespace chromeos {

class ExtensionEchoPrivateApiTest : public ExtensionApiTest {
 public:
  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 {
    ExtensionApiTest::SetUpCommandLine(command_line);

    // Force usage of stub cros settings provider instead of device settings
    // 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.get(),
        "[{\"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) {
  EXPECT_TRUE(RunComponentExtensionTest("echo/component_extension"))
      << message_;
}

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(
      EchoPrivateGetUserConsentFunction::CreateForTest(base::Bind(
          &ExtensionEchoPrivateApiTest::OnDialogShown,
          base::Unretained(this))));

  std::string error = utils::RunFunctionAndReturnError(
      function.get(),
      "[{\"serviceName\":\"some name\",\"origin\":\"invalid\"}]",
      browser());

  EXPECT_EQ("Invalid origin.", error);
  EXPECT_EQ(0, dialog_invocation_count());
}

IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
                       GetUserConsent_AllowRedeemPrefNotSet) {
  expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
  dialog_action_ = DIALOG_TEST_ACTION_ACCEPT;

  RunDefaultGetUserFunctionAndExpectResultEquals(true);

  EXPECT_EQ(1, dialog_invocation_count());
}

IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
                       GetUserConsent_AllowRedeemPrefTrue) {
  chromeos::CrosSettings::Get()->SetBoolean(
            chromeos::kAllowRedeemChromeOsRegistrationOffers, true);

  expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
  dialog_action_ = DIALOG_TEST_ACTION_ACCEPT;

  RunDefaultGetUserFunctionAndExpectResultEquals(true);

  EXPECT_EQ(1, dialog_invocation_count());
}

IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
                       GetUserConsent_ConsentDenied) {
  chromeos::CrosSettings::Get()->SetBoolean(
            chromeos::kAllowRedeemChromeOsRegistrationOffers, true);

  expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL | ui::DIALOG_BUTTON_OK;
  dialog_action_ = DIALOG_TEST_ACTION_CANCEL;

  RunDefaultGetUserFunctionAndExpectResultEquals(false);

  EXPECT_EQ(1, dialog_invocation_count());
}

IN_PROC_BROWSER_TEST_F(ExtensionEchoPrivateApiTest,
                       GetUserConsent_AllowRedeemPrefFalse) {
  chromeos::CrosSettings::Get()->SetBoolean(
            chromeos::kAllowRedeemChromeOsRegistrationOffers, false);

  expected_dialog_buttons_ = ui::DIALOG_BUTTON_CANCEL;
  dialog_action_ = DIALOG_TEST_ACTION_CANCEL;

  RunDefaultGetUserFunctionAndExpectResultEquals(false);

  EXPECT_EQ(1, dialog_invocation_count());
}

}  // namespace chromeos