text:
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.autofill_assistant;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.IntentMatchers.anyIntent;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasData;
import static androidx.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE;
import static androidx.test.espresso.matcher.ViewMatchers.assertThat;
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
import static androidx.test.espresso.matcher.ViewMatchers.hasSibling;
import static androidx.test.espresso.matcher.ViewMatchers.hasTextColor;
import static androidx.test.espresso.matcher.ViewMatchers.isChecked;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.Matchers.not;

import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.hasTintColor;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.hasTypefaceSpan;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.startAutofillAssistant;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.waitUntilViewMatchesCondition;
import static org.chromium.chrome.browser.autofill_assistant.AutofillAssistantUiTestUtil.withParentIndex;

import android.app.Activity;
import android.app.Instrumentation.ActivityResult;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.widget.RadioButton;

import androidx.test.espresso.intent.Intents;
import androidx.test.filters.MediumTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;

import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisableIf;
import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.autofill_assistant.proto.ActionProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ChipProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ChipType;
import org.chromium.chrome.browser.autofill_assistant.proto.CounterInputProto;
import org.chromium.chrome.browser.autofill_assistant.proto.CounterInputProto.Counter;
import org.chromium.chrome.browser.autofill_assistant.proto.FormInputProto;
import org.chromium.chrome.browser.autofill_assistant.proto.FormProto;
import org.chromium.chrome.browser.autofill_assistant.proto.InfoPopupProto;
import org.chromium.chrome.browser.autofill_assistant.proto.InfoPopupProto.DialogButton;
import org.chromium.chrome.browser.autofill_assistant.proto.InfoPopupProto.DialogButton.OpenUrlInCCT;
import org.chromium.chrome.browser.autofill_assistant.proto.ProcessedActionProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ProcessedActionStatusProto;
import org.chromium.chrome.browser.autofill_assistant.proto.PromptProto;
import org.chromium.chrome.browser.autofill_assistant.proto.PromptProto.Choice;
import org.chromium.chrome.browser.autofill_assistant.proto.SelectionInputProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ShowFormProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SupportedScriptProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SupportedScriptProto.PresentationProto;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.util.ChromeAccessibilityUtil;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.TestThreadUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Tests autofill assistant bottomsheet.
 */
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@RunWith(ChromeJUnit4ClassRunner.class)
public class AutofillAssistantFormActionTest {
    private final CustomTabActivityTestRule mTestRule = new CustomTabActivityTestRule();

    @Rule
    public final TestRule mRulesChain =
            RuleChain.outerRule(mTestRule).around(new AutofillAssistantCustomTabTestRule(
                    mTestRule, "autofill_assistant_target_website.html"));

