Example usage for java.util.concurrent.atomic AtomicBoolean AtomicBoolean

List of usage examples for java.util.concurrent.atomic AtomicBoolean AtomicBoolean

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicBoolean AtomicBoolean.

Prototype

public AtomicBoolean(boolean initialValue) 

Source Link

Document

Creates a new AtomicBoolean with the given initial value.

Usage

From source file:android.webkit.cts.WebViewTest.java

public void testAddJavascriptInterfaceExceptions() throws Exception {
    WebSettings settings = mOnUiThread.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);

    final AtomicBoolean mJsInterfaceWasCalled = new AtomicBoolean(false) {
        @JavascriptInterface//ww  w.j a  v a 2s  . co m
        public synchronized void call() {
            set(true);
            // The main purpose of this test is to ensure an exception here does not
            // crash the implementation.
            throw new RuntimeException("Javascript Interface exception");
        }
    };

    mOnUiThread.addJavascriptInterface(mJsInterfaceWasCalled, "dummy");

    mOnUiThread.loadUrlAndWaitForCompletion("about:blank");

    assertFalse(mJsInterfaceWasCalled.get());

    final CountDownLatch resultLatch = new CountDownLatch(1);
    mOnUiThread.evaluateJavascript("try {dummy.call(); 'fail'; } catch (exception) { 'pass'; } ",
            new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String result) {
                    assertEquals("\"pass\"", result);
                    resultLatch.countDown();
                }
            });

    assertTrue(resultLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
    assertTrue(mJsInterfaceWasCalled.get());
}

From source file:com.jayway.maven.plugins.android.AbstractAndroidMojo.java

/**
 * Undeploys an apk, specified by package name, from a connected emulator
 * or usb device. Also deletes the application's data and cache
 * directories on the device.//from   w ww.  ja v  a  2 s .  com
 *
 * @param packageName the package name to undeploy.
 * @return <code>true</code> if successfully undeployed, <code>false</code> otherwise.
 */
protected boolean undeployApk(final String packageName) throws MojoExecutionException, MojoFailureException {

    final AtomicBoolean result = new AtomicBoolean(true); // if no devices are present, it counts as successful

    doWithDevices(new DeviceCallback() {
        public void doWithDevice(final IDevice device) throws MojoExecutionException {
            String deviceLogLinePrefix = DeviceHelper.getDeviceLogLinePrefix(device);
            try {
                device.uninstallPackage(packageName);
                getLog().info(deviceLogLinePrefix + "Successfully uninstalled " + packageName);
                getLog().debug(" from " + DeviceHelper.getDescriptiveName(device));
                result.set(true);
            } catch (InstallException e) {
                result.set(false);
                throw new MojoExecutionException(
                        deviceLogLinePrefix + "Uninstall of " + packageName + " failed.", e);
            }
        }
    });

    return result.get();
}

From source file:com.android.tools.idea.tests.gui.gradle.GradleSyncTest.java

