Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / password_manager / password_manager_internals_service_unittest.cc
blob07baddeb9695145175fd74fba10e7bfc38951c32
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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using password_manager::PasswordManagerInternalsService;
16 using password_manager::PasswordManagerInternalsServiceFactory;
18 namespace {
20 const char kTestText[] = "abcd1234";
22 class MockLogReceiver : public password_manager::LogReceiver {
23 public:
24 MockLogReceiver() {}
26 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
29 enum ProfileType { NORMAL_PROFILE, INCOGNITO_PROFILE };
31 scoped_ptr<TestingProfile> CreateProfile(ProfileType type) {
32 TestingProfile::Builder builder;
33 scoped_ptr<TestingProfile> profile(builder.Build());
34 #if !defined(NDEBUG)
35 // During the test cases, the profiles may get created on the same address. To
36 // avoid over-zealous asserts we need to mark the newly created one as "live".
37 // See declaration of MarkBrowserContextLiveForTesting for more details.
38 BrowserContextDependencyManager::GetInstance()
39 ->MarkBrowserContextLiveForTesting(profile.get());
40 if (type == INCOGNITO_PROFILE) {
41 BrowserContextDependencyManager::GetInstance()
42 ->MarkBrowserContextLiveForTesting(profile->GetOffTheRecordProfile());
44 #endif
45 return profile.Pass();
48 } // namespace
50 class PasswordManagerInternalsServiceTest : public testing::Test {
51 content::TestBrowserThreadBundle thread_bundle_;
54 // When the profile is not incognito, it should be possible to activate the
55 // service.
56 TEST_F(PasswordManagerInternalsServiceTest, ServiceActiveNonIncognito) {
57 scoped_ptr<TestingProfile> profile(CreateProfile(NORMAL_PROFILE));
58 PasswordManagerInternalsService* service =
59 PasswordManagerInternalsServiceFactory::GetForBrowserContext(
60 profile.get());
61 testing::StrictMock<MockLogReceiver> receiver;
63 ASSERT_TRUE(profile);
64 ASSERT_TRUE(service);
65 EXPECT_EQ(std::string(), service->RegisterReceiver(&receiver));
67 // TODO(vabr): Use a MockPasswordManagerClient to detect activity changes.
68 EXPECT_CALL(receiver, LogSavePasswordProgress(kTestText)).Times(1);
69 service->ProcessLog(kTestText);
71 service->UnregisterReceiver(&receiver);
74 // When the browser profile is incognito, it should not be possible to activate
75 // the service.
76 TEST_F(PasswordManagerInternalsServiceTest, ServiceNotActiveIncognito) {
77 scoped_ptr<TestingProfile> profile(CreateProfile(INCOGNITO_PROFILE));
78 ASSERT_TRUE(profile);
80 Profile* incognito_profile = profile->GetOffTheRecordProfile();
81 PasswordManagerInternalsService* service =
82 PasswordManagerInternalsServiceFactory::GetForBrowserContext(
83 incognito_profile);
84 // BrowserContextKeyedBaseFactory::GetBrowserContextToUse should return NULL
85 // for |profile|, because |profile| is incognito. Therefore the returned
86 // |service| should also be NULL.
87 EXPECT_FALSE(service);