Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.browser.tests; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.AppConstants; |
michael@0 | 8 | import org.mozilla.gecko.menu.GeckoMenu; |
michael@0 | 9 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 10 | |
michael@0 | 11 | public class TestGeckoMenu extends BrowserTestCase { |
michael@0 | 12 | |
michael@0 | 13 | private volatile Exception exception; |
michael@0 | 14 | private void setException(Exception e) { |
michael@0 | 15 | this.exception = e; |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | public void testMenuThreading() throws InterruptedException { |
michael@0 | 19 | final GeckoMenu menu = new GeckoMenu(getActivity()); |
michael@0 | 20 | final Object semaphore = new Object(); |
michael@0 | 21 | |
michael@0 | 22 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 23 | @Override |
michael@0 | 24 | public void run() { |
michael@0 | 25 | try { |
michael@0 | 26 | menu.add("test1"); |
michael@0 | 27 | } catch (Exception e) { |
michael@0 | 28 | setException(e); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | synchronized (semaphore) { |
michael@0 | 32 | semaphore.notify(); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | }); |
michael@0 | 36 | synchronized (semaphore) { |
michael@0 | 37 | semaphore.wait(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | // No exception thrown if called on UI thread. |
michael@0 | 41 | assertNull(exception); |
michael@0 | 42 | |
michael@0 | 43 | new Thread(new Runnable() { |
michael@0 | 44 | @Override |
michael@0 | 45 | public void run() { |
michael@0 | 46 | try { |
michael@0 | 47 | menu.add("test2"); |
michael@0 | 48 | } catch (Exception e) { |
michael@0 | 49 | setException(e); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | synchronized (semaphore) { |
michael@0 | 53 | semaphore.notify(); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | }).start(); |
michael@0 | 57 | |
michael@0 | 58 | synchronized (semaphore) { |
michael@0 | 59 | semaphore.wait(); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | if (AppConstants.RELEASE_BUILD) { |
michael@0 | 63 | // No exception thrown: release build. |
michael@0 | 64 | assertNull(exception); |
michael@0 | 65 | return; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | assertNotNull(exception); |
michael@0 | 69 | assertEquals(exception.getClass(), IllegalThreadStateException.class); |
michael@0 | 70 | } |
michael@0 | 71 | } |