    /**
     * Creates a close-to-real example of a form action with multiple counters and choices,
     * interacts with those widgets, and then checks the response to the server.
     */
    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testFormAction() {
        ArrayList<ActionProto> list = new ArrayList<>();
        // FromProto.Builder, extracted to avoid excessive line widths.
        FormProto.Builder formProto =
                FormProto.newBuilder()
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder()
                                        .addCounters(CounterInputProto.Counter.newBuilder()
                                                             .setMinValue(0)
                                                             .setMaxValue(1)
                                                             .setLabel("Counter 1")
                                                             .setDescriptionLine1("$34.00 per item")
                                                             .setDescriptionLine2(
                                                                     "<link1>Details</link1>"))
                                        .addCounters(
                                                CounterInputProto.Counter.newBuilder()
                                                        .setMinValue(1)
                                                        .setMaxValue(9)
                                                        .setLabel("Counter 2")
                                                        .setDescriptionLine1("$387.00 per item"))
                                        .setMinimizedCount(1)
                                        .setMinCountersSum(2)
                                        .setMinimizeText("Minimize")
                                        .setExpandText("Expand")))
                        .addInputs(FormInputProto.newBuilder().setSelection(
                                SelectionInputProto.newBuilder()
                                        .addChoices(SelectionInputProto.Choice.newBuilder()
                                                            .setLabel("Choice 1")
                                                            .setDescriptionLine1("$10.00 option")
                                                            .setDescriptionLine2(
                                                                    "<link1>Details</link1>"))
                                        .addChoices(SelectionInputProto.Choice.newBuilder()
                                                            .setLabel("Choice 2")
                                                            .setDescriptionLine1("$20.00 option")
                                                            .setDescriptionLine2(
                                                                    "<link1>Details</link1>"))
                                        .setAllowMultiple(false)))
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder().addCounters(
                                        CounterInputProto.Counter.newBuilder()
                                                .setMinValue(1)
                                                .setMaxValue(9)
                                                .setLabel("Counter 3")
                                                .setDescriptionLine1("$20.00 per item")
                                                .setDescriptionLine2("<link1>Details</link1>"))));
        // FormAction.
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Continue"))
                                              .setForm(formProto))
                         .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        AutofillAssistantTestService testService =
                new AutofillAssistantTestService(Collections.singletonList(script));
        startAutofillAssistant(mTestRule.getActivity(), testService);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        // TODO(b/144690738) Remove the isDisplayed() condition.
        onView(allOf(withId(R.id.value), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .check(matches(hasTextColor(R.color.baseline_neutral_900_alpha_38)));
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .check(matches(hasTintColor(R.color.modern_blue_600)));
        onView(allOf(withId(R.id.decrease_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .check(matches(hasTintColor(R.color.baseline_neutral_900_alpha_38)));
        // Click on Counter 1 +, increase from 0 to 1.
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .perform(scrollTo(), click());
        onView(allOf(withId(R.id.value), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .check(matches(hasTextColor(R.color.modern_blue_600)));
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .check(matches(hasTintColor(R.color.baseline_neutral_900_alpha_38)));
        // Decrease button is still disabled due to the minCountersSum requirement.

        // Click expand label to make Counter 2 visible.
        onView(allOf(withId(R.id.expand_label), withEffectiveVisibility(VISIBLE)))
                .perform(scrollTo(), click());
        // Click on Counter 3 +, increase from 0 to 1.
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 3")))))
                .perform(scrollTo(), click());

        // Click on Choice 1, then Choice 2, then back to Choice 1.
        onView(allOf(withClassName(is(RadioButton.class.getName())), withParentIndex(0),
                       withEffectiveVisibility(VISIBLE)))
                .perform(scrollTo(), click());
        onView(allOf(withClassName(is(RadioButton.class.getName())), withParentIndex(3),
                       withEffectiveVisibility(VISIBLE)))
                .perform(scrollTo(), click());
        onView(allOf(withClassName(is(RadioButton.class.getName())), withParentIndex(0),
                       withEffectiveVisibility(VISIBLE)))
                .perform(scrollTo(), click());

        // Check that choice 1 is visually selected and choice 2 is de-selected.
        onView(allOf(withClassName(is(RadioButton.class.getName())), withParentIndex(0),
                       withEffectiveVisibility(VISIBLE)))
                .check(matches(isChecked()));
        onView(allOf(withClassName(is(RadioButton.class.getName())), withParentIndex(3),
                       withEffectiveVisibility(VISIBLE)))
                .check(matches(not(isChecked())));

        // Click on Counter 2 +, increase from 0 to 1.
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 2")))))
                .perform(scrollTo(), click());

        // Finish form action, wait for response and prepare next set of actions.
        List<ActionProto> nextActions = new ArrayList<>();
        nextActions.add(ActionProto.newBuilder()
                                .setPrompt(PromptProto.newBuilder()
                                                   .setMessage("Finished")
                                                   .addChoices(Choice.newBuilder().setChip(
                                                           ChipProto.newBuilder()
                                                                   .setType(ChipType.DONE_ACTION)
                                                                   .setText("End"))))
                                .build());
        testService.setNextActions(nextActions);
        waitUntilViewMatchesCondition(withText("Continue"), isEnabled());
        int numNextActionsCalled = testService.getNextActionsCounter();
        onView(withText("Continue")).perform(click());
        testService.waitUntilGetNextActions(numNextActionsCalled + 1);

        List<ProcessedActionProto> processedActions = testService.getProcessedActions();
        assertThat(processedActions, iterableWithSize(1));
        assertThat(
                processedActions.get(0).getStatus(), is(ProcessedActionStatusProto.ACTION_APPLIED));
        assertThat(processedActions.get(0).getResultDataCase(),
                is(ProcessedActionProto.ResultDataCase.FORM_RESULT));

        List<FormInputProto.Result> formResult =
                processedActions.get(0).getFormResult().getInputResultsList();
        assertThat(formResult.size(), is(3));
        assertThat(formResult.get(0).getInputTypeCase(),
                is(FormInputProto.Result.InputTypeCase.COUNTER));
        assertThat(formResult.get(0).getCounter().getValuesCount(), is(2));
        // Counter 1
        assertThat(formResult.get(0).getCounter().getValues(0), is(1));
        // Counter 2
        assertThat(formResult.get(0).getCounter().getValues(1), is(1));

        // Choice 1
        assertThat(formResult.get(1).getInputTypeCase(),
                is(FormInputProto.Result.InputTypeCase.SELECTION));
        assertThat(formResult.get(1).getSelection().getSelectedCount(), is(2));
        assertThat(formResult.get(1).getSelection().getSelected(0), is(true));
        assertThat(formResult.get(1).getSelection().getSelected(1), is(false));

        // Counter 3
        assertThat(formResult.get(2).getInputTypeCase(),
                is(FormInputProto.Result.InputTypeCase.COUNTER));
        assertThat(formResult.get(2).getCounter().getValuesCount(), is(1));
        assertThat(formResult.get(2).getCounter().getValues(0), is(1));

        waitUntilViewMatchesCondition(withText("End"), isCompletelyDisplayed());
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testFormActionClickLink() {
        ArrayList<ActionProto> list = new ArrayList<>();
        // FromProto.Builder, extracted to avoid excessive line widths.
        FormProto.Builder formProto =
                FormProto.newBuilder().addInputs(FormInputProto.newBuilder().setCounter(
                        CounterInputProto.newBuilder()
                                .addCounters(CounterInputProto.Counter.newBuilder()
                                                     .setMinValue(1)
                                                     .setMaxValue(9)
                                                     .setLabel("Counter 1")
                                                     .setDescriptionLine1("$34.00 per item")
                                                     .setDescriptionLine2("<link4>Details</link4>")
                                                     .setInitialValue(1))
                                .setMinimizedCount(1)
                                .setMinCountersSum(2)
                                .setMinimizeText("Minimize")
                                .setExpandText("Expand")));
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Continue"))
                                              .setForm(formProto))
                         .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        AutofillAssistantTestService testService =
                new AutofillAssistantTestService(Collections.singletonList(script));
        startAutofillAssistant(mTestRule.getActivity(), testService);

        // Click on Counter 1 +, increase from 1 to 2.
        onView(allOf(withId(R.id.increase_button), withEffectiveVisibility(VISIBLE),
                       hasSibling(hasDescendant(withText("Counter 1")))))
                .perform(scrollTo(), click());

        int numNextActionsCalled = testService.getNextActionsCounter();
        onView(allOf(isDisplayed(), withText("Details"))).perform(click());
        testService.waitUntilGetNextActions(numNextActionsCalled + 1);

        List<ProcessedActionProto> processedActions = testService.getProcessedActions();
        assertThat(processedActions, iterableWithSize(1));
        assertThat(
                processedActions.get(0).getStatus(), is(ProcessedActionStatusProto.ACTION_APPLIED));
        assertThat(processedActions.get(0).getResultDataCase(),
                is(ProcessedActionProto.ResultDataCase.FORM_RESULT));

        List<FormInputProto.Result> formResult =
                processedActions.get(0).getFormResult().getInputResultsList();
        assertThat(formResult.size(), is(1));
        assertThat(processedActions.get(0).getFormResult().getLink(), is(4));
        assertThat(formResult.get(0).getInputTypeCase(),
                is(FormInputProto.Result.InputTypeCase.COUNTER));
        assertThat(formResult.get(0).getCounter().getValuesCount(), is(1));
        // Counter 1
        assertThat(formResult.get(0).getCounter().getValues(0), is(2));
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testInfoPopup() {
        ArrayList<ActionProto> list = new ArrayList<>();
        // FromProto.Builder, extracted to avoid excessive line widths.
        FormProto.Builder formProto =
                FormProto.newBuilder()
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder().addCounters(
                                        Counter.newBuilder()
                                                .setLabel("Counter 1")
                                                .setDescriptionLine1("$20.00 per tick")
                                                .setDescriptionLine2("<link1>Details</link1>"))))
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder().addCounters(
                                        Counter.newBuilder()
                                                .setLabel("Counter 2")
                                                .setDescriptionLine1("$20.00 per tick")
                                                .setDescriptionLine2("<link1>Details</link1>"))))
                        .setInfoLabel("<b>Info label with bold text</b>")
                        .setInfoPopup(
                                InfoPopupProto.newBuilder()
                                        .setTitle("Prompt title")
                                        .setText("Prompt text")
                                        .setNeutralButton(
                                                DialogButton.newBuilder()
                                                        .setLabel("Go to url")
                                                        .setOpenUrlInCct(
                                                                OpenUrlInCCT.newBuilder().setUrl(
                                                                        "https://www.google.com"))));
        // FormAction.
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Continue"))
                                              .setForm(formProto))
                         .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        AutofillAssistantTestService testService =
                new AutofillAssistantTestService(Collections.singletonList(script));
        startAutofillAssistant(mTestRule.getActivity(), testService);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        onView(withText("Info label with bold text"))
                .check(matches(withEffectiveVisibility(VISIBLE)));
        onView(withText("Info label with bold text"))
                .check(matches(hasTypefaceSpan(
                        0, "Info label with bold text".length() - 1, Typeface.BOLD)));

        onView(withId(R.id.info_button)).perform(click());
        waitUntilViewMatchesCondition(withText("Prompt title"), isCompletelyDisplayed());
        waitUntilViewMatchesCondition(withText("Prompt text"), isCompletelyDisplayed());

        Intent intent = new Intent();
        ActivityResult intentResult = new ActivityResult(Activity.RESULT_OK, intent);

        Intents.init();
        intending(anyIntent()).respondWith(intentResult);

        onView(withText("Go to url")).perform(click());

        intended(
                allOf(hasAction(Intent.ACTION_VIEW), hasData(Uri.parse("https://www.google.com"))));
        Intents.release();
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testInfoPopupNoButtons() {
        ArrayList<ActionProto> list = new ArrayList<>();
        // FromProto.Builder, extracted to avoid excessive line widths.
        FormProto.Builder formProto =
                FormProto.newBuilder()
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder().addCounters(
                                        Counter.newBuilder()
                                                .setLabel("Counter")
                                                .setDescriptionLine1("$20.00 per tick")
                                                .setDescriptionLine2("<link1>Details</link1>"))))
                        .setInfoLabel("Info label")
                        .setInfoPopup(InfoPopupProto.newBuilder()
                                              .setTitle("Prompt title")
                                              .setText("Prompt text"));
        // FormAction.
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Continue"))
                                              .setForm(formProto))
                         .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        AutofillAssistantTestService testService =
                new AutofillAssistantTestService(Collections.singletonList(script));
        startAutofillAssistant(mTestRule.getActivity(), testService);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        onView(withId(R.id.info_button)).perform(click());
        waitUntilViewMatchesCondition(withText("Prompt title"), isCompletelyDisplayed());
        // If no button is set in the proto a "Close" button should be added by default.
        onView(withText("Close")).perform(click());
        onView(withText("Prompt title")).check(doesNotExist());
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testMultipleForms() {
        ArrayList<ActionProto> list = new ArrayList<>();
        // FromProto.Builder, extracted to avoid excessive line widths.
        FormProto.Builder formProtoWithInfo =
                FormProto.newBuilder()
                        .addInputs(FormInputProto.newBuilder().setCounter(
                                CounterInputProto.newBuilder().addCounters(
                                        Counter.newBuilder()
                                                .setLabel("Counter 1")
                                                .setDescriptionLine1("$20.00 per tick")
                                                .setDescriptionLine2("<link1>Details</link1>"))))
                        .setInfoLabel("Info label");
        // FormAction.
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Continue"))
                                              .setForm(formProtoWithInfo))
                         .build());

        FormProto.Builder formProto = FormProto.newBuilder().addInputs(
                FormInputProto.newBuilder().setCounter(CounterInputProto.newBuilder().addCounters(
                        Counter.newBuilder()
                                .setLabel("Counter 1")
                                .setDescriptionLine1("$20.00 per tick")
                                .setDescriptionLine2("<link1>Details</link1>"))));

        // FormAction.
        list.add(ActionProto.newBuilder()
                         .setShowForm(ShowFormProto.newBuilder()
                                              .setChip(ChipProto.newBuilder()
                                                               .setType(ChipType.HIGHLIGHTED_ACTION)
                                                               .setText("Finish"))
                                              .setForm(formProto))
                         .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        AutofillAssistantTestService testService =
                new AutofillAssistantTestService(Collections.singletonList(script));
        startAutofillAssistant(mTestRule.getActivity(), testService);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        onView(withText("Info label")).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.info_button)).check(matches(not(isDisplayed())));
        onView(withText("Continue")).perform(click());
        waitUntilViewMatchesCondition(withText("Finish"), isCompletelyDisplayed());
        onView(withText("Info label")).check(matches(not(isDisplayed())));
        onView(withId(R.id.info_button)).check(matches(not(isDisplayed())));
    }

    private void startTestCounterExpansion(boolean accessibilityEnabled) {
        TestThreadUtils.runOnUiThreadBlocking(
                ()
                        -> ChromeAccessibilityUtil.get().setAccessibilityEnabledForTesting(
                                accessibilityEnabled));

        FormProto.Builder formProto =
                FormProto.newBuilder().addInputs(FormInputProto.newBuilder().setCounter(
                        CounterInputProto.newBuilder()
                                .addCounters(CounterInputProto.Counter.newBuilder()
                                                     .setLabel("Counter 1")
                                                     .setDescriptionLine1("$34.00 per item"))
                                .addCounters(CounterInputProto.Counter.newBuilder()
                                                     .setLabel("Counter 2")
                                                     .setDescriptionLine1("$387.00 per item"))
                                .setMinimizedCount(1)
                                .setMinCountersSum(2)
                                .setMinimizeText("Minimize")
                                .setExpandText("Expand")));

        // FormAction.
        List<ActionProto> list = Collections.singletonList(
                ActionProto.newBuilder()
                        .setShowForm(ShowFormProto.newBuilder()
                                             .setChip(ChipProto.newBuilder()
                                                              .setType(ChipType.HIGHLIGHTED_ACTION)
                                                              .setText("Continue"))
                                             .setForm(formProto))
                        .build());

        AutofillAssistantTestScript script = new AutofillAssistantTestScript(
                SupportedScriptProto.newBuilder()
                        .setPath("autofill_assistant_target_website.html")
                        .setPresentation(PresentationProto.newBuilder().setAutostart(true))
                        .build(),
                list);

        startAutofillAssistant(mTestRule.getActivity(),
                new AutofillAssistantTestService(Collections.singletonList(script)));
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testCounterExpandEnabledWithoutAccessibility() {
        startTestCounterExpansion(false);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        // Expand label is visible when accessibility is disabled.
        onView(withId(R.id.expand_label)).check(matches(isDisplayed()));
    }

    @Test
    @MediumTest
    @DisableIf.Build(sdk_is_less_than = 21)
    public void testCounterExpandDisabledWithAccessibility() {
        startTestCounterExpansion(true);

        waitUntilViewMatchesCondition(withText("Continue"), isCompletelyDisplayed());
        // Expand label is hidden when accessibility is enabled.
        onView(withId(R.id.expand_label)).check(matches(not(isDisplayed())));
    }
}


