<commit_before>// 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 <vector>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/stringprintf.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_install_dialog.h"
#include "chrome/browser/extensions/extension_install_ui.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_webstore_private_api.h"
#include "chrome/browser/extensions/webstore_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/test_launcher_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/gpu/gpu_blacklist.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "net/base/mock_host_resolver.h"
#include "ui/gfx/gl/gl_switches.h"

using namespace extension_function_test_utils;

namespace {

class WebstoreInstallListener : public WebstoreInstaller::Delegate {
 public:
  WebstoreInstallListener()
      : received_failure_(false), received_success_(false), waiting_(false) {}

  void OnExtensionInstallSuccess(const std::string& id) OVERRIDE;
  void OnExtensionInstallFailure(const std::string& id,
                                 const std::string& error) OVERRIDE;
  void Wait();

  bool received_failure() const { return received_failure_; }
  bool received_success() const { return received_success_; }
  const std::string& id() const { return id_; }
  const std::string& error() const { return error_; }

 private:
  bool received_failure_;
  bool received_success_;
  bool waiting_;
  std::string id_;
  std::string error_;
};

void WebstoreInstallListener::OnExtensionInstallSuccess(const std::string& id) {
  received_success_ = true;
  id_ = id;

  if (waiting_) {
    waiting_ = false;
    MessageLoopForUI::current()->Quit();
  }
}

void WebstoreInstallListener::OnExtensionInstallFailure(
    const std::string& id, const std::string& error) {
  received_failure_ = true;
  id_ = id;
  error_ = error;

  if (waiting_) {
    waiting_ = false;
    MessageLoopForUI::current()->Quit();
  }
}

void WebstoreInstallListener::Wait() {
  if (received_success_ || received_failure_)
    return;

  waiting_ = true;
  ui_test_utils::RunMessageLoop();
}

}  // namespace

// A base class for tests below.
class ExtensionWebstorePrivateApiTest : public ExtensionApiTest {
 public:
  void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitchASCII(
        switches::kAppsGalleryURL, "http://www.example.com");
    command_line->AppendSwitchASCII(
        switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
  }

  void SetUpInProcessBrowserTestFixture() OVERRIDE {
    // Start up the test server and get us ready for calling the install
    // API functions.
    host_resolver()->AddRule("www.example.com", "127.0.0.1");
    ASSERT_TRUE(test_server()->Start());
    ExtensionInstallUI::DisableFailureUIForTests();
  }

 protected:
  // Returns a test server URL, but with host 'www.example.com' so it matches
  // the web store app's extent that we set up via command line flags.
  GURL GetTestServerURL(const std::string& path) {
    GURL url = test_server()->GetURL(
        std::string("files/extensions/api_test/webstore_private/") + path);

    // Replace the host with 'www.example.com' so it matches the web store
    // app's extent.
    GURL::Replacements replace_host;
    std::string host_str("www.example.com");
    replace_host.SetHostStr(host_str);

    return url.ReplaceComponents(replace_host);
  }

  // Navigates to |page| and runs the Extension API test there. Any downloads
  // of extensions will return the contents of |crx_file|.
  bool RunInstallTest(const std::string& page, const std::string& crx_file) {
    GURL crx_url = GetTestServerURL(crx_file);
    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
        switches::kAppsGalleryUpdateURL, crx_url.spec());

    GURL page_url = GetTestServerURL(page);
    return RunPageTest(page_url.spec());
  }

  ExtensionService* service() {
    return browser()->profile()->GetExtensionService();
  }
};

class ExtensionWebstorePrivateBundleTest
    : public ExtensionWebstorePrivateApiTest {
 public:
  void SetUpInProcessBrowserTestFixture() OVERRIDE {
    ExtensionWebstorePrivateApiTest::SetUpInProcessBrowserTestFixture();

    // The test server needs to have already started, so setup the switch here
    // rather than in SetUpCommandLine.
    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
        switches::kAppsGalleryDownloadURL,
        GetTestServerURL("bundle/%s.crx").spec());

    PackCRX("begfmnajjkbjdgmffnjaojchoncnmngg");
    PackCRX("bmfoocgfinpmkmlbjhcbofejhkhlbchk");
    PackCRX("mpneghmdnmaolkljkipbhaienajcflfe");
  }

  void TearDownInProcessBrowserTestFixture() OVERRIDE {
    ExtensionWebstorePrivateApiTest::TearDownInProcessBrowserTestFixture();
    for (size_t i = 0; i < test_crx_.size(); ++i)
      ASSERT_TRUE(file_util::Delete(test_crx_[i], false));
  }

 private:
  void PackCRX(const std::string& id) {
    FilePath data_path = test_data_dir_.AppendASCII("webstore_private/bundle");
    FilePath dir_path = data_path.AppendASCII(id);
    FilePath pem_path = data_path.AppendASCII(id + ".pem");
    FilePath crx_path = data_path.AppendASCII(id + ".crx");
    FilePath destination = PackExtensionWithOptions(
        dir_path, crx_path, pem_path, FilePath());

    ASSERT_FALSE(destination.empty());
    ASSERT_EQ(destination, crx_path);

    test_crx_.push_back(destination);
  }

  std::vector<FilePath> test_crx_;
};

class ExtensionWebstoreGetWebGLStatusTest : public InProcessBrowserTest {
 public:
  void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    // In linux, we need to launch GPU process to decide if WebGL is allowed.
    // Run it on top of osmesa to avoid bot driver issues.
#if defined(OS_LINUX)
    CHECK(test_launcher_utils::OverrideGLImplementation(
        command_line, gfx::kGLImplementationOSMesaName)) <<
        "kUseGL must not be set multiple times!";
#endif
  }

 protected:
  void RunTest(bool webgl_allowed) {
    static const char kEmptyArgs[] = "[]";
    static const char kWebGLStatusAllowed[] = "webgl_allowed";
    static const char kWebGLStatusBlocked[] = "webgl_blocked";
    scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
            new GetWebGLStatusFunction(), kEmptyArgs, browser()));
    EXPECT_EQ(base::Value::TYPE_STRING, result->GetType());
    StringValue* value = static_cast<StringValue*>(result.get());
    std::string webgl_status = "";
    EXPECT_TRUE(value && value->GetAsString(&webgl_status));
    EXPECT_STREQ(webgl_allowed ? kWebGLStatusAllowed : kWebGLStatusBlocked,
                 webgl_status.c_str());
  }
};

// Test cases where the user accepts the install confirmation dialog.
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, FLAKY_InstallAccepted) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  ASSERT_TRUE(RunInstallTest("accepted.html", "extension.crx"));
}

// Test having the default download directory missing.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, MissingDownloadDir) {
  // Set a non-existent directory as the download path.
  ScopedTempDir temp_dir;
  EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath missing_directory = temp_dir.Take();
  EXPECT_TRUE(file_util::Delete(missing_directory, true));
  WebstoreInstaller::SetDownloadDirectoryForTests(&missing_directory);

  // Now run the install test, which should succeed.
  ASSERT_TRUE(RunInstallTest("accepted.html", "extension.crx"));

  // Cleanup.
  if (file_util::DirectoryExists(missing_directory))
    EXPECT_TRUE(file_util::Delete(missing_directory, true));
}

// Tests passing a localized name.
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       FLAKY_InstallLocalized) {
  ASSERT_TRUE(RunInstallTest("localized.html", "localized_extension.crx"));
}

// Now test the case where the user cancels the confirmation dialog.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, InstallCancelled) {
  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kAppsGalleryInstallAutoConfirmForTests, "cancel");
  ASSERT_TRUE(RunInstallTest("cancelled.html", "extension.crx"));
}

IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IncorrectManifest1) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("incorrect_manifest1.html", "extension.crx"));
  listener.Wait();
  ASSERT_TRUE(listener.received_failure());
  ASSERT_EQ("Manifest file is invalid.", listener.error());
}

IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IncorrectManifest2) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("incorrect_manifest2.html", "extension.crx"));
  listener.Wait();
  EXPECT_TRUE(listener.received_failure());
  ASSERT_EQ("Manifest file is invalid.", listener.error());
}

// Tests that we can request an app installed bubble (instead of the default
// UI when an app is installed).
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       FLAKY_AppInstallBubble) {
  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("app_install_bubble.html", "app.crx"));
  listener.Wait();
  ASSERT_TRUE(listener.received_success());
  ASSERT_EQ("iladmdjkfniedhfhcfoefgojhgaiaccc", listener.id());
}

// Tests using the iconUrl parameter to the install function.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IconUrl) {
  ASSERT_TRUE(RunInstallTest("icon_url.html", "extension.crx"));
}

// Tests using silentlyInstall to install extensions.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateBundleTest, SilentlyInstall) {
  WebstorePrivateApi::SetTrustTestIDsForTesting(true);
  ASSERT_TRUE(RunPageTest(GetTestServerURL("silently_install.html").spec()));
}

// Tests getWebGLStatus function when WebGL is allowed.
IN_PROC_BROWSER_TEST_F(ExtensionWebstoreGetWebGLStatusTest, Allowed) {
  bool webgl_allowed = true;
  RunTest(webgl_allowed);
}

// Tests getWebGLStatus function when WebGL is blacklisted.
IN_PROC_BROWSER_TEST_F(ExtensionWebstoreGetWebGLStatusTest, Blocked) {
  static const std::string json_blacklist =
      "{\n"
      "  \"name\": \"gpu blacklist\",\n"
      "  \"version\": \"1.0\",\n"
      "  \"entries\": [\n"
      "    {\n"
      "      \"id\": 1,\n"
      "      \"blacklist\": [\n"
      "        \"webgl\"\n"
      "      ]\n"
      "    }\n"
      "  ]\n"
      "}";
  scoped_ptr<Version> os_version(Version::GetVersionFromString("1.0"));
  GpuBlacklist* blacklist = new GpuBlacklist("1.0");

  ASSERT_TRUE(blacklist->LoadGpuBlacklist(
      json_blacklist, GpuBlacklist::kAllOs));
  GpuDataManager::GetInstance()->SetGpuBlacklist(blacklist);
  GpuFeatureFlags flags = GpuDataManager::GetInstance()->GetGpuFeatureFlags();
  EXPECT_EQ(
      flags.flags(), static_cast<uint32>(GpuFeatureFlags::kGpuFeatureWebgl));

  bool webgl_allowed = false;
  RunTest(webgl_allowed);
}
<commit_msg>Mark ExtensionWebstorePrivateApiTest.SilentlyInstall as flaky.<commit_after>// 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 <vector>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/stringprintf.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_install_dialog.h"
#include "chrome/browser/extensions/extension_install_ui.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_webstore_private_api.h"
#include "chrome/browser/extensions/webstore_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/test_launcher_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/gpu/gpu_blacklist.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "net/base/mock_host_resolver.h"
#include "ui/gfx/gl/gl_switches.h"

