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 "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
19 class ActiveStateManagerImplTest : public PlatformTest {
21 void SetUp() override {
22 PlatformTest::SetUp();
23 browser_state_.reset(new TestBrowserState());
25 void TearDown() override {
26 // The BrowserState needs to be destroyed first so that it is outlived by
27 // the WebThreadBundle.
29 BrowserState::GetActiveStateManager(browser_state_.get())
31 browser_state_.reset();
33 PlatformTest::TearDown();
36 // The BrowserState used for testing purposes.
37 scoped_ptr<BrowserState> browser_state_;
40 // Used to create TestWebThreads.
41 TestWebThreadBundle thread_bundle_;
44 // An ActiveStateManagerImpl::Observer used for testing purposes.
45 class ActiveStateManagerImplObserver : public ActiveStateManagerImpl::Observer {
47 ActiveStateManagerImplObserver() {}
48 virtual ~ActiveStateManagerImplObserver() {}
50 // ActiveStateManagerImpl::Observer implementation.
51 MOCK_METHOD0(OnActive, void());
52 MOCK_METHOD0(OnInactive, void());
53 MOCK_METHOD0(WillBeDestroyed, void());
58 // Tests that an ActiveStateManagerImpl is succesfully created with a
59 // BrowserState and that it can be made active/inactive.
60 TEST_F(ActiveStateManagerImplTest, ActiveState) {
61 ActiveStateManager* active_state_manager =
62 BrowserState::GetActiveStateManager(browser_state_.get());
63 ASSERT_TRUE(active_state_manager);
65 EXPECT_FALSE(active_state_manager->IsActive());
67 active_state_manager->SetActive(true);
68 EXPECT_TRUE(active_state_manager->IsActive());
70 // Make sure it is ok to SetActive(true) on an already active
71 // ActiveStateManager.
72 active_state_manager->SetActive(true);
73 EXPECT_TRUE(active_state_manager->IsActive());
75 active_state_manager->SetActive(false);
76 EXPECT_FALSE(active_state_manager->IsActive());
79 // Tests that ActiveStateManagerImpl::Observer are notified correctly.
80 TEST_F(ActiveStateManagerImplTest, ObserverMethod) {
81 scoped_ptr<ActiveStateManagerImplObserver> observer(
82 new ActiveStateManagerImplObserver());
83 ActiveStateManagerImpl* active_state_manager =
84 static_cast<ActiveStateManagerImpl*>(
85 BrowserState::GetActiveStateManager(browser_state_.get()));
87 active_state_manager->AddObserver(observer.get());
89 EXPECT_CALL(*observer, OnActive()).Times(1);
90 EXPECT_CALL(*observer, OnInactive()).Times(1);
91 EXPECT_CALL(*observer, WillBeDestroyed()).Times(1);
93 active_state_manager->SetActive(true);
94 active_state_manager->SetActive(false);
95 // There is no need to explicitly remove the observer since this call takes
97 browser_state_.reset();