@Test
public void gradleModelCache() throws IOException {
    guiTest.importSimpleApplication();//from   w  w w. j  a  va 2s .c  om
    IdeFrameFixture ideFrameFixture = guiTest.ideFrame();

    File projectPath = ideFrameFixture.getProjectPath();
    ideFrameFixture.closeProject();

    AtomicBoolean syncSkipped = new AtomicBoolean(false);

    // Reopen project and verify that sync was skipped (i.e. model loaded from cache)
    execute(new GuiTask() {
        @Override
        protected void executeInEDT() throws Throwable {
            ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
            Project project = projectManager.convertAndLoadProject(projectPath.getPath());
            GradleSyncState.subscribe(project, new GradleSyncListener.Adapter() {
                @Override
                public void syncSkipped(@NotNull Project project) {
                    syncSkipped.set(true);
                }
            });
            projectManager.openProject(project);
        }
    });

    Wait.seconds(5).expecting("sync to be skipped").until(syncSkipped::get);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.java

private static boolean selectsPseudoClass(final BrowserVersion browserVersion,
        final AttributeCondition condition, final DomElement element) {
    if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        final Object sobj = element.getPage().getScriptableObject();
        if (sobj instanceof HTMLDocument && ((HTMLDocument) sobj).getDocumentMode() < 8) {
            return false;
        }/*  ww w  . java  2 s.  c  om*/
    }

    final String value = condition.getValue();
    if ("root".equals(value)) {
        return element == element.getPage().getDocumentElement();
    } else if ("enabled".equals(value)) {
        return element instanceof DisabledElement && !((DisabledElement) element).isDisabled();
    }
    if ("disabled".equals(value)) {
        return element instanceof DisabledElement && ((DisabledElement) element).isDisabled();
    }
    if ("focus".equals(value)) {
        final HtmlPage htmlPage = element.getHtmlPageOrNull();
        if (htmlPage != null) {
            final DomElement focus = htmlPage.getFocusedElement();
            return element == focus;
        }
    } else if ("checked".equals(value)) {
        return (element instanceof HtmlCheckBoxInput && ((HtmlCheckBoxInput) element).isChecked())
                || (element instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) element).isChecked()
                        || (element instanceof HtmlOption && ((HtmlOption) element).isSelected()));
    } else if ("first-child".equals(value)) {
        for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement) {
                return false;
            }
        }
        return true;
    } else if ("last-child".equals(value)) {
        for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement) {
                return false;
            }
        }
        return true;
    } else if ("first-of-type".equals(value)) {
        final String type = element.getNodeName();
        for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                return false;
            }
        }
        return true;
    } else if ("last-of-type".equals(value)) {
        final String type = element.getNodeName();
        for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                return false;
            }
        }
        return true;
    } else if (value.startsWith("nth-child(")) {
        final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
        int index = 0;
        for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement) {
                index++;
            }
        }
        return getNth(nth, index);
    } else if (value.startsWith("nth-last-child(")) {
        final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
        int index = 0;
        for (DomNode n = element; n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement) {
                index++;
            }
        }
        return getNth(nth, index);
    } else if (value.startsWith("nth-of-type(")) {
        final String type = element.getNodeName();
        final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
        int index = 0;
        for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                index++;
            }
        }
        return getNth(nth, index);
    } else if (value.startsWith("nth-last-of-type(")) {
        final String type = element.getNodeName();
        final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
        int index = 0;
        for (DomNode n = element; n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                index++;
            }
        }
        return getNth(nth, index);
    } else if ("only-child".equals(value)) {
        for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement) {
                return false;
            }
        }
        for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement) {
                return false;
            }
        }
        return true;
    } else if ("only-of-type".equals(value)) {
        final String type = element.getNodeName();
        for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                return false;
            }
        }
        for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
            if (n instanceof DomElement && n.getNodeName().equals(type)) {
                return false;
            }
        }
        return true;
    } else if ("empty".equals(value)) {
        return isEmpty(element);
    } else if ("target".equals(value)) {
        final String ref = element.getPage().getUrl().getRef();
        return StringUtils.isNotBlank(ref) && ref.equals(element.getId());
    } else if (value.startsWith("not(")) {
        final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
        final AtomicBoolean errorOccured = new AtomicBoolean(false);
        final ErrorHandler errorHandler = new ErrorHandler() {
            @Override
            public void warning(final CSSParseException exception) throws CSSException {
                // ignore
            }

            @Override
            public void fatalError(final CSSParseException exception) throws CSSException {
                errorOccured.set(true);
            }

            @Override
            public void error(final CSSParseException exception) throws CSSException {
                errorOccured.set(true);
            }
        };
        final CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
        parser.setErrorHandler(errorHandler);
        try {
            final SelectorList selectorList = parser
                    .parseSelectors(new InputSource(new StringReader(selectors)));
            if (errorOccured.get() || selectorList == null || selectorList.getLength() != 1) {
                throw new CSSException("Invalid selectors: " + selectors);
            }

            validateSelectors(selectorList, 9, element);

            return !CSSStyleSheet.selects(browserVersion, selectorList.item(0), element);
        } catch (final IOException e) {
            throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
        }
    }
    return false;
}

From source file:com.arpnetworking.metrics.impl.ApacheHttpSinkTest.java

@Test
public void testSafeSleep() throws InterruptedException {
    final AtomicBoolean value = new AtomicBoolean(false);
    final Thread thread = new Thread(() -> {
        // Value will not be set if safe sleep throws or is not interrupted.
        ApacheHttpSink.safeSleep(500);/*from  w w w  .  jav  a 2s.c o m*/
        value.set(true);
    });

    thread.start();
    Thread.sleep(100);
    thread.interrupt();
    thread.join(600);

    Assert.assertFalse(thread.isAlive());
    Assert.assertTrue(value.get());
}