text:
<ion-view view-title="WEIGHING">
  <ion-content>

CONNECTED TO INDICATOR

        DISCONNECTED FROM INDICATOR

        <button class="button button-small button-assertive" ng-click="goToSettings()">Change Settings</button>

{{isConnected ? weight : '----'}}

kg

        <button class="button button-light button-assertive" ng-disabled="!isConnected" ng-click="tare()">TARE</button>
        <button class="button button-light button-assertive" ng-disabled="!isConnected" ng-click="getWeight()">GET WEIGHT</button>
        <button class="button button-light button-assertive" ng-disabled="!isConnected" ng-click="saveWeight()">SAVE</button>
        <button class="button button-light button-assertive" ng-disabled="!isConnected" ng-click="takeScreenShotAndShare()">SHARE</button>

  </ion-content>
</ion-view>

redpajama_set_name: RedPajamaGithub


text:
from optparse import OptionParser
from sys import maxint
import string
import time
import random
import socket
import logging
import logging.handlers

fixed_line = ""
hostname = socket.gethostname()

# Possible run times are:
#  - fixed number of seconds
#  - set number of messages
#  - run until stopped
def determine_run_time() :
    if int(options.time) > 0 :
        print "time option found, num-lines option ignored"
        fixed_count = False
        infinite = False
        fixed_time = True
    elif int(options.num_lines) > 0 :
        fixed_count = True
        infinite = False
        fixed_time = False
    else:
        fixed_count = False
        infinite = True
        fixed_time = False

    return fixed_count, fixed_time, infinite

def delay(sleep_time,num_messages) :

# Avoid unnecessary calls to time.sleep()
    if sleep_time > 0.0 :
       sleep_this_time = True;

# Calling time.sleep() more than 10 times per second is pointless and adds too much overhead
# Back off to batches of messages big enough so that sleep is called 10 times per second max
       if sleep_time < 0.05 :
           sleep_this_time = False
           batch_size = (0.05 / sleep_time) * 10.0
           sleep_time = 0.5
           if num_messages % int(round(batch_size)) == 0 :
              sleep_this_time = True
       if sleep_this_time :
          time.sleep(sleep_time)
    return

# When file input used, pull a line from the file or re-open file if wrapped/eof
def next_line_from_file() :
    global infile
    current_line = ""
    if infile :
        while len(current_line) < options.line_length :
            in_line = infile.readline()
            if in_line == "" :
                infile.close()
                infile = open(options.file,'r')
            else :
                current_line = current_line + " " + in_line.rstrip()

    return current_line[:options.line_length]

def get_word() :
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(options.word_length))

def get_new_line():

    current_line = ""
    if options.text_type == "random" :
        while len(current_line) < options.line_length :
            current_line = current_line + get_word() + " "
    else:
        current_line = next_line_from_file()

    return current_line[:options.line_length]

def single_line():
    if options.fixed_line and (not fixed_line == ""):
        single_line = fixed_line
    else:
        single_line = get_new_line()

    return single_line

def create_message(seq_number, msg) :
    global hostname
    return hostname + " : " + str(seq_number) + " : " + msg

# Fixed time period, in seconds
def generate_for_time():
    now = time.time()
    then = now + options.time
    number_generated = 0
    while now <= then :
        number_generated += 1
        logger.info( create_message(number_generated, single_line()) )
        delay(options.sleep_time, number_generated)
        now = time.time()

    return

#Set number of lines, or infinite if time is 0
def generate_num_lines() :
    global hostname
    if options.num_lines == 0 :
        number_to_generate = maxint
    else :
        number_to_generate = options.num_lines

    number_generated = 0
    while (number_generated < number_to_generate) :
        number_generated += 1
        logger.info( create_message(number_generated, single_line()) )
        delay(options.sleep_time, number_generated)

def generate_messages() :
    global fixed_line
    if options.fixed_line :
        fixed_line = single_line()
    (fixed_count, fixed_time, infinite) = determine_run_time()
    if fixed_time :
        generate_for_time()
    else:
        generate_num_lines()

def init_logger(my_logger):
    my_logger.setLevel(logging.INFO)

    if options.journal :
        jh = logging.handlers.SysLogHandler(address = '/dev/log')
        my_logger.addHandler(jh)
    elif options.log_on_file:
        print 'log_on_file: {}'.format(options.log_on_file)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh = logging.FileHandler(options.log_on_file)
        fh.setFormatter(formatter)
        my_logger.addHandler(fh)
    else :
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        sh = logging.StreamHandler()
        sh.setFormatter(formatter)
        my_logger.addHandler(sh)

if __name__ ==  "__main__":

    parser = OptionParser()
    parser.add_option("-l", "--line-length", dest="line_length", type="int", default=100,
                     help="length of each output line")
    parser.add_option("--text-type", dest="text_type",
                     help="random or input", default="random")
    parser.add_option("--word-length", dest="word_length", type="int", default=9,
                     help="word length for random text")
    parser.add_option("--fixed-line", dest="fixed_line", action="store_true", default=False,
                      help="the same line is repeated if true, variable line content if false")
    parser.add_option("-f","--file", dest="file", default="",
                     help="file for input text")
    parser.add_option("-j","--journal", dest="journal", action="store_true", default=False,
                      help="use logger to log messages to journald instead of stdout")
    parser.add_option("-o", "--log-on-file", dest="log_on_file",
                      help="the file path to which the log outputs")
    parser.add_option("-r", "--rate", dest="rate", type="float", default=10.0,
                     help="rate in lines per minute")
    parser.add_option("-n", "--num-lines", dest="num_lines", type="int", default=0,
                     help="number of lines to generate, 0 is infinite - cannot be used with -t")
    parser.add_option("-t", "--time", dest="time", type="int", default=0,
                     help="time to run in seconds, cannot be used with -n")

    (options, args) = parser.parse_args()

    if not options.file == "" :
        infile = open(options.file,'r')

    options.sleep_time = 0.0
    if options.rate > 0.0 :
        options.sleep_time = 60.0/options.rate

    logger = logging.getLogger('SVTLogger')
    init_logger(logger)

    generate_messages()


text:
module MongoidExt
  module Tags
    def self.included(klass)
      klass.class_eval do
        extend ClassMethods

        field :tags, :type => Array, :default => []
        index({:tags => 1})
      end
    end

    module ClassMethods
      def tag_cloud(conditions = {}, limit = 30)
        Mongoid.session(:default).command({
          :eval => "function(collection, q,l) { return tag_cloud(collection, q,l); }",
          :args => [self.collection_name, conditions, limit]
        })['retval']
      end

      # Model.find_with_tags("budget", "big").limit(4)
      def find_with_tags(*tags)
        self.where({:tags.in => tags})
      end

      def find_tags(regex, conditions = {}, limit = 30)
        Mongoid.session(:default).command({
          :eval => "function(collection, a,b,c) { return find_tags(collection, a,b,c); }",
          :args => [self.collection_name, regex, conditions, limit]
        })['retval']
      end
    end
  end
end


text:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Hugo on </title>
    <link>https://www.foundry.codes/tags/hugo/</link>
    <description>Recent content in Hugo on </description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>

	<atom:link href="https://www.foundry.codes/tags/hugo/index.xml" rel="self" type="application/rss+xml" />

    <item>
      <title>How To Setup Meghna Hugo</title>
      <link>https://www.foundry.codes/blog/installation/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>

      <guid>https://www.foundry.codes/blog/installation/</guid>
      <description>Install this template by following those simple steps: STEP-1 : Hugo installation Check this link below for install hugo on your computer.</description>
    </item>

  </channel>
</rss>


text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.network.fluent.models;

import com.azure.core.annotation.Fluent;
import com.azure.resourcemanager.network.models.ProvisioningState;
import com.fasterxml.jackson.annotation.JsonProperty;

/** IP configuration profile properties. */
@Fluent
public final class IpConfigurationProfilePropertiesFormatInner {
    /*
     * The reference to the subnet resource to create a container network interface ip configuration.
     */
    @JsonProperty(value = "subnet")
    private SubnetInner subnet;

    /*
     * The provisioning state of the IP configuration profile resource.
     */
    @JsonProperty(value = "provisioningState", access = JsonProperty.Access.WRITE_ONLY)
    private ProvisioningState provisioningState;

    /** Creates an instance of IpConfigurationProfilePropertiesFormatInner class. */
    public IpConfigurationProfilePropertiesFormatInner() {
    }

    /**
     * Get the subnet property: The reference to the subnet resource to create a container network interface ip
     * configuration.
     *
     * @return the subnet value.
     */
    public SubnetInner subnet() {
        return this.subnet;
    }

    /**
     * Set the subnet property: The reference to the subnet resource to create a container network interface ip
     * configuration.
     *
     * @param subnet the subnet value to set.
     * @return the IpConfigurationProfilePropertiesFormatInner object itself.
     */
    public IpConfigurationProfilePropertiesFormatInner withSubnet(SubnetInner subnet) {
        this.subnet = subnet;
        return this;
    }

    /**
     * Get the provisioningState property: The provisioning state of the IP configuration profile resource.
     *
     * @return the provisioningState value.
     */
    public ProvisioningState provisioningState() {
        return this.provisioningState;
    }