using namespace extension_function_test_utils;

namespace {

class WebstoreInstallListener : public WebstoreInstaller::Delegate {
 public:
  WebstoreInstallListener()
      : received_failure_(false), received_success_(false), waiting_(false) {}

  void OnExtensionInstallSuccess(const std::string& id) OVERRIDE;
  void OnExtensionInstallFailure(const std::string& id,
                                 const std::string& error) OVERRIDE;
  void Wait();

  bool received_failure() const { return received_failure_; }
  bool received_success() const { return received_success_; }
  const std::string& id() const { return id_; }
  const std::string& error() const { return error_; }

 private:
  bool received_failure_;
  bool received_success_;
  bool waiting_;
  std::string id_;
  std::string error_;
};

void WebstoreInstallListener::OnExtensionInstallSuccess(const std::string& id) {
  received_success_ = true;
  id_ = id;

  if (waiting_) {
    waiting_ = false;
    MessageLoopForUI::current()->Quit();
  }
}

void WebstoreInstallListener::OnExtensionInstallFailure(
    const std::string& id, const std::string& error) {
  received_failure_ = true;
  id_ = id;
  error_ = error;

  if (waiting_) {
    waiting_ = false;
    MessageLoopForUI::current()->Quit();
  }
}

void WebstoreInstallListener::Wait() {
  if (received_success_ || received_failure_)
    return;

  waiting_ = true;
  ui_test_utils::RunMessageLoop();
}

}  // namespace

// A base class for tests below.
class ExtensionWebstorePrivateApiTest : public ExtensionApiTest {
 public:
  void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitchASCII(
        switches::kAppsGalleryURL, "http://www.example.com");
    command_line->AppendSwitchASCII(
        switches::kAppsGalleryInstallAutoConfirmForTests, "accept");
  }

  void SetUpInProcessBrowserTestFixture() OVERRIDE {
    // Start up the test server and get us ready for calling the install
    // API functions.
    host_resolver()->AddRule("www.example.com", "127.0.0.1");
    ASSERT_TRUE(test_server()->Start());
    ExtensionInstallUI::DisableFailureUIForTests();
  }

 protected:
  // Returns a test server URL, but with host 'www.example.com' so it matches
  // the web store app's extent that we set up via command line flags.
  GURL GetTestServerURL(const std::string& path) {
    GURL url = test_server()->GetURL(
        std::string("files/extensions/api_test/webstore_private/") + path);

    // Replace the host with 'www.example.com' so it matches the web store
    // app's extent.
    GURL::Replacements replace_host;
    std::string host_str("www.example.com");
    replace_host.SetHostStr(host_str);

    return url.ReplaceComponents(replace_host);
  }

  // Navigates to |page| and runs the Extension API test there. Any downloads
  // of extensions will return the contents of |crx_file|.
  bool RunInstallTest(const std::string& page, const std::string& crx_file) {
    GURL crx_url = GetTestServerURL(crx_file);
    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
        switches::kAppsGalleryUpdateURL, crx_url.spec());

    GURL page_url = GetTestServerURL(page);
    return RunPageTest(page_url.spec());
  }

  ExtensionService* service() {
    return browser()->profile()->GetExtensionService();
  }
};

class ExtensionWebstorePrivateBundleTest
    : public ExtensionWebstorePrivateApiTest {
 public:
  void SetUpInProcessBrowserTestFixture() OVERRIDE {
    ExtensionWebstorePrivateApiTest::SetUpInProcessBrowserTestFixture();

    // The test server needs to have already started, so setup the switch here
    // rather than in SetUpCommandLine.
    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
        switches::kAppsGalleryDownloadURL,
        GetTestServerURL("bundle/%s.crx").spec());

    PackCRX("begfmnajjkbjdgmffnjaojchoncnmngg");
    PackCRX("bmfoocgfinpmkmlbjhcbofejhkhlbchk");
    PackCRX("mpneghmdnmaolkljkipbhaienajcflfe");
  }

  void TearDownInProcessBrowserTestFixture() OVERRIDE {
    ExtensionWebstorePrivateApiTest::TearDownInProcessBrowserTestFixture();
    for (size_t i = 0; i < test_crx_.size(); ++i)
      ASSERT_TRUE(file_util::Delete(test_crx_[i], false));
  }

 private:
  void PackCRX(const std::string& id) {
    FilePath data_path = test_data_dir_.AppendASCII("webstore_private/bundle");
    FilePath dir_path = data_path.AppendASCII(id);
    FilePath pem_path = data_path.AppendASCII(id + ".pem");
    FilePath crx_path = data_path.AppendASCII(id + ".crx");
    FilePath destination = PackExtensionWithOptions(
        dir_path, crx_path, pem_path, FilePath());

    ASSERT_FALSE(destination.empty());
    ASSERT_EQ(destination, crx_path);

    test_crx_.push_back(destination);
  }

  std::vector<FilePath> test_crx_;
};

class ExtensionWebstoreGetWebGLStatusTest : public InProcessBrowserTest {
 public:
  void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    // In linux, we need to launch GPU process to decide if WebGL is allowed.
    // Run it on top of osmesa to avoid bot driver issues.
#if defined(OS_LINUX)
    CHECK(test_launcher_utils::OverrideGLImplementation(
        command_line, gfx::kGLImplementationOSMesaName)) <<
        "kUseGL must not be set multiple times!";
#endif
  }

 protected:
  void RunTest(bool webgl_allowed) {
    static const char kEmptyArgs[] = "[]";
    static const char kWebGLStatusAllowed[] = "webgl_allowed";
    static const char kWebGLStatusBlocked[] = "webgl_blocked";
    scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
            new GetWebGLStatusFunction(), kEmptyArgs, browser()));
    EXPECT_EQ(base::Value::TYPE_STRING, result->GetType());
    StringValue* value = static_cast<StringValue*>(result.get());
    std::string webgl_status = "";
    EXPECT_TRUE(value && value->GetAsString(&webgl_status));
    EXPECT_STREQ(webgl_allowed ? kWebGLStatusAllowed : kWebGLStatusBlocked,
                 webgl_status.c_str());
  }
};

// Test cases where the user accepts the install confirmation dialog.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, InstallAccepted) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  ASSERT_TRUE(RunInstallTest("accepted.html", "extension.crx"));
}

// Test having the default download directory missing.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, MissingDownloadDir) {
  // Set a non-existent directory as the download path.
  ScopedTempDir temp_dir;
  EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath missing_directory = temp_dir.Take();
  EXPECT_TRUE(file_util::Delete(missing_directory, true));
  WebstoreInstaller::SetDownloadDirectoryForTests(&missing_directory);

  // Now run the install test, which should succeed.
  ASSERT_TRUE(RunInstallTest("accepted.html", "extension.crx"));

  // Cleanup.
  if (file_util::DirectoryExists(missing_directory))
    EXPECT_TRUE(file_util::Delete(missing_directory, true));
}

// Tests passing a localized name.
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       FLAKY_InstallLocalized) {
  ASSERT_TRUE(RunInstallTest("localized.html", "localized_extension.crx"));
}

// Now test the case where the user cancels the confirmation dialog.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest, InstallCancelled) {
  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kAppsGalleryInstallAutoConfirmForTests, "cancel");
  ASSERT_TRUE(RunInstallTest("cancelled.html", "extension.crx"));
}

IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IncorrectManifest1) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("incorrect_manifest1.html", "extension.crx"));
  listener.Wait();
  ASSERT_TRUE(listener.received_failure());
  ASSERT_EQ("Manifest file is invalid.", listener.error());
}

IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IncorrectManifest2) {
  ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  FilePath temp_path = temp_dir.path();
  WebstoreInstaller::SetDownloadDirectoryForTests(&temp_path);

  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("incorrect_manifest2.html", "extension.crx"));
  listener.Wait();
  EXPECT_TRUE(listener.received_failure());
  ASSERT_EQ("Manifest file is invalid.", listener.error());
}

// Tests that we can request an app installed bubble (instead of the default
// UI when an app is installed).
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       FLAKY_AppInstallBubble) {
  WebstoreInstallListener listener;
  WebstorePrivateApi::SetWebstoreInstallerDelegateForTesting(&listener);
  ASSERT_TRUE(RunInstallTest("app_install_bubble.html", "app.crx"));
  listener.Wait();
  ASSERT_TRUE(listener.received_success());
  ASSERT_EQ("iladmdjkfniedhfhcfoefgojhgaiaccc", listener.id());
}

// Tests using the iconUrl parameter to the install function.
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateApiTest,
                       IconUrl) {
  ASSERT_TRUE(RunInstallTest("icon_url.html", "extension.crx"));
}

// Tests using silentlyInstall to install extensions.
//
// flaky: http://crbug.com/111308
IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateBundleTest,
                       FLAKY_SilentlyInstall) {
  WebstorePrivateApi::SetTrustTestIDsForTesting(true);
  ASSERT_TRUE(RunPageTest(GetTestServerURL("silently_install.html").spec()));
}

// Tests getWebGLStatus function when WebGL is allowed.
IN_PROC_BROWSER_TEST_F(ExtensionWebstoreGetWebGLStatusTest, Allowed) {
  bool webgl_allowed = true;
  RunTest(webgl_allowed);
}

// Tests getWebGLStatus function when WebGL is blacklisted.
IN_PROC_BROWSER_TEST_F(ExtensionWebstoreGetWebGLStatusTest, Blocked) {
  static const std::string json_blacklist =
      "{\n"
      "  \"name\": \"gpu blacklist\",\n"
      "  \"version\": \"1.0\",\n"
      "  \"entries\": [\n"
      "    {\n"
      "      \"id\": 1,\n"
      "      \"blacklist\": [\n"
      "        \"webgl\"\n"
      "      ]\n"
      "    }\n"
      "  ]\n"
      "}";
  scoped_ptr<Version> os_version(Version::GetVersionFromString("1.0"));
  GpuBlacklist* blacklist = new GpuBlacklist("1.0");

  ASSERT_TRUE(blacklist->LoadGpuBlacklist(
      json_blacklist, GpuBlacklist::kAllOs));
  GpuDataManager::GetInstance()->SetGpuBlacklist(blacklist);
  GpuFeatureFlags flags = GpuDataManager::GetInstance()->GetGpuFeatureFlags();
  EXPECT_EQ(
      flags.flags(), static_cast<uint32>(GpuFeatureFlags::kGpuFeatureWebgl));

  bool webgl_allowed = false;
  RunTest(webgl_allowed);
}
<|endoftext|>


