using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Xml.Linq; using Everything2Everything.App.Views; using Everything2Everything.Core; using Everything2Everything.Core.Ads; using Xunit; namespace Everything2Everything.Tests; public class SettingsWindowAdMobTests { private static readonly string SolutionRoot = FindSolutionRoot(); private static readonly string SettingsXamlPath = Path.Combine(SolutionRoot, "src", "Everything2Everything.App", "Views", "SettingsWindow.xaml"); private static string FindSolutionRoot() { var dir = Directory.GetCurrentDirectory(); while (dir != null && !File.Exists(Path.Combine(dir, "Everything2Everything.slnx"))) { dir = Directory.GetParent(dir)?.FullName; } return dir ?? throw new DirectoryNotFoundException("솔루션 루트를 찾을 수 없습니다."); } private sealed class FakeSettingsStore : ISettingsStore { private readonly Dictionary _d = new(); public string? Get(string key) => _d.TryGetValue(key, out var v) ? v : null; public void Set(string key, string value) => _d[key] = value; public void Remove(string key) => _d.Remove(key); public bool Contains(string key) => _d.ContainsKey(key); } private static readonly object AppInitLock = new(); private static void RunOnSta(Action action) { Exception? ex = null; var thread = new Thread(() => { try { lock (AppInitLock) { if (Application.Current == null) { try { _ = new Application(); } catch (InvalidOperationException) { } } } action(); } catch (Exception e) { ex = e; } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); if (ex != null) { throw new AggregateException("STA 스레드 실행 중 예외 발생", ex); } } [Fact] public void SettingsWindowXaml_ContainsAdMobConfigurationCard() { Assert.True(File.Exists(SettingsXamlPath), "SettingsWindow.xaml 이 존재해야 합니다."); var doc = XDocument.Parse(File.ReadAllText(SettingsXamlPath)); var elementNames = doc.Descendants() .Select(e => e.Attribute(XName.Get("Name", "http://schemas.microsoft.com/winfx/2006/xaml"))?.Value) .Where(n => !string.IsNullOrEmpty(n)) .ToHashSet(); Assert.Contains("AdMobPublisherIdBox", elementNames); Assert.Contains("AdMobAppIdBox", elementNames); Assert.Contains("AdMobBannerIdBox", elementNames); Assert.Contains("AdMobInterstitialIdBox", elementNames); Assert.Contains("AdMobTestAdsCheck", elementNames); Assert.Contains("ExportAppAdsTxtBtn", elementNames); Assert.Contains("AdMobStatusDot", elementNames); Assert.Contains("AdMobStatusText", elementNames); } [Fact] public void SettingsWindowXaml_AdMobControls_ComplyWithDesignAudit() { var doc = XDocument.Parse(File.ReadAllText(SettingsXamlPath)); var rawEmojis = new[] { "⚙", "📁", "🗑", "✖", "🔧", "✨", "🚀", "⚡" }; var violations = new List(); // 1. 유니코드 이모지 금지 var buttons = doc.Descendants().Where(e => e.Name.LocalName is "Button" or "ToggleButton"); foreach (var btn in buttons) { var content = btn.Attribute("Content")?.Value; if (!string.IsNullOrEmpty(content) && rawEmojis.Any(emoji => content.Contains(emoji))) { violations.Add($"버튼에 이모지 사용 감지: {content}"); } } // 2. 가로 StackPanel 내 아이콘/텍스트 VerticalAlignment="Center" var stackPanels = doc.Descendants().Where(e => e.Name.LocalName == "StackPanel" && e.Attribute("Orientation")?.Value == "Horizontal"); foreach (var sp in stackPanels) { foreach (var child in sp.Elements()) { var va = child.Attribute("VerticalAlignment")?.Value; if (va != "Center") { violations.Add($"가로 StackPanel의 자식 <{child.Name.LocalName}>에 VerticalAlignment=\"Center\" 누락"); } } } Assert.True(violations.Count == 0, "SettingsWindow 디자인 감사 위반이 발견되었습니다:\n" + string.Join("\n", violations)); } [Fact] public void SettingsWindow_LoadsAndSavesAdMobConfig() { RunOnSta(() => { var settings = new FakeSettingsStore(); settings.Set("admob.publisherid", "pub-1112223334445556"); settings.Set("admob.appid", "ca-app-pub-1112223334445556~1234567890"); settings.Set("admob.bannerid", "ca-app-pub-1112223334445556/9876543210"); settings.Set("admob.interstitialid", "ca-app-pub-1112223334445556/5555555555"); settings.Set("admob.usetestads", "false"); var adService = new AdService(); var win = new SettingsWindow(settings, adService); var pubBox = (TextBox)win.FindName("AdMobPublisherIdBox"); var appBox = (TextBox)win.FindName("AdMobAppIdBox"); var bannerBox = (TextBox)win.FindName("AdMobBannerIdBox"); var testCheck = (CheckBox)win.FindName("AdMobTestAdsCheck"); Assert.Equal("pub-1112223334445556", pubBox.Text); Assert.Equal("ca-app-pub-1112223334445556~1234567890", appBox.Text); Assert.Equal("ca-app-pub-1112223334445556/9876543210", bannerBox.Text); Assert.False(testCheck.IsChecked); // 수정 후 저장 시뮬레이션 pubBox.Text = "pub-9999888877776666"; testCheck.IsChecked = true; win.SaveAdMobSettings(); Assert.Equal("pub-9999888877776666", settings.Get("admob.publisherid")); Assert.Equal("true", settings.Get("admob.usetestads")); Assert.Equal("pub-9999888877776666", adService.CurrentConfig.PublisherId); Assert.True(adService.CurrentConfig.UseTestAds); }); } }