    /**
     * Validates the instance.
     *
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (subnet() != null) {
            subnet().validate();
        }
    }
}


text:
__description__ = "Librato Silverline process monitoring and management"
__config__ = {
    "librato.api_token": dict(
        description = "API token for account",
        default = "0",
    ),
    "librato.server_id_cmd": dict(
        description = "Command to run to generate server ID",
        default = None,
    ),
    "librato.template_id": dict(
        description = "Template ID for this server",
        default = None,
    ),
    "librato.email_address": dict(
        description = "Email address of account",
        default = None,
    ),
}


text:
/**
 * An updateable progress bar component. The progress bar supports two different modes: manual and automatic.
 *
 * In manual mode, you are responsible for showing, updating (via {@link #updateProgress}) and clearing the progress bar
 * as needed from your own code. This method is most appropriate when you want to show progress throughout an operation
 * that has predictable points of interest at which you can update the control.
 *
 * In automatic mode, you simply call {@link #wait} and let the progress bar run indefinitely, only clearing it once the
 * operation is complete. You can optionally have the progress bar wait for a specific amount of time and then clear
 * itself. Automatic mode is most appropriate for timed operations or asynchronous operations in which you have no need
 * for indicating intermediate progress.
 *
 *     @example
 *     var p = Ext.create('Ext.ProgressBar', {
 *        renderTo: Ext.getBody(),
 *        width: 300
 *     });
 *
 *     // Wait for 5 seconds, then update the status el (progress bar will auto-reset)
 *     p.wait({
 *         interval: 500, //bar will move fast!
 *         duration: 50000,
 *         increment: 15,
 *         text: 'Updating...',
 *         scope: this,
 *         fn: function(){
 *             p.updateText('Done!');
 *         }
 *     });
 */
Ext.define('Ext.ProgressBar', {
    extend: 'Ext.Component',
    alias: 'widget.progressbar',

    requires: [
        'Ext.Template',
        'Ext.CompositeElement',
        'Ext.TaskManager',
        'Ext.layout.component.ProgressBar'
    ],

    uses: ['Ext.fx.Anim'],

   /**
    * @cfg {Number} [value=0]
    * A floating point value between 0 and 1 (e.g., .5)
    */

   /**
    * @cfg {String} [text='']
    * The progress bar text (defaults to '')
    */

   /**
    * @cfg {String/HTMLElement/Ext.Element} textEl
    * The element to render the progress text to (defaults to the progress bar's internal text element)
    */

   /**
    * @cfg {String} id
    * The progress bar element's id (defaults to an auto-generated id)
    */

   /**
    * @cfg {String} [baseCls='x-progress']
    * The base CSS class to apply to the progress bar's wrapper element.
    */
    baseCls: Ext.baseCSSPrefix + 'progress',

    config: {
        /**
        * @cfg {Boolean} animate
        * True to animate the progress bar during transitions
        */
        animate: false,

        /**
         * @cfg {String} text
         * The text shown in the progress bar
         */
        text: ''
    },

    // private
    waitTimer: null,

    renderTpl: [
        '
',
            '

',
        '
',
        '
',
            '
',
                '

',
            '
',
        '
'
    ],

    componentLayout: 'progressbar',

    // private
    initComponent: function() {
        this.callParent();

        this.addChildEls('bar');

        this.addEvents(
            /**
             * @event update
             * Fires after each update interval
             * @param {Ext.ProgressBar} this
             * @param {Number} value The current progress value
             * @param {String} text The current progress text
             */
            "update"
        );
    },

    afterRender : function() {
        var me = this;

        // This produces a composite w/2 el's (which is why we cannot use childEls or
        // renderSelectors):
        me.textEl = me.textEl ? Ext.get(me.textEl) : me.el.select('.' + me.baseCls + '-text');

        me.callParent(arguments);

        if (me.value) {
            me.updateProgress(me.value, me.text);
        }
        else {
            me.updateText(me.text);
        }
    },

    /**
     * Updates the progress bar value, and optionally its text. If the text argument is not specified, any existing text
     * value will be unchanged. To blank out existing text, pass ''. Note that even if the progress bar value exceeds 1,
     * it will never automatically reset -- you are responsible for determining when the progress is complete and
     * calling {@link #reset} to clear and/or hide the control.
     * @param {Number} [value=0] A floating point value between 0 and 1 (e.g., .5)
     * @param {String} [text=''] The string to display in the progress text element
     * @param {Boolean} [animate=false] Whether to animate the transition of the progress bar. If this value is not
     * specified, the default for the class is used
     * @return {Ext.ProgressBar} this
     */
    updateProgress: function(value, text, animate) {
        var me = this,
            newWidth;

        me.value = value || 0;
        if (text) {
            me.updateText(text);
        }
        if (me.rendered && !me.isDestroyed) {
            if (me.isVisible(true)) {
                newWidth = Math.floor(me.value * me.el.getWidth(true));
                if (Ext.isForcedBorderBox) {
                    newWidth += me.bar.getBorderWidth("lr");
                }
                if (animate === true || (animate !== false && me.animate)) {
                    me.bar.stopAnimation();
                    me.bar.animate(Ext.apply({
                        to: {
                            width: newWidth + 'px'
                        }
                    }, me.animate));
                } else {
                    me.bar.setWidth(newWidth);
                }
            } else {
                // force a layout when we're visible again
                me.doComponentLayout();
            }
        }
        me.fireEvent('update', me, me.value, text);
        return me;
    },

    /**
     * Updates the progress bar text. If specified, textEl will be updated, otherwise the progress bar itself will
     * display the updated text.
     * @param {String} [text=''] The string to display in the progress text element
     * @return {Ext.ProgressBar} this
     */
    updateText: function(text) {
        var me = this;

        me.text = text;
        if (me.rendered) {
            me.textEl.update(me.text);
        }
        return me;
    },

    applyText : function(text) {
        this.updateText(text);
    },

    /**
     * Initiates an auto-updating progress bar. A duration can be specified, in which case the progress bar will
     * automatically reset after a fixed amount of time and optionally call a callback function if specified. If no
     * duration is passed in, then the progress bar will run indefinitely and must be manually cleared by calling
     * {@link #reset}.
     *
     * Example usage:
     *
     *     var p = new Ext.ProgressBar({
     *        renderTo: 'my-el'
     *     });
     *
     *     //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
     *     var p = Ext.create('Ext.ProgressBar', {
     *        renderTo: Ext.getBody(),
     *        width: 300
     *     });
     *
     *     //Wait for 5 seconds, then update the status el (progress bar will auto-reset)
     *     p.wait({
     *        interval: 500, //bar will move fast!
     *        duration: 50000,
     *        increment: 15,
     *        text: 'Updating...',
     *        scope: this,
     *        fn: function(){
     *           p.updateText('Done!');
     *        }
     *     });
     *
     *     //Or update indefinitely until some async action completes, then reset manually
     *     p.wait();
     *     myAction.on('complete', function(){
     *         p.reset();
     *         p.updateText('Done!');
     *     });
     *
     * @param {Object} config (optional) Configuration options
     * @param {Number} config.duration The length of time in milliseconds that the progress bar should
     * run before resetting itself (defaults to undefined, in which case it will run indefinitely
     * until reset is called)
     * @param {Number} config.interval The length of time in milliseconds between each progress update
     * (defaults to 1000 ms)
     * @param {Boolean} config.animate Whether to animate the transition of the progress bar. If this
     * value is not specified, the default for the class is used.
     * @param {Number} config.increment The number of progress update segments to display within the
     * progress bar (defaults to 10).  If the bar reaches the end and is still updating, it will
     * automatically wrap back to the beginning.
     * @param {String} config.text Optional text to display in the progress bar element (defaults to '').
     * @param {Function} config.fn A callback function to execute after the progress bar finishes auto-
     * updating.  The function will be called with no arguments.  This function will be ignored if
     * duration is not specified since in that case the progress bar can only be stopped programmatically,
     * so any required function should be called by the same code after it resets the progress bar.
     * @param {Object} config.scope The scope that is passed to the callback function (only applies when
     * duration and fn are both passed).
     * @return {Ext.ProgressBar} this
     */
    wait: function(o) {
        var me = this;

        if (!me.waitTimer) {
            scope = me;
            o = o || {};
            me.updateText(o.text);
            me.waitTimer = Ext.TaskManager.start({
                run: function(i){
                    var inc = o.increment || 10;
                    i -= 1;
                    me.updateProgress(((((i+inc)%inc)+1)*(100/inc))*0.01, null, o.animate);
                },
                interval: o.interval || 1000,
                duration: o.duration,
                onStop: function(){
                    if (o.fn) {
                        o.fn.apply(o.scope || me);
                    }
                    me.reset();
                },
                scope: scope
            });
        }
        return me;
    },

    /**
     * Returns true if the progress bar is currently in a {@link #wait} operation
     * @return {Boolean} True if waiting, else false
     */
    isWaiting: function(){
        return this.waitTimer !== null;
    },

    /**
     * Resets the progress bar value to 0 and text to empty string. If hide = true, the progress bar will also be hidden
     * (using the {@link #hideMode} property internally).
     * @param {Boolean} [hide=false] True to hide the progress bar.
     * @return {Ext.ProgressBar} this
     */
    reset: function(hide){
        var me = this;

        me.updateProgress(0);
        me.clearTimer();
        if (hide === true) {
            me.hide();
        }
        return me;
    },

    // private
    clearTimer: function(){
        var me = this;

        if (me.waitTimer) {
            me.waitTimer.onStop = null; //prevent recursion
            Ext.TaskManager.stop(me.waitTimer);
            me.waitTimer = null;
        }
    },

    onDestroy: function(){
        var me = this;

        me.clearTimer();
        if (me.rendered) {
            if (me.textEl.isComposite) {
                me.textEl.clear();
            }
            Ext.destroyMembers(me, 'textEl', 'progressBar');
        }
        me.callParent();
    }
});


text:
const BaseRouter = require('../');
const expect = require('chai').expect;

describe('Router', function () {

  describe('::extend', function () {
    it('should allow default routes to be overridden and custom routes to be added', function () {
      var customRoutes = {
        get: {
          '/:id': true, // override existing
          '/test': true // create new
        },
        put: null // replace entire verb
      };
      var router = BaseRouter.extend({
        model: {},
        controller: {},
        routes: customRoutes
      });
      expect(router.get['/:id']).to.equal(customRoutes.get['/:id']);
      expect(router.get['/test']).to.equal(customRoutes.get['/test']);
      expect(router.put).to.equal(customRoutes.put);
    });
  });

  require('./lib/assign_model');
  require('./lib/created_by');
  require('./lib/is_admin');
  require('./lib/is_authenticated');
});


text:
package org.sirix.axis.filter;

import java.util.ArrayList;
import java.util.List;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.sirix.api.Filter;
import org.sirix.api.xml.XmlNodeReadOnlyTrx;

/**
 * <p>
 * Nests two or more IFilters.
 * </p>
 */
public final class NestedFilter extends AbstractFilter<XmlNodeReadOnlyTrx> {

