public void Constructor_CreateDeviceHelperIfRequested(string baseXmatterName, bool useDeviceVersionIfAvailable, string expected)
        {
            var fileLocator = new FileLocator(new[] { _factoryXMatter, _testXmatter });
            var helper      = new XMatterHelper(_dom, baseXmatterName, fileLocator, useDeviceVersionIfAvailable);

            Assert.AreEqual(expected, helper.GetStyleSheetFileName());
        }
Exemple #2
0
        /// <summary>
        /// Finds the XMatterName for this book. If it cannot be determined, falls back to "Device"
        /// </summary>
        private string GetBestXMatter()
        {
            string xmatterName = "Device";              // This is the default, in case anything goes wrong.

            if (String.IsNullOrEmpty(_bookDirectory))
            {
                return(xmatterName);
            }

            DirectoryInfo dirInfo;

            try
            {
                dirInfo = new DirectoryInfo(_bookDirectory);
            }
            catch
            {
                return(xmatterName);
            }

            string suffix  = "-XMatter.css";
            var    files   = dirInfo.GetFiles();
            var    matches = files.Where(x => x.Name.EndsWith(suffix));

            if (matches.Any())
            {
                string xmatterFilename = matches.First().Name;
                xmatterName = XMatterHelper.GetXMatterFromStyleSheetFileName(xmatterFilename);
            }

            return(xmatterName ?? "Device");
        }
        public void InjectXMatter_GenericLanguageFieldsHaveCorrectEffectiveLangAttribute()
        {
            var xMatterDom = new XmlDocument();
            var dom        = new HtmlDom(@"<html><head></head><body>
					<div class='bloom-page titlePage bloom-frontMatter'>
		                <div class='bloom-editable' lang='en' data-book='bookTitle'>
		                    <p>The Moon and the Cap</p>
		                </div>
						<div class='langName bloom-writeOnly' data-library='languageLocation'></div>
					</div>
					<div class='bloom-page credits bloom-frontMatter'>
						<div class='copyright Credits-Page-style' data-derived='originalCopyrightAndLicense' lang='*'>
			                Adapted from original, <cite data-book='originalTitle'>The Moon and the Cap</cite>, Copyright © 2007, Pratham Books. Licensed under CC BY 4.0.
			            </div>
					</div>
				</body></html>"                );
            var helper     = new XMatterHelper(dom, "Factory", new FileLocator(new[] { _factoryXMatter }));

            xMatterDom.LoadXml(@"<html><head></head><body>
					<div class='bloom-page titlePage bloom-frontMatter' data-page='required'>
		                <div class='bloom-editable' lang='en' data-book='bookTitle'>
		                </div>
						<div class='langName bloom-writeOnly' data-library='languageLocation'></div>
					</div>
					<div class='bloom-page credits bloom-frontMatter' data-page='required'>
						<div class='copyright Credits-Page-style' data-derived='originalCopyrightAndLicense' lang='*'>
			            </div>
					</div>
				</body></html>"                );
            helper.XMatterDom = xMatterDom;

            // SUT
            helper.InjectXMatter(_dataSet, Layout.A5Portrait, false, "ru");

            // The closest parent with a meaningful lang needs to have language2. Then we get the proper font. See BL-8545.
            XmlElement languageLocationDiv            = dom.SelectSingleNode("//div[@data-library='languageLocation']");
            XmlElement originalCopyrightAndLicenseDiv = dom.SelectSingleNode("//div[@data-derived='originalCopyrightAndLicense']");

            XmlElement[] elementsToCheck = { languageLocationDiv, originalCopyrightAndLicenseDiv };
            elementsToCheck.ForEach(elementToCheck =>
            {
                string lang = languageLocationDiv.Attributes["lang"]?.Value;
                while (string.IsNullOrEmpty(lang) || lang == "*")
                {
                    elementToCheck = (XmlElement)elementToCheck.ParentNode;
                    lang           = elementToCheck.Attributes["lang"]?.Value;
                }

                Assert.That(lang, Is.EqualTo("ru"));
            });

            // Verify we didn't break other fields
            XmlElement titleDiv = dom.SelectSingleNode("//div[@data-book='bookTitle']");

            Assert.That(titleDiv.Attributes["lang"]?.Value, Is.EqualTo("en"));
        }
        /// <summary>
        /// The collection settings point to object which might not exist. For example, the xmatter pack might not exist.
        /// So this should be called as soon as it is ok to show some UI. It will find any dependencies it can't meet,
        /// revert them to defaults, and notify the user.
        /// </summary>
        public void CheckAndFixDependencies(BloomFileLocator bloomFileLocator)
        {
            var errorTemplate = LocalizationManager.GetString("Errors.XMatterNotFound",
                                                              "This Collection called for Front/Back Matter pack named '{0}', but this version of Bloom does not have it, and Bloom could not find it on this computer. The collection has been changed to use the default Front/Back Matter pages.");
            var errorMessage = String.Format(errorTemplate, XMatterPackName);

            XMatterPackName = XMatterHelper.MigrateXMatterName(XMatterPackName);
            if (string.IsNullOrEmpty(XMatterHelper.GetXMatterDirectory(XMatterPackName, bloomFileLocator, errorMessage, false)))
            {
                this.XMatterPackName = kDefaultXmatterName;
                Save();
            }
        }
Exemple #5
0
        public void TestBookSpecifiesXMatter(string xmatterBook, string expected)
        {
            var factoryXMatter = BloomFileLocator.GetInstalledXMatterDirectory();
            var fileLocator    = new FileLocator(new string[] { factoryXMatter });

            // Test that the XMatterHelper finds a required xmatter setting.
            var dom1 = new HtmlDom("<html>" +
                                   "<head>" +
                                   "<meta charset='UTF-8'></meta>" +
                                   "<meta name='BloomFormatVersion' content='2.0'></meta>" +
                                   "<meta name='pageTemplateSource' content='Basic Book'></meta>" +
                                   xmatterBook +
                                   "</head>" +
                                   "<body>" +
                                   "<div id='bloomDataDiv'>" +
                                   "<div data-book='contentLanguage1' lang='*'>en</div>" +
                                   "<div data-book='contentLanguage1Rtl' lang='*'>False</div>" +
                                   "<div data-book='languagesOfBook' lang='*'>English</div>" +
                                   "</div>" +
                                   "</body>" +
                                   "</html>");
            XMatterHelper helper1;

            if (xmatterBook.Contains("DoesNotExist"))
            {
                using (new NonFatalProblem.ExpectedByUnitTest())
                {
                    helper1 = new XMatterHelper(dom1, "Factory", fileLocator);
                }
            }
            else
            {
                helper1 = new XMatterHelper(dom1, "Factory", fileLocator);
            }
            if (xmatterBook.Contains("DoesNotExist") || string.IsNullOrEmpty(xmatterBook))
            {
                // An xmatter specification that cannot be found should be removed from the DOM.
                // An empty xmatter specification is also removed.
                Assert.That(dom1.GetMetaValue("xmatter", null), Is.Null);
            }
            else
            {
                // Verify that we may have what we want for the xmatter specification, valid or not.
                Assert.That(dom1.GetMetaValue("xmatter", null), Is.Not.Null);
            }
            Assert.That(helper1.GetStyleSheetFileName(), Is.EqualTo(expected));
        }
        public void GetXMatterFromStyleSheetFileName_AllDefaults_ExtractsPrefix(string filename, string expectedXMatterName)
        {
            string actual = XMatterHelper.GetXMatterFromStyleSheetFileName(filename);

            Assert.AreEqual(expectedXMatterName, actual, "XMatterName did not match.");
        }