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.
4
5#include "components/password_manager/core/browser/password_manager_internals_service.h"
6
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"
13
14using password_manager::PasswordManagerInternalsService;
15using password_manager::PasswordManagerInternalsServiceFactory;
16
17namespace {
18
19const char kTestText[] = "abcd1234";
20
21class MockLogReceiver : public password_manager::LogReceiver {
22 public:
23  MockLogReceiver() {}
24
25  MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
26};
27
28enum ProfileType { NORMAL_PROFILE, INCOGNITO_PROFILE };
29
30scoped_ptr<TestingProfile> CreateProfile(ProfileType type) {
31  TestingProfile::Builder builder;
32  scoped_ptr<TestingProfile> profile(builder.Build());
33#if !defined(NDEBUG)
34  // During the test cases, the profiles may get created on the same address. To
35  // avoid over-zealous asserts we need to mark the newly created one as "live".
36  // See declaration of MarkBrowserContextLiveForTesting for more details.
37  BrowserContextDependencyManager::GetInstance()
38      ->MarkBrowserContextLiveForTesting(profile.get());
39  if (type == INCOGNITO_PROFILE) {
40    BrowserContextDependencyManager::GetInstance()
41        ->MarkBrowserContextLiveForTesting(profile->GetOffTheRecordProfile());
42  }
43#endif
44  return profile.Pass();
45}
46
47}  // namespace
48
49// When the profile is not incognito, it should be possible to activate the
50// service.
51TEST(PasswordManagerInternalsServiceTest, ServiceActiveNonIncognito) {
52  scoped_ptr<TestingProfile> profile(CreateProfile(NORMAL_PROFILE));
53  PasswordManagerInternalsService* service =
54      PasswordManagerInternalsServiceFactory::GetForBrowserContext(
55          profile.get());
56  testing::StrictMock<MockLogReceiver> receiver;
57
58  ASSERT_TRUE(profile);
59  ASSERT_TRUE(service);
60  EXPECT_EQ(std::string(), service->RegisterReceiver(&receiver));
61
62  // TODO(vabr): Use a MockPasswordManagerClient to detect activity changes.
63  EXPECT_CALL(receiver, LogSavePasswordProgress(kTestText)).Times(1);
64  service->ProcessLog(kTestText);
65
66  service->UnregisterReceiver(&receiver);
67}
68
69// When the browser profile is incognito, it should not be possible to activate
70// the service.
71TEST(PasswordManagerInternalsServiceTest, ServiceNotActiveIncognito) {
72  scoped_ptr<TestingProfile> profile(CreateProfile(INCOGNITO_PROFILE));
73  ASSERT_TRUE(profile);
74
75  Profile* incognito_profile = profile->GetOffTheRecordProfile();
76  PasswordManagerInternalsService* service =
77      PasswordManagerInternalsServiceFactory::GetForBrowserContext(
78          incognito_profile);
79  // BrowserContextKeyedBaseFactory::GetBrowserContextToUse should return NULL
80  // for |profile|, because |profile| is incognito. Therefore the returned
81  // |service| should also be NULL.
82  EXPECT_FALSE(service);
83}
84