<commit_before>// Copyright (c) 2011 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 "chrome/browser/notifications/notification_options_menu_model.h"

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/notifications/balloon_collection.h"
#include "chrome/browser/notifications/balloon_host.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/notifications_prefs_cache.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/content_settings_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

// Menu commands
const int kTogglePermissionCommand = 0;
const int kToggleExtensionCommand = 1;
const int kOpenContentSettingsCommand = 2;
const int kCornerSelectionSubMenu = 3;

const int kCornerGroupId = 10;
const int kCornerUpperLeft = 11;
const int kCornerUpperRight = 12;
const int kCornerLowerLeft = 13;
const int kCornerLowerRight = 14;
const int kCornerDefault = 20;

CornerSelectionMenuModel::CornerSelectionMenuModel(Balloon* balloon)
    : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
      balloon_(balloon) {
  AddRadioItem(kCornerDefault,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_DEFAULT),
               kCornerGroupId);
  AddSeparator();
  AddRadioItem(kCornerUpperLeft,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_UPPER_LEFT),
               kCornerGroupId);
  AddRadioItem(kCornerUpperRight,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_UPPER_RIGHT),
               kCornerGroupId);
  AddRadioItem(kCornerLowerLeft,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_LOWER_LEFT),
               kCornerGroupId);
  AddRadioItem(kCornerLowerRight,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_LOWER_RIGHT),
               kCornerGroupId);
}

CornerSelectionMenuModel::~CornerSelectionMenuModel() {
}

bool CornerSelectionMenuModel::IsCommandIdChecked(int command_id) const {
  NotificationUIManager* ui = g_browser_process->notification_ui_manager();
  BalloonCollection::PositionPreference current = ui->GetPositionPreference();

  LOG(INFO) << "Current position preference: " << current;

  if (command_id == kCornerUpperLeft)
    return (current == BalloonCollection::UPPER_LEFT);
  else if (command_id == kCornerUpperRight)
    return (current == BalloonCollection::UPPER_RIGHT);
  else if (command_id == kCornerLowerLeft)
    return (current == BalloonCollection::LOWER_LEFT);
  else if (command_id == kCornerLowerRight)
    return (current == BalloonCollection::LOWER_RIGHT);
  else if (command_id == kCornerDefault)
    return (current == BalloonCollection::DEFAULT_POSITION);

  NOTREACHED();
  return false;
}

bool CornerSelectionMenuModel::IsCommandIdEnabled(int command_id) const {
  // All the menu options are always enabled.
  return true;
}

bool CornerSelectionMenuModel::GetAcceleratorForCommandId(
    int command_id, ui::Accelerator* accelerator) {
  // Currently no accelerators.
  return false;
}

void CornerSelectionMenuModel::ExecuteCommand(int command_id) {
  NotificationUIManager* ui = g_browser_process->notification_ui_manager();

  LOG(INFO) << "Executing command: " << command_id;

  if (command_id == kCornerUpperLeft)
    ui->SetPositionPreference(BalloonCollection::UPPER_LEFT);
  else if (command_id == kCornerUpperRight)
    ui->SetPositionPreference(BalloonCollection::UPPER_RIGHT);
  else if (command_id == kCornerLowerLeft)
    ui->SetPositionPreference(BalloonCollection::LOWER_LEFT);
  else if (command_id == kCornerLowerRight)
    ui->SetPositionPreference(BalloonCollection::LOWER_RIGHT);
  else if (command_id == kCornerDefault)
    ui->SetPositionPreference(BalloonCollection::DEFAULT_POSITION);
  else
    NOTREACHED();
}

NotificationOptionsMenuModel::NotificationOptionsMenuModel(Balloon* balloon)
    : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
      balloon_(balloon) {

  const Notification& notification = balloon->notification();
  const GURL& origin = notification.origin_url();

  if (origin.SchemeIs(chrome::kExtensionScheme)) {
    const string16 disable_label = l10n_util::GetStringUTF16(
        IDS_EXTENSIONS_DISABLE);
    AddItem(kToggleExtensionCommand, disable_label);
  } else {
    const string16 disable_label = l10n_util::GetStringFUTF16(
        IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
        notification.display_source());
    AddItem(kTogglePermissionCommand, disable_label);
  }

  const string16 settings_label = l10n_util::GetStringUTF16(
      IDS_NOTIFICATIONS_SETTINGS_BUTTON);
  AddItem(kOpenContentSettingsCommand, settings_label);

  corner_menu_model_.reset(new CornerSelectionMenuModel(balloon));
  AddSubMenu(kCornerSelectionSubMenu,
             l10n_util::GetStringUTF16(IDS_NOTIFICATION_CHOOSE_POSITION),
             corner_menu_model_.get());
}

NotificationOptionsMenuModel::~NotificationOptionsMenuModel() {
}

bool NotificationOptionsMenuModel::IsItemForCommandIdDynamic(int command_id)
    const {
  return command_id == kTogglePermissionCommand ||
         command_id == kToggleExtensionCommand;
}

string16 NotificationOptionsMenuModel::GetLabelForCommandId(int command_id)
    const {
  // TODO(tfarina,johnnyg): Remove this code if we decide to close notifications
  // after permissions are revoked.
  if (command_id == kTogglePermissionCommand ||
      command_id == kToggleExtensionCommand) {
    const Notification& notification = balloon_->notification();
    const GURL& origin = notification.origin_url();

    DesktopNotificationService* service =
        DesktopNotificationServiceFactory::GetForProfile(balloon_->profile());
    if (origin.SchemeIs(chrome::kExtensionScheme)) {
      ExtensionService* ext_service =
          balloon_->profile()->GetExtensionService();
      const Extension* extension = ext_service->GetExtensionByURL(origin);
      if (extension) {
        ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
        const std::string& id = extension->id();
        if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
          return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE);
        else
          return l10n_util::GetStringUTF16(IDS_EXTENSIONS_ENABLE);
      }
    } else {
      if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW) {
        return l10n_util::GetStringFUTF16(
            IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
            notification.display_source());
      } else {
        return l10n_util::GetStringFUTF16(
            IDS_NOTIFICATION_BALLOON_ENABLE_MESSAGE,
            notification.display_source());
      }
    }
  } else if (command_id == kOpenContentSettingsCommand) {
    return l10n_util::GetStringUTF16(IDS_NOTIFICATIONS_SETTINGS_BUTTON);
  }
  return string16();
}

bool NotificationOptionsMenuModel::IsCommandIdChecked(int /* command_id */)
    const {
  // Nothing in the menu is checked.
  return false;
}

bool NotificationOptionsMenuModel::IsCommandIdEnabled(int /* command_id */)
    const {
  // All the menu options are always enabled.
  return true;
}

bool NotificationOptionsMenuModel::GetAcceleratorForCommandId(
    int /* command_id */, ui::Accelerator* /* accelerator */) {
  // Currently no accelerators.
  return false;
}

void NotificationOptionsMenuModel::ExecuteCommand(int command_id) {
  DesktopNotificationService* service =
      DesktopNotificationServiceFactory::GetForProfile(balloon_->profile());
  ExtensionService* ext_service =
      balloon_->profile()->GetExtensionService();
  const GURL& origin = balloon_->notification().origin_url();
  switch (command_id) {
    case kTogglePermissionCommand:
      if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW)
        service->DenyPermission(origin);
      else
        service->GrantPermission(origin);
      break;
    case kToggleExtensionCommand: {
      const Extension* extension = ext_service->GetExtensionByURL(origin);
      if (extension) {
        ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
        const std::string& id = extension->id();
        if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
          ext_service->DisableExtension(id);
        else
          ext_service->EnableExtension(id);
      }
      break;
    }
    case kOpenContentSettingsCommand: {
      TabContents* tab_contents =
          balloon_->view()->GetHost()->associated_tab_contents();
      tab_contents->delegate()->ShowContentSettingsPage(
          CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
      break;
    }
    default:
      NOTREACHED();
      break;
  }
}
<commit_msg>Fix opening settings page for balloons.<commit_after>// Copyright (c) 2011 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 "chrome/browser/notifications/notification_options_menu_model.h"

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/notifications/balloon_collection.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "chrome/browser/notifications/notifications_prefs_cache.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/content_settings_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents_delegate.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

// Menu commands
const int kTogglePermissionCommand = 0;
const int kToggleExtensionCommand = 1;
const int kOpenContentSettingsCommand = 2;
const int kCornerSelectionSubMenu = 3;

const int kCornerGroupId = 10;
const int kCornerUpperLeft = 11;
const int kCornerUpperRight = 12;
const int kCornerLowerLeft = 13;
const int kCornerLowerRight = 14;
const int kCornerDefault = 20;

CornerSelectionMenuModel::CornerSelectionMenuModel(Balloon* balloon)
    : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
      balloon_(balloon) {
  AddRadioItem(kCornerDefault,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_DEFAULT),
               kCornerGroupId);
  AddSeparator();
  AddRadioItem(kCornerUpperLeft,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_UPPER_LEFT),
               kCornerGroupId);
  AddRadioItem(kCornerUpperRight,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_UPPER_RIGHT),
               kCornerGroupId);
  AddRadioItem(kCornerLowerLeft,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_LOWER_LEFT),
               kCornerGroupId);
  AddRadioItem(kCornerLowerRight,
               l10n_util::GetStringUTF16(IDS_NOTIFICATION_POSITION_LOWER_RIGHT),
               kCornerGroupId);
}

CornerSelectionMenuModel::~CornerSelectionMenuModel() {
}

bool CornerSelectionMenuModel::IsCommandIdChecked(int command_id) const {
  NotificationUIManager* ui = g_browser_process->notification_ui_manager();
  BalloonCollection::PositionPreference current = ui->GetPositionPreference();

  LOG(INFO) << "Current position preference: " << current;

  if (command_id == kCornerUpperLeft)
    return (current == BalloonCollection::UPPER_LEFT);
  else if (command_id == kCornerUpperRight)
    return (current == BalloonCollection::UPPER_RIGHT);
  else if (command_id == kCornerLowerLeft)
    return (current == BalloonCollection::LOWER_LEFT);
  else if (command_id == kCornerLowerRight)
    return (current == BalloonCollection::LOWER_RIGHT);
  else if (command_id == kCornerDefault)
    return (current == BalloonCollection::DEFAULT_POSITION);

  NOTREACHED();
  return false;
}

