Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / plugins / plugin_installer_unittest.cc
index ea450d8..cfd44d6 100644 (file)
 
 #include "chrome/browser/plugins/plugin_installer.h"
 
-#include "base/strings/utf_string_conversions.h"
-#include "content/public/common/webplugininfo.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "chrome/browser/plugins/plugin_installer_observer.h"
+#include "chrome/test/base/chrome_render_view_host_test_harness.h"
+#include "content/public/browser/download_url_parameters.h"
+#include "content/public/test/mock_download_item.h"
+#include "content/public/test/mock_download_manager.h"
+#include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using ::testing::_;
+
 namespace {
 
-PluginInstaller::SecurityStatus GetSecurityStatus(PluginInstaller* installer,
-                                                  const char* version) {
-  content:: WebPluginInfo plugin(
-      base::ASCIIToUTF16("Foo plug-in"),
-      base::FilePath(FILE_PATH_LITERAL("/tmp/plugin.so")),
-      base::ASCIIToUTF16(version),
-      base::ASCIIToUTF16("Foo plug-in."));
-  return installer->GetSecurityStatus(plugin);
+class PluginInstallerTest : public ChromeRenderViewHostTestHarness {
+ public:
+  PluginInstallerTest();
+  virtual void SetUp() OVERRIDE;
+  virtual void TearDown() OVERRIDE;
+
+  PluginInstaller* installer() { return installer_.get(); }
+  content::DownloadItem::Observer* last_download_item_observer() {
+    return last_download_item_observer_;
+  }
+
+  scoped_ptr<content::MockDownloadItem> CreateMockDownloadItem();
+
+ private:
+  scoped_ptr<PluginInstaller> installer_;
+  content::DownloadItem::Observer* last_download_item_observer_;
+};
+
+PluginInstallerTest::PluginInstallerTest()
+    : last_download_item_observer_(NULL) {}
+
+void PluginInstallerTest::SetUp() {
+  content::RenderViewHostTestHarness::SetUp();
+  installer_.reset(new PluginInstaller());
+}
+
+void PluginInstallerTest::TearDown() {
+  installer_.reset();
+  content::RenderViewHostTestHarness::TearDown();
+}
+
+scoped_ptr<content::MockDownloadItem>
+PluginInstallerTest::CreateMockDownloadItem() {
+  scoped_ptr<content::MockDownloadItem> mock_download_item(
+      new testing::StrictMock<content::MockDownloadItem>());
+  ON_CALL(*mock_download_item, AddObserver(_))
+      .WillByDefault(testing::SaveArg<0>(&last_download_item_observer_));
+  ON_CALL(*mock_download_item, RemoveObserver(_)).WillByDefault(
+      testing::Assign(&last_download_item_observer_,
+                      static_cast<content::DownloadItem::Observer*>(NULL)));
+  ON_CALL(*mock_download_item, GetState())
+      .WillByDefault(testing::Return(content::DownloadItem::IN_PROGRESS));
+  return mock_download_item.Pass();
+}
+
+class TestPluginInstallerObserver : public PluginInstallerObserver {
+ public:
+  explicit TestPluginInstallerObserver(PluginInstaller* installer)
+      : PluginInstallerObserver(installer),
+        download_started_(false),
+        download_finished_(false),
+        download_cancelled_(false) {}
+
+  bool download_started() const { return download_started_; }
+  bool download_finished() const { return download_finished_; }
+  bool download_cancelled() const { return download_cancelled_; }
+  const std::string& download_error() const { return download_error_; }
+
+ private:
+  virtual void DownloadStarted() OVERRIDE { download_started_ = true; }
+  virtual void DownloadFinished() OVERRIDE { download_finished_ = true; }
+  virtual void DownloadError(const std::string& message) OVERRIDE {
+    download_error_ = message;
+  }
+  virtual void DownloadCancelled() OVERRIDE { download_cancelled_ = true; }
+
+  bool download_started_;
+  bool download_finished_;
+  std::string download_error_;
+  bool download_cancelled_;
+};
+
+// Action for invoking the OnStartedCallback of DownloadURLParameters object
+// which is assumed to be pointed to by arg0.
+ACTION_P2(InvokeOnStartedCallback, download_item, interrupt_reason) {
+  arg0->callback().Run(download_item, interrupt_reason);
+}
+
+ACTION_P(InvokeClosure, closure) {
+  closure.Run();
 }
 
+const char kTestUrl[] = "http://example.com/some-url";
+
 }  // namespace
 
-TEST(PluginInstallerTest, SecurityStatus) {
-  const PluginInstaller::SecurityStatus kUpToDate =
-      PluginInstaller::SECURITY_STATUS_UP_TO_DATE;
-  const PluginInstaller::SecurityStatus kOutOfDate =
-      PluginInstaller::SECURITY_STATUS_OUT_OF_DATE;
-  const PluginInstaller::SecurityStatus kRequiresAuthorization =
-      PluginInstaller::SECURITY_STATUS_REQUIRES_AUTHORIZATION;
-
-  PluginInstaller installer("claybrick-writer",
-                            base::ASCIIToUTF16("ClayBrick Writer"),
-                            true, GURL(), GURL(),
-                            base::ASCIIToUTF16("ClayBrick"));
-
-#if defined(OS_LINUX)
-  EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "1.2.3"));
-#else
-  EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "1.2.3"));
-#endif
-
-  installer.AddVersion(Version("9.4.1"), kRequiresAuthorization);
-  installer.AddVersion(Version("10"), kOutOfDate);
-  installer.AddVersion(Version("10.2.1"), kUpToDate);
-
-  // Invalid version.
-  EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "foo"));
-
-  EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "0"));
-  EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "1.2.3"));
-  EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "9.4.1"));
-  EXPECT_EQ(kRequiresAuthorization, GetSecurityStatus(&installer, "9.4.2"));
-  EXPECT_EQ(kOutOfDate, GetSecurityStatus(&installer, "10.2.0"));
-  EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "10.2.1"));
-  EXPECT_EQ(kUpToDate, GetSecurityStatus(&installer, "11"));
+// Test that DownloadStarted()/DownloadFinished() notifications are sent to
+// observers when a download initiated by PluginInstaller completes
+// successfully.
+TEST_F(PluginInstallerTest, StartInstalling_SuccessfulDownload) {
+  content::MockDownloadManager mock_download_manager;
+  base::RunLoop run_loop;
+  scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
+
+  EXPECT_CALL(mock_download_manager,
+              DownloadUrlMock(testing::Property(
+                  &content::DownloadUrlParameters::url, GURL(kTestUrl))))
+      .WillOnce(testing::DoAll(
+          InvokeOnStartedCallback(download_item.get(),
+                                  content::DOWNLOAD_INTERRUPT_REASON_NONE),
+          InvokeClosure(run_loop.QuitClosure())));
+  EXPECT_CALL(*download_item, AddObserver(_));
+  EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
+
+  TestPluginInstallerObserver installer_observer(installer());
+  installer()->StartInstallingWithDownloadManager(
+      GURL(kTestUrl), web_contents(), &mock_download_manager);
+  run_loop.Run();
+
+  ASSERT_TRUE(last_download_item_observer());
+  EXPECT_TRUE(installer_observer.download_started());
+  EXPECT_FALSE(installer_observer.download_finished());
+
+  EXPECT_CALL(*download_item, GetState())
+      .WillOnce(testing::Return(content::DownloadItem::COMPLETE));
+  EXPECT_CALL(*download_item, RemoveObserver(_));
+  last_download_item_observer()->OnDownloadUpdated(download_item.get());
+  EXPECT_TRUE(installer_observer.download_finished());
+}
+
+// Test that DownloadStarted()/DownloadError() notifications are sent to
+// observers when a download initiated by PluginInstaller fails to start.
+TEST_F(PluginInstallerTest, StartInstalling_FailedStart) {
+  content::MockDownloadManager mock_download_manager;
+  base::RunLoop run_loop;
+  scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
+
+  EXPECT_CALL(mock_download_manager,
+              DownloadUrlMock(testing::Property(
+                  &content::DownloadUrlParameters::url, GURL(kTestUrl))))
+      .WillOnce(
+          testing::DoAll(InvokeOnStartedCallback(
+                             download_item.get(),
+                             content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED),
+                         InvokeClosure(run_loop.QuitClosure())));
+
+  TestPluginInstallerObserver installer_observer(installer());
+  installer()->StartInstallingWithDownloadManager(
+      GURL(kTestUrl), web_contents(), &mock_download_manager);
+  run_loop.Run();
+
+  EXPECT_FALSE(last_download_item_observer());
+  EXPECT_TRUE(installer_observer.download_started());
+  EXPECT_FALSE(installer_observer.download_finished());
+  EXPECT_EQ("Error 20: NETWORK_FAILED", installer_observer.download_error());
+}
+
+// Test that DownloadStarted()/DownloadError() notifications are sent to
+// observers when a download initiated by PluginInstaller starts successfully
+// but is interrupted later.
+TEST_F(PluginInstallerTest, StartInstalling_Interrupted) {
+  content::MockDownloadManager mock_download_manager;
+  base::RunLoop run_loop;
+  scoped_ptr<content::MockDownloadItem> download_item(CreateMockDownloadItem());
+
+  EXPECT_CALL(mock_download_manager,
+              DownloadUrlMock(testing::Property(
+                  &content::DownloadUrlParameters::url, GURL(kTestUrl))))
+      .WillOnce(testing::DoAll(
+          InvokeOnStartedCallback(download_item.get(),
+                                  content::DOWNLOAD_INTERRUPT_REASON_NONE),
+          InvokeClosure(run_loop.QuitClosure())));
+  EXPECT_CALL(*download_item, AddObserver(_));
+  EXPECT_CALL(*download_item, SetOpenWhenComplete(_));
+
+  TestPluginInstallerObserver installer_observer(installer());
+  installer()->StartInstallingWithDownloadManager(
+      GURL(kTestUrl), web_contents(), &mock_download_manager);
+  run_loop.Run();
+
+  ASSERT_TRUE(last_download_item_observer());
+  EXPECT_TRUE(installer_observer.download_started());
+  EXPECT_FALSE(installer_observer.download_finished());
+
+  EXPECT_CALL(*download_item, GetState())
+      .WillOnce(testing::Return(content::DownloadItem::INTERRUPTED));
+  EXPECT_CALL(*download_item, RemoveObserver(_));
+  EXPECT_CALL(*download_item, GetLastReason()).WillOnce(
+      testing::Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
+  last_download_item_observer()->OnDownloadUpdated(download_item.get());
+
+  EXPECT_FALSE(last_download_item_observer());
+  EXPECT_TRUE(installer_observer.download_started());
+  EXPECT_FALSE(installer_observer.download_finished());
+  EXPECT_EQ("NETWORK_FAILED", installer_observer.download_error());
 }