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