1 // Copyright (c) 2012 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 "chrome/browser/android/tab_android.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/ui/android/tab_model/tab_model.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_source.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 typedef testing::Test TabModelTest;
17 class TabModelAndroidProfileMock : public TestingProfile {
19 TabModelAndroidProfileMock() {}
20 virtual ~TabModelAndroidProfileMock() {}
22 MOCK_METHOD0(GetOffTheRecordProfile, Profile*());
23 MOCK_METHOD0(HasOffTheRecordProfile, bool());
27 class TestTabModel : public TabModel {
29 explicit TestTabModel(Profile* profile)
30 : TabModel(profile) {}
32 virtual int GetTabCount() const OVERRIDE { return 0; }
33 virtual int GetActiveIndex() const OVERRIDE { return 0; }
34 virtual content::WebContents* GetWebContentsAt(int index) const OVERRIDE {
37 virtual void CreateTab(content::WebContents* web_contents) OVERRIDE {}
38 virtual content::WebContents* CreateTabForTesting(const GURL& url) OVERRIDE {
41 virtual bool IsSessionRestoreInProgress() const OVERRIDE { return false; }
42 virtual void OpenClearBrowsingData() const OVERRIDE {}
43 virtual TabAndroid* GetTabAt(int index) const OVERRIDE {
46 virtual void SetActiveIndex(int index) OVERRIDE {}
47 virtual void CloseTabAt(int index) OVERRIDE {}
50 TEST_F(TabModelTest, TestProfileHandling) {
51 // Construct TabModel with standard Profile.
52 TestingProfile testing_profile;
53 TestTabModel tab_model(&testing_profile);
55 // Verify TabModel has the correct profile and profile type.
56 EXPECT_EQ(&testing_profile, tab_model.GetProfile());
57 EXPECT_FALSE(tab_model.IsOffTheRecord());
59 // Notify profile is being destroyed and verify pointer is cleared.
60 content::NotificationService::current()->Notify(
61 chrome::NOTIFICATION_PROFILE_DESTROYED,
62 content::Source<Profile>(&testing_profile),
63 content::NotificationService::NoDetails());
64 EXPECT_EQ(NULL, tab_model.GetProfile());
67 TEST_F(TabModelTest, TestProfileHandlingOffTheRecord) {
68 // Construct TabModel with off-the-record Profile.
69 TabModelAndroidProfileMock testing_profile;
70 EXPECT_CALL(testing_profile, HasOffTheRecordProfile())
71 .WillOnce(testing::Return(true));
72 EXPECT_CALL(testing_profile, GetOffTheRecordProfile())
73 .WillOnce(testing::Return(&testing_profile));
74 TestTabModel tab_model(&testing_profile);
76 // Verify TabModel has the correct profile and profile type.
77 EXPECT_EQ(&testing_profile, tab_model.GetProfile());
78 EXPECT_TRUE(tab_model.IsOffTheRecord());
80 // Notify profile is being destroyed and verify pointer is cleared.
81 content::NotificationService::current()->Notify(
82 chrome::NOTIFICATION_PROFILE_DESTROYED,
83 content::Source<Profile>(&testing_profile),
84 content::NotificationService::NoDetails());
85 EXPECT_EQ(NULL, tab_model.GetProfile());