|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2025 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include <yup_core/yup_core.h> |
| 25 | + |
| 26 | +using namespace yup; |
| 27 | + |
| 28 | +class ProcessTests : public ::testing::Test |
| 29 | +{ |
| 30 | +protected: |
| 31 | + void SetUp() override |
| 32 | + { |
| 33 | + testFile = File::getSpecialLocation (File::tempDirectory) |
| 34 | + .getChildFile ("YUP_ProcessTests_" + String::toHexString (Random::getSystemRandom().nextInt())) |
| 35 | + .getChildFile ("test_document.txt"); |
| 36 | + |
| 37 | + testFile.getParentDirectory().createDirectory(); |
| 38 | + testFile.replaceWithText ("Test content for Process::openDocument"); |
| 39 | + } |
| 40 | + |
| 41 | + void TearDown() override |
| 42 | + { |
| 43 | + testFile.getParentDirectory().deleteRecursively(); |
| 44 | + } |
| 45 | + |
| 46 | + File testFile; |
| 47 | +}; |
| 48 | + |
| 49 | +TEST_F (ProcessTests, OpenDocumentWithFileName) |
| 50 | +{ |
| 51 | + // Test Process::openDocument() with a file name |
| 52 | + // This attempts to open the file with the default application |
| 53 | + // It may fail if there's no default application or if running in CI |
| 54 | + |
| 55 | + bool result = Process::openDocument (testFile.getFullPathName(), ""); |
| 56 | + |
| 57 | + // We don't assert the result because: |
| 58 | + // 1. It may fail in CI environments |
| 59 | + // 2. It requires a default application to be registered |
| 60 | + // 3. It's platform-dependent |
| 61 | + // Just verify it doesn't crash |
| 62 | + (void) result; |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F (ProcessTests, OpenDocumentWithUrl) |
| 66 | +{ |
| 67 | + // Test opening a URL (this should be safer than opening a file) |
| 68 | + // Most systems have a default browser |
| 69 | + |
| 70 | + // Use a safe, non-intrusive URL |
| 71 | + String testUrl = "about:blank"; |
| 72 | + |
| 73 | + bool result = Process::openDocument (testUrl, ""); |
| 74 | + |
| 75 | + // Again, don't assert the result for the same reasons as above |
| 76 | + (void) result; |
| 77 | +} |
| 78 | + |
| 79 | +TEST_F (ProcessTests, OpenDocumentWithParameters) |
| 80 | +{ |
| 81 | + // Test Process::openDocument() with parameters |
| 82 | + bool result = Process::openDocument (testFile.getFullPathName(), "--test-param"); |
| 83 | + |
| 84 | + // Don't assert success, just verify it doesn't crash |
| 85 | + (void) result; |
| 86 | +} |
| 87 | + |
| 88 | +TEST_F (ProcessTests, OpenDocumentWithEnvironment) |
| 89 | +{ |
| 90 | + // Test Process::openDocument() with custom environment variables |
| 91 | + StringPairArray environment; |
| 92 | + environment.set ("TEST_VAR", "test_value"); |
| 93 | + |
| 94 | + bool result = Process::openDocument (testFile.getFullPathName(), "", environment); |
| 95 | + |
| 96 | + // Don't assert success, just verify it doesn't crash |
| 97 | + (void) result; |
| 98 | +} |
| 99 | + |
| 100 | +TEST_F (ProcessTests, OpenDocumentWithEmptyPath) |
| 101 | +{ |
| 102 | + // Test with empty path (should fail gracefully) |
| 103 | + bool result = Process::openDocument ("", ""); |
| 104 | + |
| 105 | + EXPECT_FALSE (result); // Empty path should fail |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F (ProcessTests, OpenDocumentWithNonExistentFile) |
| 109 | +{ |
| 110 | + // Test with a file that doesn't exist |
| 111 | + File nonExistent = File::getSpecialLocation (File::tempDirectory) |
| 112 | + .getChildFile ("this_file_does_not_exist_12345.xyz"); |
| 113 | + |
| 114 | + bool result = Process::openDocument (nonExistent.getFullPathName(), ""); |
| 115 | + |
| 116 | + // Most systems will fail to open a non-existent file |
| 117 | + // but we don't assert because behavior is platform-dependent |
| 118 | + (void) result; |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F (ProcessTests, OpenDocumentWithSpecialCharacters) |
| 122 | +{ |
| 123 | + // Create a file with special characters in the name |
| 124 | + File specialFile = testFile.getParentDirectory().getChildFile ("test file with spaces & special.txt"); |
| 125 | + specialFile.replaceWithText ("Test content"); |
| 126 | + |
| 127 | + bool result = Process::openDocument (specialFile.getFullPathName(), ""); |
| 128 | + |
| 129 | + // Clean up |
| 130 | + specialFile.deleteFile(); |
| 131 | + |
| 132 | + // Don't assert success due to platform differences |
| 133 | + (void) result; |
| 134 | +} |
0 commit comments