bool CornerSelectionMenuModel::IsCommandIdEnabled(int command_id) const {
  // All the menu options are always enabled.
  return true;
}

bool CornerSelectionMenuModel::GetAcceleratorForCommandId(
    int command_id, ui::Accelerator* accelerator) {
  // Currently no accelerators.
  return false;
}

void CornerSelectionMenuModel::ExecuteCommand(int command_id) {
  NotificationUIManager* ui = g_browser_process->notification_ui_manager();

  LOG(INFO) << "Executing command: " << command_id;

  if (command_id == kCornerUpperLeft)
    ui->SetPositionPreference(BalloonCollection::UPPER_LEFT);
  else if (command_id == kCornerUpperRight)
    ui->SetPositionPreference(BalloonCollection::UPPER_RIGHT);
  else if (command_id == kCornerLowerLeft)
    ui->SetPositionPreference(BalloonCollection::LOWER_LEFT);
  else if (command_id == kCornerLowerRight)
    ui->SetPositionPreference(BalloonCollection::LOWER_RIGHT);
  else if (command_id == kCornerDefault)
    ui->SetPositionPreference(BalloonCollection::DEFAULT_POSITION);
  else
    NOTREACHED();
}

NotificationOptionsMenuModel::NotificationOptionsMenuModel(Balloon* balloon)
    : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
      balloon_(balloon) {

  const Notification& notification = balloon->notification();
  const GURL& origin = notification.origin_url();

  if (origin.SchemeIs(chrome::kExtensionScheme)) {
    const string16 disable_label = l10n_util::GetStringUTF16(
        IDS_EXTENSIONS_DISABLE);
    AddItem(kToggleExtensionCommand, disable_label);
  } else {
    const string16 disable_label = l10n_util::GetStringFUTF16(
        IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
        notification.display_source());
    AddItem(kTogglePermissionCommand, disable_label);
  }

  const string16 settings_label = l10n_util::GetStringUTF16(
      IDS_NOTIFICATIONS_SETTINGS_BUTTON);
  AddItem(kOpenContentSettingsCommand, settings_label);

  corner_menu_model_.reset(new CornerSelectionMenuModel(balloon));
  AddSubMenu(kCornerSelectionSubMenu,
             l10n_util::GetStringUTF16(IDS_NOTIFICATION_CHOOSE_POSITION),
             corner_menu_model_.get());
}

NotificationOptionsMenuModel::~NotificationOptionsMenuModel() {
}

bool NotificationOptionsMenuModel::IsItemForCommandIdDynamic(int command_id)
    const {
  return command_id == kTogglePermissionCommand ||
         command_id == kToggleExtensionCommand;
}

string16 NotificationOptionsMenuModel::GetLabelForCommandId(int command_id)
    const {
  // TODO(tfarina,johnnyg): Remove this code if we decide to close notifications
  // after permissions are revoked.
  if (command_id == kTogglePermissionCommand ||
      command_id == kToggleExtensionCommand) {
    const Notification& notification = balloon_->notification();
    const GURL& origin = notification.origin_url();

    DesktopNotificationService* service =
        DesktopNotificationServiceFactory::GetForProfile(balloon_->profile());
    if (origin.SchemeIs(chrome::kExtensionScheme)) {
      ExtensionService* ext_service =
          balloon_->profile()->GetExtensionService();
      const Extension* extension = ext_service->GetExtensionByURL(origin);
      if (extension) {
        ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
        const std::string& id = extension->id();
        if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
          return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE);
        else
          return l10n_util::GetStringUTF16(IDS_EXTENSIONS_ENABLE);
      }
    } else {
      if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW) {
        return l10n_util::GetStringFUTF16(
            IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
            notification.display_source());
      } else {
        return l10n_util::GetStringFUTF16(
            IDS_NOTIFICATION_BALLOON_ENABLE_MESSAGE,
            notification.display_source());
      }
    }
  } else if (command_id == kOpenContentSettingsCommand) {
    return l10n_util::GetStringUTF16(IDS_NOTIFICATIONS_SETTINGS_BUTTON);
  }
  return string16();
}

bool NotificationOptionsMenuModel::IsCommandIdChecked(int /* command_id */)
    const {
  // Nothing in the menu is checked.
  return false;
}

bool NotificationOptionsMenuModel::IsCommandIdEnabled(int /* command_id */)
    const {
  // All the menu options are always enabled.
  return true;
}

bool NotificationOptionsMenuModel::GetAcceleratorForCommandId(
    int /* command_id */, ui::Accelerator* /* accelerator */) {
  // Currently no accelerators.
  return false;
}

void NotificationOptionsMenuModel::ExecuteCommand(int command_id) {
  DesktopNotificationService* service =
      DesktopNotificationServiceFactory::GetForProfile(balloon_->profile());
  ExtensionService* ext_service =
      balloon_->profile()->GetExtensionService();
  const GURL& origin = balloon_->notification().origin_url();
  switch (command_id) {
    case kTogglePermissionCommand:
      if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW)
        service->DenyPermission(origin);
      else
        service->GrantPermission(origin);
      break;
    case kToggleExtensionCommand: {
      const Extension* extension = ext_service->GetExtensionByURL(origin);
      if (extension) {
        ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
        const std::string& id = extension->id();
        if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
          ext_service->DisableExtension(id);
        else
          ext_service->EnableExtension(id);
      }
      break;
    }
    case kOpenContentSettingsCommand: {
      Browser* browser =
          BrowserList::GetLastActiveWithProfile(balloon_->profile());
      if (!browser) {
        // It is possible that there is no browser window (e.g. when there are
        // background pages, or for a chrome frame process on windows).
        browser = Browser::Create(balloon_->profile());
      }
      static_cast<TabContentsDelegate*>(browser)->ShowContentSettingsPage(
          CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
      break;
    }
    default:
      NOTREACHED();
      break;
  }
}
<|endoftext|>


<commit_before>/*
Reproducing xfstest generic/321 directory_test()

1. Create directory A and fsync it

After a crash, the directory should still be present.

https://github.com/kdave/xfstests/blob/master/tests/generic/321
*/

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <errno.h>

#include "BaseTestCase.h"
#include "../user_tools/api/workload.h"
#include "../user_tools/api/actions.h"
#define TEST_DIR_A "test_dir_a"

using fs_testing::tests::DataTestResult;
using fs_testing::user_tools::api::WriteData;
using fs_testing::user_tools::api::WriteDataMmap;
using fs_testing::user_tools::api::Checkpoint;
using std::string;

#define TEST_FILE_PERMS  ((mode_t) (S_IRWXU | S_IRWXG | S_IRWXO))

namespace fs_testing {
namespace tests {

class Generic321_1: public BaseTestCase {
 public:
  virtual int setup() override {

	init_paths();

	// Create test directory A.
	int res = mkdir(dir_path.c_str(), 0777);
    if (res < 0) {
      return -1;
    }

    // fsync the directory
    int dir = open(dir_path.c_str(), O_RDONLY, O_DIRECTORY);
    if (dir < 0) {
    	return -4;
    }

    if (fsync(dir) < 0) {
    	return -5;
    }
    close(dir);

    return 0;
  }

  virtual int run() override {

	// nothing here since it's a simple test
	if (Checkpoint() < 0){
      return -5;
    }

    return 0;
  }

  virtual int check_test(unsigned int last_checkpoint,
      DataTestResult *test_result) override {

	init_paths();

    struct stat stats;
    const int stats_res = stat(dir_path.c_str(), &stats);
    const int errno_stats = errno;

    if (stats_res < 0 && errno_stats == ENOENT) {
      test_result->SetError(DataTestResult::kFileMissing);
      test_result->error_description = " : " + dir_path + " is missing";
      return 0;
    }

    // If it is not a directory
    if (!S_ISDIR(stats.st_mode)) {
        test_result->SetError(DataTestResult::kFileMetadataCorrupted);
        test_result->error_description = " : " + dir_path + " is not a directory";
        return 0;
    }

    return 0;
  }

 private:
  string dir_path;

  void init_paths() {
	  dir_path = mnt_dir_ + "/" TEST_DIR_A;
  }
};

}  // namespace tests
}  // namespace fs_testing

extern "C" fs_testing::tests::BaseTestCase *test_case_get_instance() {
  return new fs_testing::tests::Generic321_1;
}

extern "C" void test_case_delete_instance(fs_testing::tests::BaseTestCase *tc) {
  delete tc;
}
<commit_msg>Moving create and fsync to run method in generic 321_1<commit_after>/*
Reproducing xfstest generic/321 directory_test()

1. Create directory A and fsync it

After a crash, the directory should still be present.

https://github.com/kdave/xfstests/blob/master/tests/generic/321
*/

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <errno.h>

#include "BaseTestCase.h"
#include "../user_tools/api/workload.h"
#include "../user_tools/api/actions.h"
#define TEST_DIR_A "test_dir_a"

using fs_testing::tests::DataTestResult;
using fs_testing::user_tools::api::WriteData;
using fs_testing::user_tools::api::WriteDataMmap;
using fs_testing::user_tools::api::Checkpoint;
using std::string;

#define TEST_FILE_PERMS  ((mode_t) (S_IRWXU | S_IRWXG | S_IRWXO))

namespace fs_testing {
namespace tests {

class Generic321_1: public BaseTestCase {
 public:
  virtual int setup() override {
    return 0;
  }

  virtual int run() override {

	// Create test directory A.
	int res = mkdir(dir_path.c_str(), 0777);
	if (res < 0) {
	  return -1;
	}

	// fsync the directory
	int dir = open(dir_path.c_str(), O_RDONLY, O_DIRECTORY);
	if (dir < 0) {
		return -4;
	}

	if (fsync(dir) < 0) {
		return -5;
	}

	if (Checkpoint() < 0){
      return -5;
    }

	close(dir);
    return 0;
  }

  virtual int check_test(unsigned int last_checkpoint,
      DataTestResult *test_result) override {

	init_paths();

    struct stat stats;
    const int stats_res = stat(dir_path.c_str(), &stats);
    const int errno_stats = errno;

    if (stats_res < 0 && errno_stats == ENOENT && last_checkpoint == 1) {
      test_result->SetError(DataTestResult::kFileMissing);
      test_result->error_description = " : " + dir_path + " is missing";
      return 0;
    }

    // If it is not a directory
    if (!S_ISDIR(stats.st_mode) && last_checkpoint == 1) {
        test_result->SetError(DataTestResult::kFileMetadataCorrupted);
        test_result->error_description = " : " + dir_path + " is not a directory";
        return 0;
    }

    return 0;
  }

