1// Copyright 2014 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/ui/android/tab_model/tab_model.h"
6#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
7#include "chrome/test/base/chrome_render_view_host_test_harness.h"
8#include "chrome/test/base/testing_profile.h"
9#include "content/public/browser/web_contents.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13class TabModelListTest : public ChromeRenderViewHostTestHarness {};
14}  // namespace
15
16class TestTabModel : public TabModel {
17 public:
18  explicit TestTabModel(Profile* profile) : TabModel(profile), tab_count_(0) {}
19
20  virtual int GetTabCount() const override { return tab_count_; }
21  virtual int GetActiveIndex() const override { return 0; }
22  virtual content::WebContents* GetWebContentsAt(int index) const override {
23    return nullptr;
24  }
25  virtual void CreateTab(content::WebContents* web_contents,
26                         int parent_tab_id) override {}
27  virtual content::WebContents* CreateNewTabForDevTools(
28      const GURL& url) override {
29    return nullptr;
30  }
31  virtual bool IsSessionRestoreInProgress() const override { return false; }
32  virtual TabAndroid* GetTabAt(int index) const override { return nullptr; }
33  virtual void SetActiveIndex(int index) override {}
34  virtual void CloseTabAt(int index) override {}
35
36  // A fake value for the current number of tabs.
37  int tab_count_;
38};
39
40// Regression test for http://crbug.com/432685.
41TEST_F(TabModelListTest, TestGetTabModelForWebContents) {
42  TestTabModel tab_model(profile());
43  TabModelList::AddTabModel(&tab_model);
44
45  scoped_ptr<content::WebContents> contents(CreateTestWebContents());
46
47  // Should not crash when there are no tabs.
48  EXPECT_EQ(NULL, TabModelList::GetTabModelForWebContents(contents.get()));
49
50  // Should not crash when there is an uninitialized tab, i.e. when
51  // TabModel::GetTabAt returns NULL.
52  tab_model.tab_count_ = 1;
53  EXPECT_EQ(NULL, TabModelList::GetTabModelForWebContents(contents.get()));
54
55  TabModelList::RemoveTabModel(&tab_model);
56}
57