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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 class TabModelTest
: public testing::Test
{
16 content::TestBrowserThreadBundle thread_bundle_
;
20 class TabModelAndroidProfileMock
: public TestingProfile
{
22 TabModelAndroidProfileMock() {}
23 virtual ~TabModelAndroidProfileMock() {}
25 MOCK_METHOD0(GetOffTheRecordProfile
, Profile
*());
26 MOCK_METHOD0(HasOffTheRecordProfile
, bool());
30 class TestTabModel
: public TabModel
{
32 explicit TestTabModel(Profile
* profile
)
33 : TabModel(profile
) {}
35 int GetTabCount() const override
{ return 0; }
36 int GetActiveIndex() const override
{ return 0; }
37 content::WebContents
* GetWebContentsAt(int index
) const override
{
40 void CreateTab(content::WebContents
* web_contents
,
41 int parent_tab_id
) override
{}
42 content::WebContents
* CreateNewTabForDevTools(const GURL
& url
) override
{
45 bool IsSessionRestoreInProgress() const override
{ return false; }
46 TabAndroid
* GetTabAt(int index
) const override
{ return NULL
; }
47 void SetActiveIndex(int index
) override
{}
48 void CloseTabAt(int index
) override
{}
51 TEST_F(TabModelTest
, TestProfileHandling
) {
52 // Construct TabModel with standard Profile.
53 TestingProfile testing_profile
;
54 TestTabModel
tab_model(&testing_profile
);
56 // Verify TabModel has the correct profile and profile type.
57 EXPECT_EQ(&testing_profile
, tab_model
.GetProfile());
58 EXPECT_FALSE(tab_model
.IsOffTheRecord());
60 // Notify profile is being destroyed and verify pointer is cleared.
61 content::NotificationService::current()->Notify(
62 chrome::NOTIFICATION_PROFILE_DESTROYED
,
63 content::Source
<Profile
>(&testing_profile
),
64 content::NotificationService::NoDetails());
65 EXPECT_EQ(NULL
, tab_model
.GetProfile());
68 TEST_F(TabModelTest
, TestProfileHandlingOffTheRecord
) {
69 // Construct TabModel with off-the-record Profile.
70 TabModelAndroidProfileMock testing_profile
;
71 EXPECT_CALL(testing_profile
, HasOffTheRecordProfile())
72 .WillOnce(testing::Return(true));
73 EXPECT_CALL(testing_profile
, GetOffTheRecordProfile())
74 .WillOnce(testing::Return(&testing_profile
));
75 TestTabModel
tab_model(&testing_profile
);
77 // Verify TabModel has the correct profile and profile type.
78 EXPECT_EQ(&testing_profile
, tab_model
.GetProfile());
79 EXPECT_TRUE(tab_model
.IsOffTheRecord());
81 // Notify profile is being destroyed and verify pointer is cleared.
82 content::NotificationService::current()->Notify(
83 chrome::NOTIFICATION_PROFILE_DESTROYED
,
84 content::Source
<Profile
>(&testing_profile
),
85 content::NotificationService::NoDetails());
86 EXPECT_EQ(NULL
, tab_model
.GetProfile());