 private:
  string dir_path;

  void init_paths() {
	  dir_path = mnt_dir_ + "/" TEST_DIR_A;
  }
};

}  // namespace tests
}  // namespace fs_testing

extern "C" fs_testing::tests::BaseTestCase *test_case_get_instance() {
  return new fs_testing::tests::Generic321_1;
}

extern "C" void test_case_delete_instance(fs_testing::tests::BaseTestCase *tc) {
  delete tc;
}
<|endoftext|>


<commit_before>#include "eprosimartps/CDRMessage.h"

TEST(CDRMessage, constructor)
{
	CDRMessage_t msg;
	ASSERT_EQ(0,msg.pos);
	ASSERT_EQ(0,msg.length);
	ASSERT_EQ(RTPSMESSAGE_MAX_SIZE,msg.max_size);
	ASSERT_TRUE(NULL != msg.buffer);
	ASSERT_EQ(EPROSIMA_ENDIAN,msg.msg_endian);
}

TEST(CDRMessage, constructor_size)
{
	CDRMessage_t msg2(20);
	ASSERT_EQ(0,msg2.pos);
	ASSERT_EQ(0,msg2.length);
	ASSERT_EQ(20,msg2.max_size);
	ASSERT_TRUE(NULL != msg2.buffer);
	ASSERT_EQ(EPROSIMA_ENDIAN,msg2.msg_endian);
}

class CDRMessageTest: public ::testing::Test
{
protected:
	void SetUp()
	{

	}
	CDRMessage_t t_msg;
	uint16_t t_length;
	uint16_t t_pos;
	void saveLengthPos()
	{
		t_length = t_msg.length;
		t_pos = t_msg.pos;
	}
	bool checkLengthIncrement(uint16_t inc)
	{
		if(t_length+inc == t_msg.length)
			return true;
		else
			return false;
	}
	bool checkPosIncrement(uint16_t inc)
	{
		if(t_pos+inc == t_msg.pos)
			return true;
		else
			return false;
	}

};

TEST_F(CDRMessageTest, octet)
{
	octet o = 35;
	saveLengthPos();
	CDRMessage::addOctet(&t_msg,o);
	EXPECT_TRUE(checkLengthIncrement(1));
	EXPECT_TRUE(checkPosIncrement(1));
	ASSERT_EQ(35,t_msg.buffer[t_msg.pos-1]) << "Octet not added correctly";
	t_msg.pos--;
	saveLengthPos();
	CDRMessage::readOctet(&t_msg,&o);
	EXPECT_FALSE(checkLengthIncrement(1));
	EXPECT_TRUE(checkPosIncrement(1));
	ASSERT_EQ(35,o) << "Octet not readed correctly";
}

TEST_F(CDRMessageTest, uint16)
{
	uint16_t n = 645;
	saveLengthPos();
	CDRMessage::addUInt16(&t_msg,n);
	EXPECT_TRUE(checkLengthIncrement(2));
	EXPECT_TRUE(checkPosIncrement(2));
	t_msg.pos-=2;
	CDRMessage::readUInt16(&t_msg,&n);
	ASSERT_EQ(645,n);
}

TEST_F(CDRMessageTest, uint32)
{
	uint32_t n = 123456;
	saveLengthPos();
	CDRMessage::addUInt32(&t_msg,n);
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	CDRMessage::readUInt32(&t_msg,&n);
	ASSERT_EQ(123456,n);
}

TEST_F(CDRMessageTest, int32)
{
	int32_t n = 123456;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addInt32(&t_msg,n));
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	EXPECT_TRUE(CDRMessage::readInt32(&t_msg,&n));
	ASSERT_EQ(123456,n);
	n = -123456;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addInt32(&t_msg,n));
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	EXPECT_TRUE(CDRMessage::readInt32(&t_msg,&n));
	ASSERT_EQ(-123456,n);
}

TEST_F(CDRMessageTest, SequenceNumber)
{
	SequenceNumber_t seq;
	seq.high = 10;
	seq.low = 25;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addSequenceNumber(&t_msg,&seq));
	EXPECT_TRUE(checkLengthIncrement(8));
	EXPECT_TRUE(checkPosIncrement(8));
	t_msg.pos-=8;
	CDRMessage::readSequenceNumber(&t_msg,&n);
	ASSERT_EQ(123456,n);
	n = -123456;
	saveLengthPos();
	CDRMessage::addInt32(&t_msg,n);
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	CDRMessage::readInt32(&t_msg,&n);
	ASSERT_EQ(-123456,n);
}

<commit_msg>CDRMessage SeqNum unittest<commit_after>#include "eprosimartps/CDRMessage.h"

TEST(CDRMessage, constructor)
{
	CDRMessage_t msg;
	ASSERT_EQ(0,msg.pos);
	ASSERT_EQ(0,msg.length);
	ASSERT_EQ(RTPSMESSAGE_MAX_SIZE,msg.max_size);
	ASSERT_TRUE(NULL != msg.buffer);
	ASSERT_EQ(EPROSIMA_ENDIAN,msg.msg_endian);
}

TEST(CDRMessage, constructor_size)
{
	CDRMessage_t msg2(20);
	ASSERT_EQ(0,msg2.pos);
	ASSERT_EQ(0,msg2.length);
	ASSERT_EQ(20,msg2.max_size);
	ASSERT_TRUE(NULL != msg2.buffer);
	ASSERT_EQ(EPROSIMA_ENDIAN,msg2.msg_endian);
}

class CDRMessageTest: public ::testing::Test
{
protected:
	void SetUp()
	{

	}
	CDRMessage_t t_msg;
	uint16_t t_length;
	uint16_t t_pos;
	void saveLengthPos()
	{
		t_length = t_msg.length;
		t_pos = t_msg.pos;
	}
	bool checkLengthIncrement(uint16_t inc)
	{
		if(t_length+inc == t_msg.length)
			return true;
		else
			return false;
	}
	bool checkPosIncrement(uint16_t inc)
	{
		if(t_pos+inc == t_msg.pos)
			return true;
		else
			return false;
	}

};

TEST_F(CDRMessageTest, octet)
{
	octet o = 35;
	saveLengthPos();
	CDRMessage::addOctet(&t_msg,o);
	EXPECT_TRUE(checkLengthIncrement(1));
	EXPECT_TRUE(checkPosIncrement(1));
	ASSERT_EQ(35,t_msg.buffer[t_msg.pos-1]) << "Octet not added correctly";
	t_msg.pos--;
	saveLengthPos();
	CDRMessage::readOctet(&t_msg,&o);
	EXPECT_FALSE(checkLengthIncrement(1));
	EXPECT_TRUE(checkPosIncrement(1));
	ASSERT_EQ(35,o) << "Octet not readed correctly";
}

TEST_F(CDRMessageTest, uint16)
{
	uint16_t n = 645;
	saveLengthPos();
	CDRMessage::addUInt16(&t_msg,n);
	EXPECT_TRUE(checkLengthIncrement(2));
	EXPECT_TRUE(checkPosIncrement(2));
	t_msg.pos-=2;
	CDRMessage::readUInt16(&t_msg,&n);
	ASSERT_EQ(645,n);
}

TEST_F(CDRMessageTest, uint32)
{
	uint32_t n = 123456;
	saveLengthPos();
	CDRMessage::addUInt32(&t_msg,n);
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	CDRMessage::readUInt32(&t_msg,&n);
	ASSERT_EQ(123456,n);
}

TEST_F(CDRMessageTest, int32)
{
	int32_t n = 123456;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addInt32(&t_msg,n));
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	EXPECT_TRUE(CDRMessage::readInt32(&t_msg,&n));
	ASSERT_EQ(123456,n);
	n = -123456;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addInt32(&t_msg,n));
	EXPECT_TRUE(checkLengthIncrement(4));
	EXPECT_TRUE(checkPosIncrement(4));
	t_msg.pos-=4;
	EXPECT_TRUE(CDRMessage::readInt32(&t_msg,&n));
	ASSERT_EQ(-123456,n);
}

TEST_F(CDRMessageTest, SequenceNumber)
{
	SequenceNumber_t seq;
	seq.high = 10;
	seq.low = 25;
	saveLengthPos();
	EXPECT_TRUE(CDRMessage::addSequenceNumber(&t_msg,&seq));
	EXPECT_TRUE(checkLengthIncrement(8));
	EXPECT_TRUE(checkPosIncrement(8));
	t_msg.pos-=8;
	CDRMessage::readSequenceNumber(&t_msg,&seq);
	ASSERT_EQ(10,seq.high);
	ASSERT_EQ(25,seq.low);

}

<|endoftext|>


