1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/app_mode/kiosk_session_plugin_handler.h"
#include "chrome/browser/chromeos/app_mode/kiosk_session_plugin_handler_delegate.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::WebContents;
using content::WebContentsObserver;
namespace chromeos {
namespace {
constexpr char kValidPluginPath[] = "/path/to/valid_plugin";
constexpr char kInvalidPluginPath[] = "/path/to/invalid_plugin";
constexpr char kProcessId = 2;
constexpr int kPluginChildId = 2;
} // namespace
class TestKioskSessionPluginHandlerDelegate
: public KioskSessionPluginHandlerDelegate {
public:
TestKioskSessionPluginHandlerDelegate() = default;
~TestKioskSessionPluginHandlerDelegate() override = default;
bool ShouldHandlePlugin(const base::FilePath& plugin_path) const override {
return plugin_path.AsUTF8Unsafe() == kValidPluginPath;
}
void OnPluginCrashed(const base::FilePath& plugin_path) override {
has_crashed_ = true;
}
void OnPluginHung(const std::set<int>& hung_plugins) override {}
bool has_crashed() const { return has_crashed_; }
private:
bool has_crashed_ = false;
};
class KioskSessionPluginHandlerTest : public testing::Test {
public:
KioskSessionPluginHandlerTest() = default;
~KioskSessionPluginHandlerTest() override = default;
void SetUp() override {
delegate_ = std::make_unique<TestKioskSessionPluginHandlerDelegate>();
handler_ = std::make_unique<KioskSessionPluginHandler>(delegate_.get());
}
TestKioskSessionPluginHandlerDelegate* delegate() const {
return delegate_.get();
}
KioskSessionPluginHandler* handler() const { return handler_.get(); }
private:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestKioskSessionPluginHandlerDelegate> delegate_;
std::unique_ptr<KioskSessionPluginHandler> handler_;
};
TEST_F(KioskSessionPluginHandlerTest, ObserveAndDestroyWebContents) {
// At the beginning, there is no watcher.
EXPECT_EQ(handler()->GetWatchersForTesting().size(), 0U);
// The number of watchers increases after a new WebContents instance is
// observed.
TestingProfile profile1;
std::unique_ptr<WebContents> contents1 =
WebContents::Create(WebContents::CreateParams(&profile1));
handler()->Observe(contents1.get());
EXPECT_EQ(handler()->GetWatchersForTesting().size(), 1U);
// The number of watchers increases again after another new WebContents
// instance is observed.
TestingProfile profile2;
std::unique_ptr<WebContents> contents2 =
WebContents::Create(WebContents::CreateParams(&profile2));
handler()->Observe(contents2.get());
std::vector<KioskSessionPluginHandler::Observer*> watchers =
handler()->GetWatchersForTesting();
EXPECT_EQ(watchers.size(), 2U);
// The number of watchers returns to zero after each WebContents instance is
// destroyed.
for (WebContentsObserver* observer : watchers) {
observer->WebContentsDestroyed();
}
EXPECT_EQ(handler()->GetWatchersForTesting().size(), 0U);
}
TEST_F(KioskSessionPluginHandlerTest, PluginCrashed) {
TestingProfile profile;
std::unique_ptr<WebContents> contents =
WebContents::Create(WebContents::CreateParams(&profile));
handler()->Observe(contents.get());
WebContentsObserver* watcher = handler()->GetWatchersForTesting().front();
// At the beginning, no crash is notified to the delegate.
EXPECT_FALSE(delegate()->has_crashed());
// No crash is notified if the `plugin_path` is invalid.
watcher->PluginCrashed(base::FilePath(kInvalidPluginPath),
base::ProcessId(kProcessId));
EXPECT_FALSE(delegate()->has_crashed());
// Crash is notified if the `plugin_path` is valid.
watcher->PluginCrashed(base::FilePath(kValidPluginPath),
base::ProcessId(kProcessId));
EXPECT_TRUE(delegate()->has_crashed());
}
TEST_F(KioskSessionPluginHandlerTest, PluginHungStatusChanged) {
TestingProfile profile;
std::unique_ptr<WebContents> contents =
WebContents::Create(WebContents::CreateParams(&profile));
handler()->Observe(contents.get());
KioskSessionPluginHandler::Observer* observer =
handler()->GetWatchersForTesting().front();
WebContentsObserver* watcher = observer;
// At the beginning, there is no hung plugin.
EXPECT_EQ(observer->GetHungPluginsForTesting().size(), 0U);
// The hung plugin is not stored if the `plugin_path` is invalid.
watcher->PluginHungStatusChanged(kPluginChildId,
base::FilePath(kInvalidPluginPath), true);
EXPECT_EQ(observer->GetHungPluginsForTesting().size(), 0U);
// The hung plugin is not stored if the `is_hung` is false.
watcher->PluginHungStatusChanged(kPluginChildId,
base::FilePath(kValidPluginPath), false);
EXPECT_EQ(observer->GetHungPluginsForTesting().size(), 0U);
// The hung plugin is store.
watcher->PluginHungStatusChanged(kPluginChildId,
base::FilePath(kValidPluginPath), true);
EXPECT_EQ(observer->GetHungPluginsForTesting().size(), 1U);
}
} // namespace chromeos
|