11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "apps/app_keep_alive_service.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "apps/app_keep_alive_service_factory.h"
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/lifetime/application_lifetime.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if !defined(OS_ANDROID)
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class AppKeepAliveServiceUnitTest : public testing::Test {
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) protected:
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    testing::Test::SetUp();
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    service_.reset(new apps::AppKeepAliveService(&profile_));
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    while (chrome::WillKeepAlive())
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      chrome::EndKeepAlive();
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    testing::Test::TearDown();
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TestingProfile profile_;
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<apps::AppKeepAliveService> service_;
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, Basic) {
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Test that apps running in different profiles are ignored.
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, DifferentProfile) {
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(NULL, "foo");
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(NULL, "foo");
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Test that OnAppStop without a prior corresponding OnAppStart is ignored.
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, StopAppBeforeOpening) {
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Test that OnAppStart for an app that has already started is ignored.
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, StartMoreThanOnce) {
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Test that OnAppStart is ignored after the service has been shut down.
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, StartAfterShutdown) {
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service_->Shutdown();
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, MultipleApps) {
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "bar");
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "bar");
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Test that all keep alives are ended when OnChromeTerminating is called.
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, ChromeTerminateWithAppsStarted) {
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "bar");
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnChromeTerminating();
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "foo");
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStop(&profile_, "bar");
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Test that all keep alives are ended when Shutdown is called.
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(AppKeepAliveServiceUnitTest, ProfileShutdownWithAppsStarted) {
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ASSERT_FALSE(chrome::WillKeepAlive());
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "foo");
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->OnAppStart(&profile_, "bar");
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_TRUE(chrome::WillKeepAlive());
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service_->Shutdown();
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_FALSE(chrome::WillKeepAlive());
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
130