1 // Copyright 2013 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 "apps/app_keep_alive_service.h" 6 #include "apps/app_keep_alive_service_factory.h" 7 #include "chrome/browser/lifetime/application_lifetime.h" 8 #include "chrome/test/base/testing_profile.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 #if !defined(OS_ANDROID) 12 13 class AppKeepAliveServiceUnitTest : public testing::Test { 14 protected: 15 virtual void SetUp() OVERRIDE { 16 testing::Test::SetUp(); 17 service_.reset(new apps::AppKeepAliveService(&profile_)); 18 } 19 20 virtual void TearDown() OVERRIDE { 21 while (chrome::WillKeepAlive()) 22 chrome::EndKeepAlive(); 23 testing::Test::TearDown(); 24 } 25 26 TestingProfile profile_; 27 scoped_ptr<apps::AppKeepAliveService> service_; 28 }; 29 30 TEST_F(AppKeepAliveServiceUnitTest, Basic) { 31 ASSERT_FALSE(chrome::WillKeepAlive()); 32 service_->OnAppStart(&profile_, "foo"); 33 EXPECT_TRUE(chrome::WillKeepAlive()); 34 service_->OnAppStop(&profile_, "foo"); 35 EXPECT_FALSE(chrome::WillKeepAlive()); 36 service_->Shutdown(); 37 EXPECT_FALSE(chrome::WillKeepAlive()); 38 } 39 40 // Test that apps running in different profiles are ignored. 41 TEST_F(AppKeepAliveServiceUnitTest, DifferentProfile) { 42 ASSERT_FALSE(chrome::WillKeepAlive()); 43 service_->OnAppStart(NULL, "foo"); 44 EXPECT_FALSE(chrome::WillKeepAlive()); 45 service_->OnAppStart(&profile_, "foo"); 46 EXPECT_TRUE(chrome::WillKeepAlive()); 47 service_->OnAppStop(NULL, "foo"); 48 EXPECT_TRUE(chrome::WillKeepAlive()); 49 service_->OnAppStop(&profile_, "foo"); 50 EXPECT_FALSE(chrome::WillKeepAlive()); 51 service_->Shutdown(); 52 EXPECT_FALSE(chrome::WillKeepAlive()); 53 } 54 55 // Test that OnAppStop without a prior corresponding OnAppStart is ignored. 56 TEST_F(AppKeepAliveServiceUnitTest, StopAppBeforeOpening) { 57 ASSERT_FALSE(chrome::WillKeepAlive()); 58 service_->OnAppStop(&profile_, "foo"); 59 ASSERT_FALSE(chrome::WillKeepAlive()); 60 service_->OnAppStart(&profile_, "foo"); 61 EXPECT_TRUE(chrome::WillKeepAlive()); 62 service_->OnAppStop(&profile_, "foo"); 63 EXPECT_FALSE(chrome::WillKeepAlive()); 64 service_->Shutdown(); 65 EXPECT_FALSE(chrome::WillKeepAlive()); 66 } 67 68 // Test that OnAppStart for an app that has already started is ignored. 69 TEST_F(AppKeepAliveServiceUnitTest, StartMoreThanOnce) { 70 ASSERT_FALSE(chrome::WillKeepAlive()); 71 service_->OnAppStart(&profile_, "foo"); 72 EXPECT_TRUE(chrome::WillKeepAlive()); 73 service_->OnAppStart(&profile_, "foo"); 74 EXPECT_TRUE(chrome::WillKeepAlive()); 75 service_->OnAppStop(&profile_, "foo"); 76 EXPECT_FALSE(chrome::WillKeepAlive()); 77 service_->Shutdown(); 78 EXPECT_FALSE(chrome::WillKeepAlive()); 79 } 80 81 // Test that OnAppStart is ignored after the service has been shut down. 82 TEST_F(AppKeepAliveServiceUnitTest, StartAfterShutdown) { 83 ASSERT_FALSE(chrome::WillKeepAlive()); 84 service_->Shutdown(); 85 service_->OnAppStart(&profile_, "foo"); 86 EXPECT_FALSE(chrome::WillKeepAlive()); 87 } 88 89 TEST_F(AppKeepAliveServiceUnitTest, MultipleApps) { 90 ASSERT_FALSE(chrome::WillKeepAlive()); 91 service_->OnAppStart(&profile_, "foo"); 92 EXPECT_TRUE(chrome::WillKeepAlive()); 93 service_->OnAppStart(&profile_, "bar"); 94 EXPECT_TRUE(chrome::WillKeepAlive()); 95 service_->OnAppStop(&profile_, "foo"); 96 EXPECT_TRUE(chrome::WillKeepAlive()); 97 service_->OnAppStop(&profile_, "bar"); 98 EXPECT_FALSE(chrome::WillKeepAlive()); 99 service_->Shutdown(); 100 EXPECT_FALSE(chrome::WillKeepAlive()); 101 } 102 103 // Test that all keep alives are ended when OnChromeTerminating is called. 104 TEST_F(AppKeepAliveServiceUnitTest, ChromeTerminateWithAppsStarted) { 105 ASSERT_FALSE(chrome::WillKeepAlive()); 106 service_->OnAppStart(&profile_, "foo"); 107 EXPECT_TRUE(chrome::WillKeepAlive()); 108 service_->OnAppStart(&profile_, "bar"); 109 EXPECT_TRUE(chrome::WillKeepAlive()); 110 service_->OnChromeTerminating(); 111 EXPECT_FALSE(chrome::WillKeepAlive()); 112 service_->OnAppStop(&profile_, "foo"); 113 service_->OnAppStop(&profile_, "bar"); 114 EXPECT_FALSE(chrome::WillKeepAlive()); 115 service_->Shutdown(); 116 EXPECT_FALSE(chrome::WillKeepAlive()); 117 } 118 119 // Test that all keep alives are ended when Shutdown is called. 120 TEST_F(AppKeepAliveServiceUnitTest, ProfileShutdownWithAppsStarted) { 121 ASSERT_FALSE(chrome::WillKeepAlive()); 122 service_->OnAppStart(&profile_, "foo"); 123 EXPECT_TRUE(chrome::WillKeepAlive()); 124 service_->OnAppStart(&profile_, "bar"); 125 EXPECT_TRUE(chrome::WillKeepAlive()); 126 service_->Shutdown(); 127 EXPECT_FALSE(chrome::WillKeepAlive()); 128 } 129 #endif 130