  /** Tests to apply. */
  private final List<Filter<XmlNodeReadOnlyTrx>> mFilter;

  /**
   * Default constructor.
   *
   * @param rtx {@link XmlNodeReadOnlyTrx} this filter is bound to
   * @param axisTest test to perform for each node found with axis
   */
  public NestedFilter(final XmlNodeReadOnlyTrx rtx, final @NonNull List<Filter<XmlNodeReadOnlyTrx>> axisTest) {
    super(rtx);
    mFilter = new ArrayList<>(axisTest);
  }

  @Override
  public final boolean filter() {
    boolean filterResult = true;

    for (final Filter<XmlNodeReadOnlyTrx> filter : mFilter) {
      filterResult = filterResult && filter.filter();
    }

    return filterResult;
  }
}


text:
using System;
using System.Threading;
using Lokad.Cqrs;
using Lokad.Cqrs.Build;
using NUnit.Framework;

namespace Cqrs.Portable.Tests.Build
{
    public class CqrsEngineHostTest
    {
        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void when_start_task_where_empty_task()
        {
            var host = new CqrsEngineHost(new IEngineProcess[]{});
            host.Start(new CancellationToken());
        }

        [Test]
        public void when_start_task()
        {
            var testEngineProcess = new TestEngineProcess();
            var host = new CqrsEngineHost(new[] { testEngineProcess, });
            var task = host.Start(new CancellationToken());
            task.Wait();

            Assert.IsTrue(testEngineProcess.IsStarted);
        }

        [Test]
        public void when_disposed_task()
        {
            var testEngineProcess = new TestEngineProcess();
            var host = new CqrsEngineHost(new[] { testEngineProcess, });
            host.Dispose();

            Assert.IsTrue(testEngineProcess.IsDisposed);
        }
    }
}


text:
#include <sysinit.h>
#include <stdc.h>
#define KL_LOG KL_INIT
#include <klog.h>

typedef TAILQ_HEAD(tailhead, sysinit_entry) sysinit_tailq_t;

static sysinit_entry_t *find(const char *name) {
  /* locates sysinit_entry_t with given name */
  sysinit_entry_t **ptr;
  SET_FOREACH(ptr, sysinit) {
    if (strcmp(name, (*ptr)->name) == 0)
      return *ptr;
  }
  return NULL;
}

static void push_last(sysinit_tailq_t *head) {
  /* dependants field for every sysinit_entry_t need to be calculated */
  sysinit_entry_t **ptr;
  SET_FOREACH(ptr, sysinit) {
    if ((*ptr)->dependants == 0)
      TAILQ_INSERT_HEAD(head, *ptr, entries);
  }
}

static void build_queue(sysinit_tailq_t *head) {
  /* dependants field for every sysinit_entry_t need to be calculated */
  push_last(head);

  sysinit_entry_t *p;
  TAILQ_FOREACH_REVERSE(p, head, tailhead, entries) {
    char **deps = p->deps;
    while (*deps) {
      sysinit_entry_t *dependency = find(*deps);
      dependency->dependants--;
      if (dependency->dependants == 0)
        TAILQ_INSERT_HEAD(head, dependency, entries);
      deps++;
    }
  }
}

static void count_dependants(void) {
  /* requires dependants field to be zeroed */
  sysinit_entry_t **ptr;
  SET_FOREACH(ptr, sysinit) {
    klog("found module '%s'", (*ptr)->name);
    char **deps = (*ptr)->deps;
    while (*deps) {
      sysinit_entry_t *dependency = find(*deps);
      if (dependency == NULL)
        panic("module '%s' depends on '%s', which has not been found",
              (*ptr)->name, *deps);
      dependency->dependants++;
      deps++;
    }
  }
}

static void dump_cycle(void) {
  /* if there's some cycle prints its content, and panics
     works properly only after build_queue */
  sysinit_entry_t **ptr;
  bool cycle_found = false;
  SET_FOREACH(ptr, sysinit) {
    if ((*ptr)->dependants != 0) {
      klog("unreachable module '%s'", (*ptr)->name);
      cycle_found = true;
    }
  }
  if (cycle_found)
    panic("found cycle in modules dependencies");
}

void sysinit(void) {
  /* builds topological ordering and lauches modules with built order
   result is constructed from back, because of direction of relations
   given edges point to earlier modules */
  count_dependants();
  sysinit_tailq_t list = TAILQ_HEAD_INITIALIZER(list);
  build_queue(&list);
  dump_cycle();
  sysinit_entry_t *p;
  TAILQ_FOREACH (p, &list, entries) {
    if (p->func) {
      klog("initialize '%s' module", p->name);
      p->func();
    }
  }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">

001
002
007
008package org.jsimpledb.spring;
009
010import org.jsimpledb.core.DeletedObjectException;
011import org.jsimpledb.core.InvalidSchemaException;
012import org.jsimpledb.core.ReadOnlyTransactionException;
013import org.jsimpledb.core.ReferencedObjectException;
014import org.jsimpledb.core.RollbackOnlyTransactionException;
015import org.jsimpledb.core.SchemaMismatchException;
016import org.jsimpledb.core.StaleTransactionException;
017import org.jsimpledb.kv.RetryTransactionException;
018import org.jsimpledb.kv.TransactionTimeoutException;
019import org.springframework.dao.ConcurrencyFailureException;
020import org.springframework.dao.DataAccessException;
021import org.springframework.dao.DataIntegrityViolationException;
022import org.springframework.dao.EmptyResultDataAccessException;
023import org.springframework.dao.InvalidDataAccessApiUsageException;
024import org.springframework.dao.InvalidDataAccessResourceUsageException;
025import org.springframework.dao.QueryTimeoutException;
026import org.springframework.dao.support.PersistenceExceptionTranslator;
027
028/**
029 * JSimpleDB implementation of Spring's {@link PersistenceExceptionTranslator} interface.
030 *
031 * @see org.jsimpledb.spring
032 */
033@SuppressWarnings("serial")
034public class JSimpleDBExceptionTranslator implements PersistenceExceptionTranslator {
035
036    @Override
037    public DataAccessException translateExceptionIfPossible(RuntimeException e0) {
038        if (e0 instanceof DeletedObjectException) {
039            final DeletedObjectException e = (DeletedObjectException)e0;
040            return new EmptyResultDataAccessException("object " + e.getId() + " not found", 1, e);
041        }
042        if (e0 instanceof InvalidSchemaException || e0 instanceof ReadOnlyTransactionException)
043            return new InvalidDataAccessResourceUsageException(null, e0);
044        if (e0 instanceof ReferencedObjectException)
045            return new DataIntegrityViolationException(null, e0);
046        if (e0 instanceof RollbackOnlyTransactionException)
047            return new InvalidDataAccessApiUsageException(null, e0);
048        if (e0 instanceof SchemaMismatchException)
049            return new DataIntegrityViolationException(null, e0);
050        if (e0 instanceof StaleTransactionException || e0 instanceof org.jsimpledb.kv.StaleTransactionException)
051            return new InvalidDataAccessApiUsageException(null, e0);
052        if (e0 instanceof RetryTransactionException)
053            return new ConcurrencyFailureException(null, e0);
054        if (e0 instanceof TransactionTimeoutException)
055            return new QueryTimeoutException(null, e0);
056        return null;
057    }
058}
059


text:
package org.openhds.controller.service.refactor;

import org.openhds.controller.exception.ConstraintViolations;
import org.openhds.domain.model.FieldWorker;

public interface FieldWorkerService extends EntityService<FieldWorker> {
	public void generatePasswordHash(FieldWorker fieldWorker)
			throws ConstraintViolations;

	public FieldWorker generateId(FieldWorker fieldWorker)
			throws ConstraintViolations;
}


text:
package org.apache.nifi.cluster.coordination.http.replication.okhttp;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonInclude.Value;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import okhttp3.Call;
import okhttp3.ConnectionPool;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.cluster.coordination.http.replication.HttpReplicationClient;
import org.apache.nifi.cluster.coordination.http.replication.PreparedRequest;
import org.apache.nifi.framework.security.util.SslContextFactory;
import org.apache.nifi.remote.protocol.http.HttpHeaders;
import org.apache.nifi.stream.io.GZIPOutputStream;
import org.apache.nifi.util.FormatUtils;
import org.apache.nifi.util.NiFiProperties;
import org.apache.nifi.util.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
// Using static imports because of the name conflict:
import static org.apache.nifi.security.util.SslContextFactory.ClientAuth.WANT;
import static org.apache.nifi.security.util.SslContextFactory.createTrustSslContextWithTrustManagers;

public class OkHttpReplicationClient implements HttpReplicationClient {
    private static final Logger logger = LoggerFactory.getLogger(OkHttpReplicationClient.class);
    private static final Set<String> gzipEncodings = Stream.of("gzip", "x-gzip").collect(Collectors.toSet());

    private final EntitySerializer jsonSerializer;
    private final EntitySerializer xmlSerializer;

    private final ObjectMapper jsonCodec = new ObjectMapper();
    private final OkHttpClient okHttpClient;

    public OkHttpReplicationClient(final NiFiProperties properties) {
        jsonCodec.setDefaultPropertyInclusion(Value.construct(Include.NON_NULL, Include.ALWAYS));
        jsonCodec.setAnnotationIntrospector(new JaxbAnnotationIntrospector(jsonCodec.getTypeFactory()));

        jsonSerializer = new JsonEntitySerializer(jsonCodec);
        xmlSerializer = new XmlEntitySerializer();

        okHttpClient = createOkHttpClient(properties);
    }

    @Override
    public PreparedRequest prepareRequest(final String method, final Map<String, String> headers, final Object entity) {
        final boolean gzip = isUseGzip(headers);
        checkContentLengthHeader(method, headers);
        final RequestBody requestBody = createRequestBody(headers, entity, gzip);

        final Map<String, String> updatedHeaders = gzip ? updateHeadersForGzip(headers) : headers;
        return new OkHttpPreparedRequest(method, updatedHeaders, entity, requestBody);
    }

    /**
     * Checks the content length header on DELETE requests to ensure it is set to '0', avoiding request timeouts on replicated requests.
     *
     * @param method  the HTTP method of the request
     * @param headers the header keys and values
     */
    private void checkContentLengthHeader(String method, Map<String, String> headers) {
        // Only applies to DELETE requests
        if (HttpMethod.DELETE.equalsIgnoreCase(method)) {
            // Find the Content-Length header if present
            final String CONTENT_LENGTH_HEADER_KEY = "Content-Length";
            Map.Entry<String, String> contentLengthEntry = headers.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase(CONTENT_LENGTH_HEADER_KEY)).findFirst().orElse(null);
            // If no CL header, do nothing
            if (contentLengthEntry != null) {
                // If the provided CL value is non-zero, override it
                if (contentLengthEntry.getValue() != null && !contentLengthEntry.getValue().equalsIgnoreCase("0")) {
                    logger.warn("This is a DELETE request; the provided Content-Length was {}; setting Content-Length to 0", contentLengthEntry.getValue());
                    headers.put(CONTENT_LENGTH_HEADER_KEY, "0");
                }
            }
        }
    }

    @Override
    public Response replicate(final PreparedRequest request, final String uri) throws IOException {
        if (!(Objects.requireNonNull(request) instanceof OkHttpPreparedRequest)) {
            throw new IllegalArgumentException("Replication Client is only able to replicate requests that the client itself has prepared");
        }

        return replicate((OkHttpPreparedRequest) request, uri);
    }

    private Response replicate(final OkHttpPreparedRequest request, final String uri) throws IOException {
        logger.debug("Replicating request {} to {}", request, uri);
        final Call call = createCall(request, uri);
        final okhttp3.Response callResponse = call.execute();

        final byte[] responseBytes = getResponseBytes(callResponse);
        final MultivaluedMap<String, String> responseHeaders = getHeaders(callResponse);
        logger.debug("Received response code {} with headers {} for request {} to {}", callResponse.code(), responseHeaders, request, uri);

        final Response response = new JacksonResponse(jsonCodec, responseBytes, responseHeaders, URI.create(uri), callResponse.code(), callResponse::close);
        return response;
    }

    private MultivaluedMap<String, String> getHeaders(final okhttp3.Response callResponse) {
        final Headers headers = callResponse.headers();
        final MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
        for (final String name : headers.names()) {
            final List<String> values = headers.values(name);
            headerMap.addAll(name, values);
        }

        return headerMap;
    }

    private byte[] getResponseBytes(final okhttp3.Response callResponse) throws IOException {
        final byte[] rawBytes = callResponse.body().bytes();

        final String contentEncoding = callResponse.header("Content-Encoding");
        if (gzipEncodings.contains(contentEncoding)) {
            try (final InputStream gzipIn = new GZIPInputStream(new ByteArrayInputStream(rawBytes));
                 final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

                StreamUtils.copy(gzipIn, baos);
                return baos.toByteArray();
            }
        } else {
            return rawBytes;
        }
    }

    private Call createCall(final OkHttpPreparedRequest request, final String uri) {
        Request.Builder requestBuilder = new Request.Builder();

        final HttpUrl url = buildUrl(request, uri);
        requestBuilder = requestBuilder.url(url);

        // build the request body
        final String method = request.getMethod().toUpperCase();
        switch (method) {
            case "POST":
            case "PUT":
            case "PATCH":
                requestBuilder = requestBuilder.method(method, request.getRequestBody());
                break;
            default:
                requestBuilder = requestBuilder.method(method, null);
                break;
        }

        // Add appropriate headers
        for (final Map.Entry<String, String> header : request.getHeaders().entrySet()) {
            requestBuilder = requestBuilder.addHeader(header.getKey(), header.getValue());
        }

        // Build the request
        final Request okHttpRequest = requestBuilder.build();
        final Call call = okHttpClient.newCall(okHttpRequest);
        return call;
    }

    @SuppressWarnings("unchecked")
    private HttpUrl buildUrl(final OkHttpPreparedRequest request, final String uri) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(uri).newBuilder();
        switch (request.getMethod().toUpperCase()) {
            case HttpMethod.DELETE:
            case HttpMethod.HEAD:
            case HttpMethod.GET:
            case HttpMethod.OPTIONS:
                if (request.getEntity() instanceof MultivaluedMap) {
                    final MultivaluedMap<String, String> entityMap = (MultivaluedMap<String, String>) request.getEntity();

                    for (final Entry<String, List<String>> queryEntry : entityMap.entrySet()) {
                        final String queryName = queryEntry.getKey();
                        for (final String queryValue : queryEntry.getValue()) {
                            urlBuilder = urlBuilder.addQueryParameter(queryName, queryValue);
                        }
                    }
                }

                break;
        }

        return urlBuilder.build();
    }

