Introduce platform services interface
This commit is contained in:
100
tests/platform_api/platform_services_tests.cpp
Normal file
100
tests/platform_api/platform_services_tests.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "platform_api/platform_services.h"
|
||||
#include "test_harness.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
class FakePlatformServices final : public pp::platform::PlatformServices {
|
||||
public:
|
||||
explicit FakePlatformServices(std::string clipboard_value)
|
||||
: clipboard_value_(std::move(clipboard_value))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string clipboard_text() override
|
||||
{
|
||||
++clipboard_reads;
|
||||
return clipboard_value_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool set_clipboard_text(std::string_view text) override
|
||||
{
|
||||
++clipboard_writes;
|
||||
clipboard_value_.assign(text);
|
||||
return true;
|
||||
}
|
||||
|
||||
void set_cursor_visible(bool visible) override
|
||||
{
|
||||
++cursor_updates;
|
||||
cursor_visible = visible;
|
||||
}
|
||||
|
||||
void set_virtual_keyboard_visible(bool visible) override
|
||||
{
|
||||
++keyboard_updates;
|
||||
keyboard_visible = visible;
|
||||
}
|
||||
|
||||
int clipboard_reads = 0;
|
||||
int clipboard_writes = 0;
|
||||
int cursor_updates = 0;
|
||||
int keyboard_updates = 0;
|
||||
bool cursor_visible = false;
|
||||
bool keyboard_visible = false;
|
||||
|
||||
private:
|
||||
std::string clipboard_value_;
|
||||
};
|
||||
|
||||
void platform_services_dispatch_clipboard_reads_and_writes(pp::tests::Harness& harness)
|
||||
{
|
||||
FakePlatformServices fake("#112233");
|
||||
pp::platform::PlatformServices& services = fake;
|
||||
|
||||
PP_EXPECT(harness, services.clipboard_text() == "#112233");
|
||||
PP_EXPECT(harness, services.set_clipboard_text("#ff00aa"));
|
||||
PP_EXPECT(harness, services.clipboard_text() == "#ff00aa");
|
||||
PP_EXPECT(harness, fake.clipboard_reads == 2);
|
||||
PP_EXPECT(harness, fake.clipboard_writes == 1);
|
||||
}
|
||||
|
||||
void platform_services_preserve_empty_clipboard_writes(pp::tests::Harness& harness)
|
||||
{
|
||||
FakePlatformServices fake("initial");
|
||||
pp::platform::PlatformServices& services = fake;
|
||||
|
||||
PP_EXPECT(harness, services.set_clipboard_text(""));
|
||||
PP_EXPECT(harness, services.clipboard_text().empty());
|
||||
PP_EXPECT(harness, fake.clipboard_writes == 1);
|
||||
}
|
||||
|
||||
void platform_services_dispatch_visibility_updates(pp::tests::Harness& harness)
|
||||
{
|
||||
FakePlatformServices fake("unused");
|
||||
pp::platform::PlatformServices& services = fake;
|
||||
|
||||
services.set_cursor_visible(true);
|
||||
services.set_virtual_keyboard_visible(true);
|
||||
services.set_cursor_visible(false);
|
||||
services.set_virtual_keyboard_visible(false);
|
||||
|
||||
PP_EXPECT(harness, fake.cursor_updates == 2);
|
||||
PP_EXPECT(harness, fake.keyboard_updates == 2);
|
||||
PP_EXPECT(harness, !fake.cursor_visible);
|
||||
PP_EXPECT(harness, !fake.keyboard_visible);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pp::tests::Harness harness;
|
||||
harness.run("platform services dispatch clipboard reads and writes", platform_services_dispatch_clipboard_reads_and_writes);
|
||||
harness.run("platform services preserve empty clipboard writes", platform_services_preserve_empty_clipboard_writes);
|
||||
harness.run("platform services dispatch visibility updates", platform_services_dispatch_visibility_updates);
|
||||
return harness.finish();
|
||||
}
|
||||
Reference in New Issue
Block a user