<commit_before>/*
 * Copyright (C) 2004-2018 ZNC, see the NOTICE file for details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/FileUtils.h>
#include <znc/Server.h>
#include <znc/IRCNetwork.h>
#include <znc/User.h>
#include <znc/ZNCDebug.h>

class CAdminDebugMod : public CModule {
  private:
      CString m_sEnabledBy;

  public:
    MODCONSTRUCTOR(CAdminDebugMod) {
        AddHelpCommand();
        AddCommand("Enable", "", t_d("Enable Debug Mode"),
                   [=](const CString& sLine) { CommandEnable(sLine); });
        AddCommand("Disable", "", t_d("Disable Debug Mode"),
                   [=](const CString& sLine) { CommandDisable(sLine); });
        AddCommand("Status", "", t_d("Show the Debug Mode status"),
                   [=](const CString& sLine) { CommandStatus(sLine); });
    }

    void CommandEnable(const CString& sCommand) {
        if (GetUser()->IsAdmin() == false) {
            PutModule(t_s("Access denied!"));
            return;
        }

        ToggleDebug(true, GetUser()->GetNick());
    }

    void CommandDisable(const CString& sCommand) {
        if (GetUser()->IsAdmin() == false) {
            PutModule(t_s("Access denied!"));
            return;
        }

        ToggleDebug(false, m_sEnabledBy);
    }

    bool ToggleDebug(bool bEnable, CString sEnabledBy) {
        if (!CDebug::StdoutIsTTY()) {
            PutModule(t_s("Failure. We need to be running with a TTY. (is ZNC running with --foreground?)"));
            return false;
        }

        bool bValue = CDebug::Debug();

        if (bEnable == bValue) {
            if (bEnable) {
                PutModule(t_s("Already enabled."));
            } else {
                PutModule(t_s("Already disabled."));
            }
            return false;
        }

        CDebug::SetDebug(bEnable);
        CString sEnabled = CString(bEnable ? "on" : "off");
        CZNC::Get().Broadcast(CString(
            "An administrator has just turned Debug Mode \02" + sEnabled + "\02. It was enabled by \02" + sEnabledBy + "\02."
        ));
        if (bEnable) {
            CZNC::Get().Broadcast(
                CString("Messages, credentials, and other sensitive data may"
                " become exposed to the host during this period.")
            );
            m_sEnabledBy = sEnabledBy;
        } else {
            m_sEnabledBy = "";
        }

        return true;
    }

    void CommandStatus(const CString& sCommand) {
        if (CDebug::Debug()) {
            PutModule(t_s("Debugging mode is \02on\02."));
        } else {
             PutModule(t_s("Debugging mode is \02off\02."));
        }
        PutModule(t_s("Logging to: \02stdout\02."));
    }
};

template <>
void TModInfo<CAdminDebugMod>(CModInfo& Info) {
    Info.SetWikiPage("admindebug");
}

GLOBALMODULEDEFS(CAdminDebugMod, t_s("Enable Debug mode dynamically."))
<commit_msg>admindebug: language/style fixes<commit_after>/*
 * Copyright (C) 2004-2018 ZNC, see the NOTICE file for details.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/FileUtils.h>
#include <znc/Server.h>
#include <znc/IRCNetwork.h>
#include <znc/User.h>
#include <znc/ZNCDebug.h>

class CAdminDebugMod : public CModule {
  private:
      CString m_sEnabledBy;

  public:
    MODCONSTRUCTOR(CAdminDebugMod) {
        AddHelpCommand();
        AddCommand("Enable", "", t_d("Enable Debug Mode"),
                   [=](const CString& sLine) { CommandEnable(sLine); });
        AddCommand("Disable", "", t_d("Disable Debug Mode"),
                   [=](const CString& sLine) { CommandDisable(sLine); });
        AddCommand("Status", "", t_d("Show the Debug Mode status"),
                   [=](const CString& sLine) { CommandStatus(sLine); });
    }

    void CommandEnable(const CString& sCommand) {
        if (!GetUser()->IsAdmin()) {
            PutModule(t_s("Access denied!"));
            return;
        }

        ToggleDebug(true, GetUser()->GetNick());
    }

    void CommandDisable(const CString& sCommand) {
        if (!GetUser()->IsAdmin()) {
            PutModule(t_s("Access denied!"));
            return;
        }

        ToggleDebug(false, m_sEnabledBy);
    }

    bool ToggleDebug(bool bEnable, const CString& sEnabledBy) {
        if (!CDebug::StdoutIsTTY()) {
            PutModule(t_s("Failure. We need to be running with a TTY. (is ZNC running with --foreground?)"));
            return false;
        }

        bool bValue = CDebug::Debug();

        if (bEnable == bValue) {
            if (bEnable) {
                PutModule(t_s("Already enabled."));
            } else {
                PutModule(t_s("Already disabled."));
            }
            return false;
        }

        CDebug::SetDebug(bEnable);
        CString sEnabled = bEnable ? "on" : "off";
        CZNC::Get().Broadcast(
            "An administrator has just turned Debug Mode \02" + sEnabled +
            "\02. It was enabled by \02" + sEnabledBy + "\02.");
        if (bEnable) {
            CZNC::Get().Broadcast(
                "Messages, credentials, and other sensitive data may become "
                "exposed to the host during this period.");
            m_sEnabledBy = sEnabledBy;
        } else {
            m_sEnabledBy = "";
        }

        return true;
    }

    void CommandStatus(const CString& sCommand) {
        if (CDebug::Debug()) {
            PutModule(t_s("Debugging mode is \02on\02."));
        } else {
             PutModule(t_s("Debugging mode is \02off\02."));
        }
        PutModule(t_s("Logging to: \02stdout\02."));
    }
};

template <>
void TModInfo<CAdminDebugMod>(CModInfo& Info) {
    Info.SetWikiPage("admindebug");
}

GLOBALMODULEDEFS(CAdminDebugMod, t_s("Enable Debug mode dynamically."))
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: MTypeConverter.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: fs $ $Date: 2002-05-17 12:08:41 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): Willem van Dorp, Darren Kenny
 *
 *
 ************************************************************************/

#include <MNSInclude.hxx>

#ifndef _CONNECTIVITY_MAB_TYPECONVERTER_HXX_
#include "MTypeConverter.hxx"
#endif
#ifndef _UCBHELPER_CONTENT_HXX
#include <ucbhelper/content.hxx>
#endif

using namespace connectivity::mozab;

// -------------------------------------------------------------------------
void MTypeConverter::ouStringToNsString(::rtl::OUString const &ous, nsString &nss)
{
    // Convert to ::rtl::OString (utf-8 encoding).
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_UTF8);

    const char *cs = os.getStr();
    PRUint32 csLen = os.getLength();

    NS_ConvertUTF8toUCS2 mozString(cs, csLen);
    //const PRUnichar* uniMozString = (const PRUnichar*) mozString;
    nss = mozString; // temp.
}
// -------------------------------------------------------------------------
void MTypeConverter::nsStringToOUString(nsString const &nss, ::rtl::OUString &ous)
{
    // Get clone of buffer.
    PRUnichar *uc = ToNewUnicode(nss);
    sal_Int32 nssLen = nss.Length();

    // TODO check if this is ok.
    ::rtl::OUString _ous(uc, nssLen);
    ous = _ous;

    nsMemory::Free(uc);
}
// -------------------------------------------------------------------------
void MTypeConverter::prUnicharToOUString(PRUnichar const *pru, ::rtl::OUString &ous)
{
    // TODO, specify length.
    ::rtl::OUString _ous(pru);
    ous = _ous;
}
// -------------------------------------------------------------------------
char *MTypeConverter::ouStringToCCharStringUtf8(::rtl::OUString const &ous)
{
    // Convert to ::rtl::OString,
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_UTF8);

    const char *cs = os.getStr();

    return(strdup(cs));
}
// -------------------------------------------------------------------------
char *MTypeConverter::ouStringToCCharStringAscii(::rtl::OUString const &ous)
{
    // Convert ::rtl::OUString to ::rtl::OString,
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_ASCII_US);

    return(strdup(os.getStr()));
}
// -------------------------------------------------------------------------
char *MTypeConverter::nsStringToCCharStringAscii(nsString const &nss)
{
    char cs[1024];
    nss.ToCString(cs, 1024);

    return(strdup(cs));
}
// -------------------------------------------------------------------------
::std::string MTypeConverter::ouStringToStlString(::rtl::OUString const &ous)
{
    // Convert ::rtl::OUString to ::rtl::OString.
    ::rtl::OString os(ous,ous.getLength(),RTL_TEXTENCODING_ASCII_US);
    return( ::std::string(os.getStr()));
}
#if 0
// -------------------------------------------------------------------------
::std::string MTypeConverter::nsStringToStlString(nsString const &nss)
{
    return( ::std::string(nss.GetBuffer()));
}
#endif
// -------------------------------------------------------------------------
<commit_msg>INTEGRATION: CWS ooo19126 (1.4.332); FILE MERGED 2005/09/05 17:24:38 rt 1.4.332.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: MTypeConverter.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 06:30:32 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#include <MNSInclude.hxx>

#ifndef _CONNECTIVITY_MAB_TYPECONVERTER_HXX_
#include "MTypeConverter.hxx"
#endif
#ifndef _UCBHELPER_CONTENT_HXX
#include <ucbhelper/content.hxx>
#endif

using namespace connectivity::mozab;

// -------------------------------------------------------------------------
void MTypeConverter::ouStringToNsString(::rtl::OUString const &ous, nsString &nss)
{
    // Convert to ::rtl::OString (utf-8 encoding).
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_UTF8);

    const char *cs = os.getStr();
    PRUint32 csLen = os.getLength();

    NS_ConvertUTF8toUCS2 mozString(cs, csLen);
    //const PRUnichar* uniMozString = (const PRUnichar*) mozString;
    nss = mozString; // temp.
}
// -------------------------------------------------------------------------
void MTypeConverter::nsStringToOUString(nsString const &nss, ::rtl::OUString &ous)
{
    // Get clone of buffer.
    PRUnichar *uc = ToNewUnicode(nss);
    sal_Int32 nssLen = nss.Length();

    // TODO check if this is ok.
    ::rtl::OUString _ous(uc, nssLen);
    ous = _ous;

    nsMemory::Free(uc);
}
// -------------------------------------------------------------------------
void MTypeConverter::prUnicharToOUString(PRUnichar const *pru, ::rtl::OUString &ous)
{
    // TODO, specify length.
    ::rtl::OUString _ous(pru);
    ous = _ous;
}
// -------------------------------------------------------------------------
char *MTypeConverter::ouStringToCCharStringUtf8(::rtl::OUString const &ous)
{
    // Convert to ::rtl::OString,
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_UTF8);

    const char *cs = os.getStr();

    return(strdup(cs));
}
// -------------------------------------------------------------------------
char *MTypeConverter::ouStringToCCharStringAscii(::rtl::OUString const &ous)
{
    // Convert ::rtl::OUString to ::rtl::OString,
    ::rtl::OString os(ous,ous.getLength(), RTL_TEXTENCODING_ASCII_US);

    return(strdup(os.getStr()));
}
// -------------------------------------------------------------------------
char *MTypeConverter::nsStringToCCharStringAscii(nsString const &nss)
{
    char cs[1024];
    nss.ToCString(cs, 1024);

    return(strdup(cs));
}
// -------------------------------------------------------------------------
::std::string MTypeConverter::ouStringToStlString(::rtl::OUString const &ous)
{
    // Convert ::rtl::OUString to ::rtl::OString.
    ::rtl::OString os(ous,ous.getLength(),RTL_TEXTENCODING_ASCII_US);
    return( ::std::string(os.getStr()));
}
#if 0
// -------------------------------------------------------------------------
::std::string MTypeConverter::nsStringToStlString(nsString const &nss)
{
    return( ::std::string(nss.GetBuffer()));
}
#endif
// -------------------------------------------------------------------------
<|endoftext|>


<commit_before>#ifndef MORDOR_BIN_READER
#define MORDOR_BIN_READER

#include <fstream>
#include <iostream>
using namespace std;

typedef unsigned char BYTE;  // 1 byte
typedef unsigned short WORD; // 2 bytes wide
typedef signed short SWORD;
typedef unsigned long DWORD; // 4 bytes, or long word
typedef float CURRENCY;