    private RequestBody createRequestBody(final Map<String, String> headers, final Object entity, final boolean gzip) {
        final String contentType = getContentType(headers, "application/json");
        final byte[] serialized = serializeEntity(entity, contentType, gzip);

        final MediaType mediaType = MediaType.parse(contentType);
        return RequestBody.create(mediaType, serialized);
    }

    private String getContentType(final Map<String, String> headers, final String defaultValue) {
        for (final Map.Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("content-type")) {
                return entry.getValue();
            }
        }

        return defaultValue;
    }

    private byte[] serializeEntity(final Object entity, final String contentType, final boolean gzip) {
        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             final OutputStream out = gzip ? new GZIPOutputStream(baos, 1) : baos) {

            getSerializer(contentType).serialize(entity, out);
            out.close();

            return baos.toByteArray();
        } catch (final IOException e) {
            // This should never happen with a ByteArrayOutputStream
            throw new RuntimeException("Failed to serialize entity for cluster replication", e);
        }
    }

    private EntitySerializer getSerializer(final String contentType) {
        switch (contentType.toLowerCase()) {
            case "application/xml":
                return xmlSerializer;
            case "application/json":
            default:
                return jsonSerializer;
        }
    }

    private Map<String, String> updateHeadersForGzip(final Map<String, String> headers) {
        final String encodingHeader = headers.get("Content-Encoding");
        if (gzipEncodings.contains(encodingHeader)) {
            return headers;
        }

        final Map<String, String> updatedHeaders = new HashMap<>(headers);
        updatedHeaders.put("Content-Encoding", "gzip");
        return updatedHeaders;
    }

    private boolean isUseGzip(final Map<String, String> headers) {
        final String rawAcceptEncoding = headers.get(HttpHeaders.ACCEPT_ENCODING);

        if (rawAcceptEncoding == null) {
            return false;
        } else {
            final String[] acceptEncodingTokens = rawAcceptEncoding.split(",");
            return Stream.of(acceptEncodingTokens)
                    .map(String::trim)
                    .filter(StringUtils::isNotEmpty)
                    .map(String::toLowerCase)
                    .anyMatch(gzipEncodings::contains);
        }
    }

    private OkHttpClient createOkHttpClient(final NiFiProperties properties) {
        final String connectionTimeout = properties.getClusterNodeConnectionTimeout();
        final long connectionTimeoutMs = FormatUtils.getTimeDuration(connectionTimeout, TimeUnit.MILLISECONDS);
        final String readTimeout = properties.getClusterNodeReadTimeout();
        final long readTimeoutMs = FormatUtils.getTimeDuration(readTimeout, TimeUnit.MILLISECONDS);

        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder();
        okHttpClientBuilder.connectTimeout(connectionTimeoutMs, TimeUnit.MILLISECONDS);
        okHttpClientBuilder.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS);
        okHttpClientBuilder.followRedirects(true);
        final int connectionPoolSize = properties.getClusterNodeMaxConcurrentRequests();
        okHttpClientBuilder.connectionPool(new ConnectionPool(connectionPoolSize, 5, TimeUnit.MINUTES));

        final Tuple<SSLSocketFactory, X509TrustManager> tuple = createSslSocketFactory(properties);
        if (tuple != null) {
            okHttpClientBuilder.sslSocketFactory(tuple.getKey(), tuple.getValue());
        }

        return okHttpClientBuilder.build();
    }

    private Tuple<SSLSocketFactory, X509TrustManager> createSslSocketFactory(final NiFiProperties properties) {
        final SSLContext sslContext = SslContextFactory.createSslContext(properties);

        if (sslContext == null) {
            return null;
        }

        try {
            Tuple<SSLContext, TrustManager[]> sslContextTuple = createTrustSslContextWithTrustManagers(
                    properties.getProperty(NiFiProperties.SECURITY_KEYSTORE),
                    properties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD) != null ? properties.getProperty(NiFiProperties.SECURITY_KEYSTORE_PASSWD).toCharArray() : null,
                    properties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD) != null ? properties.getProperty(NiFiProperties.SECURITY_KEY_PASSWD).toCharArray() : null,
                    properties.getProperty(NiFiProperties.SECURITY_KEYSTORE_TYPE),
                    properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE),
                    properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD) != null ? properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_PASSWD).toCharArray() : null,
                    properties.getProperty(NiFiProperties.SECURITY_TRUSTSTORE_TYPE),
                    WANT,
                    sslContext.getProtocol());
            List<X509TrustManager> x509TrustManagers = Arrays.stream(sslContextTuple.getValue())
                    .filter(trustManager -> trustManager instanceof X509TrustManager)
                    .map(trustManager -> (X509TrustManager) trustManager).collect(Collectors.toList());
            return new Tuple<>(sslContextTuple.getKey().getSocketFactory(), x509TrustManagers.get(0));
        } catch (CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException | IOException e) {
            return null;
        }
    }
}


