tab_model_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/android/tab_android.h" 67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h" 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h" 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_profile.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_service.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_source.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)typedef testing::Test TabModelTest; 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace { 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TabModelAndroidProfileMock : public TestingProfile { 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TabModelAndroidProfileMock() {} 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~TabModelAndroidProfileMock() {} 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MOCK_METHOD0(GetOffTheRecordProfile, Profile*()); 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MOCK_METHOD0(HasOffTheRecordProfile, bool()); 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TestTabModel : public TabModel { 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public: 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit TestTabModel(Profile* profile) 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : TabModel(profile) {} 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual int GetTabCount() const OVERRIDE { return 0; } 33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual int GetActiveIndex() const OVERRIDE { return 0; } 34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual content::WebContents* GetWebContentsAt(int index) const OVERRIDE { 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return NULL; 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual void CreateTab(content::WebContents* web_contents) OVERRIDE {} 38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) virtual content::WebContents* CreateNewTabForDevTools( 39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const GURL& url) OVERRIDE { 407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return NULL; 417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual bool IsSessionRestoreInProgress() const OVERRIDE { return false; } 43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual void OpenClearBrowsingData() const OVERRIDE {} 444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) virtual TabAndroid* GetTabAt(int index) const OVERRIDE { 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return NULL; 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) virtual void SetActiveIndex(int index) OVERRIDE {} 488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) virtual void CloseTabAt(int index) OVERRIDE {} 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(TabModelTest, TestProfileHandling) { 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Construct TabModel with standard Profile. 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestingProfile testing_profile; 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestTabModel tab_model(&testing_profile); 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Verify TabModel has the correct profile and profile type. 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(&testing_profile, tab_model.GetProfile()); 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_FALSE(tab_model.IsOffTheRecord()); 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notify profile is being destroyed and verify pointer is cleared. 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::NotificationService::current()->Notify( 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::NOTIFICATION_PROFILE_DESTROYED, 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::Source<Profile>(&testing_profile), 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::NotificationService::NoDetails()); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(NULL, tab_model.GetProfile()); 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(TabModelTest, TestProfileHandlingOffTheRecord) { 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Construct TabModel with off-the-record Profile. 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TabModelAndroidProfileMock testing_profile; 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_CALL(testing_profile, HasOffTheRecordProfile()) 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) .WillOnce(testing::Return(true)); 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_CALL(testing_profile, GetOffTheRecordProfile()) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) .WillOnce(testing::Return(&testing_profile)); 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TestTabModel tab_model(&testing_profile); 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Verify TabModel has the correct profile and profile type. 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(&testing_profile, tab_model.GetProfile()); 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_TRUE(tab_model.IsOffTheRecord()); 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notify profile is being destroyed and verify pointer is cleared. 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::NotificationService::current()->Notify( 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::NOTIFICATION_PROFILE_DESTROYED, 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::Source<Profile>(&testing_profile), 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::NotificationService::NoDetails()); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(NULL, tab_model.GetProfile()); 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 88