1 // Copyright 2015 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 "ios/web/active_state_manager_impl.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "ios/web/public/active_state_manager.h"
9 #include "ios/web/public/browser_state.h"
10 #include "ios/web/public/test/test_browser_state.h"
11 #include "ios/web/public/test/test_web_thread_bundle.h"
12 #include "ios/web/test/web_test.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
20 // A test fixture to test ActiveStateManagerImpl.
21 typedef WebTest ActiveStateManagerImplTest;
23 // An ActiveStateManager::Observer used for testing purposes.
24 class ActiveStateManagerObserver : public ActiveStateManager::Observer {
26 ActiveStateManagerObserver() {}
27 virtual ~ActiveStateManagerObserver() {}
29 // ActiveStateManager::Observer implementation.
30 MOCK_METHOD0(OnActive, void());
31 MOCK_METHOD0(OnInactive, void());
32 MOCK_METHOD0(WillBeDestroyed, void());
37 // Tests that an ActiveStateManagerImpl is succesfully created with a
38 // BrowserState and that it can be made active/inactive.
39 TEST_F(ActiveStateManagerImplTest, ActiveState) {
40 ActiveStateManager* active_state_manager =
41 BrowserState::GetActiveStateManager(GetBrowserState());
42 ASSERT_TRUE(active_state_manager);
44 ASSERT_TRUE(active_state_manager->IsActive());
46 active_state_manager->SetActive(true);
47 EXPECT_TRUE(active_state_manager->IsActive());
49 // Make sure it is ok to SetActive(true) on an already active
50 // ActiveStateManager.
51 active_state_manager->SetActive(true);
52 EXPECT_TRUE(active_state_manager->IsActive());
54 active_state_manager->SetActive(false);
55 EXPECT_FALSE(active_state_manager->IsActive());
58 // Tests that ActiveStateManager::Observer are notified correctly.
59 TEST_F(ActiveStateManagerImplTest, ObserverMethod) {
60 // |GetBrowserState()| already has its ActiveStateManager be active.
61 BrowserState::GetActiveStateManager(GetBrowserState())->SetActive(false);
63 ActiveStateManagerObserver observer;
64 TestBrowserState browser_state;
65 ActiveStateManager* active_state_manager =
66 BrowserState::GetActiveStateManager(&browser_state);
68 active_state_manager->AddObserver(&observer);
70 EXPECT_CALL(observer, OnActive()).Times(1);
71 EXPECT_CALL(observer, OnInactive()).Times(1);
72 EXPECT_CALL(observer, WillBeDestroyed()).Times(1);
74 active_state_manager->SetActive(true);
75 active_state_manager->SetActive(false);
76 // There is no need to explicitly remove the observer since it is removed when
77 // |active_state_manager| goes away -- which happens when |browser_state| goes