summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/script_bubble_controller_unittest.cc
blob: 9a9c9932690d5385622c6275828f913c96016278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/script_bubble_controller.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/tab_contents/test_tab_contents.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/feature_switch.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/test_browser_thread.h"

using content::BrowserThread;

namespace extensions {
namespace {

class ScriptBubbleControllerTest : public TabContentsTestHarness {
 public:
  ScriptBubbleControllerTest()
      : ui_thread_(BrowserThread::UI, MessageLoop::current()),
        file_thread_(BrowserThread::FILE, MessageLoop::current()),
        enable_script_bubble_(FeatureSwitch::GetScriptBubble(), true) {
  }

  virtual void SetUp() OVERRIDE {
    TabContentsTestHarness::SetUp();
    CommandLine command_line(CommandLine::NO_PROGRAM);
    extension_service_ =
        static_cast<TestExtensionSystem*>(
            ExtensionSystem::Get(tab_contents()->profile()))->
        CreateExtensionService(
            &command_line, FilePath(), false);
    extension_service_->component_loader()->AddScriptBubble();
    extension_service_->Init();

    TabHelper::CreateForWebContents(web_contents());

    script_bubble_controller_ =
        TabHelper::FromWebContents(web_contents())->script_bubble_controller();
  }

 protected:
  int tab_id() {
    return ExtensionTabUtil::GetTabId(web_contents());
  }

  ExtensionService* extension_service_;
  ScriptBubbleController* script_bubble_controller_;

 private:
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;
  FeatureSwitch::ScopedOverride enable_script_bubble_;
};

TEST_F(ScriptBubbleControllerTest, Basics) {
  ExtensionAction* script_bubble_action =
      extension_service_->component_loader()->GetScriptBubble()->
      page_action();
  ASSERT_TRUE(script_bubble_action);

  // By default, the bubble should be invisible.
  NavigateAndCommit(GURL("http://www.google.com"));
  EXPECT_FALSE(script_bubble_action->GetIsVisible(tab_id()));
  EXPECT_EQ("", script_bubble_action->GetBadgeText(tab_id()));

  // Running a script on the tab causes the bubble to be visible.
  TabHelper::ContentScriptObserver::ExecutingScriptsMap executing_scripts;
  executing_scripts["id1"].insert("script1");
  script_bubble_controller_->OnContentScriptsExecuting(
      web_contents(),
      executing_scripts,
      web_contents()->GetController().GetActiveEntry()->GetPageID(),
      web_contents()->GetController().GetActiveEntry()->GetURL());
  EXPECT_TRUE(script_bubble_action->GetIsVisible(tab_id()));
  EXPECT_EQ("1", script_bubble_action->GetBadgeText(tab_id()));

  // Running a script from another extension increments the count.
  executing_scripts["id2"].insert("script2");
  script_bubble_controller_->OnContentScriptsExecuting(
      web_contents(),
      executing_scripts,
      web_contents()->GetController().GetActiveEntry()->GetPageID(),
      web_contents()->GetController().GetActiveEntry()->GetURL());
  EXPECT_TRUE(script_bubble_action->GetIsVisible(tab_id()));
  EXPECT_EQ("2", script_bubble_action->GetBadgeText(tab_id()));

  // Running another script from an already-seen extension does not affect
  // count.
  executing_scripts["id2"].insert("script3");
  script_bubble_controller_->OnContentScriptsExecuting(
      web_contents(),
      executing_scripts,
      web_contents()->GetController().GetActiveEntry()->GetPageID(),
      web_contents()->GetController().GetActiveEntry()->GetURL());
  EXPECT_TRUE(script_bubble_action->GetIsVisible(tab_id()));
  EXPECT_EQ("2", script_bubble_action->GetBadgeText(tab_id()));

  // Navigating away resets the badge.
  NavigateAndCommit(GURL("http://www.google.com"));
  EXPECT_FALSE(script_bubble_action->GetIsVisible(tab_id()));
  EXPECT_EQ("", script_bubble_action->GetBadgeText(tab_id()));
};

}  // namespace
}  // namespace extensions