1 // Copyright 2014 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 "components/password_manager/core/browser/password_manager_internals_service.h"
7 #include "chrome/test/base/testing_profile.h"
8 #include "components/keyed_service/content/browser_context_dependency_manager.h"
9 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
10 #include "components/password_manager/core/browser/log_receiver.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using password_manager::PasswordManagerInternalsService
;
15 using password_manager::PasswordManagerInternalsServiceFactory
;
19 const char kTestText
[] = "abcd1234";
21 class MockLogReceiver
: public password_manager::LogReceiver
{
25 MOCK_METHOD1(LogSavePasswordProgress
, void(const std::string
&));
28 enum ProfileType
{ NORMAL_PROFILE
, INCOGNITO_PROFILE
};
30 scoped_ptr
<TestingProfile
> CreateProfile(ProfileType type
) {
31 TestingProfile::Builder builder
;
32 if (type
== INCOGNITO_PROFILE
)
33 builder
.SetIncognito();
34 scoped_ptr
<TestingProfile
> profile(builder
.Build());
36 // During the test cases, the profiles may get created on the same address. To
37 // avoid over-zealous asserts we need to mark the newly created one as "live".
38 // See declaration of MarkBrowserContextLiveForTesting for more details.
39 BrowserContextDependencyManager::GetInstance()
40 ->MarkBrowserContextLiveForTesting(profile
.get());
42 return profile
.Pass();
47 // When the profile is not incognito, it should be possible to activate the
49 TEST(PasswordManagerInternalsServiceTest
, ServiceActiveNonIncognito
) {
50 scoped_ptr
<TestingProfile
> profile(CreateProfile(NORMAL_PROFILE
));
51 PasswordManagerInternalsService
* service
=
52 PasswordManagerInternalsServiceFactory::GetForBrowserContext(
54 testing::StrictMock
<MockLogReceiver
> receiver
;
58 EXPECT_EQ(std::string(), service
->RegisterReceiver(&receiver
));
60 // TODO(vabr): Use a MockPasswordManagerClient to detect activity changes.
61 EXPECT_CALL(receiver
, LogSavePasswordProgress(kTestText
)).Times(1);
62 service
->ProcessLog(kTestText
);
64 service
->UnregisterReceiver(&receiver
);
67 // When the browser profile is incognito, it should not be possible to activate
69 TEST(PasswordManagerInternalsServiceTest
, ServiceNotActiveIncognito
) {
70 scoped_ptr
<TestingProfile
> profile(CreateProfile(INCOGNITO_PROFILE
));
72 PasswordManagerInternalsService
* service
=
73 PasswordManagerInternalsServiceFactory::GetForBrowserContext(
75 // BrowserContextKeyedBaseFactory::GetBrowserContextToUse should return NULL
76 // for |profile|, because |profile| is incognito. Therefore the returned
77 // |service| should also be NULL.
78 EXPECT_FALSE(service
);