void printLoc(ifstream *mdata, int offset, const char *prefix);
BYTE readByte(ifstream *mdata_input);
WORD readWord(ifstream *mdata_input);
WORD readWordByteOrder(ifstream *mdata_input);
SWORD readSWord(ifstream *mdata_input);
DWORD readDWord(ifstream *mdata_input);
int readInt(ifstream *mdata_input);
CURRENCY readCurrency(ifstream *mdata_input);
char* readVBString(ifstream *mdata_input);
char* seekTo(ifstream *mdata_input, int goal);
bool checkAlignment(ifstream *mdata_input);
bool checkByteAlignment(ifstream *mdata_input);

#endif
<commit_msg>currency is a double, not a float.<commit_after>#ifndef MORDOR_BIN_READER
#define MORDOR_BIN_READER

#include <fstream>
#include <iostream>
using namespace std;

typedef unsigned char BYTE;  // 1 byte
typedef unsigned short WORD; // 2 bytes wide
typedef signed short SWORD;
typedef unsigned long DWORD; // 4 bytes, or long word
typedef double CURRENCY;

void printLoc(ifstream *mdata, int offset, const char *prefix);
BYTE readByte(ifstream *mdata_input);
WORD readWord(ifstream *mdata_input);
WORD readWordByteOrder(ifstream *mdata_input);
SWORD readSWord(ifstream *mdata_input);
DWORD readDWord(ifstream *mdata_input);
int readInt(ifstream *mdata_input);
CURRENCY readCurrency(ifstream *mdata_input);
char* readVBString(ifstream *mdata_input);
char* seekTo(ifstream *mdata_input, int goal);
bool checkAlignment(ifstream *mdata_input);
bool checkByteAlignment(ifstream *mdata_input);

#endif
<|endoftext|>


<commit_before><commit_msg>[RArrowDS] Fix compilation error<commit_after><|endoftext|>


<commit_before>// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#include <algorithm>
#include <stdlib.h>
#include "util/check.hh"
#include "util/exception.hh"
#include "util/tokenize_piece.hh"

#include "TargetPhrase.h"
#include "PhraseDictionaryMemory.h"
#include "GenerationDictionary.h"
#include "LM/Base.h"
#include "StaticData.h"
#include "LMList.h"
#include "ScoreComponentCollection.h"
#include "Util.h"
#include "DummyScoreProducers.h"
#include "AlignmentInfoCollection.h"

using namespace std;

namespace Moses
{
TargetPhrase::TargetPhrase( std::string out_string)
  :Phrase(0), m_fullScore(0.0), m_sourcePhrase(0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{

  //ACAT
  const StaticData &staticData = StaticData::Instance();
  CreateFromString(staticData.GetInputFactorOrder(), out_string, staticData.GetFactorDelimiter());
}

TargetPhrase::TargetPhrase()
  :Phrase()
  , m_fullScore(0.0)
  ,m_sourcePhrase()
	, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
	, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}

TargetPhrase::TargetPhrase(const Phrase &phrase)
  : Phrase(phrase)
  , m_fullScore(0.0)
  , m_sourcePhrase()
	, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
	, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}

void TargetPhrase::SetScore(const TranslationSystem* system)
{
  // used when creating translations of unknown words:
  m_fullScore = - system->GetWeightWordPenalty();
}

#ifdef HAVE_PROTOBUF
void TargetPhrase::WriteToRulePB(hgmert::Rule* pb) const
{
  pb->add_trg_words("[X,1]");
  for (size_t pos = 0 ; pos < GetSize() ; pos++)
    pb->add_trg_words(GetWord(pos)[0]->GetString());
}
#endif

void TargetPhrase::SetScore(float score)
{
	//we use an existing score producer to figure out information for score setting (number of scores and weights)
	//TODO: is this a good idea?
    // Assume the default system.
    const TranslationSystem& system =  StaticData::Instance().GetTranslationSystem(TranslationSystem::DEFAULT);
	const ScoreProducer* prod = system.GetPhraseDictionaries()[0];

	vector<float> weights = StaticData::Instance().GetWeights(prod);

	//find out how many items are in the score vector for this producer
	size_t numScores = prod->GetNumScoreComponents();

	//divide up the score among all of the score vectors
	vector <float> scoreVector(numScores,score/numScores);

	//Now we have what we need to call the full SetScore method
	SetScore(prod, scoreVector, ScoreComponentCollection(), weights, system.GetWeightWordPenalty(), system.GetLanguageModels());
}

/**
 * used for setting scores for unknown words with input link features (lattice/conf. nets)
 * \param scoreVector input scores
 */
void TargetPhrase::SetScore(const TranslationSystem* system, const Scores &scoreVector)
{
	//we use an existing score producer to figure out information for score setting (number of scores and weights)

    const ScoreProducer* prod = system->GetPhraseDictionaries()[0];

	vector<float> weights = StaticData::Instance().GetWeights(prod);

	//expand the input weight vector
	CHECK(scoreVector.size() <= prod->GetNumScoreComponents());
	Scores sizedScoreVector = scoreVector;
	sizedScoreVector.resize(prod->GetNumScoreComponents(),0.0f);

	SetScore(prod,sizedScoreVector, ScoreComponentCollection(),weights,system->GetWeightWordPenalty(),system->GetLanguageModels());
}

void TargetPhrase::SetScore(const ScoreProducer* translationScoreProducer,
                            const Scores &scoreVector,
                            const ScoreComponentCollection &sparseScoreVector,
                            const vector<float> &weightT,
                            float weightWP, const LMList &languageModels)
{
  CHECK(weightT.size() == scoreVector.size());
  // calc average score if non-best

  float transScore = std::inner_product(scoreVector.begin(), scoreVector.end(), weightT.begin(), 0.0f);
  m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);
  m_scoreBreakdown.PlusEquals(sparseScoreVector);

  // Replicated from TranslationOptions.cpp
  float totalNgramScore  = 0;
  float totalFullScore   = 0;
  float totalOOVScore    = 0;

  LMList::const_iterator lmIter;
  for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
    const LanguageModel &lm = **lmIter;

    if (lm.Useable(*this)) {
      // contains factors used by this LM
      const float weightLM = lm.GetWeight();
      const float oovWeightLM = lm.GetOOVWeight();
      float fullScore, nGramScore;
      size_t oovCount;

      lm.CalcScore(*this, fullScore, nGramScore, oovCount);

      if (StaticData::Instance().GetLMEnableOOVFeature()) {
        vector<float> scores(2);
        scores[0] = nGramScore;
        scores[1] = oovCount;
        m_scoreBreakdown.Assign(&lm, scores);
        totalOOVScore += oovCount * oovWeightLM;
      } else {
        m_scoreBreakdown.Assign(&lm, nGramScore);
      }

      // total LM score so far
      totalNgramScore  += nGramScore * weightLM;
      totalFullScore   += fullScore * weightLM;

    }
  }

  m_fullScore = transScore + totalFullScore + totalOOVScore
                - (this->GetSize() * weightWP);	 // word penalty
}

void TargetPhrase::SetScoreChart(const ScoreProducer* translationScoreProducer,
                                 const Scores &scoreVector
                                 ,const vector<float> &weightT
                                 ,const LMList &languageModels
                                 ,const WordPenaltyProducer* wpProducer)
{
  CHECK(weightT.size() == scoreVector.size());

  // calc average score if non-best
  m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);

  // Replicated from TranslationOptions.cpp
  float totalNgramScore  = 0;
  float totalFullScore   = 0;
  float totalOOVScore    = 0;

  LMList::const_iterator lmIter;
  for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
    const LanguageModel &lm = **lmIter;

    if (lm.Useable(*this)) {
      // contains factors used by this LM
      const float weightLM = lm.GetWeight();
      const float oovWeightLM = lm.GetOOVWeight();
      float fullScore, nGramScore;
      size_t oovCount;

      lm.CalcScore(*this, fullScore, nGramScore, oovCount);
      fullScore = UntransformLMScore(fullScore);
      nGramScore = UntransformLMScore(nGramScore);

      if (StaticData::Instance().GetLMEnableOOVFeature()) {
        vector<float> scores(2);
        scores[0] = nGramScore;
        scores[1] = oovCount;
        m_scoreBreakdown.Assign(&lm, scores);
        totalOOVScore += oovCount * oovWeightLM;
      } else {
        m_scoreBreakdown.Assign(&lm, nGramScore);
      }

      // total LM score so far
      totalNgramScore  += nGramScore * weightLM;
      totalFullScore   += fullScore * weightLM;
    }
  }

  // word penalty
  size_t wordCount = GetNumTerminals();
  m_scoreBreakdown.Assign(wpProducer, - (float) wordCount * 0.434294482); // TODO log -> ln ??

  m_fullScore = m_scoreBreakdown.GetWeightedScore() - totalNgramScore + totalFullScore + totalOOVScore;
}

void TargetPhrase::SetScore(const ScoreProducer* producer, const Scores &scoreVector)
{
  // used when creating translations of unknown words (chart decoding)
  m_scoreBreakdown.Assign(producer, scoreVector);
  m_fullScore = m_scoreBreakdown.GetWeightedScore();
}

void TargetPhrase::SetWeights(const ScoreProducer* translationScoreProducer, const vector<float> &weightT)
{
  // calling this function in case of confusion net input is undefined
  CHECK(StaticData::Instance().GetInputType()==SentenceInput);

  /* one way to fix this, you have to make sure the weightT contains (in
     addition to the usual phrase translation scaling factors) the input
     weight factor as last element
  */
}

void TargetPhrase::ResetScore()
{
  m_fullScore = 0;
  m_scoreBreakdown.ZeroAll();
}

TargetPhrase *TargetPhrase::MergeNext(const TargetPhrase &inputPhrase) const
{
  if (! IsCompatible(inputPhrase)) {
    return NULL;
  }

  // ok, merge
  TargetPhrase *clone				= new TargetPhrase(*this);
  clone->m_sourcePhrase = m_sourcePhrase;
  int currWord = 0;
  const size_t len = GetSize();
  for (size_t currPos = 0 ; currPos < len ; currPos++) {
    const Word &inputWord	= inputPhrase.GetWord(currPos);
    Word &cloneWord = clone->GetWord(currPos);
    cloneWord.Merge(inputWord);

    currWord++;
  }

  return clone;
}

void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString)
{
	AlignmentInfo::CollType alignTerm, alignNonTerm;
  for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
    util::TokenIter<util::SingleCharacter, false> dash(*token, util::SingleCharacter('-'));

    char *endptr;
    size_t sourcePos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    ++dash;
    size_t targetPos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    UTIL_THROW_IF(++dash, util::Exception, "Extra gunk in alignment " << *token);

    if (GetWord(targetPos).IsNonTerminal()) {
    	alignNonTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
    }
  	else {
  		alignTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
  	}
  }
  SetAlignTerm(alignTerm);
  SetAlignNonTerm(alignNonTerm);

}

