Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_manager_internals_service_unittest.cc
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
14 using password_manager::PasswordManagerInternalsService;
15 using password_manager::PasswordManagerInternalsServiceFactory;
16
17 namespace {
18
19 const char kTestText[] = "abcd1234";
20
21 class MockLogReceiver : public password_manager::LogReceiver {
22  public:
23   MockLogReceiver() {}
24
25   MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
26 };
27
28 enum ProfileType { NORMAL_PROFILE, INCOGNITO_PROFILE };
29
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());
35 #if !defined(NDEBUG)
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());
41 #endif
42   return profile.Pass();
43 }
44
45 }  // namespace
46
47 // When the profile is not incognito, it should be possible to activate the
48 // service.
49 TEST(PasswordManagerInternalsServiceTest, ServiceActiveNonIncognito) {
50   scoped_ptr<TestingProfile> profile(CreateProfile(NORMAL_PROFILE));
51   PasswordManagerInternalsService* service =
52       PasswordManagerInternalsServiceFactory::GetForBrowserContext(
53           profile.get());
54   testing::StrictMock<MockLogReceiver> receiver;
55
56   ASSERT_TRUE(profile);
57   ASSERT_TRUE(service);
58   EXPECT_EQ(std::string(), service->RegisterReceiver(&receiver));
59
60   // TODO(vabr): Use a MockPasswordManagerClient to detect activity changes.
61   EXPECT_CALL(receiver, LogSavePasswordProgress(kTestText)).Times(1);
62   service->ProcessLog(kTestText);
63
64   service->UnregisterReceiver(&receiver);
65 }
66
67 // When the browser profile is incognito, it should not be possible to activate
68 // the service.
69 TEST(PasswordManagerInternalsServiceTest, ServiceNotActiveIncognito) {
70   scoped_ptr<TestingProfile> profile(CreateProfile(INCOGNITO_PROFILE));
71   ASSERT_TRUE(profile);
72   PasswordManagerInternalsService* service =
73       PasswordManagerInternalsServiceFactory::GetForBrowserContext(
74           profile.get());
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);
79 }