Index: components/password_manager/content/renderer/credential_manager_dispatcher_unittest.cc |
diff --git a/components/password_manager/content/renderer/credential_manager_dispatcher_unittest.cc b/components/password_manager/content/renderer/credential_manager_dispatcher_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0329dca0e3af7a7fa7a67dfe35432b6ebc9fd4a1 |
--- /dev/null |
+++ b/components/password_manager/content/renderer/credential_manager_dispatcher_unittest.cc |
@@ -0,0 +1,111 @@ |
+// Copyright 2014 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 "components/password_manager/content/common/credential_manager_messages.h" |
+#include "components/password_manager/content/renderer/credential_manager_dispatcher.h" |
+#include "components/password_manager/content/renderer/test_credential_manager_dispatcher.h" |
+#include "content/public/test/mock_render_thread.h" |
+#include "content/test/blink_test_environment.h" |
+#include "ipc/ipc_test_sink.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/WebKit/public/platform/WebCredential.h" |
+#include "third_party/WebKit/public/platform/WebCredentialManager.h" |
+ |
+namespace password_manager { |
+ |
+class CredentialManagerDispatcherTest : public testing::Test { |
+ public: |
+ CredentialManagerDispatcherTest() {} |
+ |
+ virtual ~CredentialManagerDispatcherTest() {} |
+ |
+ static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); } |
+ |
+ static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); } |
+ |
+ virtual void SetUp() OVERRIDE { |
+ testing::Test::SetUp(); |
Ilya Sherman
2014/08/19 23:55:53
Is this not empty? It's surprising to see this ca
Mike West
2014/08/20 13:17:20
Done.
|
+ render_thread_.reset(new content::MockRenderThread()); |
+ dispatcher_.reset(new TestCredentialManagerDispatcher()); |
Ilya Sherman
2014/08/19 23:55:53
Why not do this in the constructor?
Mike West
2014/08/20 13:17:20
Why not indeed! I think this might cause some prob
|
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ dispatcher_.reset(); |
+ render_thread_.reset(); |
Ilya Sherman
2014/08/19 23:55:53
If this happens (automatically) in the destructor,
Mike West
2014/08/20 13:17:20
Let's find out!
|
+ } |
+ |
+ TestCredentialManagerDispatcher* dispatcher() { return dispatcher_.get(); } |
Ilya Sherman
2014/08/19 23:55:53
nit: What code is this exposed for? |dispatcher_|
Mike West
2014/08/20 13:17:20
Done.
|
+ |
+ IPC::TestSink& sink() { return render_thread_->sink(); } |
+ |
+ // Searches for a |message_id| message in the queue of sent IPC messages. If |
+ // none is present, returns NULL, otherwise returns the message. Clears queue |
+ // of sent messages upon return. |
+ const IPC::Message* GetMessage(uint32 messageID) { |
Ilya Sherman
2014/08/19 23:55:53
nit: hacker_case
Mike West
2014/08/20 13:17:20
Done.
|
+ const IPC::Message* message = sink().GetFirstMessageMatching(messageID); |
+ sink().ClearMessages(); |
+ return message; |
+ } |
+ |
+ protected: |
+ scoped_ptr<content::MockRenderThread> render_thread_; |
+ scoped_ptr<TestCredentialManagerDispatcher> dispatcher_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CredentialManagerDispatcherTest); |
+}; |
+ |
+TEST_F(CredentialManagerDispatcherTest, SendNotifyFailedSignIn) { |
+ EXPECT_FALSE(!!GetMessage(CredentialManagerHostMsg_NotifyFailedSignIn::ID)); |
+ EXPECT_TRUE(dispatcher()->failed_sign_in_map()->IsEmpty()); |
Ilya Sherman
2014/08/19 23:55:53
Can you avoid accessing the internal state of the
Mike West
2014/08/20 13:17:20
I've restructured this to have the class expose mo
|
+ |
+ blink::WebCredential credential( |
+ "id", "user", GURL("https://example.com/img.png")); |
+ scoped_ptr<blink::WebCredentialManager::NotificationCallbacks> callbacks( |
+ new blink::WebCredentialManager::NotificationCallbacks()); |
+ dispatcher()->dispatchFailedSignIn(credential, callbacks.release()); |
+ |
+ EXPECT_TRUE(!!GetMessage(CredentialManagerHostMsg_NotifyFailedSignIn::ID)); |
+ EXPECT_EQ(1UL, dispatcher()->failed_sign_in_map()->size()); |
+} |
+ |
+TEST_F(CredentialManagerDispatcherTest, SendNotifySignedIn) { |
+ EXPECT_FALSE(!!GetMessage(CredentialManagerHostMsg_NotifySignedIn::ID)); |
+ EXPECT_TRUE(dispatcher()->signed_in_map()->IsEmpty()); |
+ |
+ blink::WebCredential credential( |
+ "id", "user", GURL("https://example.com/img.png")); |
+ scoped_ptr<blink::WebCredentialManager::NotificationCallbacks> callbacks( |
+ new blink::WebCredentialManager::NotificationCallbacks()); |
+ dispatcher()->dispatchSignedIn(credential, callbacks.release()); |
+ |
+ EXPECT_TRUE(!!GetMessage(CredentialManagerHostMsg_NotifySignedIn::ID)); |
+ EXPECT_EQ(1UL, dispatcher()->signed_in_map()->size()); |
+} |
+ |
+TEST_F(CredentialManagerDispatcherTest, SendNotifySignedOut) { |
+ EXPECT_FALSE(!!GetMessage(CredentialManagerHostMsg_NotifySignedOut::ID)); |
+ EXPECT_TRUE(dispatcher()->signed_out_map()->IsEmpty()); |
+ |
+ scoped_ptr<blink::WebCredentialManager::NotificationCallbacks> callbacks( |
+ new blink::WebCredentialManager::NotificationCallbacks()); |
+ dispatcher()->dispatchSignedOut(callbacks.release()); |
+ |
+ EXPECT_TRUE(!!GetMessage(CredentialManagerHostMsg_NotifySignedOut::ID)); |
+ EXPECT_EQ(1UL, dispatcher()->signed_out_map()->size()); |
+} |
+ |
+TEST_F(CredentialManagerDispatcherTest, SendRequestCredential) { |
+ EXPECT_FALSE(!!GetMessage(CredentialManagerHostMsg_RequestCredentials::ID)); |
+ EXPECT_TRUE(dispatcher()->request_credentials_map()->IsEmpty()); |
+ |
+ scoped_ptr<blink::WebCredentialManager::RequestCallbacks> callbacks( |
+ new blink::WebCredentialManager::RequestCallbacks()); |
+ std::vector<GURL> federations; |
+ dispatcher()->dispatchRequest(false, federations, callbacks.release()); |
+ |
+ EXPECT_TRUE(!!GetMessage(CredentialManagerHostMsg_RequestCredentials::ID)); |
+ EXPECT_EQ(1UL, dispatcher()->request_credentials_map()->size()); |
+} |
+ |
+} // namespace password_manager |