text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NDifference.TypeSystem
{
	/// <summary>
	/// Simple name of a type in an assembly. Does not contain namespace info.
	/// </summary>
	[DebuggerDisplay("type {Value}")]
	[Serializable]
	public class TypeName : IComparable<TypeName>
	{
		public TypeName(string value)
		{
			this.Value = value;
		}

		public string Value { get; private set; }

		public static bool operator ==(TypeName a, TypeName b)
		{
			if (object.ReferenceEquals(a, b))
			{
				return true;
			}

			if (((object)a == null) || ((object)b == null))
			{
				return false;
			}

			return string.Compare(a.Value, b.Value, StringComparison.Ordinal) == 0;
		}

		public static bool operator !=(TypeName a, TypeName b)
		{
			return !(a == b);
		}

		public override bool Equals(object obj)
		{
			if (obj == null)
			{
				return false;
			}

			TypeName another = obj as TypeName;

			if ((object)another == null)
			{
				return false;
			}

			return string.Compare(this.Value, another.Value, StringComparison.Ordinal) == 0;
		}

		public override string ToString()
		{
			return this.Value;
		}

		public override int GetHashCode()
		{
			return this.Value.GetHashCode();
		}

		public int CompareTo(TypeName other)
		{
			if (other == null)
			{
				return 1;
			}

			return this.Value.CompareTo(other.Value);
		}
	}
}


text:
var Handlebars = require("./handlebars/base");
module.exports = Handlebars;

// Each of these augment the Handlebars object. No need to setup here.
// (This is done to easily share code between commonjs and browse envs)
require("./handlebars/utils");

require("./handlebars/compiler");
require("./handlebars/runtime");

// BEGIN(BROWSER)

// END(BROWSER)


text:
"""Defines a weighted parsing model with latent states (i.e. types)."""

import tensorflow as tf

def _log_matmul_exp(tensor_a, tensor_b):
  """Numerically stable equivalent of log-matmul-exp computation.

  Is equivalent to:
    tf.math.log(
      tf.matmul(tf.math.exp(tensor_a),
                tf.math.exp(tensor_b)))

  See:
  https://en.wikipedia.org/wiki/LogSumExp#log-sum-exp_trick_for_log-domain_calculations

  Args:
    tensor_a: <float>[L, M].
    tensor_b: <float>[M, N].

  Returns:
    <float>[L, N].
  """
  max_a = tf.math.reduce_max(tensor_a, axis=1, keepdims=True)
  max_b = tf.math.reduce_max(tensor_b, axis=0, keepdims=True)
  tensor_a -= max_a
  tensor_b -= max_b
  tensor_c = tf.matmul(tf.math.exp(tensor_a), tf.math.exp(tensor_b))
  tensor_c = tf.math.log(tensor_c)
  tensor_c += max_a
  tensor_c += max_b
  return tensor_c

class ApplicationScoringLayer(tf.keras.layers.Layer):
  """Layer for interfacing with rule application scores."""

  def __init__(self, config):
    super(ApplicationScoringLayer, self).__init__()
    weights_init = tf.random_normal_initializer()

    self.rhs_type_scores = tf.Variable(
        initial_value=weights_init(
            shape=(config["num_rhs_emb"], config["num_types"]),
            dtype="float32"),
        trainable=True,
    )

    self.type_lhs_scores = tf.Variable(
        initial_value=weights_init(
            shape=(config["num_types"], config["num_lhs_emb"]),
            dtype="float32"),
        trainable=True,
    )

  def get_scores(self,
                 temperature=1,
                 lhs_nonterminal_bias=None,
                 rhs_emb_idxs=None,
                 lhs_emb_idxs=None,
                 approximate_denominator=False):
    """Computes scores.

    P(lhs|rhs) = sum over types { P(type|rhs) * P(lhs|type) }
    logP(lhs|rhs) = log sum over types { P(type|rhs) * P(lhs|type) }

    Can be computed as below for greater numerical stability:

    logP(lhs|rhs) = log sum exp over types { logP(type|rhs) + logP(lhs|type) }

    P(type|rhs) is "softmax" of score(rhs, type) normalized across type.
    P(lhs|type) is "softmax" of score(type, lhs) normalized across lhs.

    Args:
      temperature: Float indicating temperature for softmax (1 = standard
        softmax).
      lhs_nonterminal_bias: <float>[1, num_lhs_emb].
      rhs_emb_idxs: <int32>[num_rhs_emb_idxs].
      lhs_emb_idxs: <int32>[num_lhs_emb_idxs].
      approximate_denominator: Bool indicating whether to approximate the
        denominator of the softmax over rules by only considering rules in the
        current batch.

    Returns:
      <float>[num_lhs_emb, num_rhs_emb]
    """
    rhs_type_scores = self.rhs_type_scores
    type_lhs_scores = self.type_lhs_scores
    if rhs_emb_idxs is not None:
      rhs_type_scores = tf.gather(rhs_type_scores, rhs_emb_idxs, axis=0)
    # Gather *before* the softmax if approximate_denominator.
    if lhs_emb_idxs is not None and approximate_denominator:
      type_lhs_scores = tf.gather(type_lhs_scores, lhs_emb_idxs, axis=1)
    if lhs_nonterminal_bias is None:
      lhs_nonterminal_bias = tf.zeros_like(type_lhs_scores)

    # <float>[num_rhs_emb, num_types]
    logp_type_given_rhs = tf.nn.log_softmax(
        rhs_type_scores / temperature, axis=1)
    # <float>[num_types, num_lhs_emb]
    logp_lhs_given_type = tf.nn.log_softmax(
        type_lhs_scores / temperature + lhs_nonterminal_bias, axis=1)

    # Gather *after* the softmax to not affect denominator.
    if lhs_emb_idxs is not None and not approximate_denominator:
      logp_lhs_given_type = tf.gather(logp_lhs_given_type, lhs_emb_idxs, axis=1)

    # Avoid broadcast to large matrix.
    logp_lhs_given_rhs = _log_matmul_exp(logp_type_given_rhs,
                                         logp_lhs_given_type)

    # Transpose to provide expected return type.
    logp_lhs_given_rhs_t = tf.transpose(logp_lhs_given_rhs)
    return logp_lhs_given_rhs_t

  def get_scores_unstable(self, temperature=1, lhs_nonterminal_bias=None):
    """Same as get_scores but is less numerically stable."""
    # TODO(petershaw): Consider removing this and just using the above method?
    if lhs_nonterminal_bias is None:
      lhs_nonterminal_bias = tf.zeros_like(self.type_lhs_scores)
    # <float>[num_rhs_emb, num_types]
    type_given_rhs = tf.nn.softmax(self.rhs_type_scores / temperature, axis=1)
    # <float>[num_types, num_lhs_emb]
    lhs_given_type = tf.nn.softmax(
        self.type_lhs_scores / temperature + lhs_nonterminal_bias, axis=1)
    # Avoid broadcast to large matrix.
    logp_lhs_given_rhs = tf.math.log(tf.matmul(type_given_rhs, lhs_given_type))
    logp_lhs_given_rhs_t = tf.transpose(logp_lhs_given_rhs)
    return logp_lhs_given_rhs_t

class Model(tf.keras.layers.Layer):
  """Wraps rule application scores."""

  def __init__(self, batch_size, config, training, verbose=False):
    super(Model, self).__init__()
    self.config = config
    self.scoring_layer = ApplicationScoringLayer(config)
    self.training = training
    self.batch_size = batch_size


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>定制 Fiddler 之抓获 WinHTTP 请求 — 刘华健</title>
    <link rel="stylesheet" href="/assets/vendor/primer-css/css/primer.css">
    <link rel="stylesheet" href="/assets/vendor/primer-markdown/dist/user-content.min.css">
    <link rel="stylesheet" href="/assets/vendor/octicons/octicons/octicons.css">
    <link rel="stylesheet" href="/assets/css/components/collection.css">
    <link rel="stylesheet" href="/assets/css/components/repo-card.css">
    <link rel="stylesheet" href="/assets/css/sections/repo-list.css">
    <link rel="stylesheet" href="/assets/css/sections/mini-repo-list.css">
    <link rel="stylesheet" href="/assets/css/components/boxed-group.css">
    <link rel="stylesheet" href="/assets/css/globals/common.css">
    <link rel="stylesheet" href="/assets/vendor/share.js/dist/css/share.min.css">
    <link rel="stylesheet" href="/assets/css/globals/responsive.css">
    <link rel="stylesheet" href="/assets/css/posts/index.css">

    <link rel="alternate" type="application/atom+xml" title="刘华健" href="/feed.xml">
    <link rel="shortcut icon" href="/favicon.ico">

    <meta property="og:title" content="定制 Fiddler 之抓获 WinHTTP 请求">

    <meta name="keywords" content="Fiddler, WinHTTP">
    <meta name="og:keywords" content="Fiddler, WinHTTP">

    <meta name="description" content="背景">
    <meta name="og:description" content="背景">

    <meta property="og:url" content="https://ztmengyuxuan.github.io/2014/07/19/fiddlerwithwinhttp/">
    <meta property="og:site_name" content="刘华健">
    <meta property="og:type" content="article">
    <meta property="og:locale" content="zh_CN" />

    <meta property="article:published_time" content="2014-07-19">

 刘华健

                首页

                分类

                链接

                关于

定制 Fiddler 之抓获 WinHTTP 请求

             2014/07/19

            Fiddler

背景

发现使用 Fiddler 进行抓包时有一部分请求总是没抓到,查看了一下源代码,发现使用 WinINET 这套 API 发送的请求都能正常抓到,而使用 WinHTTP 这套 API 发送的请求都没有抓到,遂搜索了一下,果然前人们早已给出答案,解决方案原文可以参看 Fiddler 作者 Eric Lawrence 大神的一篇博客 Using Fiddler with WinHTTP,博客里表示 Fiddler 对各种 HTTP(s) stacks 都是能支持的,只是默认启动时只是接管了 WinINET 代理设置。

