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/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"
13
14typedef testing::Test TabModelTest;
15
16namespace {
17class TabModelAndroidProfileMock : public TestingProfile {
18 public:
19  TabModelAndroidProfileMock() {}
20  virtual ~TabModelAndroidProfileMock() {}
21
22  MOCK_METHOD0(GetOffTheRecordProfile, Profile*());
23  MOCK_METHOD0(HasOffTheRecordProfile, bool());
24};
25}  // namespace
26
27class TestTabModel : public TabModel {
28 public:
29  explicit TestTabModel(Profile* profile)
30    : TabModel(profile) {}
31
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 {
35    return NULL;
36  }
37  virtual void CreateTab(content::WebContents* web_contents,
38                         int parent_tab_id) OVERRIDE {}
39  virtual content::WebContents* CreateNewTabForDevTools(
40      const GURL& url) OVERRIDE {
41    return NULL;
42  }
43  virtual bool IsSessionRestoreInProgress() const OVERRIDE { return false; }
44  virtual TabAndroid* GetTabAt(int index) const OVERRIDE {
45    return NULL;
46  }
47  virtual void SetActiveIndex(int index) OVERRIDE {}
48  virtual void CloseTabAt(int index) OVERRIDE {}
49};
50
51TEST_F(TabModelTest, TestProfileHandling) {
52  // Construct TabModel with standard Profile.
53  TestingProfile testing_profile;
54  TestTabModel tab_model(&testing_profile);
55
56  // Verify TabModel has the correct profile and profile type.
57  EXPECT_EQ(&testing_profile, tab_model.GetProfile());
58  EXPECT_FALSE(tab_model.IsOffTheRecord());
59
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());
66}
67
68TEST_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);
76
77  // Verify TabModel has the correct profile and profile type.
78  EXPECT_EQ(&testing_profile, tab_model.GetProfile());
79  EXPECT_TRUE(tab_model.IsOffTheRecord());
80
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());
87}
88