summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/chromeos/login/login_ui_unittest.cc
blob: f0bb79b36819c19d870678cb25906cd9850f362c (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
// Copyright (c) 2011 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 <string>

#include "base/memory/scoped_ptr.h"
#include "base/string_piece.h"
#include "base/values.h"
#include "chrome/browser/ui/webui/chromeos/login/login_ui.h"
#include "chrome/browser/ui/webui/chromeos/login/login_ui_unittest.h"
#include "chrome/test/testing_profile.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::Return;
using ::testing::StrEq;

// Listing of untested functions:
//
// LoginUI::*
// LoginUIHTML::*
//
// The objects LoginUI and LoginUIHTMLSource do not have any testing,
// since there are rather trivial/boilerplatey. They are used for hooking the
// LoginUI code into the greater system and depend extensively on calling
// external functions. The external functions should be unit tested, but the
// these objects are better tested in a functional manner.
//
//
// LoginUIHandler::RegisterMessage
//
// There is currently no test for LoginUIHandler::RegisterMessage, b/c the
// function WebUI::RegisterMesssageCallback is not declared virtual. This means
// that it cannot be mocked easily and it is non-trivial to resolve, since just
// making the function virtual causes other problems. Either this class or that
// class needs to be refactored to deal with this before one can write this
// test. Realistically there isn't much to fail in this function, so testing
// should not be needed in this class.
//
//
// LoginUIHandler::HandleLaunchIncognito
// LoginUIHandler::OnLoginFailure
// LoginUIHandler::OnLoginSuccess
// LoginUIHandler::OnOffTheRecordLoginSuccess
//
// There no tests for these functions since all of them are pretty straight
// forward assuming that the called functions perform as expected and it is
// non-trivial to mock the Browser class.
namespace chromeos {

// LoginUIHandler::Attach
TEST(LoginUIHandlerTest, Attach) {
  // Don't care about the expected in this test
  LoginUIHandlerHarness handler_harness("", "");
  MockWebUI web_ui;

  EXPECT_EQ(&handler_harness, handler_harness.Attach(&web_ui));
  EXPECT_EQ(&web_ui, handler_harness.GetWebUI());
}

// Helper for LoginUIHandler::HandleAuthenticateUser
void RunHandleAuthenticateUserTest(const std::string& expected_username,
                                   const std::string& expected_password,
                                   const std::string& supplied_username,
                                   const std::string& supplied_password) {
  ListValue arg_list;
  StringValue* username_value = new StringValue(supplied_username);
  StringValue* password_value = new StringValue(supplied_password);
  TestingProfile mock_profile;
  LoginUIHandlerHarness handler(expected_username,
                                          expected_password);

  EXPECT_EQ(expected_username, handler.GetMockFacade()->GetUsername());
  EXPECT_EQ(expected_password, handler.GetMockFacade()->GetPassword());

  arg_list.Append(username_value);
  arg_list.Append(password_value);
  EXPECT_CALL(*(handler.GetMockProfileOperations()),
              GetDefaultProfile())
              .Times(1)
              .WillRepeatedly(Return(&mock_profile));

  EXPECT_CALL(*(handler.GetMockFacade()),
              AuthenticateToLogin(&mock_profile,
                                  StrEq(supplied_username),
                                  StrEq(supplied_password),
                                  StrEq(std::string()),
                                  StrEq(std::string())))
              .Times(1);

  handler.HandleAuthenticateUser(&arg_list);
  // This code does not simulate the callback that occurs in the
  // AuthenticatorFacade, since that would be a) a functional test and b)
  // require a working mock of Browser.
}

// LoginUIHandler::HandleAuthenticateUser on success
TEST(LoginUIHandlerTest, HandleAuthenticateUserSuccess) {
  RunHandleAuthenticateUserTest("chronos",
                                "chronos",
                                "chronos",
                                "chronos");

  RunHandleAuthenticateUserTest("bob",
                                "mumistheword",
                                "bob",
                                "mumistheword");
}

// LoginUIHandler::HandleAuthenticateUser on failure
TEST(LoginUIHandlerTest, HandleAuthenticateUserFailure) {
  RunHandleAuthenticateUserTest("chronos",
                                "chronos",
                                std::string(),
                                "chronos");

  RunHandleAuthenticateUserTest("chronos",
                                "chronos",
                                std::string(),
                                std::string());

  RunHandleAuthenticateUserTest("chronos",
                                "chronos",
                                "chronos",
                                std::string());

  RunHandleAuthenticateUserTest("chronos",
                                "chronos",
                                "bob",
                                "mumistheword");

  RunHandleAuthenticateUserTest("bob",
                                "mumistheword",
                                "bob",
                                std::string());

  RunHandleAuthenticateUserTest("bob",
                                "mumistheword",
                                std::string(),
                                std::string());

  RunHandleAuthenticateUserTest("bob",
                                "mumistheword",
                                std::string(),
                                "mumistheword");

  RunHandleAuthenticateUserTest("bob",
                                "mumistheword",
                                "chronos",
                                "chronos");
}

}  // namespace chromeos