Eric 的那篇博客里已经列出了相关的方法和代码,本文只是对其略做改进,让同一段代码可以适配不同的 Windows 版本。

分析

我们需要让 Fiddler 抓取 WinHTTP 的包时,要做的就是让 WinHTTP 的代理设置改为与 WinINET 一致,因为 WinINET 在 Fiddler 启动后使用 Fiddler 作为代理。这些通过 Windows 自带命令就可以做到:

在 XP 下:

proxycfg -u

在 Win7 下(使用管理员权限的命令行):

netsh winhttp import proxy ie

注:在 Win7 64 位系统下需要将 System32 目录和 SysWOW64 目录下的 netsh 命令各执行一次,下方将给出的脚本已覆盖这种情况。

但是如果使用频繁,每次都还要去手动敲命令行还是挺痛苦的,作为能偷懒的地方绝不多放过的少年,一劳永逸的方法当然是让它随 Fiddler 的启动与关闭自动执行这些命令(当然这就是 Eric 的博客里讲述的方法)。

实现

这可以通过修改 CustomRules.js 实现(如果想对 Fiddler 的扩展机制进行深入了解可以去参阅 Fiddler 官网的文档)。

操作方法:

打开 Fiddler > 点击菜单 Rules > 点击 Customize Rules…

然后就打开了 CustomRules.js 文件,寻找到OnAttach与OnDetach函数,可以将 Fiddler 启动后与关闭前需要定制的一些自动动作分别填写在它们里头,我们为实现让 Fiddler 能抓取 WinHTTP 发送的请求的目的而修改后的代码如下,添加了UpdateWinHTTPSettings函数,在OnAttach和OnDetach里添加了对它的调用,修改完后保存即可生效。

static function OnAttach() {
    UpdateWinHTTPSettings();
}
static function OnDetach() {
    UpdateWinHTTPSettings();
}

static function UpdateWinHTTPSettings() {
    var oPSI: System.Diagnostics.ProcessStartInfo
        = new System.Diagnostics.ProcessStartInfo();
    var os : OperatingSystem = Environment.OSVersion;
    if (os.Version.Major >= 6) {
        oPSI.UseShellExecute = true;
        oPSI.FileName = "netsh.exe";
        oPSI.Verb = "runas";
        oPSI.Arguments = "winhttp import proxy ie";
        System.Diagnostics.Process.Start(oPSI);

        // Re-run 32bit version
        oPSI.FileName = oPSI.FileName =
            Environment.SystemDirectory.Replace("system32", "syswow64")
            + "\\netsh.exe";
        if (System.IO.File.Exists(oPSI.FileName)) {
            System.Diagnostics.Process.Start(oPSI);
        }
    }
    else {
        oPSI.UseShellExecute = true;
        oPSI.FileName = "proxycfg.exe";
        oPSI.Verb = "open";
        oPSI.Arguments = "-u";
        System.Diagnostics.Process.Start(oPSI);
    }
}

UpdateWinHTTPSettings函数里做的事情其实很简单,就是使用管理员权限执行文章前面说到的命令。

附注

我使用的完整最新的 CustomRules.js 文件我上传到了一个 Gist 里,详见:https://gist.github.com/mzlogin/3c5f9781c5bedff3fcfb,如果想直接使用可以复制脚本内容后放置到「我的文档 /Fiddler 2/Scripts/CustomRules.js」,也可以在此目录下使用 git 抓取我的最新定制 js 文件。

Search

<form action="get" id="site_search">
    <input type="text" id="search_box" placeholder="Search">
    <button class="btn btn-default" type="submit"></button>
</form>

<link rel="stylesheet" type="text/css" href="/assets/css/modules/sidebar-search.css">

Post Directory

                    © 2015
                    Liu Hua Jian
                    TOP

                    TOP

                    首页

                    分类

                    链接

                    关于

    <link rel="stylesheet" href="/assets/css/globals/prism.css">


text:
package com.francis.coolweather.util;

import com.francis.coolweather.interfaces.HttpCallbackListener;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Francis on 2015/11/12.
 */
public class HttpUtil {
    public static void sendHttpRequest(final String address,final HttpCallbackListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8*1000);
                    connection.setReadTimeout(8*1000);
                    if(connection.getResponseCode() == 200){
                        InputStream in = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        StringBuilder response = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null){
                            response.append(line);
                        }
                        if(listener != null){
                            listener.onFinish(response.toString());
                        }
                    }
                } catch (Exception e) {
                    if(listener != null){
                        listener.onError(e);
                    }
                }finally {
                    if(connection != null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }
}


text:
void test1(){

    // given

    ESEntityRepository *repo = [ESEntityRepository new];

    MGBenchStart(@"Test");

    for (int j = 0; j < 1000000; j++) {
        ESEntity *entity = [repo createEntity];
        if (j%25 == 0){
            [entity addComponent:[SomeComponent new]];
        }
        if (j%25 == 1){
            [entity addComponent:[SomeOtherComponent new]];
        }
    }

    MGBenchStep(@"Test", @"Mio entities are created");

    ESCollection *collection = [repo collectionForTypes:[NSSet setWithObject:[SomeComponent class]]];

    MGBenchStep(@"Test", @"Collection is created");

    NSArray *collectedEntities = collection.entities;

    MGBenchStep(@"Test", @"Getting all entities initially");

    for (int i = 0; i < 100; i++) {
        collectedEntities = collection.entities;
    }

    MGBenchStep(@"Test", @"Getting all entities from collection cache 100 times");

    ESMatcher *matcher = [ESMatcher just:[SomeComponent class]];
    for (int i = 0; i < 100; i++) {
        collectedEntities = [repo entitiesForMatcher:matcher];
    }

    MGBenchStep(@"Test", @"Getting all entities direct from repository 100 times");

    for (ESEntity *entity in collectedEntities) {
        [entity exchangeComponent:[SomeComponent new]];
    }

    MGBenchStep(@"Test", @"Exchanged component in all entities of the collection");

    for (ESEntity *entity in repo.allEntities) {
        [repo destroyEntity:entity];
    }

    MGBenchStep(@"Test", @"Destroy all entities");

    NSLog(@"Entities Left: %lu", (unsigned long) repo.allEntities.count);

    MGBenchEnd(@"Test");

}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        test1();

    }
    return 0;
}


text:
package com.wideplay.warp.persist.jpa;

import com.wideplay.warp.persist.internal.InternalWorkManager;
import com.wideplay.warp.persist.WorkManager;
import net.jcip.annotations.Immutable;
import net.jcip.annotations.ThreadSafe;

import javax.persistence.EntityManager;

/**
 * @author Dhanji R. Prasanna (dhanji gmail com)
 */
@Immutable
@ThreadSafe // Thread confinement through ThreadLocal
class JpaWorkManager implements WorkManager {
    private final InternalWorkManager<EntityManager> internalWorkManager;

    public JpaWorkManager(InternalWorkManager<EntityManager> internalWorkManager) {
        this.internalWorkManager = internalWorkManager;
    }

    public void beginWork() {
        this.internalWorkManager.beginWork();
    }

    public void endWork() {
        this.internalWorkManager.endWork();
    }

    public String toString() {
        return super.toString();
    }
}


text:
from django.contrib.auth.forms import (
    UserCreationForm as BaseUserCreationForm,
    UserChangeForm as BaseUserChangeForm,
)
from .models import User

class UserCreationForm(BaseUserCreationForm):
    class Meta(BaseUserCreationForm.Meta):
        model = User

class UserChangeForm(BaseUserChangeForm):
    class Meta(BaseUserChangeForm.Meta):
        model = User
        fields = (
            'username',
            'email',
            'display_name',
            'profile_icon',
            'self_introduction',
        )


text:
<!DOCTYPE html>

<title>How do I enable student peer review for an assignment?</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="sakai.assignment" name="description">
<meta content="peer review, peer assessment, peer evaluation" name="search">
<link href="/library/skin/tool_base.css" media="screen" rel="stylesheet" type="text/css" charset="utf-8">
<link href="../css/help.css" media="screen" rel="stylesheet" type="text/css" charset="utf-8">
<link href="/library/skin/morpheus-default/tool.css" media="screen" rel="stylesheet" type="text/css" charset="utf-8">
<link href="/library/js/jquery/featherlight/0.4.0/featherlight.min.css" media="screen" rel="stylesheet" type="text/css" charset="utf-8">

How do I batch import multiple site archives?

Go to Site Archive

Select the Site Archive tool from the Tool Menu in the Administration Workspace.

Select Batch Import/Export.

Click Choose File to locate your batch import file.

Once you have selected the batch import file, click Import.

Import file format.

Your import file should list the target site id followed by a comma and then the archive id in the siteid-archive format, one archive per line.


text:
    <form class="form-horizontal" role="form" name="mapControlForm">

            Species:

                <select id="speciesInput" class="form-control" ng-model="httpConfig.params.species_ids" ng-change="reloadMapData()" ng-options="value as key for (key , value) in species">
                </select>

            For the past <input type="number" ng-required="true" min="1" ng-model="httpConfig.params.days" ng-change="reloadMapData()" style="width: 60px; height: 34px;"></input> days.

    </form>


text:
<?php

require_once 'BaseTestCase.php';
require_once HAMLPHP_ROOT . '/ContentEvaluator/EvaluatorProxy.php';

class EvaluatorClassesHelper extends ContentEvaluator
{
	public $pubProp = 'public';
	protected $prtProp = 'protected';
	private $prvProp = 'private';

	/**
	 * @var EvaluatorProxy
	 */
	private $evaluator;

	public function __construct()
	{
		$this->evaluator = new EvaluatorProxy($this);
	}

	public function proxyEvaluate($content, array $contentVariables = array())
	{
		return $this->evaluator->evaluate($content, $contentVariables);
	}

	public function proxyEvaluateFile($filepath, array $contentVariables = array())
	{
		return $this->evaluator->evaluate_file($filepath, $contentVariables);
	}
}