void TargetPhrase::SetAlignTerm(const AlignmentInfo::CollType &coll)
{
	const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
	m_alignTerm = alignmentInfo;

}

void TargetPhrase::SetAlignNonTerm(const AlignmentInfo::CollType &coll)
{
	const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
	m_alignNonTerm = alignmentInfo;
}

TO_STRING_BODY(TargetPhrase);

std::ostream& operator<<(std::ostream& os, const TargetPhrase& tp)
{
  os << static_cast<const Phrase&>(tp) << ":" << tp.GetAlignNonTerm();
  os << ": c=" << tp.m_fullScore;

  return os;
}

}

<commit_msg>added alignment output to n-best list<commit_after>// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#include <algorithm>
#include <stdlib.h>
#include "util/check.hh"
#include "util/exception.hh"
#include "util/tokenize_piece.hh"

#include "TargetPhrase.h"
#include "PhraseDictionaryMemory.h"
#include "GenerationDictionary.h"
#include "LM/Base.h"
#include "StaticData.h"
#include "LMList.h"
#include "ScoreComponentCollection.h"
#include "Util.h"
#include "DummyScoreProducers.h"
#include "AlignmentInfoCollection.h"

using namespace std;

namespace Moses
{
TargetPhrase::TargetPhrase( std::string out_string)
  :Phrase(0), m_fullScore(0.0), m_sourcePhrase(0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{

  //ACAT
  const StaticData &staticData = StaticData::Instance();
  CreateFromString(staticData.GetInputFactorOrder(), out_string, staticData.GetFactorDelimiter());
}

TargetPhrase::TargetPhrase()
  :Phrase()
  , m_fullScore(0.0)
  ,m_sourcePhrase()
	, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
	, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}

TargetPhrase::TargetPhrase(const Phrase &phrase)
  : Phrase(phrase)
  , m_fullScore(0.0)
  , m_sourcePhrase()
	, m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
	, m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}

void TargetPhrase::SetScore(const TranslationSystem* system)
{
  // used when creating translations of unknown words:
  m_fullScore = - system->GetWeightWordPenalty();
}

#ifdef HAVE_PROTOBUF
void TargetPhrase::WriteToRulePB(hgmert::Rule* pb) const
{
  pb->add_trg_words("[X,1]");
  for (size_t pos = 0 ; pos < GetSize() ; pos++)
    pb->add_trg_words(GetWord(pos)[0]->GetString());
}
#endif

void TargetPhrase::SetScore(float score)
{
	//we use an existing score producer to figure out information for score setting (number of scores and weights)
	//TODO: is this a good idea?
    // Assume the default system.
    const TranslationSystem& system =  StaticData::Instance().GetTranslationSystem(TranslationSystem::DEFAULT);
	const ScoreProducer* prod = system.GetPhraseDictionaries()[0];

	vector<float> weights = StaticData::Instance().GetWeights(prod);

	//find out how many items are in the score vector for this producer
	size_t numScores = prod->GetNumScoreComponents();

	//divide up the score among all of the score vectors
	vector <float> scoreVector(numScores,score/numScores);

	//Now we have what we need to call the full SetScore method
	SetScore(prod, scoreVector, ScoreComponentCollection(), weights, system.GetWeightWordPenalty(), system.GetLanguageModels());
}

/**
 * used for setting scores for unknown words with input link features (lattice/conf. nets)
 * \param scoreVector input scores
 */
void TargetPhrase::SetScore(const TranslationSystem* system, const Scores &scoreVector)
{
	//we use an existing score producer to figure out information for score setting (number of scores and weights)

    const ScoreProducer* prod = system->GetPhraseDictionaries()[0];

	vector<float> weights = StaticData::Instance().GetWeights(prod);

	//expand the input weight vector
	CHECK(scoreVector.size() <= prod->GetNumScoreComponents());
	Scores sizedScoreVector = scoreVector;
	sizedScoreVector.resize(prod->GetNumScoreComponents(),0.0f);

	SetScore(prod,sizedScoreVector, ScoreComponentCollection(),weights,system->GetWeightWordPenalty(),system->GetLanguageModels());
}

void TargetPhrase::SetScore(const ScoreProducer* translationScoreProducer,
                            const Scores &scoreVector,
                            const ScoreComponentCollection &sparseScoreVector,
                            const vector<float> &weightT,
                            float weightWP, const LMList &languageModels)
{
  CHECK(weightT.size() == scoreVector.size());
  // calc average score if non-best

  float transScore = std::inner_product(scoreVector.begin(), scoreVector.end(), weightT.begin(), 0.0f);
  m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);
  m_scoreBreakdown.PlusEquals(sparseScoreVector);

  // Replicated from TranslationOptions.cpp
  float totalNgramScore  = 0;
  float totalFullScore   = 0;
  float totalOOVScore    = 0;

  LMList::const_iterator lmIter;
  for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
    const LanguageModel &lm = **lmIter;

    if (lm.Useable(*this)) {
      // contains factors used by this LM
      const float weightLM = lm.GetWeight();
      const float oovWeightLM = lm.GetOOVWeight();
      float fullScore, nGramScore;
      size_t oovCount;

      lm.CalcScore(*this, fullScore, nGramScore, oovCount);

      if (StaticData::Instance().GetLMEnableOOVFeature()) {
        vector<float> scores(2);
        scores[0] = nGramScore;
        scores[1] = oovCount;
        m_scoreBreakdown.Assign(&lm, scores);
        totalOOVScore += oovCount * oovWeightLM;
      } else {
        m_scoreBreakdown.Assign(&lm, nGramScore);
      }

      // total LM score so far
      totalNgramScore  += nGramScore * weightLM;
      totalFullScore   += fullScore * weightLM;

    }
  }

  m_fullScore = transScore + totalFullScore + totalOOVScore
                - (this->GetSize() * weightWP);	 // word penalty
}

void TargetPhrase::SetScoreChart(const ScoreProducer* translationScoreProducer,
                                 const Scores &scoreVector
                                 ,const vector<float> &weightT
                                 ,const LMList &languageModels
                                 ,const WordPenaltyProducer* wpProducer)
{
  CHECK(weightT.size() == scoreVector.size());

  // calc average score if non-best
  m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);

  // Replicated from TranslationOptions.cpp
  float totalNgramScore  = 0;
  float totalFullScore   = 0;
  float totalOOVScore    = 0;

  LMList::const_iterator lmIter;
  for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
    const LanguageModel &lm = **lmIter;

    if (lm.Useable(*this)) {
      // contains factors used by this LM
      const float weightLM = lm.GetWeight();
      const float oovWeightLM = lm.GetOOVWeight();
      float fullScore, nGramScore;
      size_t oovCount;

      lm.CalcScore(*this, fullScore, nGramScore, oovCount);
      fullScore = UntransformLMScore(fullScore);
      nGramScore = UntransformLMScore(nGramScore);

      if (StaticData::Instance().GetLMEnableOOVFeature()) {
        vector<float> scores(2);
        scores[0] = nGramScore;
        scores[1] = oovCount;
        m_scoreBreakdown.Assign(&lm, scores);
        totalOOVScore += oovCount * oovWeightLM;
      } else {
        m_scoreBreakdown.Assign(&lm, nGramScore);
      }

      // total LM score so far
      totalNgramScore  += nGramScore * weightLM;
      totalFullScore   += fullScore * weightLM;
    }
  }

  // word penalty
  size_t wordCount = GetNumTerminals();
  m_scoreBreakdown.Assign(wpProducer, - (float) wordCount * 0.434294482); // TODO log -> ln ??

  m_fullScore = m_scoreBreakdown.GetWeightedScore() - totalNgramScore + totalFullScore + totalOOVScore;
}

void TargetPhrase::SetScore(const ScoreProducer* producer, const Scores &scoreVector)
{
  // used when creating translations of unknown words (chart decoding)
  m_scoreBreakdown.Assign(producer, scoreVector);
  m_fullScore = m_scoreBreakdown.GetWeightedScore();
}

void TargetPhrase::SetWeights(const ScoreProducer* translationScoreProducer, const vector<float> &weightT)
{
  // calling this function in case of confusion net input is undefined
  CHECK(StaticData::Instance().GetInputType()==SentenceInput);

  /* one way to fix this, you have to make sure the weightT contains (in
     addition to the usual phrase translation scaling factors) the input
     weight factor as last element
  */
}

void TargetPhrase::ResetScore()
{
  m_fullScore = 0;
  m_scoreBreakdown.ZeroAll();
}

TargetPhrase *TargetPhrase::MergeNext(const TargetPhrase &inputPhrase) const
{
  if (! IsCompatible(inputPhrase)) {
    return NULL;
  }

  // ok, merge
  TargetPhrase *clone				= new TargetPhrase(*this);
  clone->m_sourcePhrase = m_sourcePhrase;
  int currWord = 0;
  const size_t len = GetSize();
  for (size_t currPos = 0 ; currPos < len ; currPos++) {
    const Word &inputWord	= inputPhrase.GetWord(currPos);
    Word &cloneWord = clone->GetWord(currPos);
    cloneWord.Merge(inputWord);

    currWord++;
  }

  return clone;
}

void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString)
{
	AlignmentInfo::CollType alignTerm, alignNonTerm;
  for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
    util::TokenIter<util::SingleCharacter, false> dash(*token, util::SingleCharacter('-'));

    char *endptr;
    size_t sourcePos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    ++dash;
    size_t targetPos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    UTIL_THROW_IF(++dash, util::Exception, "Extra gunk in alignment " << *token);

    if (GetWord(targetPos).IsNonTerminal()) {
    	alignNonTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
    }
  	else {
  		alignTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
  	}
  }
  SetAlignTerm(alignTerm);
  SetAlignNonTerm(alignNonTerm);

}

void TargetPhrase::SetAlignTerm(const AlignmentInfo::CollType &coll)
{
	const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
	m_alignTerm = alignmentInfo;

}

void TargetPhrase::SetAlignNonTerm(const AlignmentInfo::CollType &coll)
{
	const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
	m_alignNonTerm = alignmentInfo;
}

TO_STRING_BODY(TargetPhrase);

std::ostream& operator<<(std::ostream& os, const TargetPhrase& tp)
{
  os << static_cast<const Phrase&>(tp) << ":" << tp.GetAlignNonTerm();
  os << ": c=" << tp.m_fullScore;

  return os;
}

}

<|endoftext|>